use serde::Serialize;
use zbus::zvariant::{DeserializeDict, SerializeDict, Type};
use super::{HandleToken, DESTINATION, PATH};
use crate::{helpers::call_request_method, Error, WindowIdentifier};
#[derive(SerializeDict, DeserializeDict, Type, Debug, Clone, Default)]
#[zvariant(signature = "dict")]
struct BackgroundOptions {
handle_token: HandleToken,
reason: Option<String>,
autostart: Option<bool>,
#[zvariant(rename = "dbus-activatable")]
dbus_activatable: Option<bool>,
#[zvariant(rename = "commandline")]
command: Option<Vec<String>>,
}
impl BackgroundOptions {
pub fn reason(mut self, reason: &str) -> Self {
self.reason = Some(reason.to_string());
self
}
pub fn autostart(mut self, autostart: bool) -> Self {
self.autostart = Some(autostart);
self
}
pub fn dbus_activatable(mut self, dbus_activatable: bool) -> Self {
self.dbus_activatable = Some(dbus_activatable);
self
}
pub fn command(mut self, command: Option<&[impl AsRef<str> + Type + Serialize]>) -> Self {
self.command = command.map(|s| s.iter().map(|s| s.as_ref().to_string()).collect());
self
}
}
#[derive(SerializeDict, DeserializeDict, Type, Debug)]
#[zvariant(signature = "dict")]
pub struct Background {
background: bool,
autostart: bool,
}
impl Background {
pub fn run_in_background(&self) -> bool {
self.background
}
pub fn auto_start(&self) -> bool {
self.autostart
}
}
#[derive(Debug)]
#[doc(alias = "org.freedesktop.portal.Background")]
pub struct BackgroundProxy<'a>(zbus::Proxy<'a>);
impl<'a> BackgroundProxy<'a> {
pub async fn new(connection: &zbus::Connection) -> Result<BackgroundProxy<'a>, Error> {
let proxy = zbus::ProxyBuilder::new_bare(connection)
.interface("org.freedesktop.portal.Background")?
.path(PATH)?
.destination(DESTINATION)?
.build()
.await?;
Ok(Self(proxy))
}
pub fn inner(&self) -> &zbus::Proxy<'_> {
&self.0
}
#[doc(alias = "RequestBackground")]
pub async fn request_background(
&self,
identifier: &WindowIdentifier,
reason: &str,
auto_start: bool,
command_line: Option<&[impl AsRef<str> + Type + Serialize]>,
dbus_activatable: bool,
) -> Result<Background, Error> {
let options = BackgroundOptions::default()
.reason(reason)
.autostart(auto_start)
.dbus_activatable(dbus_activatable)
.command(command_line);
call_request_method(
self.inner(),
&options.handle_token,
"RequestBackground",
&(&identifier, &options),
)
.await
}
}
#[doc(alias = "xdp_portal_request_background")]
pub async fn request(
identifier: &WindowIdentifier,
reason: &str,
auto_start: bool,
command_line: Option<&[impl AsRef<str> + Type + Serialize]>,
dbus_activatable: bool,
) -> Result<Background, Error> {
let connection = zbus::Connection::session().await?;
let proxy = BackgroundProxy::new(&connection).await?;
proxy
.request_background(
identifier,
reason,
auto_start,
command_line,
dbus_activatable,
)
.await
}