pub mod desktop_env;
pub mod utils;
use crate::{ImageMode, Platform};
use anyhow::Result;
use desktop_env::DesktopEnv;
use std::path::PathBuf;
use utils::*;
use waraq::xcb::{self, get_display_info};
pub struct LinuxEnv {
current_desktop: DesktopEnv,
config_path: PathBuf,
}
impl LinuxEnv {
pub fn new() -> Result<Self> {
let current_desktop = DesktopEnv::get_current()?;
let config_path: PathBuf = desktop_config_path(¤t_desktop)?;
Ok(Self {
current_desktop,
config_path,
})
}
pub fn set_bg_shell(&self, bg_path: PathBuf, mode: ImageMode) -> Result<()> {
let copied_path = copy_bg_with_mode(bg_path, mode)?;
run_shell(&self.config_path, &copied_path)
}
pub fn set_bg_xcb(bg_path: PathBuf, mode: ImageMode) -> Result<()> {
let image = image::open(bg_path)?;
let dim = get_display_info()?;
let resized_image = mode.apply(image, dim);
Ok(xcb::set_bg(resized_image.as_raw())?)
}
}
impl Platform for LinuxEnv {
fn set_bg(bg_path: PathBuf, mode: ImageMode) -> Result<()> {
let env = Self::new()?;
match (env.config_path.exists(), &env.current_desktop) {
(false, DesktopEnv::Other) => Self::set_bg_xcb(bg_path, mode),
(false, _) => {
create_config_dir()?;
parse_default_setters(&env.config_path, &env.current_desktop)?;
env.set_bg_shell(bg_path, mode)
}
_ => env.set_bg_shell(bg_path, mode),
}
}
}