From b30cadff915c0d97c06f9ecba05ad2ac7674d7fe Mon Sep 17 00:00:00 2001 From: pml68 Date: Sun, 15 Jun 2025 00:30:27 +0200 Subject: style: `ḋialogs` -> `dialog` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/dialog.rs | 103 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/dialogs.rs | 103 --------------------------------------------------------- src/main.rs | 8 ++--- 3 files changed, 107 insertions(+), 107 deletions(-) create mode 100644 src/dialog.rs delete mode 100644 src/dialogs.rs (limited to 'src') diff --git a/src/dialog.rs b/src/dialog.rs new file mode 100644 index 0000000..68bbd6e --- /dev/null +++ b/src/dialog.rs @@ -0,0 +1,103 @@ +use std::borrow::Cow; + +use iced::widget::text; +use iced_dialog::button; + +use crate::Message; +use crate::types::Element; + +pub const UNSAVED_CHANGES_TITLE: &str = "Hold on for a sec!"; +pub const WARNING_TITLE: &str = "Heads up!"; +pub const ERROR_TITLE: &str = "Oops! Something went wrong."; + +#[derive(Debug, Clone, Copy, Default)] +pub enum Action { + #[default] + None, + Close, + UnsavedChanges(UnsavedChanges), +} + +#[derive(Debug, Clone, Copy)] +pub enum UnsavedChanges { + New, + Open, + Exit, +} + +impl From for Vec> { + fn from(action: Action) -> Self { + match action { + Action::None => vec![], + Action::Close => vec![button("Close", Message::DialogYes).into()], + Action::UnsavedChanges(_) => vec![ + button("Don't Save", Message::DialogNo).into(), + button("Save", Message::DialogYes).into(), + button("Cancel", Message::DialogCancel).into(), + ], + } + } +} + +#[derive(Debug, Default)] +pub struct Dialog { + is_open: bool, + title: Cow<'static, str>, + content: Cow<'static, str>, + action: Action, +} + +impl Dialog { + pub fn new( + title: impl Into>, + content: impl Into>, + action: Action, + ) -> Self { + Self { + is_open: true, + title: title.into(), + content: content.into(), + action, + } + } + + pub fn error(content: impl Into>) -> Self { + Self::new(ERROR_TITLE, content, Action::Close) + } + + pub fn warning(content: impl Into>) -> Self { + Self::new(WARNING_TITLE, content, Action::Close) + } + + pub fn unsaved_changes( + content: impl Into>, + unsaved_changes: UnsavedChanges, + ) -> Self { + Self::new( + UNSAVED_CHANGES_TITLE, + content, + Action::UnsavedChanges(unsaved_changes), + ) + } + + pub fn close(&mut self) { + self.is_open = false; + } + + pub fn action(&self) -> Action { + self.action + } + + pub fn as_iced_dialog<'a>( + &'a self, + base: impl Into>, + ) -> iced_dialog::Dialog<'a, Message, material_theme::Theme> { + iced_dialog::Dialog::with_buttons( + self.is_open, + base, + text(&*self.content), + self.action.into(), + ) + .title(&*self.title) + } +} diff --git a/src/dialogs.rs b/src/dialogs.rs deleted file mode 100644 index 68bbd6e..0000000 --- a/src/dialogs.rs +++ /dev/null @@ -1,103 +0,0 @@ -use std::borrow::Cow; - -use iced::widget::text; -use iced_dialog::button; - -use crate::Message; -use crate::types::Element; - -pub const UNSAVED_CHANGES_TITLE: &str = "Hold on for a sec!"; -pub const WARNING_TITLE: &str = "Heads up!"; -pub const ERROR_TITLE: &str = "Oops! Something went wrong."; - -#[derive(Debug, Clone, Copy, Default)] -pub enum Action { - #[default] - None, - Close, - UnsavedChanges(UnsavedChanges), -} - -#[derive(Debug, Clone, Copy)] -pub enum UnsavedChanges { - New, - Open, - Exit, -} - -impl From for Vec> { - fn from(action: Action) -> Self { - match action { - Action::None => vec![], - Action::Close => vec![button("Close", Message::DialogYes).into()], - Action::UnsavedChanges(_) => vec![ - button("Don't Save", Message::DialogNo).into(), - button("Save", Message::DialogYes).into(), - button("Cancel", Message::DialogCancel).into(), - ], - } - } -} - -#[derive(Debug, Default)] -pub struct Dialog { - is_open: bool, - title: Cow<'static, str>, - content: Cow<'static, str>, - action: Action, -} - -impl Dialog { - pub fn new( - title: impl Into>, - content: impl Into>, - action: Action, - ) -> Self { - Self { - is_open: true, - title: title.into(), - content: content.into(), - action, - } - } - - pub fn error(content: impl Into>) -> Self { - Self::new(ERROR_TITLE, content, Action::Close) - } - - pub fn warning(content: impl Into>) -> Self { - Self::new(WARNING_TITLE, content, Action::Close) - } - - pub fn unsaved_changes( - content: impl Into>, - unsaved_changes: UnsavedChanges, - ) -> Self { - Self::new( - UNSAVED_CHANGES_TITLE, - content, - Action::UnsavedChanges(unsaved_changes), - ) - } - - pub fn close(&mut self) { - self.is_open = false; - } - - pub fn action(&self) -> Action { - self.action - } - - pub fn as_iced_dialog<'a>( - &'a self, - base: impl Into>, - ) -> iced_dialog::Dialog<'a, Message, material_theme::Theme> { - iced_dialog::Dialog::with_buttons( - self.is_open, - base, - text(&*self.content), - self.action.into(), - ) - .title(&*self.title) - } -} diff --git a/src/main.rs b/src/main.rs index db8e4e9..d0dbd9e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,6 @@ mod appearance; mod config; -mod dialogs; +mod dialog; mod environment; mod error; #[allow(dead_code)] @@ -14,7 +14,7 @@ mod widget; use std::path::PathBuf; use config::Config; -use dialogs::{Action as DialogAction, Dialog, UnsavedChanges}; +use dialog::{Dialog, UnsavedChanges}; use error::Error; use iced::advanced::widget::Id; use iced::widget::{Column, container, pane_grid, pick_list, row, text_editor}; @@ -234,7 +234,7 @@ impl IcedBuilder { Message::DialogYes => { return if matches!( self.dialog.action(), - DialogAction::UnsavedChanges(_) + dialog::Action::UnsavedChanges(_) ) { self.is_loading = true; Task::perform( @@ -251,7 +251,7 @@ impl IcedBuilder { Message::DialogNo => { let mut task = Task::done(Message::CloseDialog); - if let DialogAction::UnsavedChanges(unsaved_changes) = + if let dialog::Action::UnsavedChanges(unsaved_changes) = self.dialog.action() { match unsaved_changes { -- cgit v1.2.3