From 4e5f908c844088225f22233541b2a2aacb48c8c0 Mon Sep 17 00:00:00 2001 From: Michael Aaron Murphy Date: Tue, 15 Aug 2023 17:20:19 +0200 Subject: [PATCH] refactor: combine open and save dialogs --- examples/open-dialog/src/main.rs | 20 +- src/command/mod.rs | 2 +- src/dialog/file_chooser/mod.rs | 216 +++++++++++++++++++++ src/dialog/file_chooser/open.rs | 90 +++++++++ src/dialog/file_chooser/save.rs | 99 ++++++++++ src/dialog/mod.rs | 3 +- src/dialog/open_file.rs | 252 ------------------------- src/dialog/save_file.rs | 262 -------------------------- src/widget/aspect_ratio.rs | 2 +- src/widget/cosmic_container.rs | 2 +- src/widget/rectangle_tracker/mod.rs | 2 +- src/widget/segmented_button/widget.rs | 2 +- 12 files changed, 421 insertions(+), 531 deletions(-) create mode 100644 src/dialog/file_chooser/mod.rs create mode 100644 src/dialog/file_chooser/open.rs create mode 100644 src/dialog/file_chooser/save.rs delete mode 100644 src/dialog/open_file.rs delete mode 100644 src/dialog/save_file.rs diff --git a/examples/open-dialog/src/main.rs b/examples/open-dialog/src/main.rs index 7c1cdebd057..6533394ae06 100644 --- a/examples/open-dialog/src/main.rs +++ b/examples/open-dialog/src/main.rs @@ -5,7 +5,7 @@ use apply::Apply; use cosmic::app::{Command, Core, Settings}; -use cosmic::dialog::{open_file, FileFilter}; +use cosmic::dialog::file_chooser::{self, FileFilter}; use cosmic::iced_core::Length; use cosmic::{executor, iced, ApplicationExt, Element}; use tokio::io::AsyncReadExt; @@ -27,7 +27,7 @@ fn main() -> Result<(), Box> { pub enum Message { CloseError, DialogClosed, - DialogInit(open_file::Sender), + DialogInit(file_chooser::Sender), DialogOpened, Error(String), FileRead(Url, String), @@ -38,7 +38,7 @@ pub enum Message { /// The [`App`] stores application-specific state. pub struct App { core: Core, - open_sender: Option, + open_sender: Option, file_contents: String, selected_file: Option, error_status: Option, @@ -90,15 +90,15 @@ impl cosmic::Application for App { fn subscription(&self) -> cosmic::iced_futures::Subscription { // Creates a subscription for handling open dialogs. - open_file::subscription(|response| match response { - open_file::Message::Closed => Message::DialogClosed, - open_file::Message::Opened => Message::DialogOpened, - open_file::Message::Selected(files) => match files.uris().first() { + file_chooser::subscription(|response| match response { + file_chooser::Message::Closed => Message::DialogClosed, + file_chooser::Message::Opened => Message::DialogOpened, + file_chooser::Message::Selected(files) => match files.uris().first() { Some(file) => Message::Selected(file.to_owned()), None => Message::DialogClosed, }, - open_file::Message::Init(sender) => Message::DialogInit(sender), - open_file::Message::Err(why) => { + file_chooser::Message::Init(sender) => Message::DialogInit(sender), + file_chooser::Message::Err(why) => { let mut source: &dyn std::error::Error = &why; let mut string = format!("open dialog subscription errored\n cause: {source}"); @@ -180,7 +180,7 @@ impl cosmic::Application for App { // Creates a new open dialog. Message::OpenFile => { if let Some(sender) = self.open_sender.as_mut() { - if let Some(dialog) = open_file::builder() { + if let Some(dialog) = file_chooser::open_file() { eprintln!("opening new dialog"); return dialog diff --git a/src/command/mod.rs b/src/command/mod.rs index 48300e2ba6b..39975d373e0 100644 --- a/src/command/mod.rs +++ b/src/command/mod.rs @@ -23,7 +23,7 @@ pub fn batch(commands: impl IntoIterator>) -> Command { Command::batch(commands) } -/// Yields a command which will run the future on thet runtime executor. +/// Yields a command which will run the future on the runtime executor. pub fn future(future: impl Future + Send + 'static) -> Command { Command::single(Action::Future(Box::pin(future))) } diff --git a/src/dialog/file_chooser/mod.rs b/src/dialog/file_chooser/mod.rs new file mode 100644 index 00000000000..adf61a513a6 --- /dev/null +++ b/src/dialog/file_chooser/mod.rs @@ -0,0 +1,216 @@ +// Copyright 2023 System76 +// SPDX-License-Identifier: MPL-2.0 + +//! Dialogs for opening and save files. + +pub mod open; +pub mod save; + +pub use ashpd::desktop::file_chooser::{Choice, FileFilter, SelectedFiles}; +use iced::futures::{channel, SinkExt, StreamExt}; +use iced::{Command, Subscription}; +use std::sync::atomic::{AtomicBool, Ordering}; +use thiserror::Error; + +/// Prevents duplicate file chooser dialog requests. +static OPENED: AtomicBool = AtomicBool::new(false); + +/// Whether a file chooser dialog is currently active. +fn dialog_active() -> bool { + OPENED.load(Ordering::Relaxed) +} + +/// Sets the existence of a file chooser dialog. +fn dialog_active_set(value: bool) { + OPENED.store(value, Ordering::SeqCst); +} + +/// Creates an [`open::Dialog`] if no other file chooser exists. +pub fn open_file() -> Option { + if dialog_active() { + None + } else { + Some(open::Dialog::new()) + } +} + +/// Creates a [`save::Dialog`] if no other file chooser exists. +pub fn save_file() -> Option { + if dialog_active() { + None + } else { + Some(save::Dialog::new()) + } +} + +/// Creates a subscription fthe file chooser events. +pub fn subscription(handle: fn(Message) -> M) -> Subscription { + let type_id = std::any::TypeId::of::>(); + + iced::subscription::channel(type_id, 1, move |output| async move { + let mut state = Handler { + active: None, + handle, + output, + }; + + loop { + let (sender, mut receiver) = channel::mpsc::channel(1); + + state.emit(Message::Init(Sender(sender))).await; + + while let Some(request) = receiver.next().await { + match request { + Request::Close => state.close().await, + + Request::Open(dialog) => { + state.open(dialog).await; + dialog_active_set(false); + } + + Request::Save(dialog) => { + state.save(dialog).await; + dialog_active_set(false); + } + + Request::Response => state.response().await, + } + } + } + }) +} + +/// Errors that my occur when interacting with the file chooser subscription +#[derive(Debug, Error)] +pub enum Error { + #[error("dialog close failed")] + Close(#[source] ashpd::Error), + #[error("dialog open failed")] + Open(#[source] ashpd::Error), + #[error("dialog response failed")] + Response(#[source] ashpd::Error), +} + +/// Requests for the file chooser subscription +enum Request { + Close, + Open(open::Dialog), + Save(save::Dialog), + Response, +} + +/// Messages from the file chooser subscription. +pub enum Message { + Closed, + Err(Error), + Init(Sender), + Opened, + Selected(SelectedFiles), +} + +/// Sends requests to the file chooser subscription. +#[derive(Clone, Debug)] +pub struct Sender(channel::mpsc::Sender); + +impl Sender { + /// Creates a [`Command`] that closes an a file chooser dialog. + pub fn close(&mut self) -> Command<()> { + let mut sender = self.0.clone(); + + crate::command::future(async move { + let _res = sender.send(Request::Close).await; + () + }) + } + + /// Creates a [`Command`] that opens a nthe file chooser. + pub fn open(&mut self, dialog: open::Dialog) -> Command<()> { + dialog_active_set(true); + let mut sender = self.0.clone(); + + crate::command::future(async move { + let _res = sender.send(Request::Open(dialog)).await; + () + }) + } + + /// Creates a [`Command`] that requests the response from a file chooser dialog. + pub fn response(&mut self) -> Command<()> { + let mut sender = self.0.clone(); + + crate::command::future(async move { + let _res = sender.send(Request::Response).await; + () + }) + } + + /// Creates a [`Command`] that opens a new save file dialog. + pub fn save(&mut self, dialog: save::Dialog) -> Command<()> { + dialog_active_set(true); + let mut sender = self.0.clone(); + + crate::command::future(async move { + let _res = sender.send(Request::Save(dialog)).await; + () + }) + } +} + +struct Handler { + active: Option>, + handle: fn(Message) -> M, + output: channel::mpsc::Sender, +} + +impl Handler { + /// Emits close request if there is an active dialog request. + async fn close(&mut self) { + if let Some(request) = self.active.take() { + if let Err(why) = request.close().await { + self.emit(Message::Err(Error::Close(why))).await; + } + } + } + + async fn emit(&mut self, response: Message) { + let _res = self.output.send((self.handle)(response)).await; + } + + /// Creates a new dialog, and closes any prior active dialogs. + async fn open(&mut self, dialog: open::Dialog) { + let response = match open::create(dialog).await { + Ok(request) => { + self.active = Some(request); + Message::Opened + } + Err(why) => Message::Err(Error::Open(why)), + }; + + self.emit(response).await; + } + + /// Collects selected files from the active dialog. + async fn response(&mut self) { + if let Some(request) = self.active.as_ref() { + let response = match request.response() { + Ok(selected) => Message::Selected(selected), + Err(why) => Message::Err(Error::Response(why)), + }; + + self.emit(response).await; + } + } + + /// Creates a new dialog, and closes any prior active dialogs. + async fn save(&mut self, dialog: save::Dialog) { + let response = match save::create(dialog).await { + Ok(request) => { + self.active = Some(request); + Message::Opened + } + Err(why) => Message::Err(Error::Open(why)), + }; + + self.emit(response).await; + } +} diff --git a/src/dialog/file_chooser/open.rs b/src/dialog/file_chooser/open.rs new file mode 100644 index 00000000000..5e2ca38c639 --- /dev/null +++ b/src/dialog/file_chooser/open.rs @@ -0,0 +1,90 @@ +// Copyright 2023 System76 +// SPDX-License-Identifier: MPL-2.0 + +//! Request to open files and/or directories. +//! +//! Check out the [open-dialog](https://github.com/pop-os/libcosmic/tree/master/examples/open-dialog) +//! example in our repository. + +use derive_setters::Setters; +use iced::Command; + +/// A builder for an open file dialog, passed as a request by a [`Sender`] +#[derive(Setters)] +#[must_use] +pub struct Dialog { + /// The lab for the dialog's window title. + title: String, + + /// The label for the accept button. Mnemonic underlines are allowed. + #[setters(strip_option)] + accept_label: Option, + + /// Whether to select for folders instead of files. Default is to select files. + include_directories: bool, + + /// Modal dialogs require user input before continuing the program. + modal: bool, + + /// Whether to allow selection of multiple files. Default is no. + multiple_files: bool, + + /// Adds a list of choices. + choices: Vec, + + /// Specifies the default file filter. + #[setters(into)] + current_filter: Option, + + /// A collection of file filters. + filters: Vec, +} + +impl Dialog { + pub(super) const fn new() -> Self { + Self { + title: String::new(), + accept_label: None, + include_directories: false, + modal: true, + multiple_files: false, + current_filter: None, + choices: Vec::new(), + filters: Vec::new(), + } + } + + /// Creates a [`Command`] which opens the dialog. + pub fn create(self, sender: &mut super::Sender) -> Command<()> { + sender.open(self) + } + + /// Adds a choice. + pub fn choice(mut self, choice: impl Into) -> Self { + self.choices.push(choice.into()); + self + } + + /// Adds a files filter. + pub fn filter(mut self, filter: impl Into) -> Self { + self.filters.push(filter.into()); + self + } +} + +/// Creates a new file dialog, and begins to await its responses. +pub(super) async fn create( + dialog: Dialog, +) -> ashpd::Result> { + ashpd::desktop::file_chooser::OpenFileRequest::default() + .title(Some(dialog.title.as_str())) + .accept_label(dialog.accept_label.as_deref()) + .directory(dialog.include_directories) + .modal(dialog.modal) + .multiple(dialog.multiple_files) + .choices(dialog.choices) + .filters(dialog.filters) + .current_filter(dialog.current_filter) + .send() + .await +} diff --git a/src/dialog/file_chooser/save.rs b/src/dialog/file_chooser/save.rs new file mode 100644 index 00000000000..2204584d926 --- /dev/null +++ b/src/dialog/file_chooser/save.rs @@ -0,0 +1,99 @@ +// Copyright 2023 System76 +// SPDX-License-Identifier: MPL-2.0 + +//! Choose a location to save a file to. +//! +//! Check out the [open-dialog](https://github.com/pop-os/libcosmic/tree/master/examples/open-dialog) +//! example in our repository. + +use derive_setters::Setters; +use iced::Command; +use std::path::{Path, PathBuf}; + +/// A builder for an save file dialog, passed as a request by a [`Sender`] +#[derive(Setters)] +#[must_use] +pub struct Dialog { + /// The lab for the dialog's window title. + title: String, + + /// The label for the accept button. Mnemonic underlines are allowed. + #[setters(strip_option)] + accept_label: Option, + + /// Modal dialogs require user input before continuing the program. + modal: bool, + + /// Sets the current file name. + #[setters(strip_option)] + current_name: Option, + + /// Sets the current folder. + #[setters(strip_option)] + current_folder: Option, + + /// Sets the absolute path of the file + #[setters(strip_option)] + current_file: Option, + + /// Adds a list of choices. + choices: Vec, + + /// Specifies the default file filter. + #[setters(into)] + current_filter: Option, + + /// A collection of file filters. + filters: Vec, +} + +impl Dialog { + pub(super) const fn new() -> Self { + Self { + title: String::new(), + accept_label: None, + modal: true, + current_name: None, + current_folder: None, + current_file: None, + current_filter: None, + choices: Vec::new(), + filters: Vec::new(), + } + } + + /// Creates a [`Command`] which opens the dialog. + pub fn create(self, sender: &mut super::Sender) -> Command<()> { + sender.save(self) + } + + /// Adds a choice. + pub fn choice(mut self, choice: impl Into) -> Self { + self.choices.push(choice.into()); + self + } + + /// Adds a files filter. + pub fn filter(mut self, filter: impl Into) -> Self { + self.filters.push(filter.into()); + self + } +} + +/// Creates a new file dialog, and begins to await its responses. +pub(super) async fn create( + dialog: Dialog, +) -> ashpd::Result> { + ashpd::desktop::file_chooser::SaveFileRequest::default() + .title(Some(dialog.title.as_str())) + .accept_label(dialog.accept_label.as_deref()) + .modal(dialog.modal) + .choices(dialog.choices) + .filters(dialog.filters) + .current_filter(dialog.current_filter) + .current_name(dialog.current_name.as_deref()) + .current_folder::<&Path>(dialog.current_folder.as_deref())? + .current_file::<&Path>(dialog.current_file.as_deref())? + .send() + .await +} diff --git a/src/dialog/mod.rs b/src/dialog/mod.rs index f4d85537f7f..dc7530965d5 100644 --- a/src/dialog/mod.rs +++ b/src/dialog/mod.rs @@ -3,7 +3,6 @@ //! Create dialogs for retrieving user input. -pub use ashpd::desktop::file_chooser::{Choice, FileFilter, SelectedFiles}; pub use ashpd::WindowIdentifier; -pub mod open_file; +pub mod file_chooser; diff --git a/src/dialog/open_file.rs b/src/dialog/open_file.rs deleted file mode 100644 index cbb92cf14e9..00000000000 --- a/src/dialog/open_file.rs +++ /dev/null @@ -1,252 +0,0 @@ -// Copyright 2023 System76 -// SPDX-License-Identifier: MPL-2.0 - -//! Request to open files and/or directories. -//! -//! Check out the [open-dialog](https://github.com/pop-os/libcosmic/tree/master/examples/open-dialog) -//! example in our repository. - -use derive_setters::Setters; -use iced::futures::{channel, SinkExt, StreamExt}; -use iced::{Command, Subscription}; -use std::cell::Cell; -use thiserror::Error; - -thread_local! { - /// Prevents duplicate dialog open requests. - static OPENED: Cell = Cell::new(false); -} - -fn dialog_is_open() -> bool { - OPENED.with(Cell::get) -} - -/// Creates a [`Builder`] if no other open file dialog exists. -pub fn builder() -> Option { - if dialog_is_open() { - None - } else { - Some(Builder::new()) - } -} - -/// Creates a subscription for open file dialog events. -pub fn subscription(handle: fn(Message) -> M) -> Subscription { - let type_id = std::any::TypeId::of::>(); - - iced::subscription::channel(type_id, 1, move |output| async move { - let mut state = State { - active: None, - handle, - output, - }; - - loop { - let (sender, mut receiver) = channel::mpsc::channel(1); - - state.emit(Message::Init(Sender(sender))).await; - - while let Some(request) = receiver.next().await { - match request { - Request::Close => state.close().await, - - Request::Open(dialog) => { - state.open(dialog).await; - OPENED.with(|last| last.set(false)); - } - - Request::Response => state.response().await, - } - } - } - }) -} - -/// Errors that my occur when interacting with an open file dialog subscription -#[derive(Debug, Error)] -pub enum Error { - #[error("dialog close failed")] - Close(#[source] ashpd::Error), - #[error("dialog open failed")] - Open(#[source] ashpd::Error), - #[error("dialog response failed")] - Response(#[source] ashpd::Error), -} - -/// Requests for an open file dialog subscription -enum Request { - Close, - Open(Builder), - Response, -} - -/// Messages from an open file dialog subscription. -pub enum Message { - Closed, - Err(Error), - Init(Sender), - Opened, - Selected(super::SelectedFiles), -} - -/// Sends requests to an open file dialog subscription. -#[derive(Clone, Debug)] -pub struct Sender(channel::mpsc::Sender); - -impl Sender { - /// Creates a [`Command`] that closes an active open file dialog. - pub fn close(&mut self) -> Command<()> { - let mut sender = self.0.clone(); - - crate::command::future(async move { - let _res = sender.send(Request::Close).await; - () - }) - } - - /// Creates a [`Command`] that opens a new open file dialog. - pub fn open(&mut self, dialog: Builder) -> Command<()> { - OPENED.with(|opened| opened.set(true)); - - let mut sender = self.0.clone(); - - crate::command::future(async move { - let _res = sender.send(Request::Open(dialog)).await; - () - }) - } - - /// Creates a [`Command`] that requests the response from an active open file dialog. - pub fn response(&mut self) -> Command<()> { - let mut sender = self.0.clone(); - - crate::command::future(async move { - let _res = sender.send(Request::Response).await; - () - }) - } -} - -/// A builder for an open file dialog, passed as a request by a [`Sender`] -#[derive(Setters)] -#[must_use] -pub struct Builder { - /// The lab for the dialog's window title. - title: String, - - /// The label for the accept button. Mnemonic underlines are allowed. - #[setters(strip_option)] - accept_label: Option, - - /// Whether to select for folders instead of files. Default is to select files. - include_directories: bool, - - /// Modal dialogs require user input before continuing the program. - modal: bool, - - /// Whether to allow selection of multiple files. Default is no. - multiple_files: bool, - - /// Adds a list of choices. - choices: Vec, - - /// Specifies the default file filter. - #[setters(into)] - current_filter: Option, - - /// A collection of file filters. - filters: Vec, -} - -impl Builder { - const fn new() -> Self { - Self { - title: String::new(), - accept_label: None, - include_directories: false, - modal: true, - multiple_files: false, - current_filter: None, - choices: Vec::new(), - filters: Vec::new(), - } - } - - /// Creates a [`Command`] which opens the dialog. - pub fn create(self, sender: &mut Sender) -> Command<()> { - sender.open(self) - } - - /// Adds a choice. - pub fn choice(mut self, choice: impl Into) -> Self { - self.choices.push(choice.into()); - self - } - - /// Adds a files filter. - pub fn filter(mut self, filter: impl Into) -> Self { - self.filters.push(filter.into()); - self - } -} - -struct State { - active: Option>, - handle: fn(Message) -> M, - output: channel::mpsc::Sender, -} - -impl State { - /// Emits close request if there is an active dialog request. - async fn close(&mut self) { - if let Some(request) = self.active.take() { - if let Err(why) = request.close().await { - self.emit(Message::Err(Error::Close(why))).await; - } - } - } - - async fn emit(&mut self, response: Message) { - let _res = self.output.send((self.handle)(response)).await; - } - - /// Creates a new dialog, and closes any prior active dialogs. - async fn open(&mut self, dialog: Builder) { - let response = match create(dialog).await { - Ok(request) => { - self.active = Some(request); - Message::Opened - } - Err(why) => Message::Err(Error::Open(why)), - }; - - self.emit(response).await; - } - - /// Collects selected files from the active dialog. - async fn response(&mut self) { - if let Some(request) = self.active.as_ref() { - let response = match request.response() { - Ok(selected) => Message::Selected(selected), - Err(why) => Message::Err(Error::Response(why)), - }; - - self.emit(response).await; - } - } -} - -/// Creates a new file dialog, and begins to await its responses. -async fn create(dialog: Builder) -> ashpd::Result> { - ashpd::desktop::file_chooser::OpenFileRequest::default() - .title(Some(dialog.title.as_str())) - .accept_label(dialog.accept_label.as_deref()) - .directory(dialog.include_directories) - .modal(dialog.modal) - .multiple(dialog.multiple_files) - .choices(dialog.choices) - .filters(dialog.filters) - .current_filter(dialog.current_filter) - .send() - .await -} diff --git a/src/dialog/save_file.rs b/src/dialog/save_file.rs deleted file mode 100644 index 9da9c7d9b68..00000000000 --- a/src/dialog/save_file.rs +++ /dev/null @@ -1,262 +0,0 @@ -// Copyright 2023 System76 -// SPDX-License-Identifier: MPL-2.0 - -//! Choose a location to save a file to. -//! -//! Check out the [open-dialog](https://github.com/pop-os/libcosmic/tree/master/examples/open-dialog) -//! example in our repository. - -use derive_setters::Setters; -use iced::{Command, Subscription}; -use iced::futures::{channel, SinkExt, StreamExt}; -use std::cell::Cell; -use std::path::PathBuf; -use std::time::Instant; -use thiserror::Error; - -thread_local! { - /// Prevents duplicate dialog open requests. - static OPENED: Cell = Cell::new(false); -} - -fn dialog_is_open() -> bool { - OPENED.with(Cell::get) -} - -/// Creates a [`Builder`] if no other save file dialog exists. -pub fn builder() -> Option { - if dialog_is_open() { - None - } else { - Some(Builder::new()) - } -} - -/// Creates a subscription for save file dialog events. -pub fn subscription(handle: fn(Message) -> M) -> Subscription { - let type_id = std::any::TypeId::of::>(); - - iced::subscription::channel(type_id, 1, move |output| async move { - let mut state = State { - active: None, - handle, - output, - }; - - loop { - let (sender, mut receiver) = channel::mpsc::channel(1); - - state.emit(Message::Init(Sender(sender))).await; - - while let Some(request) = receiver.next().await { - match request { - Request::Close => state.close().await, - - Request::Open(dialog) => { - state.open(dialog).await; - OPENED.with(|last| last.set(false)); - }, - - Request::Response => state.response().await, - } - } - } - }) -} - -/// Errors that my occur when interacting with an save file dialog subscription -#[derive(Debug, Error)] -pub enum Error { - #[error("dialog close failed")] - Close(#[source] ashpd::Error), - #[error("dialog open failed")] - Open(#[source] ashpd::Error), - #[error("dialog response failed")] - Response(#[source] ashpd::Error), -} - -/// Requests for an save file dialog subscription -enum Request { - Close, - Open(Builder), - Response, -} - -/// Messages from an save file dialog subscription. -pub enum Message { - Closed, - Err(Error), - Init(Sender), - Opened, - Selected(super::SelectedFiles), -} - -/// Sends requests to an save file dialog subscription. -#[derive(Clone, Debug)] -pub struct Sender(channel::mpsc::Sender); - -impl Sender { - /// Creates a [`Command`] that closes an active save file dialog. - pub fn close(&mut self) -> Command<()> { - let mut sender = self.0.clone(); - - crate::command::future(async move { - let _res = sender.send(Request::Close).await; - () - }) - } - - /// Creates a [`Command`] that opens a new save file dialog. - pub fn open(&mut self, dialog: Builder) -> Command<()> { - OPENED.with(|opened| opened.set(true)); - - let mut sender = self.0.clone(); - - crate::command::future(async move { - let _res = sender.send(Request::Open(dialog)).await; - () - }) - } - - /// Creates a [`Command`] that requests the response from an active save file dialog. - pub fn response(&mut self) -> Command<()> { - let mut sender = self.0.clone(); - - crate::command::future(async move { - let _res = sender.send(Request::Response).await; - () - }) - } -} - -/// A builder for an save file dialog, passed as a request by a [`Sender`] -#[derive(Setters)] -#[must_use] -pub struct Builder { - /// The lab for the dialog's window title. - title: String, - - /// The label for the accept button. Mnemonic underlines are allowed. - #[setters(strip_option)] - accept_label: Option, - - /// Modal dialogs require user input before continuing the program. - modal: bool, - - /// Sets the current file name. - #[setters(strip_option)] - current_name: Option, - - /// Sets the current folder. - #[setters(strip_option)] - current_folder: Option, - - /// Sets the absolute path of the file - #[setters(strip_option)] - current_file: Option, - - /// Adds a list of choices. - choices: Vec, - - /// Specifies the default file filter. - #[setters(into)] - current_filter: Option, - - /// A collection of file filters. - filters: Vec, -} - -impl Builder { - const fn new() -> Self { - Self { - title: String::new(), - accept_label: None, - modal: true, - current_name: None, - current_folder: None, - current_file: None, - current_filter: None, - choices: Vec::new(), - filters: Vec::new(), - } - } - - /// Creates a [`Command`] which opens the dialog. - pub fn create(self, sender: &mut Sender) -> Command<()> { - sender.open(self) - } - - /// Adds a choice. - pub fn choice(mut self, choice: impl Into) -> Self { - self.choices.push(choice.into()); - self - } - - /// Adds a files filter. - pub fn filter(mut self, filter: impl Into) -> Self { - self.filters.push(filter.into()); - self - } -} - -struct State { - active: Option>, - handle: fn(Message) -> M, - output: channel::mpsc::Sender, -} - -impl State { - /// Emits close request if there is an active dialog request. - async fn close(&mut self) { - if let Some(request) = self.active.take() { - if let Err(why) = request.close().await { - self.emit(Message::Err(Error::Close(why))).await; - } - } - } - - async fn emit(&mut self, response: Message) { - let _res = self.output.send((self.handle)(response)).await; - } - - /// Creates a new dialog, and closes any prior active dialogs. - async fn open(&mut self, dialog: Builder) { - let response = match create(dialog).await { - Ok(request) => { - self.active = Some(request); - Message::Opened - } - Err(why) => Message::Err(Error::Open(why)), - }; - - self.emit(response).await; - } - - /// Collects selected files from the active dialog. - async fn response(&mut self) { - if let Some(request) = self.active.as_ref() { - let response = match request.response() { - Ok(selected) => Message::Selected(selected), - Err(why) => Message::Err(Error::Message(why)), - }; - - self.emit(response).await; - } - } -} - -/// Creates a new file dialog, and begins to await its responses. -async fn create(dialog: Builder) -> ashpd::Result> { - ashpd::desktop::file_chooser::SaveFileRequest::default() - .title(Some(dialog.title.as_str())) - .accept_label(dialog.accept_label.as_deref()) - .modal(dialog.modal) - .choices(dialog.choices) - .filters(dialog.filters) - .current_filter(dialog.current_filter) - .current_name(dialog.current_name) - .current_folder(dialog.current_folder)? - .current_file(dialog.current_file)? - .send() - .await -} diff --git a/src/widget/aspect_ratio.rs b/src/widget/aspect_ratio.rs index bf000006719..eae0f89d48c 100644 --- a/src/widget/aspect_ratio.rs +++ b/src/widget/aspect_ratio.rs @@ -7,7 +7,7 @@ use iced_core::mouse; use iced_core::overlay; use iced_core::renderer; use iced_core::widget::Tree; -use iced_core::{Clipboard, Element, Layout, Length, Padding, Point, Rectangle, Shell, Widget}; +use iced_core::{Clipboard, Element, Layout, Length, Padding, Rectangle, Shell, Widget}; pub use iced_style::container::{Appearance, StyleSheet}; diff --git a/src/widget/cosmic_container.rs b/src/widget/cosmic_container.rs index 3214b7fa2ce..8972a01f5e9 100644 --- a/src/widget/cosmic_container.rs +++ b/src/widget/cosmic_container.rs @@ -7,7 +7,7 @@ use iced_core::mouse; use iced_core::overlay; use iced_core::renderer; use iced_core::widget::Tree; -use iced_core::{Clipboard, Element, Layout, Length, Padding, Point, Rectangle, Shell, Widget}; +use iced_core::{Clipboard, Element, Layout, Length, Padding, Rectangle, Shell, Widget}; pub use iced_style::container::{Appearance, StyleSheet}; pub fn container<'a, Message: 'static, T>( diff --git a/src/widget/rectangle_tracker/mod.rs b/src/widget/rectangle_tracker/mod.rs index 6b84e5d261e..305634cbc1a 100644 --- a/src/widget/rectangle_tracker/mod.rs +++ b/src/widget/rectangle_tracker/mod.rs @@ -11,7 +11,7 @@ use iced_core::mouse; use iced_core::overlay; use iced_core::renderer; use iced_core::widget::Tree; -use iced_core::{Clipboard, Element, Layout, Length, Padding, Point, Rectangle, Shell, Widget}; +use iced_core::{Clipboard, Element, Layout, Length, Padding, Rectangle, Shell, Widget}; use std::{fmt::Debug, hash::Hash}; pub use iced_style::container::{Appearance, StyleSheet}; diff --git a/src/widget/segmented_button/widget.rs b/src/widget/segmented_button/widget.rs index d7114fa4039..f91654d01b0 100644 --- a/src/widget/segmented_button/widget.rs +++ b/src/widget/segmented_button/widget.rs @@ -8,7 +8,7 @@ use crate::widget::{icon, IconSource}; use derive_setters::Setters; use iced::{ alignment, event, keyboard, mouse, touch, Background, Color, Command, Element, Event, Length, - Point, Rectangle, Size, + Rectangle, Size, }; use iced_core::text::{LineHeight, Shaping}; use iced_core::widget::{self, operation, tree};