Refactor: Move wallpaper settings to new module
- Args parsing - Config loading - Improve readability of `main()`
This commit is contained in:
parent
6bb785be02
commit
f95ff94dfc
44
src/main.rs
44
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(),
|
||||
)?;
|
||||
|
||||
56
src/settings.rs
Normal file
56
src/settings.rs
Normal file
@ -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<config::Config>,
|
||||
) -> Result<WallpaperSettings> {
|
||||
// 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 })
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user