1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// THIS MODULE HANDLES THE SETTING AND GETTING
// OF THE WALLPAPER
use std::error::Error;
use std::path::PathBuf;

// Only one of these three sets gets compiled based on the
// OS being run on
#[cfg(target_os = "linux")]
mod linux;
#[cfg(target_os = "linux")]
pub use linux::DesktopEnvt;

#[cfg(target_os = "macos")]
mod macos;
#[cfg(target_os = "macos")]
pub use macos::DesktopEnvt;

#[cfg(target_os = "windows")]
mod windows;
#[cfg(target_os = "windows")]
pub use windows::DesktopEnvt;

/// A trait implemented by desktop environments. It allows setting or getting a wallpaper.
///
/// On platforms where only one desktop environment exists (e.g. Windows, macOS), this can
/// be implemented with a zero-sized type. On Linux, it is an enum.
pub trait Desktop: Sized {
    /// Creates a new instance of this desktop.
    ///
    /// On Linux, this function detects the desktop environment.
    /// It panics if the desktop environment is unsupported. It returns an error
    /// if the desktop environment couldn't be determined (i.e., the `XDG_CURRENT_DESKTOP`
    /// environment variable isn't set).
    fn new() -> Result<Self, Box<dyn Error>>;

    /// Sets the wallpaper for all computer screens to the specified file path.
    ///
    /// The file should be an image file supported by the patform, e.g. a JPEG.
    fn set_wallpaper(&self, path: &str) -> Result<(), Box<dyn Error>>;

    /// Returns the file path to the image used as the wallpaper.
    ///
    /// If different screens have different wallpapers, only one of them is returned;
    /// the behavior depends on the platform and desktop environment.
    fn get_wallpaper(&self) -> Result<PathBuf, Box<dyn Error>>;
}