Forward selected path to caller

This commit is contained in:
Jeremy Soller 2024-02-01 17:43:41 -07:00
parent c6eecbddd8
commit 2832e6066e
3 changed files with 39 additions and 9 deletions

View file

@ -1,3 +1,11 @@
fn main() -> Result<(), Box<dyn std::error::Error>> {
cosmic_files::dialog()
match cosmic_files::dialog()? {
Some(paths) => {
for path in paths {
println!("{}", path.display());
}
}
None => {}
}
Ok(())
}

View file

@ -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<Mutex<Option<Vec<PathBuf>>>>,
}
/// 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<segmented_button::SingleSelect>,
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<Self::Message> {
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());

View file

@ -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<dyn std::error::Error>> {
pub fn dialog() -> Result<Option<Vec<PathBuf>>, Box<dyn std::error::Error>> {
localize::localize();
let mut settings = Settings::default();
@ -42,10 +46,16 @@ pub fn dialog() -> Result<(), Box<dyn std::error::Error>> {
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::<dialog::App>(settings, flags)?;
Ok(())
let mut result_guard = result_lock.lock().unwrap();
let result = result_guard.take();
Ok(result)
}
/// Runs application with these settings