Add CLI args

- Dumb UI widget that processes nothing
- Accept icon, value, and color as arguments
This commit is contained in:
Candifloss 2025-12-10 16:19:33 +05:30
parent 1cde7cf7f3
commit e56461d081
3 changed files with 63 additions and 7 deletions

View File

@ -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<u8>,
#[arg(long = "icon")]
pub icon: Option<String>,
#[arg(long = "color")]
pub color: Option<String>,
}
pub struct OsdArgs {
pub value: Option<u8>,
pub icon: Option<String>,
pub color: Option<String>,
}
impl From<CliArgs> for OsdArgs {
fn from(c: CliArgs) -> Self {
Self {
value: c.value,
icon: c.icon,
color: c.color,
}
}
}

View File

@ -1,6 +1,13 @@
mod args;
mod show_popup; mod show_popup;
use args::{CliArgs, OsdArgs};
use clap::Parser;
fn main() -> Result<(), Box<dyn std::error::Error>> { fn main() -> Result<(), Box<dyn std::error::Error>> {
show_popup::show_popup()?; let cli = CliArgs::parse();
let parsed_args: OsdArgs = cli.into();
show_popup::show_popup(&parsed_args)?;
Ok(()) Ok(())
} }

View File

@ -1,3 +1,4 @@
use crate::args::OsdArgs;
use i_slint_backend_winit::{ use i_slint_backend_winit::{
Backend, Backend,
winit::{ winit::{
@ -13,8 +14,20 @@ fn argb_col(color_hex: u32) -> Color {
Color::from_argb_encoded(color_hex) Color::from_argb_encoded(color_hex)
} }
fn parse_hex_color(s: &str) -> Result<Color, String> {
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 /// Set Slint properties from config data
fn set_ui_props(ui: &OSDpopup) { fn set_ui_props(ui: &OSDpopup, args: &OsdArgs) -> Result<(), Box<dyn std::error::Error>> {
// Config values // Config values
let popup_width = 200; let popup_width = 200;
let popup_height = 50; let popup_height = 50;
@ -24,17 +37,21 @@ fn set_ui_props(ui: &OSDpopup) {
let window_position_y = 35; let window_position_y = 35;
let osd_bg_color = argb_col(0x_67_5f_5f_5f); 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_size = 35;
let value_font_color = argb_col(0x_ff_ff_ff_ff); let value_font_color = argb_col(0x_ff_ff_ff_ff);
let value_font = "Iosevka Extrabold"; 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_size = value_font_size;
let icon_font_color = value_font_color; let icon_font_color = value_font_color;
let icon_font = "IosevkaTermSlab Nerd Font Mono"; let icon_font = "IosevkaTermSlab Nerd Font Mono";
let icon_glyph = ""; let icon_glyph = args.icon.as_deref().unwrap_or("");
// Config values -> Slint component properties // Config values -> Slint component properties
ui.set_popup_width(popup_width); ui.set_popup_width(popup_width);
@ -66,10 +83,12 @@ fn set_ui_props(ui: &OSDpopup) {
window_position_x as f32, window_position_x as f32,
window_position_y as f32, window_position_y as f32,
)); ));
Ok(())
} }
/// Create and show the Window UI /// Create and show the Window UI
pub fn show_popup() -> Result<(), Box<dyn std::error::Error>> { pub fn show_popup(args: &OsdArgs) -> Result<(), Box<dyn std::error::Error>> {
// Closure that adjusts winit WindowAttributes before Slint creates the window. // Closure that adjusts winit WindowAttributes before Slint creates the window.
let window_attrs = |attrs: WindowAttributes| { 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. // 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<dyn std::error::Error>> {
let ui = OSDpopup::new()?; let ui = OSDpopup::new()?;
// Send the data to the UI as properties // Send the data to the UI as properties
set_ui_props(&ui); let _ = set_ui_props(&ui, args);
// Run the UI // Run the UI
ui.run()?; ui.run()?;