| 1 |
//! Set a wallpaper on lockscreen, background or both. |
| 2 |
//! |
| 3 |
//! Wrapper of the DBus interface: [`org.freedesktop.portal.Wallpaper`](https://flatpak.github.io/xdg-desktop-portal/docs/doc-org.freedesktop.portal.Wallpaper.html). |
| 4 |
//! |
| 5 |
//! # Examples |
| 6 |
//! |
| 7 |
//! ## Sets a wallpaper from a file: |
| 8 |
//! |
| 9 |
//! ```rust,no_run |
| 10 |
//! use std::{fs::File, os::fd::AsFd}; |
| 11 |
//! |
| 12 |
//! use ashpd::desktop::wallpaper::{SetOn, WallpaperRequest}; |
| 13 |
//! |
| 14 |
//! async fn run() -> ashpd::Result<()> { |
| 15 |
//! let file = File::open("/home/bilelmoussaoui/adwaita-day.jpg").unwrap(); |
| 16 |
//! WallpaperRequest::default() |
| 17 |
//! .set_on(SetOn::Both) |
| 18 |
//! .show_preview(true) |
| 19 |
//! .build_file(&file.as_fd()) |
| 20 |
//! .await?; |
| 21 |
//! Ok(()) |
| 22 |
//! } |
| 23 |
//! ``` |
| 24 |
//! |
| 25 |
//! ## Sets a wallpaper from a URI: |
| 26 |
//! |
| 27 |
//! ```rust,no_run |
| 28 |
//! use ashpd::desktop::wallpaper::{SetOn, WallpaperRequest}; |
| 29 |
//! |
| 30 |
//! async fn run() -> ashpd::Result<()> { |
| 31 |
//! let uri = |
| 32 |
//! ashpd::Uri::parse("file:///home/bilelmoussaoui/Downloads/adwaita-night.jpg").unwrap(); |
| 33 |
//! WallpaperRequest::default() |
| 34 |
//! .set_on(SetOn::Both) |
| 35 |
//! .show_preview(true) |
| 36 |
//! .build_uri(&uri) |
| 37 |
//! .await?; |
| 38 |
//! Ok(()) |
| 39 |
//! } |
| 40 |
//! ``` |
| 41 |
|
| 42 |
use std::{fmt, os::fd::AsFd, str::FromStr}; |
| 43 |
|
| 44 |
use serde::{self, Deserialize, Serialize}; |
| 45 |
use zbus::zvariant::{ |
| 46 |
Fd, Optional, Type, |
| 47 |
as_value::{self, optional}, |
| 48 |
}; |
| 49 |
|
| 50 |
use super::Request; |
| 51 |
use crate::{Error, Uri, WindowIdentifier, desktop::HandleToken, proxy::Proxy}; |
| 52 |
|
| 53 |
#[cfg_attr(feature = "glib", derive(glib::Enum))] |
| 54 |
#[cfg_attr(feature = "glib", enum_type(name = "AshpdSetOn"))] |
| 55 |
#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq, Hash, Type)] |
| 56 |
#[zvariant(signature = "s")] |
| 57 |
#[serde(rename_all = "lowercase")] |
| 58 |
/// Where to set the wallpaper on. |
| 59 |
pub enum SetOn { |
| 60 |
/// Set the wallpaper only on the lock-screen. |
| 61 |
Lockscreen, |
| 62 |
/// Set the wallpaper only on the background. |
| 63 |
Background, |
| 64 |
/// Set the wallpaper on both lock-screen and background. |
| 65 |
Both, |
| 66 |
} |
| 67 |
|
| 68 |
impl fmt::Display for SetOn { |
| 69 |
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 70 |
match self { |
| 71 |
Self::Lockscreen => write!(f, "Lockscreen"), |
| 72 |
Self::Background => write!(f, "Background"), |
| 73 |
Self::Both => write!(f, "Both"), |
| 74 |
} |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
impl AsRef<str> for SetOn { |
| 79 |
fn as_ref(&self) -> &str { |
| 80 |
match self { |
| 81 |
Self::Lockscreen => "Lockscreen", |
| 82 |
Self::Background => "Background", |
| 83 |
Self::Both => "Both", |
| 84 |
} |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
impl From<SetOn> for &'static str { |
| 89 |
fn from(s: SetOn) -> Self { |
| 90 |
match s { |
| 91 |
SetOn::Lockscreen => "Lockscreen", |
| 92 |
SetOn::Background => "Background", |
| 93 |
SetOn::Both => "Both", |
| 94 |
} |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
impl FromStr for SetOn { |
| 99 |
type Err = Error; |
| 100 |
|
| 101 |
fn from_str(s: &str) -> Result<Self, Self::Err> { |
| 102 |
match s { |
| 103 |
"Lockscreen" => Ok(SetOn::Lockscreen), |
| 104 |
"Background" => Ok(SetOn::Background), |
| 105 |
"Both" => Ok(SetOn::Both), |
| 106 |
_ => Err(Error::ParseError("Failed to parse SetOn, invalid value")), |
| 107 |
} |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
/// Options for setting a wallpaper. |
| 112 |
#[derive(Serialize, Deserialize, Type, Debug, Default)] |
| 113 |
#[zvariant(signature = "dict")] |
| 114 |
#[serde(rename_all = "kebab-case")] |
| 115 |
pub struct WallpaperOptions { |
| 116 |
#[serde(rename = "handle_token", with = "as_value", skip_deserializing)] |
| 117 |
handle_token: HandleToken, |
| 118 |
#[serde(default, with = "optional", skip_serializing_if = "Option::is_none")] |
| 119 |
show_preview: Option<bool>, |
| 120 |
#[serde(default, with = "optional", skip_serializing_if = "Option::is_none")] |
| 121 |
set_on: Option<SetOn>, |
| 122 |
} |
| 123 |
|
| 124 |
impl WallpaperOptions { |
| 125 |
/// Sets whether to show a preview of the wallpaper. |
| 126 |
#[must_use] |
| 127 |
pub fn set_show_preview(mut self, show_preview: impl Into<Option<bool>>) -> Self { |
| 128 |
self.show_preview = show_preview.into(); |
| 129 |
self |
| 130 |
} |
| 131 |
|
| 132 |
/// Gets whether to show a preview of the wallpaper. |
| 133 |
#[cfg(feature = "backend")] |
| 134 |
pub fn show_preview(&self) -> Option<bool> { |
| 135 |
self.show_preview |
| 136 |
} |
| 137 |
|
| 138 |
/// Sets where to set the wallpaper. |
| 139 |
#[must_use] |
| 140 |
pub fn set_set_on(mut self, set_on: impl Into<Option<SetOn>>) -> Self { |
| 141 |
self.set_on = set_on.into(); |
| 142 |
self |
| 143 |
} |
| 144 |
|
| 145 |
/// Gets where to set the wallpaper. |
| 146 |
#[cfg(feature = "backend")] |
| 147 |
pub fn set_on(&self) -> Option<SetOn> { |
| 148 |
self.set_on |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
/// Wrapper of the DBus interface: [`org.freedesktop.portal.Wallpaper`](https://flatpak.github.io/xdg-desktop-portal/docs/doc-org.freedesktop.portal.Wallpaper.html). |
| 153 |
#[doc(alias = "org.freedesktop.portal.Wallpaper")] |
| 154 |
pub struct WallpaperProxy(Proxy<'static>); |
| 155 |
|
| 156 |
impl WallpaperProxy { |
| 157 |
/// Create a new instance of [`WallpaperProxy`]. |
| 158 |
pub async fn new() -> Result<Self, Error> { |
| 159 |
let proxy = Proxy::new_desktop("org.freedesktop.portal.Wallpaper").await?; |
| 160 |
Ok(Self(proxy)) |
| 161 |
} |
| 162 |
|
| 163 |
/// Create a new instance of [`WallpaperProxy`] with a specific connection. |
| 164 |
pub async fn with_connection(connection: zbus::Connection) -> Result<Self, Error> { |
| 165 |
let proxy = |
| 166 |
Proxy::new_desktop_with_connection(connection, "org.freedesktop.portal.Wallpaper") |
| 167 |
.await?; |
| 168 |
Ok(Self(proxy)) |
| 169 |
} |
| 170 |
|
| 171 |
/// Returns the portal interface version. |
| 172 |
pub fn version(&self) -> u32 { |
| 173 |
self.0.version() |
| 174 |
} |
| 175 |
|
| 176 |
/// Sets the wallpaper from a file. |
| 177 |
#[doc(alias = "SetWallpaperFile")] |
| 178 |
pub async fn set_wallpaper_file( |
| 179 |
&self, |
| 180 |
identifier: Option<&WindowIdentifier>, |
| 181 |
file: &impl AsFd, |
| 182 |
options: WallpaperOptions, |
| 183 |
) -> Result<Request<()>, Error> { |
| 184 |
let identifier = Optional::from(identifier); |
| 185 |
self.0 |
| 186 |
.empty_request( |
| 187 |
&options.handle_token, |
| 188 |
"SetWallpaperFile", |
| 189 |
&(identifier, Fd::from(file), &options), |
| 190 |
) |
| 191 |
.await |
| 192 |
} |
| 193 |
|
| 194 |
/// Sets the wallpaper from a URI. |
| 195 |
#[doc(alias = "SetWallpaperURI")] |
| 196 |
pub async fn set_wallpaper_uri( |
| 197 |
&self, |
| 198 |
identifier: Option<&WindowIdentifier>, |
| 199 |
uri: &Uri, |
| 200 |
options: WallpaperOptions, |
| 201 |
) -> Result<Request<()>, Error> { |
| 202 |
let identifier = Optional::from(identifier); |
| 203 |
self.0 |
| 204 |
.empty_request( |
| 205 |
&options.handle_token, |
| 206 |
"SetWallpaperURI", |
| 207 |
&(identifier, uri, &options), |
| 208 |
) |
| 209 |
.await |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
impl std::ops::Deref for WallpaperProxy { |
| 214 |
type Target = zbus::Proxy<'static>; |
| 215 |
|
| 216 |
fn deref(&self) -> &Self::Target { |
| 217 |
&self.0 |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
#[derive(Debug, Default)] |
| 222 |
#[doc(alias = "xdp_portal_set_wallpaper")] |
| 223 |
#[doc(alias = "org.freedesktop.portal.Wallpaper")] |
| 224 |
/// A [builder-pattern] type to set the wallpaper. |
| 225 |
/// |
| 226 |
/// [builder-pattern]: https://doc.rust-lang.org/1.0.0/style/ownership/builders.html |
| 227 |
pub struct WallpaperRequest { |
| 228 |
identifier: Option<WindowIdentifier>, |
| 229 |
options: WallpaperOptions, |
| 230 |
connection: Option<zbus::Connection>, |
| 231 |
} |
| 232 |
|
| 233 |
impl WallpaperRequest { |
| 234 |
#[must_use] |
| 235 |
/// Sets a window identifier. |
| 236 |
pub fn identifier(mut self, identifier: impl Into<Option<WindowIdentifier>>) -> Self { |
| 237 |
self.identifier = identifier.into(); |
| 238 |
self |
| 239 |
} |
| 240 |
|
| 241 |
/// Whether to show a preview of the picture. |
| 242 |
/// **Note** the portal may decide to show a preview even if this option is |
| 243 |
/// not set. |
| 244 |
#[must_use] |
| 245 |
pub fn show_preview(mut self, show_preview: impl Into<Option<bool>>) -> Self { |
| 246 |
self.options.show_preview = show_preview.into(); |
| 247 |
self |
| 248 |
} |
| 249 |
|
| 250 |
/// Sets where to set the wallpaper on. |
| 251 |
#[must_use] |
| 252 |
pub fn set_on(mut self, set_on: impl Into<Option<SetOn>>) -> Self { |
| 253 |
self.options.set_on = set_on.into(); |
| 254 |
self |
| 255 |
} |
| 256 |
|
| 257 |
#[must_use] |
| 258 |
/// Sets a connection to use other than the internal one. |
| 259 |
pub fn connection(mut self, connection: Option<zbus::Connection>) -> Self { |
| 260 |
self.connection = connection; |
| 261 |
self |
| 262 |
} |
| 263 |
|
| 264 |
/// Build using a URI. |
| 265 |
pub async fn build_uri(self, uri: &Uri) -> Result<Request<()>, Error> { |
| 266 |
let proxy = if let Some(connection) = self.connection { |
| 267 |
WallpaperProxy::with_connection(connection).await? |
| 268 |
} else { |
| 269 |
WallpaperProxy::new().await? |
| 270 |
}; |
| 271 |
proxy |
| 272 |
.set_wallpaper_uri(self.identifier.as_ref(), uri, self.options) |
| 273 |
.await |
| 274 |
} |
| 275 |
|
| 276 |
/// Build using a file. |
| 277 |
pub async fn build_file(self, file: &impl AsFd) -> Result<Request<()>, Error> { |
| 278 |
let proxy = if let Some(connection) = self.connection { |
| 279 |
WallpaperProxy::with_connection(connection).await? |
| 280 |
} else { |
| 281 |
WallpaperProxy::new().await? |
| 282 |
}; |
| 283 |
proxy |
| 284 |
.set_wallpaper_file(self.identifier.as_ref(), file, self.options) |
| 285 |
.await |
| 286 |
} |
| 287 |
} |
| 288 |
#[cfg(test)] |
| 289 |
mod tests { |
| 290 |
use super::SetOn; |
| 291 |
|
| 292 |
#[test] |
| 293 |
fn serialize_deserialize() { |
| 294 |
let set_on = SetOn::Both; |
| 295 |
let string = serde_json::to_string(&set_on).unwrap(); |
| 296 |
assert_eq!(string, "\"both\""); |
| 297 |
|
| 298 |
let decoded = serde_json::from_str(&string).unwrap(); |
| 299 |
assert_eq!(set_on, decoded); |
| 300 |
} |
| 301 |
} |