From 2832e6066e9691eadbddc93212b7d5427807443a Mon Sep 17 00:00:00 2001 From: Jeremy Soller Date: Thu, 1 Feb 2024 17:43:41 -0700 Subject: [PATCH] Forward selected path to caller --- examples/dialog.rs | 10 +++++++++- src/dialog.rs | 20 ++++++++++++++++---- src/lib.rs | 18 ++++++++++++++---- 3 files changed, 39 insertions(+), 9 deletions(-) diff --git a/examples/dialog.rs b/examples/dialog.rs index 11d7344..6ac8c3d 100644 --- a/examples/dialog.rs +++ b/examples/dialog.rs @@ -1,3 +1,11 @@ fn main() -> Result<(), Box> { - cosmic_files::dialog() + match cosmic_files::dialog()? { + Some(paths) => { + for path in paths { + println!("{}", path.display()); + } + } + None => {} + } + Ok(()) } diff --git a/src/dialog.rs b/src/dialog.rs index a6b5a02..baacca4 100644 --- a/src/dialog.rs +++ b/src/dialog.rs @@ -16,7 +16,13 @@ use cosmic::{ Application, ApplicationExt, Element, }; use notify::Watcher; -use std::{any::TypeId, collections::HashSet, path::PathBuf, time}; +use std::{ + any::TypeId, + collections::HashSet, + path::PathBuf, + sync::{Arc, Mutex}, + time, +}; use crate::{ fl, home_dir, @@ -24,7 +30,9 @@ use crate::{ }; #[derive(Clone, Debug)] -pub struct Flags {} +pub struct Flags { + pub result_lock: Arc>>>, +} /// Messages that are used specifically by our [`App`]. #[derive(Clone, Debug)] @@ -61,6 +69,7 @@ impl PartialEq for WatcherWrapper { /// The [`App`] stores application-specific state. pub struct App { core: Core, + flags: Flags, nav_model: segmented_button::SingleSelectModel, tab_model: segmented_button::Model, modifiers: Modifiers, @@ -221,6 +230,7 @@ impl Application for App { let mut app = App { core, + flags, nav_model: nav_model.build(), tab_model: segmented_button::ModelBuilder::default().build(), modifiers: Modifiers::empty(), @@ -274,7 +284,8 @@ impl Application for App { fn update(&mut self, message: Self::Message) -> Command { match message { Message::Cancel => { - println!("CANCEL"); + *self.flags.result_lock.lock().unwrap() = None; + return window::close(window::Id::MAIN); } Message::Modifiers(modifiers) => { self.modifiers = modifiers; @@ -329,7 +340,8 @@ impl Application for App { } } } - println!("OPEN {:?}", paths); + *self.flags.result_lock.lock().unwrap() = Some(paths); + return window::close(window::Id::MAIN); } Message::SelectAll(entity_opt) => { let entity = entity_opt.unwrap_or_else(|| self.tab_model.active()); diff --git a/src/lib.rs b/src/lib.rs index d60a87d..4556ac0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,7 +5,11 @@ use cosmic::{ app::{Application, Settings}, cosmic_config::{self, CosmicConfigEntry}, }; -use std::{path::PathBuf, process}; +use std::{ + path::PathBuf, + process, + sync::{Arc, Mutex}, +}; use app::{App, Flags}; mod app; @@ -31,7 +35,7 @@ pub fn home_dir() -> PathBuf { } /// Runs application with these settings -pub fn dialog() -> Result<(), Box> { +pub fn dialog() -> Result>, Box> { localize::localize(); let mut settings = Settings::default(); @@ -42,10 +46,16 @@ pub fn dialog() -> Result<(), Box> { settings = settings.client_decorations(false); } - let flags = dialog::Flags {}; + let mut result_lock = Arc::new(Mutex::new(None)); + + let flags = dialog::Flags { + result_lock: result_lock.clone(), + }; cosmic::app::run::(settings, flags)?; - Ok(()) + let mut result_guard = result_lock.lock().unwrap(); + let result = result_guard.take(); + Ok(result) } /// Runs application with these settings