summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorpml68 <contact@pml68.me>2024-10-26 21:52:37 +0200
committerpml68 <contact@pml68.me>2024-10-26 21:52:37 +0200
commita2f3e993ac1e0d4f57d7c41d6601f7067190255b (patch)
tree8c8c764a836c7461732aa12b9cc54b38aab7ace4
parenttest(rendered_element): remove unused `test` method (diff)
downloadiced-builder-a2f3e993ac1e0d4f57d7c41d6601f7067190255b.tar.gz
feat: add C-S-s keybind for `Save As`
Diffstat (limited to '')
-rw-r--r--iced_builder/src/lib.rs1
-rw-r--r--iced_builder/src/main.rs34
-rw-r--r--iced_builder/src/types/project.rs16
-rwxr-xr-xiced_builder/src/types/rendered_element.rs24
4 files changed, 43 insertions, 32 deletions
diff --git a/iced_builder/src/lib.rs b/iced_builder/src/lib.rs
index f18e31d..1c7af06 100644
--- a/iced_builder/src/lib.rs
+++ b/iced_builder/src/lib.rs
@@ -88,5 +88,6 @@ pub enum Message {
OpenFile,
FileOpened(Result<(PathBuf, Project), Error>),
SaveFile,
+ SaveFileAs,
FileSaved(Result<PathBuf, Error>),
}
diff --git a/iced_builder/src/main.rs b/iced_builder/src/main.rs
index 32b52c5..8d8c382 100644
--- a/iced_builder/src/main.rs
+++ b/iced_builder/src/main.rs
@@ -14,9 +14,9 @@ use iced_builder::{
types::{
element_name::ElementName, project::Project, rendered_element::ActionKind, DesignerPage,
},
- views::{designer_view, element_list},
+ views::{code_view, designer_view, element_list},
+ Message,
};
-use iced_builder::{views::code_view, Message};
fn main() -> iced::Result {
iced::application(App::title, App::update, App::view)
@@ -131,10 +131,10 @@ impl App {
Message::HandleNew(name, zones) => {
let ids: Vec<Id> = zones.into_iter().map(|z| z.0).collect();
if ids.len() > 0 {
- let action = ActionKind::new(ids, &mut self.project.content.clone(), None);
- let result = name.handle_action(self.project.content.as_mut(), action);
+ let action = ActionKind::new(ids, &mut self.project.element_tree.clone(), None);
+ let result = name.handle_action(self.project.element_tree.as_mut(), action);
if let Ok(Some(ref element)) = result {
- self.project.content = Some(element.clone());
+ self.project.element_tree = Some(element.clone());
}
println!("{:?}", result);
}
@@ -155,10 +155,10 @@ impl App {
if ids.len() > 0 {
let action = ActionKind::new(
ids,
- &mut self.project.content.clone(),
+ &mut self.project.element_tree.clone(),
Some(element.get_id()),
);
- let result = element.handle_action(self.project.content.as_mut(), action);
+ let result = element.handle_action(self.project.element_tree.as_mut(), action);
println!("{result:?}");
}
@@ -213,6 +213,16 @@ impl App {
);
}
}
+ Message::SaveFileAs => {
+ if !self.is_loading {
+ self.is_loading = true;
+
+ return Task::perform(
+ self.project.clone().write_to_file(None),
+ Message::FileSaved,
+ );
+ }
+ }
Message::FileSaved(result) => {
self.is_loading = false;
@@ -229,7 +239,13 @@ impl App {
fn subscription(&self) -> iced::Subscription<Message> {
keyboard::on_key_press(|key, modifiers| match key.as_ref() {
keyboard::Key::Character("o") if modifiers.command() => Some(Message::OpenFile),
- keyboard::Key::Character("s") if modifiers.command() => Some(Message::SaveFile),
+ keyboard::Key::Character("s") if modifiers.command() => {
+ if modifiers.shift() {
+ Some(Message::SaveFileAs)
+ } else {
+ Some(Message::SaveFile)
+ }
+ }
keyboard::Key::Character("n") if modifiers.command() => Some(Message::NewFile),
_ => None,
})
@@ -245,7 +261,7 @@ impl App {
match pane {
Panes::Designer => match &self.designer_page {
DesignerPage::Designer => designer_view::view(
- &self.project.content,
+ &self.project.element_tree,
self.project.get_theme(),
is_focused,
),
diff --git a/iced_builder/src/types/project.rs b/iced_builder/src/types/project.rs
index 95de9e6..52da41c 100644
--- a/iced_builder/src/types/project.rs
+++ b/iced_builder/src/types/project.rs
@@ -12,7 +12,7 @@ use super::rendered_element::RenderedElement;
pub struct Project {
pub title: Option<String>,
pub theme: Option<String>,
- pub content: Option<RenderedElement>,
+ pub element_tree: Option<RenderedElement>,
}
impl Project {
@@ -20,7 +20,7 @@ impl Project {
Self {
title: None,
theme: None,
- content: None,
+ element_tree: None,
}
}
@@ -92,10 +92,10 @@ impl Project {
Ok(path)
}
- pub fn app_code(self) -> Result<String, Error> {
- match &self.content {
- Some(el) => {
- let (imports, view) = el.codegen();
+ pub fn app_code(&self) -> 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!(
@@ -119,8 +119,8 @@ impl Project {
{view}.into()
}}
}}"#,
- match &self.title {
- Some(t) => t,
+ match self.title {
+ Some(ref t) => t,
None => "New app",
},
self.get_theme().to_string().replace(" ", "")
diff --git a/iced_builder/src/types/rendered_element.rs b/iced_builder/src/types/rendered_element.rs
index 5b7777a..50e591a 100755
--- a/iced_builder/src/types/rendered_element.rs
+++ b/iced_builder/src/types/rendered_element.rs
@@ -127,7 +127,7 @@ impl RenderedElement {
match action {
ActionKind::Stop => Ok(()),
ActionKind::AddNew => Err(
- "The action was of kind `AddNew`, but invoking it on an existing element tree is not possible.".into(),
+ "the action was of kind `AddNew`, but invoking it on an existing element tree is not possible".into(),
),
ActionKind::PushFront(id) => {
let old_parent = element_tree.find_parent(self).unwrap();
@@ -335,14 +335,14 @@ impl ActionKind {
.find_by_id(id.clone())
.unwrap();
- match (
- element.is_parent(),
- element.name == ElementName::Container && !element.is_empty(),
- ) {
- (true, false) => {
+ // Element IS a parent but ISN'T a non-empty container
+ match element.is_parent()
+ && !(element.name == ElementName::Container && !element.is_empty())
+ {
+ true => {
action = Self::PushFront(id);
}
- _ if ids.len() > 2 => {
+ false if ids.len() > 2 => {
let parent = element_tree
.as_mut()
.unwrap()
@@ -396,15 +396,9 @@ pub fn container(content: Option<RenderedElement>) -> RenderedElement {
}
pub fn row(child_elements: Option<Vec<RenderedElement>>) -> RenderedElement {
- match child_elements {
- Some(els) => RenderedElement::with(ElementName::Row, els),
- None => RenderedElement::with(ElementName::Row, vec![]),
- }
+ RenderedElement::with(ElementName::Row, child_elements.unwrap_or_default())
}
pub fn column(child_elements: Option<Vec<RenderedElement>>) -> RenderedElement {
- match child_elements {
- Some(els) => RenderedElement::with(ElementName::Column, els),
- None => RenderedElement::with(ElementName::Column, vec![]),
- }
+ RenderedElement::with(ElementName::Column, child_elements.unwrap_or_default())
}