| 1 |
use std::sync::Arc; |
| 2 |
|
| 3 |
use async_trait::async_trait; |
| 4 |
use serde::{Deserialize, Serialize}; |
| 5 |
|
| 6 |
use crate::{ |
| 7 |
MaybeAppID, WindowIdentifierType, |
| 8 |
backend::{ |
| 9 |
Result, |
| 10 |
request::{Request, RequestImpl}, |
| 11 |
}, |
| 12 |
desktop::{HandleToken, Icon, request::Response}, |
| 13 |
zvariant::{ |
| 14 |
self, Optional, OwnedObjectPath, |
| 15 |
as_value::{self, optional}, |
| 16 |
}, |
| 17 |
}; |
| 18 |
|
| 19 |
#[derive(Deserialize, zvariant::Type)] |
| 20 |
#[zvariant(signature = "dict")] |
| 21 |
pub struct AccessOptions { |
| 22 |
#[serde(default, with = "optional")] |
| 23 |
modal: Option<bool>, |
| 24 |
#[serde(default, with = "optional")] |
| 25 |
deny_label: Option<String>, |
| 26 |
#[serde(default, with = "optional")] |
| 27 |
grant_label: Option<String>, |
| 28 |
#[serde(default, with = "optional")] |
| 29 |
icon: Option<String>, |
| 30 |
#[serde(default, with = "as_value")] |
| 31 |
choices: Vec<Choice>, |
| 32 |
} |
| 33 |
|
| 34 |
#[derive(Clone, Deserialize, zvariant::Type, Debug)] |
| 35 |
pub struct Choice(String, String, Vec<(String, String)>, String); |
| 36 |
|
| 37 |
impl Choice { |
| 38 |
/// The choice's unique id |
| 39 |
pub fn id(&self) -> &str { |
| 40 |
&self.0 |
| 41 |
} |
| 42 |
|
| 43 |
/// The user visible label of the choice. |
| 44 |
pub fn label(&self) -> &str { |
| 45 |
&self.1 |
| 46 |
} |
| 47 |
|
| 48 |
/// Pairs of choices. |
| 49 |
pub fn pairs(&self) -> Vec<(&str, &str)> { |
| 50 |
self.2 |
| 51 |
.iter() |
| 52 |
.map(|(x, y)| (x.as_str(), y.as_str())) |
| 53 |
.collect::<Vec<_>>() |
| 54 |
} |
| 55 |
|
| 56 |
/// The initially selected value. |
| 57 |
pub fn initial_selection(&self) -> &str { |
| 58 |
&self.3 |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
impl AccessOptions { |
| 63 |
pub fn is_modal(&self) -> Option<bool> { |
| 64 |
self.modal |
| 65 |
} |
| 66 |
|
| 67 |
pub fn deny_label(&self) -> Option<&str> { |
| 68 |
self.deny_label.as_deref() |
| 69 |
} |
| 70 |
|
| 71 |
pub fn grant_label(&self) -> Option<&str> { |
| 72 |
self.grant_label.as_deref() |
| 73 |
} |
| 74 |
|
| 75 |
pub fn icon(&self) -> Option<Icon> { |
| 76 |
self.icon.as_ref().map(|i| Icon::with_names([i])) |
| 77 |
} |
| 78 |
|
| 79 |
pub fn choices(&self) -> &[Choice] { |
| 80 |
&self.choices |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
#[derive(Serialize, Debug, zvariant::Type, Default)] |
| 85 |
#[zvariant(signature = "dict")] |
| 86 |
pub struct AccessResponse { |
| 87 |
#[serde(with = "as_value", skip_serializing_if = "Vec::is_empty")] |
| 88 |
choices: Vec<(String, String)>, |
| 89 |
} |
| 90 |
|
| 91 |
impl AccessResponse { |
| 92 |
/// Adds a selected choice (key, value). |
| 93 |
#[must_use] |
| 94 |
pub fn choice(mut self, key: &str, value: &str) -> Self { |
| 95 |
self.choices.push((key.to_owned(), value.to_owned())); |
| 96 |
self |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
#[async_trait] |
| 101 |
pub trait AccessImpl: RequestImpl { |
| 102 |
#[allow(clippy::too_many_arguments)] |
| 103 |
#[doc(alias = "AccessDialog")] |
| 104 |
async fn access_dialog( |
| 105 |
&self, |
| 106 |
token: HandleToken, |
| 107 |
app_id: Option<MaybeAppID>, |
| 108 |
window_identifier: Option<WindowIdentifierType>, |
| 109 |
title: String, |
| 110 |
subtitle: String, |
| 111 |
body: String, |
| 112 |
options: AccessOptions, |
| 113 |
) -> Result<AccessResponse>; |
| 114 |
} |
| 115 |
|
| 116 |
pub(crate) struct AccessInterface { |
| 117 |
imp: Arc<dyn AccessImpl>, |
| 118 |
spawn: Arc<dyn futures_util::task::Spawn + Send + Sync>, |
| 119 |
cnx: zbus::Connection, |
| 120 |
} |
| 121 |
|
| 122 |
impl AccessInterface { |
| 123 |
pub fn new( |
| 124 |
imp: Arc<dyn AccessImpl>, |
| 125 |
cnx: zbus::Connection, |
| 126 |
spawn: Arc<dyn futures_util::task::Spawn + Send + Sync>, |
| 127 |
) -> Self { |
| 128 |
Self { imp, cnx, spawn } |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
#[zbus::interface(name = "org.freedesktop.impl.portal.Access")] |
| 133 |
impl AccessInterface { |
| 134 |
#[zbus(property(emits_changed_signal = "const"), name = "version")] |
| 135 |
fn version(&self) -> u32 { |
| 136 |
1 |
| 137 |
} |
| 138 |
|
| 139 |
#[allow(clippy::too_many_arguments)] |
| 140 |
#[zbus(out_args("response", "results"))] |
| 141 |
async fn access_dialog( |
| 142 |
&self, |
| 143 |
handle: OwnedObjectPath, |
| 144 |
app_id: Optional<MaybeAppID>, |
| 145 |
window_identifier: Optional<WindowIdentifierType>, |
| 146 |
title: String, |
| 147 |
subtitle: String, |
| 148 |
body: String, |
| 149 |
options: AccessOptions, |
| 150 |
) -> Result<Response<AccessResponse>> { |
| 151 |
let imp = Arc::clone(&self.imp); |
| 152 |
|
| 153 |
Request::spawn( |
| 154 |
"Access::AccessDialog", |
| 155 |
&self.cnx, |
| 156 |
handle.clone(), |
| 157 |
Arc::clone(&self.imp), |
| 158 |
Arc::clone(&self.spawn), |
| 159 |
async move { |
| 160 |
imp.access_dialog( |
| 161 |
HandleToken::try_from(&handle).unwrap(), |
| 162 |
app_id.into(), |
| 163 |
window_identifier.into(), |
| 164 |
title, |
| 165 |
subtitle, |
| 166 |
body, |
| 167 |
options, |
| 168 |
) |
| 169 |
.await |
| 170 |
}, |
| 171 |
) |
| 172 |
.await |
| 173 |
} |
| 174 |
} |