From f95ff94dfc0ba398cb1191d5e9a0899843620df2 Mon Sep 17 00:00:00 2001 From: Candifloss Date: Thu, 18 Dec 2025 00:19:51 +0530 Subject: [PATCH] Refactor: Move wallpaper settings to new module - Args parsing - Config loading - Improve readability of `main()` --- src/main.rs | 44 +++++--------------------------------- src/settings.rs | 56 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 39 deletions(-) create mode 100644 src/settings.rs diff --git a/src/main.rs b/src/main.rs index a7aac76..d40c1ca 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,6 +12,7 @@ use x11rb::{ }; mod args; mod config; +mod settings; mod wallpaper; fn main() -> Result<()> { @@ -21,43 +22,8 @@ fn main() -> Result<()> { // Load config file if it exists. Can be missing. let config = config::Config::load().ok(); - // Resolve wallpaper image path by precedence: - // 1. args (`--set`, `--update`): Use path from args. - // 2. config: Use path from config. - let image_path = match (&args.set, &args.update) { - // Case: arg `--set`: One-shot wallpaper change. Not persistent. - (Some(path), None) => path.clone(), - - // Case: arg `--update`. Persist wallpaper path to config (create if missing), then apply it. - (None, Some(path)) => { - let cfg = config.unwrap_or_else(|| config::Config { - background_image: String::new(), - mode: None, - }); - - // Write to config. - cfg.update_background_image( - path.to_str() - // Unsupported path format. - .ok_or_else(|| anyhow::anyhow!("Non-UTF8 path"))?, - )?; - path.clone() - } - - // Case: No args. Fallback to config file. - (None, None) => { - let cfg = config.ok_or_else(|| { - // No CLI args and no valid config. - anyhow::anyhow!("No or invalid image path specified in config or args.") - })?; - - // Successfully loaded. - cfg.background_image.into() - } - - // Case: Both args `--set` & `--update`. This case is already rejected during argument parsing. - _ => unreachable!(), - }; + // Resolve wallpaper settings + let wallpaper_settings = settings::resolve_wallpaper(&args, config)?; // Connect to the running graphical session, the X11 server. let (conn, screen_num) = RustConnection::connect(None)?; @@ -103,8 +69,8 @@ fn main() -> Result<()> { // Prepare wallpaper image (scaling + pixel format conversion) let prepared = wallpaper::prepare_wallpaper( - &image_path, - wallpaper::ScalingMode::Stretch, // default for now + &wallpaper_settings.path, + wallpaper_settings.mode, width.into(), height.into(), )?; diff --git a/src/settings.rs b/src/settings.rs new file mode 100644 index 0000000..1ec1865 --- /dev/null +++ b/src/settings.rs @@ -0,0 +1,56 @@ +use anyhow::Result; +use std::path::PathBuf; + +use crate::{args, config}; + +pub struct WallpaperSettings { + pub path: PathBuf, + pub mode: crate::wallpaper::ScalingMode, +} + +pub fn resolve_wallpaper( + args: &args::Args, + config: Option, +) -> Result { + // Resolve wallpaper image path by precedence: + // 1. args (`--set`, `--update`): Use path from args. + // 2. config: Use path from config. + let path = match (&args.set, &args.update) { + // Case: arg `--set`: One-shot wallpaper change. Not persistent. + (Some(path), None) => path.clone(), + + // Case: arg `--update`. Persist wallpaper path to config (create if missing), then apply it. + (None, Some(path)) => { + let cfg = config.unwrap_or_else(|| config::Config { + background_image: String::new(), + mode: None, + }); + + // Write to config. + cfg.update_background_image( + path.to_str() + // Unsupported path format. + .ok_or_else(|| anyhow::anyhow!("Non-UTF8 path"))?, + )?; + path.clone() + } + + // Case: No args. Fallback to config file. + (None, None) => { + let cfg = config.ok_or_else(|| { + // No CLI args and no valid config. + anyhow::anyhow!("No or invalid image path specified in config or args.") + })?; + + // Successfully loaded. + cfg.background_image.into() + } + + // Case: Both args `--set` & `--update`. This case is already rejected during argument parsing. + _ => unreachable!(), + }; + + let mode = crate::wallpaper::ScalingMode::Stretch; // Default for now. Fix later + + Ok(WallpaperSettings { path, mode }) +}