summaryrefslogtreecommitdiff
path: root/src/main.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs99
1 files changed, 77 insertions, 22 deletions
diff --git a/src/main.rs b/src/main.rs
index d04183a..add73ec 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -20,13 +20,19 @@ use config::Config;
use dialog::{Dialog, UnsavedChanges};
use error::Error;
use iced::advanced::widget::Id;
-use iced::widget::{column, container, pane_grid, pick_list, row, text_editor};
+use iced::system::theme_changes;
+use iced::theme::{self, Base};
+use iced::time::Duration;
+use iced::widget::{
+ checkbox, column, container, pane_grid, pick_list, row, text, text_editor,
+};
use iced::{
- Alignment, Length, Subscription, Task, clipboard, keyboard, window,
+ Alignment, Length, Subscription, Task, border, clipboard, keyboard, window,
};
use iced_anim::transition::Easing;
use iced_anim::{Animated, Animation};
use iced_material::Theme;
+use iced_material::utils::{disabled_container, disabled_text};
use panes::{code_view, designer_view, element_list};
use types::{
Action, ConfigChangeType, DesignerPane, Element, Message, Panes, Project,
@@ -79,6 +85,8 @@ struct IcedBuilder {
project: Project,
config: Config,
theme: Animated<Theme>,
+ theme_mode: theme::Mode,
+ use_system_theme: bool,
pane_state: pane_grid::State<Panes>,
focus: Option<pane_grid::Pane>,
designer_page: DesignerPane,
@@ -119,7 +127,10 @@ impl IcedBuilder {
project_path,
project: Project::new(),
config,
- theme: Animated::new(theme, Easing::EASE_IN),
+ theme: Animated::new(theme, Easing::EASE_IN)
+ .with_duration(Duration::from_millis(300)),
+ theme_mode: theme::Mode::Dark,
+ use_system_theme: false,
pane_state: state,
focus: None,
designer_page: DesignerPane::DesignerView,
@@ -203,10 +214,25 @@ impl IcedBuilder {
Message::SwitchTheme(event) => {
self.theme.update(event);
- return self.update(ConfigChangeType::SelectedTheme.into());
+ if !self.use_system_theme {
+ return self.update(ConfigChangeType::SelectedTheme.into());
+ }
}
- Message::SystemThemeChanged(theme) => {
- Theme::update_system_theme(theme);
+ Message::SystemThemeChanged(mode) => {
+ self.theme_mode = mode;
+
+ if self.use_system_theme {
+ self.theme.set_target(Theme::default(mode));
+ }
+ }
+ Message::UseSystemTheme(use_system_theme) => {
+ self.use_system_theme = use_system_theme;
+
+ self.theme.set_target(if use_system_theme {
+ Theme::default(self.theme_mode)
+ } else {
+ self.config.selected_theme()
+ });
}
Message::CopyCode => {
return clipboard::write(self.editor_content.text());
@@ -217,13 +243,15 @@ impl IcedBuilder {
self.editor_content.perform(action);
}
}
- Message::RefreshEditorContent => match self.project.app_code() {
- Ok(code) => {
- self.editor_content =
- text_editor::Content::with_text(&code);
+ Message::RefreshEditorContent => {
+ match self.project.app_code(self.theme_mode) {
+ Ok(code) => {
+ self.editor_content =
+ text_editor::Content::with_text(&code);
+ }
+ Err(error) => self.dialog = Dialog::error(error),
}
- Err(error) => self.dialog = Dialog::error(error),
- },
+ }
Message::DropNewElement(name, point, _) => {
return iced_drop::zones_on_point(
move |zones| Message::HandleNew(name.clone(), zones),
@@ -434,7 +462,7 @@ impl IcedBuilder {
}
}
Message::CloseApp => {
- return window::get_latest().and_then(window::close);
+ return iced::exit();
}
Message::EscapePressed
if self.dialog.action() == dialog::Action::Close =>
@@ -485,19 +513,46 @@ impl IcedBuilder {
let window_events =
window::events().map(|(_id, event)| Message::WindowEvent(event));
- let system_theme =
- Theme::subscription().map(Message::SystemThemeChanged);
+ let system_theme = theme_changes().map(Message::SystemThemeChanged);
Subscription::batch([keyboard, window_events, system_theme])
}
fn view(&self) -> Element<'_, Message> {
- let header = row![pick_list(
- self.config.themes(),
- Some(self.theme.target()),
- |theme| Message::SwitchTheme(theme.into())
- )]
- .width(200);
+ let header = row![
+ if self.use_system_theme {
+ Element::from(
+ container(text(self.config.selected_theme().to_string()))
+ .padding([5, 10])
+ .width(Length::Fill)
+ .style(|theme: &Theme| {
+ let color = theme.colors().surface.on_surface;
+ container::Style {
+ background: Some(
+ disabled_container(color).into(),
+ ),
+ text_color: Some(disabled_text(color)),
+ border: border::rounded(4),
+ ..Default::default()
+ }
+ }),
+ )
+ } else {
+ Element::from(
+ pick_list(
+ self.config.themes(),
+ Some(self.theme.target()),
+ |theme| Message::SwitchTheme(theme.into()),
+ )
+ .width(Length::Fill),
+ )
+ },
+ checkbox("Use system theme", self.use_system_theme)
+ .on_toggle(Message::UseSystemTheme)
+ ]
+ .spacing(10)
+ .align_y(Alignment::Center)
+ .width(500);
let pane_grid =
pane_grid(&self.pane_state, |id, pane, _is_maximized| {
let is_focused = Some(id) == self.focus;
@@ -505,7 +560,7 @@ impl IcedBuilder {
Panes::Designer => match &self.designer_page {
DesignerPane::DesignerView => designer_view::view(
self.project.element_tree.as_ref(),
- self.project.get_theme(),
+ self.project.get_theme(self.theme_mode),
is_focused,
),
DesignerPane::CodeView => {