use serde::Serialize;
use zbus::zvariant::{DeserializeDict, SerializeDict, Type};
use super::{HandleToken, Request};
use crate::{Error, WindowIdentifier, proxy::Proxy, window_identifier::MaybeWindowIdentifierExt};
#[derive(SerializeDict, Type, Debug, 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>>,
}
#[derive(DeserializeDict, Type, Debug)]
#[zvariant(signature = "dict")]
pub struct Background {
background: bool,
autostart: bool,
}
impl Background {
pub fn request() -> BackgroundRequest {
BackgroundRequest::default()
}
pub fn run_in_background(&self) -> bool {
self.background
}
pub fn auto_start(&self) -> bool {
self.autostart
}
}
#[derive(SerializeDict, Type, Debug, Default)]
#[zvariant(signature = "dict")]
struct SetStatusOptions {
message: String,
}
#[doc(alias = "org.freedesktop.portal.Background")]
pub struct BackgroundProxy(Proxy<'static>);
impl BackgroundProxy {
pub async fn new() -> Result<BackgroundProxy, Error> {
let proxy = Proxy::new_desktop("org.freedesktop.portal.Background").await?;
Ok(Self(proxy))
}
pub async fn with_connection(connection: zbus::Connection) -> Result<BackgroundProxy, Error> {
let proxy =
Proxy::new_desktop_with_connection(connection, "org.freedesktop.portal.Background")
.await?;
Ok(Self(proxy))
}
#[doc(alias = "SetStatus")]
pub async fn set_status(&self, message: &str) -> Result<(), Error> {
self.0
.call_versioned(
"SetStatus",
&(SetStatusOptions {
message: message.to_owned(),
}),
2,
)
.await
}
#[doc(alias = "RequestBackground")]
async fn request_background(
&self,
identifier: Option<&WindowIdentifier>,
options: BackgroundOptions,
) -> Result<Request<Background>, Error> {
let identifier = identifier.to_string_or_empty();
self.0
.request(
&options.handle_token,
"RequestBackground",
(&identifier, &options),
)
.await
}
}
impl std::ops::Deref for BackgroundProxy {
type Target = zbus::Proxy<'static>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
#[doc(alias = "xdp_portal_request_background")]
#[derive(Debug, Default)]
pub struct BackgroundRequest {
identifier: Option<WindowIdentifier>,
options: BackgroundOptions,
}
impl BackgroundRequest {
#[must_use]
pub fn identifier(mut self, identifier: impl Into<Option<WindowIdentifier>>) -> Self {
self.identifier = identifier.into();
self
}
#[must_use]
pub fn auto_start(mut self, auto_start: impl Into<Option<bool>>) -> Self {
self.options.autostart = auto_start.into();
self
}
#[must_use]
pub fn dbus_activatable(mut self, dbus_activatable: impl Into<Option<bool>>) -> Self {
self.options.dbus_activatable = dbus_activatable.into();
self
}
#[must_use]
pub fn command<P: IntoIterator<Item = I>, I: AsRef<str> + Type + Serialize>(
mut self,
command: impl Into<Option<P>>,
) -> Self {
self.options.command = command
.into()
.map(|a| a.into_iter().map(|s| s.as_ref().to_owned()).collect());
self
}
#[must_use]
pub fn reason<'a>(mut self, reason: impl Into<Option<&'a str>>) -> Self {
self.options.reason = reason.into().map(ToOwned::to_owned);
self
}
pub async fn send(self) -> Result<Request<Background>, Error> {
let proxy = BackgroundProxy::new().await?;
proxy
.request_background(self.identifier.as_ref(), self.options)
.await
}
}