From aba90279e6cda3d29a5b7ccf598857ee8761cf68 Mon Sep 17 00:00:00 2001 From: ellieplayswow <164806095+ellieplayswow@users.noreply.github.com> Date: Fri, 27 Jun 2025 23:26:24 +0100 Subject: [PATCH] add ability to pass appid through to file dialogs (#1042) * dialog: add new Dialog::create() method with new DialogSettings struct to allow passing app_id from portal; deprecating Dialog::new() * dialog: remove deprecation, move to builder pattern, add path_opt and kind options to DialogSettings * dialog: instances of -> DialogSettings to -> Self --- src/app.rs | 6 ++++-- src/dialog.rs | 52 +++++++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 48 insertions(+), 10 deletions(-) diff --git a/src/app.rs b/src/app.rs index 7ca324b..98df57b 100644 --- a/src/app.rs +++ b/src/app.rs @@ -66,6 +66,7 @@ use trash::TrashItem; #[cfg(all(feature = "wayland", feature = "desktop-applet"))] use wayland_client::{protocol::wl_output::WlOutput, Proxy}; +use crate::dialog::DialogSettings; use crate::{ clipboard::{ClipboardCopy, ClipboardKind, ClipboardPaste}, config::{ @@ -2562,8 +2563,9 @@ impl Application for App { .map(|parent| parent.to_path_buf()) { let (mut dialog, dialog_task) = Dialog::new( - DialogKind::OpenFolder, - Some(destination), + DialogSettings::new() + .kind(DialogKind::OpenFolder) + .path(destination), Message::FileDialogMessage, Message::ExtractToResult, ); diff --git a/src/dialog.rs b/src/dialog.rs index d557391..d75f100 100644 --- a/src/dialog.rs +++ b/src/dialog.rs @@ -204,6 +204,43 @@ impl<'a, M: Clone + 'static> From<&'a DialogLabel> for Element<'a, M> { } } +pub struct DialogSettings { + app_id: String, + kind: DialogKind, + path_opt: Option, +} + +impl DialogSettings { + pub fn new() -> Self { + Default::default() + } + + pub fn app_id(mut self, app_id: String) -> Self { + self.app_id = app_id; + self + } + + pub fn kind(mut self, kind: DialogKind) -> Self { + self.kind = kind; + self + } + + pub fn path(mut self, path: PathBuf) -> Self { + self.path_opt = Some(path); + self + } +} + +impl Default for DialogSettings { + fn default() -> Self { + Self { + app_id: App::APP_ID.to_string(), + kind: DialogKind::OpenFile, + path_opt: None, + } + } +} + pub struct Dialog { cosmic: Cosmic, mapper: fn(DialogMessage) -> M, @@ -212,8 +249,7 @@ pub struct Dialog { impl Dialog { pub fn new( - kind: DialogKind, - path_opt: Option, + dialog_settings: DialogSettings, mapper: fn(DialogMessage) -> M, on_result: impl Fn(DialogResult) -> M + 'static, ) -> (Self, Task) { @@ -234,7 +270,7 @@ impl Dialog { #[cfg(target_os = "linux")] { - settings.platform_specific.application_id = App::APP_ID.to_string(); + settings.platform_specific.application_id = dialog_settings.app_id; } let (window_id, window_command) = window::open(settings.clone()); @@ -242,16 +278,16 @@ impl Dialog { let mut core = Core::default(); core.set_main_window_id(Some(window_id)); let flags = Flags { - kind, - path_opt: path_opt - .as_ref() - .and_then(|path| match fs::canonicalize(path) { + kind: dialog_settings.kind, + path_opt: dialog_settings.path_opt.as_ref().and_then(|path| { + match fs::canonicalize(path) { Ok(ok) => Some(ok), Err(err) => { log::warn!("failed to canonicalize {:?}: {}", path, err); None } - }), + } + }), window_id, config_handler, config,