summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorpml68 <contact@pml68.dev>2025-04-18 00:54:08 +0200
committerpml68 <contact@pml68.dev>2025-04-18 00:54:08 +0200
commitf7274ea5efea2055f8f71d7cde7fc7e2704648cd (patch)
tree2a3d788a5dbfd636f86e37e76280f3825f546e96
parentfix: `iced` 0.14 codegen (diff)
downloadiced-builder-f7274ea5efea2055f8f71d7cde7fc7e2704648cd.tar.gz
refactor: load config in `IcedBuilder::init` instead of `main`
-rw-r--r--src/main.rs66
-rw-r--r--src/types.rs2
2 files changed, 36 insertions, 32 deletions
diff --git a/src/main.rs b/src/main.rs
index c7e6314..0f1a1bf 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -29,7 +29,6 @@ use iced_anim::transition::Easing;
use iced_anim::{Animated, Animation};
use iced_dialog::dialog::Dialog;
use panes::{code_view, designer_view, element_list};
-use tokio::runtime;
use types::{
Action, DesignerPane, DialogAction, DialogButtons, Message, Project,
};
@@ -46,23 +45,15 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
return Ok(());
}
- let config_load = {
- let rt = runtime::Builder::new_current_thread()
- .enable_all()
- .build()?;
-
- rt.block_on(Config::load())
- };
-
iced::application(
- move || App::new(config_load.clone()),
- App::update,
- App::view,
+ IcedBuilder::init,
+ IcedBuilder::update,
+ IcedBuilder::view,
)
- .title(App::title)
+ .title(IcedBuilder::title)
.font(icon::FONT)
.theme(|state| state.theme.value().clone())
- .subscription(App::subscription)
+ .subscription(IcedBuilder::subscription)
.antialiasing(true)
.centered()
.run()?;
@@ -70,7 +61,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
Ok(())
}
-struct App {
+struct IcedBuilder {
is_dirty: bool,
is_loading: bool,
project_path: Option<PathBuf>,
@@ -94,8 +85,8 @@ enum Panes {
ElementList,
}
-impl App {
- fn new(config_load: Result<Config, Error>) -> (Self, Task<Message>) {
+impl IcedBuilder {
+ fn init() -> (Self, Task<Message>) {
let state = pane_grid::State::with_configuration(
pane_grid::Configuration::Split {
axis: pane_grid::Axis::Vertical,
@@ -105,22 +96,9 @@ impl App {
},
);
- let config = Arc::new(config_load.unwrap_or_default());
+ let config = Arc::new(Config::default());
let theme = config.selected_theme();
- let task = if let Some(path) = config.last_project.clone() {
- if path.exists() && path.is_file() {
- Task::perform(Project::from_path(path), Message::FileOpened)
- } else {
- warning_dialog(format!(
- "The file {} does not exist, or isn't a file.",
- path.to_string_lossy()
- ))
- }
- } else {
- Task::none()
- };
-
(
Self {
is_dirty: false,
@@ -139,7 +117,7 @@ impl App {
dialog_action: DialogAction::None,
editor_content: text_editor::Content::new(),
},
- task,
+ Task::perform(Config::load(), Message::ConfigLoad),
)
}
@@ -165,6 +143,30 @@ impl App {
fn update(&mut self, message: Message) -> Task<Message> {
match message {
+ Message::ConfigLoad(result) => match result {
+ Ok(config) => {
+ self.config = Arc::new(config);
+
+ self.theme.update(self.config.selected_theme().into());
+ return if let Some(path) = self.config.last_project.clone()
+ {
+ if path.exists() && path.is_file() {
+ Task::perform(
+ Project::from_path(path),
+ Message::FileOpened,
+ )
+ } else {
+ warning_dialog(format!(
+ "The file {} does not exist, or isn't a file.",
+ path.to_string_lossy()
+ ))
+ }
+ } else {
+ Task::none()
+ };
+ }
+ Err(error) => return error_dialog(error),
+ },
Message::SwitchTheme(event) => self.theme.update(event),
Message::CopyCode => {
return clipboard::write(self.editor_content.text());
diff --git a/src/types.rs b/src/types.rs
index a7fae1c..adb788e 100644
--- a/src/types.rs
+++ b/src/types.rs
@@ -12,9 +12,11 @@ pub use project::Project;
pub use rendered_element::*;
use crate::Error;
+use crate::config::Config;
#[derive(Debug, Clone)]
pub enum Message {
+ ConfigLoad(Result<Config, Error>),
SwitchTheme(Event<iced::Theme>),
CopyCode,
SwitchPage(DesignerPane),