use std::sync::Arc; use async_trait::async_trait; use crate::{ MaybeAppID, WindowIdentifierType, backend::{ Result, request::{Request, RequestImpl}, }, desktop::{ HandleToken, file_chooser::{OpenFileOptions, SaveFileOptions, SaveFilesOptions, SelectedFiles}, request::Response, }, zvariant::{Optional, OwnedObjectPath}, }; #[async_trait] pub trait FileChooserImpl: RequestImpl { #[doc(alias = "OpenFile")] async fn open_file( &self, token: HandleToken, app_id: Option, window_identifier: Option, title: &str, options: OpenFileOptions, ) -> Result; #[doc(alias = "SaveFile")] async fn save_file( &self, token: HandleToken, app_id: Option, window_identifier: Option, title: &str, options: SaveFileOptions, ) -> Result; #[doc(alias = "SaveFiles")] async fn save_files( &self, token: HandleToken, app_id: Option, window_identifier: Option, title: &str, options: SaveFilesOptions, ) -> Result; } pub(crate) struct FileChooserInterface { imp: Arc, spawn: Arc, cnx: zbus::Connection, } impl FileChooserInterface { pub fn new( imp: Arc, cnx: zbus::Connection, spawn: Arc, ) -> Self { Self { imp, cnx, spawn } } } #[zbus::interface(name = "org.freedesktop.impl.portal.FileChooser")] impl FileChooserInterface { #[zbus(property(emits_changed_signal = "const"), name = "version")] fn version(&self) -> u32 { 4 } #[zbus(out_args("response", "results"))] async fn open_file( &self, handle: OwnedObjectPath, app_id: Optional, window_identifier: Optional, title: String, options: OpenFileOptions, ) -> Result> { let imp = Arc::clone(&self.imp); Request::spawn( "FileChooser::OpenFile", &self.cnx, handle.clone(), Arc::clone(&self.imp), Arc::clone(&self.spawn), async move { imp.open_file( HandleToken::try_from(&handle).unwrap(), app_id.into(), window_identifier.into(), &title, options, ) .await }, ) .await } #[zbus(out_args("response", "results"))] async fn save_file( &self, handle: OwnedObjectPath, app_id: Optional, window_identifier: Optional, title: String, options: SaveFileOptions, ) -> Result> { let imp = Arc::clone(&self.imp); Request::spawn( "FileChooser::SaveFile", &self.cnx, handle.clone(), Arc::clone(&self.imp), Arc::clone(&self.spawn), async move { imp.save_file( HandleToken::try_from(&handle).unwrap(), app_id.into(), window_identifier.into(), &title, options, ) .await }, ) .await } #[zbus(out_args("response", "results"))] async fn save_files( &self, handle: OwnedObjectPath, app_id: Optional, window_identifier: Optional, title: String, options: SaveFilesOptions, ) -> Result> { let imp = Arc::clone(&self.imp); Request::spawn( "FileChooser::SaveFiles", &self.cnx, handle.clone(), Arc::clone(&self.imp), Arc::clone(&self.spawn), async move { imp.save_files( HandleToken::try_from(&handle).unwrap(), app_id.into(), window_identifier.into(), &title, options, ) .await }, ) .await } }