summaryrefslogtreecommitdiff
path: root/iced_builder/src/main.rs
diff options
context:
space:
mode:
authorpml68 <contact@pml68.dev>2024-12-30 02:15:10 +0100
committerpml68 <contact@pml68.dev>2024-12-30 02:15:10 +0100
commit0dad6dd5b8395d3089bed022a4b8830f7cae7d9f (patch)
treee28ccc225c07f9b49a1c233fb81c8eddd75a01c0 /iced_builder/src/main.rs
parentrefactor!: switch to `uuid` for uuid generation (diff)
downloadiced-builder-0dad6dd5b8395d3089bed022a4b8830f7cae7d9f.tar.gz
feat: add config loading, with theming support, limited to Palette for now
Diffstat (limited to 'iced_builder/src/main.rs')
-rw-r--r--iced_builder/src/main.rs71
1 files changed, 56 insertions, 15 deletions
diff --git a/iced_builder/src/main.rs b/iced_builder/src/main.rs
index a041c6f..02e1ae0 100644
--- a/iced_builder/src/main.rs
+++ b/iced_builder/src/main.rs
@@ -5,22 +5,34 @@ use iced::widget::pane_grid::{self, Pane, PaneGrid};
use iced::widget::{container, pick_list, row, text_editor, Column};
use iced::{clipboard, keyboard, Alignment, Element, Length, Task, Theme};
use iced_anim::{Animation, Spring};
-use iced_builder::dialogs::{error_dialog, unsaved_changes_dialog};
-use iced_builder::icon;
+use iced_builder::config::Config;
+use iced_builder::dialogs::{
+ error_dialog, unsaved_changes_dialog, warning_dialog,
+};
use iced_builder::panes::{code_view, designer_view, element_list};
use iced_builder::types::{
Action, DesignerPage, ElementName, Message, Project,
};
+use iced_builder::{icon, Error};
use rfd::MessageDialogResult;
+use tokio::runtime;
+
+fn main() -> Result<(), Box<dyn std::error::Error>> {
+ let config_load = {
+ let rt = runtime::Builder::new_current_thread()
+ .enable_all()
+ .build()?;
-const THEMES: &'static [Theme] = &[Theme::SolarizedDark, Theme::SolarizedLight];
+ rt.block_on(Config::load())
+ };
-fn main() -> iced::Result {
iced::application(App::title, App::update, App::view)
.font(icon::FONT)
.theme(|state| state.theme.value().clone())
.subscription(App::subscription)
- .run_with(App::new)
+ .run_with(move || App::new(config_load))?;
+
+ Ok(())
}
struct App {
@@ -28,6 +40,7 @@ struct App {
is_loading: bool,
project_path: Option<PathBuf>,
project: Project,
+ config: Config,
theme: Spring<Theme>,
pane_state: pane_grid::State<Panes>,
focus: Option<Pane>,
@@ -43,7 +56,7 @@ enum Panes {
}
impl App {
- fn new() -> (Self, Task<Message>) {
+ fn new(config_load: Result<Config, Error>) -> (Self, Task<Message>) {
let state = pane_grid::State::with_configuration(
pane_grid::Configuration::Split {
axis: pane_grid::Axis::Vertical,
@@ -52,20 +65,48 @@ impl App {
b: Box::new(pane_grid::Configuration::Pane(Panes::ElementList)),
},
);
+
+ let config = match config_load {
+ Ok(config) => {
+ println!("{config:?}");
+ config
+ }
+ Err(_) => Config::default(),
+ };
+
+ let theme = config.selected_theme();
+
+ let mut task = Task::none();
+
+ if let Some(path) = config.last_project.clone() {
+ if path.exists() && path.is_file() {
+ task = 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().to_string()
+ ));
+ }
+ }
+
(
Self {
is_dirty: false,
is_loading: false,
project_path: None,
project: Project::new(),
- theme: Spring::new(Theme::SolarizedDark),
+ config,
+ theme: Spring::new(theme),
pane_state: state,
focus: None,
designer_page: DesignerPage::DesignerView,
element_list: ElementName::ALL,
editor_content: text_editor::Content::new(),
},
- Task::none(),
+ task,
)
}
@@ -104,7 +145,7 @@ impl App {
}
}
Message::RefreshEditorContent => {
- match self.project.clone().app_code() {
+ match self.project.clone().app_code(&self.config) {
Ok(code) => {
self.editor_content =
text_editor::Content::with_text(&code);
@@ -209,14 +250,14 @@ impl App {
self.is_loading = true;
return Task::perform(
- Project::from_path(),
+ Project::from_file(),
Message::FileOpened,
);
} else {
if let MessageDialogResult::Ok = unsaved_changes_dialog("You have unsaved changes. Do you wish to discard these and open another project?") {
self.is_dirty = false;
self.is_loading = true;
- return Task::perform(Project::from_path(), Message::FileOpened);
+ return Task::perform(Project::from_file(), Message::FileOpened);
}
}
}
@@ -231,7 +272,7 @@ impl App {
self.project_path = Some(path);
self.editor_content = text_editor::Content::with_text(
&project
- .app_code()
+ .app_code(&self.config)
.unwrap_or_else(|err| err.to_string()),
);
}
@@ -299,7 +340,7 @@ impl App {
fn view(&self) -> Element<'_, Message> {
let header = row![pick_list(
- THEMES,
+ self.config.theme.all.clone(),
Some(self.theme.target().clone()),
|theme| { Message::ToggleTheme(theme.into()) }
)]
@@ -311,12 +352,12 @@ impl App {
Panes::Designer => match &self.designer_page {
DesignerPage::DesignerView => designer_view::view(
&self.project.element_tree,
- self.project.get_theme(),
+ self.project.get_theme(&self.config),
is_focused,
),
DesignerPage::CodeView => code_view::view(
&self.editor_content,
- self.theme.value().clone(),
+ self.theme.target().clone(),
is_focused,
),
},