use std::sync::Arc; use async_trait::async_trait; use serde::{Deserialize, Serialize}; use crate::{ ActivationToken, MaybeAppID, PortalError, Uri, WindowIdentifierType, backend::request::{Request, RequestImpl}, desktop::{HandleToken, Response}, zvariant::{ Optional, OwnedObjectPath, Type, as_value::{self, optional}, }, }; #[derive(Debug, Deserialize, Type)] #[zvariant(signature = "dict")] pub struct ChooserOptions { #[serde(default, with = "optional")] last_choice: Option, #[serde(default, with = "optional")] modal: Option, #[serde(default, with = "optional")] content_type: Option, #[serde(default, with = "optional")] uri: Option, #[serde(default, with = "optional")] filename: Option, #[serde(default, with = "optional")] activation_token: Option, } impl ChooserOptions { pub fn last_choice(&self) -> Option<&MaybeAppID> { self.last_choice.as_ref() } pub fn modal(&self) -> Option { self.modal } pub fn content_type(&self) -> Option<&str> { self.content_type.as_deref() } pub fn uri(&self) -> Option<&Uri> { self.uri.as_ref() } pub fn filename(&self) -> Option<&str> { self.filename.as_deref() } pub fn activation_token(&self) -> Option<&ActivationToken> { self.activation_token.as_ref() } } #[derive(Debug, Serialize, Type)] #[zvariant(signature = "dict")] pub struct Choice { #[serde(with = "as_value")] choice: MaybeAppID, #[serde(with = "optional", skip_serializing_if = "Option::is_none")] activation_token: Option, } impl Choice { pub fn new(choice: MaybeAppID) -> Self { Self { choice, activation_token: None, } } #[must_use] pub fn activation_token( mut self, activation_token: impl Into>, ) -> Self { self.activation_token = activation_token.into(); self } } #[async_trait] pub trait AppChooserImpl: RequestImpl { #[doc(alias = "ChooseApplication")] async fn choose_application( &self, token: HandleToken, app_id: Option, parent_window: Option, choices: Vec, options: ChooserOptions, ) -> Result; #[doc(alias = "UpdateChoices")] async fn update_choices( &self, token: HandleToken, choices: Vec, ) -> Result<(), PortalError>; } pub(crate) struct AppChooserInterface { imp: Arc, spawn: Arc, cnx: zbus::Connection, } impl AppChooserInterface { pub fn new( imp: Arc, cnx: zbus::Connection, spawn: Arc, ) -> Self { Self { imp, cnx, spawn } } } #[zbus::interface(name = "org.freedesktop.impl.portal.AppChooser")] impl AppChooserInterface { #[zbus(property(emits_changed_signal = "const"), name = "version")] fn version(&self) -> u32 { 2 } #[zbus(out_args("response", "results"))] async fn choose_application( &self, handle: OwnedObjectPath, app_id: Optional, parent_window: Optional, choices: Vec, options: ChooserOptions, ) -> Result, PortalError> { let imp = Arc::clone(&self.imp); Request::spawn( "AppChooser::ChooseApplication", &self.cnx, handle.clone(), Arc::clone(&self.imp), Arc::clone(&self.spawn), async move { imp.choose_application( HandleToken::try_from(&handle).unwrap(), app_id.into(), parent_window.into(), choices, options, ) .await }, ) .await } async fn update_choices( &self, handle: OwnedObjectPath, choices: Vec, ) -> Result<(), PortalError> { #[cfg(feature = "tracing")] tracing::debug!("AppChooser::UpdateChoices"); let token = HandleToken::try_from(&handle).unwrap(); let response = self.imp.update_choices(token, choices).await; #[cfg(feature = "tracing")] tracing::debug!("AppChooser::UpdateChoices returned {:#?}", response); response } }