From e56461d081142fc524d5b168fe0ec38b7801020d Mon Sep 17 00:00:00 2001 From: candifloss Date: Wed, 10 Dec 2025 16:19:33 +0530 Subject: [PATCH] Add CLI args - Dumb UI widget that processes nothing - Accept icon, value, and color as arguments --- crates/popcorn/src/args.rs | 30 ++++++++++++++++++++++++++++++ crates/popcorn/src/main.rs | 9 ++++++++- crates/popcorn/src/show_popup.rs | 31 +++++++++++++++++++++++++------ 3 files changed, 63 insertions(+), 7 deletions(-) create mode 100644 crates/popcorn/src/args.rs diff --git a/crates/popcorn/src/args.rs b/crates/popcorn/src/args.rs new file mode 100644 index 0000000..425ee60 --- /dev/null +++ b/crates/popcorn/src/args.rs @@ -0,0 +1,30 @@ +use clap::Parser; + +#[derive(Parser, Debug)] +#[command(name = "popcorn", version, about = "Minimal OSD popup renderer")] +pub struct CliArgs { + #[arg(long = "value")] + pub value: Option, + + #[arg(long = "icon")] + pub icon: Option, + + #[arg(long = "color")] + pub color: Option, +} + +pub struct OsdArgs { + pub value: Option, + pub icon: Option, + pub color: Option, +} + +impl From for OsdArgs { + fn from(c: CliArgs) -> Self { + Self { + value: c.value, + icon: c.icon, + color: c.color, + } + } +} diff --git a/crates/popcorn/src/main.rs b/crates/popcorn/src/main.rs index 22e945a..7c04dc5 100644 --- a/crates/popcorn/src/main.rs +++ b/crates/popcorn/src/main.rs @@ -1,6 +1,13 @@ +mod args; mod show_popup; +use args::{CliArgs, OsdArgs}; +use clap::Parser; + fn main() -> Result<(), Box> { - show_popup::show_popup()?; + let cli = CliArgs::parse(); + let parsed_args: OsdArgs = cli.into(); + + show_popup::show_popup(&parsed_args)?; Ok(()) } diff --git a/crates/popcorn/src/show_popup.rs b/crates/popcorn/src/show_popup.rs index f04cb0a..5828d16 100644 --- a/crates/popcorn/src/show_popup.rs +++ b/crates/popcorn/src/show_popup.rs @@ -1,3 +1,4 @@ +use crate::args::OsdArgs; use i_slint_backend_winit::{ Backend, winit::{ @@ -13,8 +14,20 @@ fn argb_col(color_hex: u32) -> Color { Color::from_argb_encoded(color_hex) } +fn parse_hex_color(s: &str) -> Result { + let clean = s.trim().trim_start_matches('#'); + + let raw = u32::from_str_radix(clean, 16).map_err(|_| "invalid hex color")?; + + if clean.len() == 8 { + Ok(Color::from_argb_encoded(raw)) + } else { + Err("color must be 8 hex chars (AARRGGBB)".into()) + } +} + /// Set Slint properties from config data -fn set_ui_props(ui: &OSDpopup) { +fn set_ui_props(ui: &OSDpopup, args: &OsdArgs) -> Result<(), Box> { // Config values let popup_width = 200; let popup_height = 50; @@ -24,17 +37,21 @@ fn set_ui_props(ui: &OSDpopup) { let window_position_y = 35; let osd_bg_color = argb_col(0x_67_5f_5f_5f); - let fill_color = argb_col(0x_ff_12_7a_9b); + let fill_color = if let Some(c) = args.color.as_deref() { + parse_hex_color(c)? + } else { + argb_col(0x_ff_12_7a_9b) + }; let value_font_size = 35; let value_font_color = argb_col(0x_ff_ff_ff_ff); let value_font = "Iosevka Extrabold"; - let percent_value = 35; + let percent_value = i32::from(args.value.unwrap_or(0)); let icon_size = value_font_size; let icon_font_color = value_font_color; let icon_font = "IosevkaTermSlab Nerd Font Mono"; - let icon_glyph = ""; + let icon_glyph = args.icon.as_deref().unwrap_or(""); // Config values -> Slint component properties ui.set_popup_width(popup_width); @@ -66,10 +83,12 @@ fn set_ui_props(ui: &OSDpopup) { window_position_x as f32, window_position_y as f32, )); + + Ok(()) } /// Create and show the Window UI -pub fn show_popup() -> Result<(), Box> { +pub fn show_popup(args: &OsdArgs) -> Result<(), Box> { // Closure that adjusts winit WindowAttributes before Slint creates the window. let window_attrs = |attrs: WindowAttributes| { // Mark the X11 window as a Dock so the WM doesn't treat it as a normal window. Window type `Notification` didn't work. Fix later. @@ -88,7 +107,7 @@ pub fn show_popup() -> Result<(), Box> { let ui = OSDpopup::new()?; // Send the data to the UI as properties - set_ui_props(&ui); + let _ = set_ui_props(&ui, args); // Run the UI ui.run()?;