use crate::{
desktop::{HandleToken, DESTINATION, PATH},
helpers::call_basic_response_method,
Error, WindowIdentifier,
};
use serde::{self, Deserialize, Serialize, Serializer};
use std::os::unix::prelude::AsRawFd;
use strum_macros::{AsRefStr, EnumString, IntoStaticStr, ToString};
use zvariant::{Fd, Signature, Type};
use zvariant_derive::{DeserializeDict, SerializeDict, TypeDict};
#[derive(
Deserialize, Debug, Clone, Copy, PartialEq, Hash, AsRefStr, EnumString, IntoStaticStr, ToString,
)]
#[serde(rename = "lowercase")]
pub enum SetOn {
Lockscreen,
Background,
Both,
}
impl Type for SetOn {
fn signature() -> Signature<'static> {
String::signature()
}
}
impl Serialize for SetOn {
fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
where
S: Serializer,
{
String::serialize(&self.to_string(), serializer)
}
}
#[derive(SerializeDict, DeserializeDict, Clone, TypeDict, Debug, Default)]
struct WallpaperOptions {
handle_token: HandleToken,
#[zvariant(rename = "show-preview")]
show_preview: Option<bool>,
#[zvariant(rename = "set-on")]
set_on: Option<SetOn>,
}
impl WallpaperOptions {
pub fn show_preview(mut self, show_preview: bool) -> Self {
self.show_preview = Some(show_preview);
self
}
pub fn set_on(mut self, set_on: SetOn) -> Self {
self.set_on = Some(set_on);
self
}
}
#[derive(Debug)]
#[doc(alias = "org.freedesktop.portal.Wallpaper")]
pub struct WallpaperProxy<'a>(zbus::azync::Proxy<'a>);
impl<'a> WallpaperProxy<'a> {
pub async fn new(connection: &zbus::azync::Connection) -> Result<WallpaperProxy<'a>, Error> {
let proxy = zbus::ProxyBuilder::new_bare(connection)
.interface("org.freedesktop.portal.Wallpaper")
.path(PATH)?
.destination(DESTINATION)
.build_async()
.await?;
Ok(Self(proxy))
}
pub fn inner(&self) -> &zbus::azync::Proxy<'_> {
&self.0
}
#[doc(alias = "SetWallpaperFile")]
pub async fn set_wallpaper_file<F>(
&self,
identifier: WindowIdentifier,
file: &F,
show_preview: bool,
set_on: SetOn,
) -> Result<(), Error>
where
F: AsRawFd,
{
let options = WallpaperOptions::default()
.show_preview(show_preview)
.set_on(set_on);
call_basic_response_method(
&self.0,
&options.handle_token,
"SetWallpaperFile",
&(identifier, Fd::from(file.as_raw_fd()), &options),
)
.await
}
#[doc(alias = "SetWallpaperURI")]
pub async fn set_wallpaper_uri(
&self,
identifier: WindowIdentifier,
uri: &str,
show_preview: bool,
set_on: SetOn,
) -> Result<(), Error> {
let options = WallpaperOptions::default()
.show_preview(show_preview)
.set_on(set_on);
call_basic_response_method(
&self.0,
&options.handle_token,
"SetWallpaperURI",
&(identifier, uri, &options),
)
.await
}
}
#[doc(alias = "xdp_portal_set_wallpaper")]
pub async fn set_from_uri(
window: WindowIdentifier,
uri: &str,
show_preview: bool,
set_on: SetOn,
) -> Result<(), Error> {
let connection = zbus::azync::Connection::new_session().await?;
let proxy = WallpaperProxy::new(&connection).await?;
proxy
.set_wallpaper_uri(window, uri, show_preview, set_on)
.await?;
Ok(())
}
#[doc(alias = "xdp_portal_set_wallpaper")]
pub async fn set_from_file<F: AsRawFd>(
window: WindowIdentifier,
file: &F,
show_preview: bool,
set_on: SetOn,
) -> Result<(), Error> {
let connection = zbus::azync::Connection::new_session().await?;
let proxy = WallpaperProxy::new(&connection).await?;
proxy
.set_wallpaper_file(window, file, show_preview, set_on)
.await?;
Ok(())
}