wallpaper_rs/lib.rs
1// THIS MODULE HANDLES THE SETTING AND GETTING
2// OF THE WALLPAPER
3use std::error::Error;
4use std::path::PathBuf;
5
6// Only one of these three sets gets compiled based on the
7// OS being run on
8#[cfg(target_os = "linux")]
9mod linux;
10#[cfg(target_os = "linux")]
11pub use linux::DesktopEnvt;
12
13#[cfg(target_os = "macos")]
14mod macos;
15#[cfg(target_os = "macos")]
16pub use macos::DesktopEnvt;
17
18#[cfg(target_os = "windows")]
19mod windows;
20#[cfg(target_os = "windows")]
21pub use windows::DesktopEnvt;
22
23/// A trait implemented by desktop environments. It allows setting or getting a wallpaper.
24///
25/// On platforms where only one desktop environment exists (e.g. Windows, macOS), this can
26/// be implemented with a zero-sized type. On Linux, it is an enum.
27pub trait Desktop: Sized {
28 /// Creates a new instance of this desktop.
29 ///
30 /// On Linux, this function detects the desktop environment.
31 /// It panics if the desktop environment is unsupported. It returns an error
32 /// if the desktop environment couldn't be determined (i.e., the `XDG_CURRENT_DESKTOP`
33 /// environment variable isn't set).
34 fn new() -> Result<Self, Box<dyn Error>>;
35
36 /// Sets the wallpaper for all computer screens to the specified file path.
37 ///
38 /// The file should be an image file supported by the patform, e.g. a JPEG.
39 fn set_wallpaper(&self, path: &str) -> Result<(), Box<dyn Error>>;
40
41 /// Returns the file path to the image used as the wallpaper.
42 ///
43 /// If different screens have different wallpapers, only one of them is returned;
44 /// the behavior depends on the platform and desktop environment.
45 fn get_wallpaper(&self) -> Result<PathBuf, Box<dyn Error>>;
46}