summaryrefslogtreecommitdiff
path: root/src/types
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/types.rs (renamed from iced_builder/src/types.rs)10
-rw-r--r--src/types/element_name.rs (renamed from iced_builder/src/types/element_name.rs)19
-rw-r--r--src/types/project.rs (renamed from iced_builder/src/types/project.rs)109
-rwxr-xr-xsrc/types/rendered_element.rs (renamed from iced_builder/src/types/rendered_element.rs)144
4 files changed, 145 insertions, 137 deletions
diff --git a/iced_builder/src/types.rs b/src/types.rs
index 161b5e1..ac9d039 100644
--- a/iced_builder/src/types.rs
+++ b/src/types.rs
@@ -7,15 +7,15 @@ use std::path::PathBuf;
pub use element_name::ElementName;
use iced::widget::{pane_grid, text_editor};
use iced::Theme;
-use iced_anim::SpringEvent;
+use iced_anim::Event;
pub use project::Project;
pub use rendered_element::*;
-use crate::Result;
+use crate::Error;
#[derive(Debug, Clone)]
pub enum Message {
- ToggleTheme(SpringEvent<Theme>),
+ ToggleTheme(Event<Theme>),
CopyCode,
SwitchPage(DesignerPage),
EditorAction(text_editor::Action),
@@ -35,10 +35,10 @@ pub enum Message {
PaneDragged(pane_grid::DragEvent),
NewFile,
OpenFile,
- FileOpened(Result<(PathBuf, Project)>),
+ FileOpened(Result<(PathBuf, Project), Error>),
SaveFile,
SaveFileAs,
- FileSaved(Result<PathBuf>),
+ FileSaved(Result<PathBuf, Error>),
}
#[derive(Debug, Clone)]
diff --git a/iced_builder/src/types/element_name.rs b/src/types/element_name.rs
index e172227..2687673 100644
--- a/iced_builder/src/types/element_name.rs
+++ b/src/types/element_name.rs
@@ -3,13 +3,13 @@ use serde::{Deserialize, Serialize};
use super::rendered_element::{
button, column, container, image, row, svg, text, Action, RenderedElement,
};
-use crate::{Error, Result};
+use crate::Error;
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum ElementName {
Text(String),
Button(String),
- SVG(String),
+ Svg(String),
Image(String),
Container,
Row,
@@ -20,7 +20,7 @@ impl ElementName {
pub const ALL: &'static [Self; 7] = &[
Self::Text(String::new()),
Self::Button(String::new()),
- Self::SVG(String::new()),
+ Self::Svg(String::new()),
Self::Image(String::new()),
Self::Container,
Self::Row,
@@ -31,23 +31,22 @@ impl ElementName {
&self,
element_tree: Option<&mut RenderedElement>,
action: Action,
- ) -> Result<Option<RenderedElement>> {
+ ) -> Result<Option<RenderedElement>, Error> {
let element = match self {
Self::Text(_) => text(""),
Self::Button(_) => button(""),
- Self::SVG(_) => svg(""),
+ Self::Svg(_) => svg(""),
Self::Image(_) => image(""),
Self::Container => container(None),
Self::Row => row(None),
Self::Column => column(None),
};
match action {
- Action::Stop => Ok(None),
- Action::Drop => Ok(None),
+ Action::Stop | Action::Drop => Ok(None),
Action::AddNew => Ok(Some(element)),
Action::PushFront(id) => {
element_tree
- .ok_or("The action was of kind `PushFront`, but no element tree was provided.")?
+ .ok_or("the action was of kind `PushFront`, but no element tree was provided.")?
.find_by_id(id)
.ok_or(Error::NonExistentElement)?
.push_front(&element);
@@ -56,7 +55,7 @@ impl ElementName {
Action::InsertAfter(parent_id, child_id) => {
element_tree
.ok_or(
- "The action was of kind `InsertAfter`, but no element tree was provided.",
+ "the action was of kind `InsertAfter`, but no element tree was provided.",
)?
.find_by_id(parent_id)
.ok_or(Error::NonExistentElement)?
@@ -75,7 +74,7 @@ impl std::fmt::Display for ElementName {
match self {
Self::Text(_) => "Text",
Self::Button(_) => "Button",
- Self::SVG(_) => "SVG",
+ Self::Svg(_) => "SVG",
Self::Image(_) => "Image",
Self::Container => "Container",
Self::Row => "Row",
diff --git a/iced_builder/src/types/project.rs b/src/types/project.rs
index f4dbcc4..27c576b 100644
--- a/iced_builder/src/types/project.rs
+++ b/src/types/project.rs
@@ -1,17 +1,29 @@
use std::path::{Path, PathBuf};
+extern crate fxhash;
+use fxhash::FxHashMap;
use iced::Theme;
-use rust_format::{Config, Edition, Formatter, RustFmt};
+use rust_format::{Edition, Formatter, RustFmt};
use serde::{Deserialize, Serialize};
use super::rendered_element::RenderedElement;
-use crate::{Error, Result};
+use crate::config::Config;
+use crate::theme::{theme_from_str, theme_index, theme_to_string};
+use crate::Error;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Project {
pub title: Option<String>,
pub theme: Option<String>,
pub element_tree: Option<RenderedElement>,
+ #[serde(skip)]
+ theme_cache: FxHashMap<String, String>,
+}
+
+impl Default for Project {
+ fn default() -> Self {
+ Self::new()
+ }
}
impl Project {
@@ -20,41 +32,43 @@ impl Project {
title: None,
theme: None,
element_tree: None,
+ theme_cache: FxHashMap::default(),
}
}
- pub fn get_theme(&self) -> Theme {
+ pub fn get_theme(&self, config: &Config) -> Theme {
match &self.theme {
- Some(theme) => match theme.as_str() {
- "Light" => Theme::Light,
- "Dark" => Theme::Dark,
- "Dracula" => Theme::Dracula,
- "Nord" => Theme::Nord,
- "Solarized Light" => Theme::SolarizedLight,
- "Solarized Dark" => Theme::SolarizedDark,
- "Gruvbox Light" => Theme::GruvboxLight,
- "Gruvbox Dark" => Theme::GruvboxDark,
- "Catppuccin Latte" => Theme::CatppuccinLatte,
- "Catppuccin Frappé" => Theme::CatppuccinFrappe,
- "Catppuccin Macchiato" => Theme::CatppuccinMacchiato,
- "Catppuccin Mocha" => Theme::CatppuccinMocha,
- "Tokyo Night" => Theme::TokyoNight,
- "Tokyo Night Storm" => Theme::TokyoNightStorm,
- "Tokyo Night Light" => Theme::TokyoNightLight,
- "Kanagawa Wave" => Theme::KanagawaWave,
- "Kanagawa Dragon" => Theme::KanagawaDragon,
- "Kanagawa Lotus" => Theme::KanagawaLotus,
- "Moonfly" => Theme::Moonfly,
- "Nightfly" => Theme::Nightfly,
- "Oxocarbon" => Theme::Oxocarbon,
- "Ferra" => Theme::Ferra,
- _ => Theme::Dark,
- },
- None => Theme::Dark,
+ Some(theme) => theme_from_str(Some(config), theme),
+ None => Theme::default(),
+ }
+ }
+
+ fn theme_code(&mut self, theme: &Theme) -> String {
+ let theme_name = theme.to_string();
+ if theme_index(&theme_name, Theme::ALL).is_none() {
+ (*self
+ .theme_cache
+ .entry(theme_name)
+ .or_insert(theme_to_string(theme)))
+ .to_string()
+ } else {
+ theme_name.replace(" ", "")
}
}
- pub async fn from_path() -> Result<(PathBuf, Self)> {
+ pub async fn from_path(
+ path: PathBuf,
+ config: Config,
+ ) -> Result<(PathBuf, Self), Error> {
+ let contents = tokio::fs::read_to_string(&path).await?;
+ let mut project: Self = serde_json::from_str(&contents)?;
+
+ let _ = project.theme_code(&project.get_theme(&config));
+
+ Ok((path, project))
+ }
+
+ pub async fn from_file(config: Config) -> Result<(PathBuf, Self), Error> {
let picked_file = rfd::AsyncFileDialog::new()
.set_title("Open a JSON file...")
.add_filter("*.json, *.JSON", &["json", "JSON"])
@@ -64,13 +78,13 @@ impl Project {
let path = picked_file.path().to_owned();
- let contents = tokio::fs::read_to_string(&path).await?;
- let element: Self = serde_json::from_str(&contents)?;
-
- Ok((path, element))
+ Self::from_path(path, config).await
}
- pub async fn write_to_file(self, path: Option<PathBuf>) -> Result<PathBuf> {
+ pub async fn write_to_file(
+ self,
+ path: Option<PathBuf>,
+ ) -> Result<PathBuf, Error> {
let path = if let Some(p) = path {
p
} else {
@@ -91,16 +105,25 @@ impl Project {
Ok(path)
}
- pub fn app_code(&self) -> Result<String> {
+ pub fn app_code(&mut self, config: &Config) -> Result<String, Error> {
match self.element_tree {
Some(ref element_tree) => {
let (imports, view) = element_tree.codegen();
- let mut app_code =
- format!("use iced::{{widget::{{{imports}}},Element}};");
-
- app_code = format!(
+ let theme = self.get_theme(config);
+ let theme_code = self.theme_code(&theme);
+ let mut theme_imports = "";
+ if theme_index(&theme.to_string(), Theme::ALL).is_none() {
+ if theme_code.contains("Extended") {
+ theme_imports = "use iced::{{color,theme::{{Palette,palette::{{Extended,Background,Primary,Secondary,Success,Danger,Pair}}}}}};\n";
+ } else {
+ theme_imports = "use iced::{{color,theme::Palette}};\n";
+ }
+ }
+
+ let app_code = format!(
r#"// Automatically generated by iced Builder
- {app_code}
+ use iced::{{widget::{{{imports}}},Element}};
+ {theme_imports}
fn main() -> iced::Result {{
iced::application("{}", State::update, State::view).theme(State::theme).run()
@@ -127,9 +150,9 @@ impl Project {
Some(ref t) => t,
None => "New app",
},
- self.get_theme().to_string().replace(" ", "")
+ theme_code
);
- let config = Config::new_str()
+ let config = rust_format::Config::new_str()
.edition(Edition::Rust2021)
.option("trailing_comma", "Never")
.option("imports_granularity", "Crate");
diff --git a/iced_builder/src/types/rendered_element.rs b/src/types/rendered_element.rs
index d4d1a6c..b001556 100755
--- a/iced_builder/src/types/rendered_element.rs
+++ b/src/types/rendered_element.rs
@@ -1,17 +1,18 @@
use std::collections::BTreeMap;
-use blob_uuid::random_blob;
use iced::advanced::widget::Id;
use iced::{widget, Element, Length};
use serde::{Deserialize, Serialize};
+use uuid::Uuid;
use super::ElementName;
use crate::types::Message;
-use crate::Result;
+use crate::Error;
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct RenderedElement {
- id: String,
+ #[serde(skip, default = "Uuid::new_v4")]
+ id: Uuid,
child_elements: Option<Vec<RenderedElement>>,
name: ElementName,
options: BTreeMap<String, Option<String>>,
@@ -20,7 +21,7 @@ pub struct RenderedElement {
impl RenderedElement {
fn new(name: ElementName) -> Self {
Self {
- id: random_blob(),
+ id: Uuid::new_v4(),
child_elements: None,
name,
options: BTreeMap::new(),
@@ -29,7 +30,7 @@ impl RenderedElement {
fn with(name: ElementName, child_elements: Vec<RenderedElement>) -> Self {
Self {
- id: random_blob(),
+ id: Uuid::new_v4(),
child_elements: Some(child_elements),
name,
options: BTreeMap::new(),
@@ -37,22 +38,22 @@ impl RenderedElement {
}
pub fn get_id(&self) -> Id {
- Id::new(self.id.clone())
+ Id::new(self.id.to_string())
}
- pub fn find_by_id(&mut self, id: Id) -> Option<&mut Self> {
- if self.get_id() == id.clone() {
- return Some(self);
+ pub fn find_by_id(&mut self, id: &Id) -> Option<&mut Self> {
+ if &self.get_id() == id {
+ Some(self)
} else if let Some(child_elements) = self.child_elements.as_mut() {
for element in child_elements {
- let element = element.find_by_id(id.clone());
+ let element = element.find_by_id(id);
if element.is_some() {
return element;
}
}
- return None;
+ None
} else {
- return None;
+ None
}
}
@@ -66,24 +67,21 @@ impl RenderedElement {
if self
.child_elements
.clone()
- .unwrap_or(vec![])
+ .unwrap_or_default()
.contains(child_element)
{
return Some(self);
- } else {
- if let Some(child_elements) = self.child_elements.as_mut() {
- for element in child_elements {
- let element = element.find_parent(child_element);
- if element.is_some() {
- return element;
- }
+ }
+ if let Some(child_elements) = self.child_elements.as_mut() {
+ for element in child_elements {
+ let element = element.find_parent(child_element);
+ if element.is_some() {
+ return element;
}
}
- return None;
}
- } else {
- return None;
}
+ None
}
pub fn is_parent(&self) -> bool {
@@ -111,10 +109,10 @@ impl RenderedElement {
}
}
- pub fn insert_after(&mut self, id: Id, element: &RenderedElement) {
+ pub fn insert_after(&mut self, id: &Id, element: &RenderedElement) {
if let Some(child_elements) = self.child_elements.as_mut() {
if let Some(index) =
- child_elements.iter().position(|x| x.get_id() == id)
+ child_elements.iter().position(|x| &x.get_id() == id)
{
child_elements.insert(index + 1, element.clone());
} else {
@@ -127,7 +125,7 @@ impl RenderedElement {
&self,
element_tree: Option<&mut RenderedElement>,
action: Action,
- ) -> Result<()> {
+ ) -> Result<(), Error> {
let element_tree = element_tree.unwrap();
match action {
@@ -159,7 +157,7 @@ impl RenderedElement {
}
}
- fn preset_options<'a>(mut self, options: &[&'a str]) -> Self {
+ fn preset_options(mut self, options: &[&str]) -> Self {
for opt in options {
let _ = self.options.insert(opt.to_string(), None);
}
@@ -174,12 +172,12 @@ impl RenderedElement {
self
}
- pub fn as_element<'a>(self) -> Element<'a, Message> {
+ pub fn into_element<'a>(self) -> Element<'a, Message> {
let mut children = widget::column![];
if let Some(els) = self.child_elements.clone() {
for el in els {
- children = children.push(el.clone().as_element());
+ children = children.push(el.clone().into_element());
}
}
iced_drop::droppable(
@@ -219,7 +217,7 @@ impl RenderedElement {
for element in els {
let (c_imports, children) = element.codegen();
imports = format!("{imports}{c_imports}");
- elements = format!("{elements}{},", children);
+ elements = format!("{elements}{children},");
}
}
@@ -262,7 +260,7 @@ impl RenderedElement {
imports = format!("{imports}image,");
view = format!("{view}\nimage(\"{path}\"){options}");
}
- ElementName::SVG(path) => {
+ ElementName::Svg(path) => {
imports = format!("{imports}svg,");
view = format!("{view}\nsvg(\"{path}\"){options}");
}
@@ -336,7 +334,7 @@ impl<'a> From<RenderedElement> for Element<'a, Message> {
widget::button(widget::text(s)).into()
}
}
- ElementName::SVG(p) => widget::svg(p).into(),
+ ElementName::Svg(p) => widget::svg(p).into(),
ElementName::Image(p) => widget::image(p).into(),
ElementName::Container => {
widget::container(if child_elements.len() == 1 {
@@ -347,13 +345,13 @@ impl<'a> From<RenderedElement> for Element<'a, Message> {
.padding(20)
.into()
}
- ElementName::Row => widget::Row::from_iter(
- child_elements.into_iter().map(|el| el.into()),
+ ElementName::Row => widget::Row::from_vec(
+ child_elements.into_iter().map(Into::into).collect(),
)
.padding(20)
.into(),
- ElementName::Column => widget::Column::from_iter(
- child_elements.into_iter().map(|el| el.into()),
+ ElementName::Column => widget::Column::from_vec(
+ child_elements.into_iter().map(Into::into).collect(),
)
.padding(20)
.into(),
@@ -369,18 +367,18 @@ impl<'a> From<RenderedElement> for Element<'a, Message> {
}
#[derive(Debug, Clone)]
-pub enum Action {
+pub enum Action<'a> {
AddNew,
- PushFront(Id),
- InsertAfter(Id, Id),
+ PushFront(&'a Id),
+ InsertAfter(&'a Id, &'a Id),
Drop,
Stop,
}
-impl Action {
+impl<'a> Action<'a> {
pub fn new(
- ids: Vec<Id>,
- element_tree: &mut Option<RenderedElement>,
+ ids: &'a [Id],
+ element_tree: &'a Option<RenderedElement>,
source_id: Option<Id>,
) -> Self {
let mut action = Self::Stop;
@@ -391,51 +389,39 @@ impl Action {
action = Self::Drop;
}
} else {
- let id: Id = match source_id {
+ let id: &Id = match source_id {
Some(id) if ids.contains(&id) => {
let element_id =
- ids[ids.iter().position(|x| *x == id).unwrap()].clone();
- if ids.len() > 2 && ids[ids.clone().len() - 1] == element_id
- {
+ &ids[ids.iter().position(|x| *x == id).unwrap()];
+ if ids.len() > 2 && &ids[ids.len() - 1] == element_id {
return Self::Stop;
}
element_id
}
- _ => ids.last().cloned().unwrap(),
+ _ => ids.last().unwrap(),
};
- let element = element_tree
- .as_mut()
- .unwrap()
- .find_by_id(id.clone())
- .unwrap();
-
- // Element IS a parent but ISN'T a non-empty container
- match element.is_parent()
- && !(element.name == ElementName::Container
- && !element.is_empty())
+ let mut element_tree = element_tree.clone().unwrap();
+ let element = element_tree.find_by_id(id).unwrap();
+
+ // Element is a parent and isn't a non-empty container
+ if (element.is_empty() || !(element.name == ElementName::Container))
+ && element.is_parent()
{
- true => {
- action = Self::PushFront(id);
- }
- false if ids.len() > 2 => {
- let parent = element_tree
- .as_mut()
- .unwrap()
- .find_by_id(ids[&ids.len() - 2].clone())
- .unwrap();
-
- if parent.name == ElementName::Container
- && parent.child_elements != Some(vec![])
- {
- action = Self::Stop;
- } else {
- action = Self::InsertAfter(
- ids[&ids.len() - 2].clone(),
- ids[&ids.len() - 1].clone(),
- );
- }
+ action = Self::PushFront(id);
+ } else if ids.len() > 2 {
+ let parent =
+ element_tree.find_by_id(&ids[ids.len() - 2]).unwrap();
+
+ if parent.name == ElementName::Container
+ && parent.child_elements != Some(vec![])
+ {
+ action = Self::Stop;
+ } else {
+ action = Self::InsertAfter(
+ &ids[ids.len() - 2],
+ &ids[ids.len() - 1],
+ );
}
- _ => {}
}
}
action
@@ -456,7 +442,7 @@ pub fn button(text: &str) -> RenderedElement {
}
pub fn svg(path: &str) -> RenderedElement {
- RenderedElement::new(ElementName::SVG(path.to_owned()))
+ RenderedElement::new(ElementName::Svg(path.to_owned()))
}
pub fn image(path: &str) -> RenderedElement {