diff --git a/src/argtypes.rs b/src/argtypes.rs index 950f619..30b6bb3 100644 --- a/src/argtypes.rs +++ b/src/argtypes.rs @@ -7,6 +7,7 @@ pub enum ArgTypes { DeviceName = isize::MIN, TopMargin = isize::MIN + 1, MaxVolume = isize::MIN + 2, + ShowPercentage = isize::MIN + 3, // Other None = 0, CapsLock = 1, @@ -29,6 +30,7 @@ impl fmt::Display for ArgTypes { ArgTypes::None => "NONE", ArgTypes::CapsLock => "CAPSLOCK", ArgTypes::MaxVolume => "MAX-VOLUME", + ArgTypes::ShowPercentage => "SHOW-PERCENTAGE", ArgTypes::SinkVolumeRaise => "SINK-VOLUME-RAISE", ArgTypes::SinkVolumeLower => "SINK-VOLUME-LOWER", ArgTypes::SinkVolumeMuteToggle => "SINK-VOLUME-MUTE-TOGGLE", @@ -67,6 +69,7 @@ impl str::FromStr for ArgTypes { "SCROLL-LOCK" => ArgTypes::ScrollLock, "DEVICE-NAME" => ArgTypes::DeviceName, "TOP-MARGIN" => ArgTypes::TopMargin, + "SHOW-PERCENTAGE" => ArgTypes::ShowPercentage, other_type => return Err(other_type.to_owned()), }; Ok(result) diff --git a/src/config/user.rs b/src/config/user.rs index 179ba89..bb09442 100644 --- a/src/config/user.rs +++ b/src/config/user.rs @@ -14,6 +14,7 @@ pub struct ServerConfig { pub style: Option, pub top_margin: Option, pub max_volume: Option, + pub show_percentage: Option, } #[derive(Deserialize, Default, Debug)] diff --git a/src/global_utils.rs b/src/global_utils.rs index 8552b11..46d12ea 100644 --- a/src/global_utils.rs +++ b/src/global_utils.rs @@ -111,6 +111,7 @@ pub(crate) fn handle_application_args( } } } + "show-percentage" => (ArgTypes::ShowPercentage, None), "style" => continue, e => { eprintln!("Unknown Variant Key: \"{}\"!...", e); diff --git a/src/server/application.rs b/src/server/application.rs index 77eb444..0587ce7 100644 --- a/src/server/application.rs +++ b/src/server/application.rs @@ -47,6 +47,15 @@ impl SwayOSDApplication { Some(""), ); + app.add_main_option( + "show-percentage", + glib::Char::from(0), + OptionFlags::NONE, + OptionArg::None, + &format!("Show percentage for volume and brightness. Default is false",), + None, + ); + let osd_app = SwayOSDApplication { app: app.clone(), windows: Rc::new(RefCell::new(Vec::new())), @@ -61,6 +70,9 @@ impl SwayOSDApplication { if let Some(max_volume) = server_config.max_volume { set_default_max_volume(max_volume); } + if let Some(show) = server_config.show_percentage { + set_show_percentage(show); + } // Parse args app.connect_handle_local_options(clone!(@strong osd_app => move |_app, args| { @@ -87,6 +99,10 @@ impl SwayOSDApplication { set_default_max_volume(max); } }, + (ArgTypes::ShowPercentage, show) => { + println!("option --show-percentage: {:?}", show); + set_show_percentage(true); + }, (arg_type, data) => Self::action_activated(&osd_app, arg_type, data), } } diff --git a/src/server/osd_window.rs b/src/server/osd_window.rs index 1436f71..0f930d3 100644 --- a/src/server/osd_window.rs +++ b/src/server/osd_window.rs @@ -11,7 +11,10 @@ use pulsectl::controllers::types::DeviceInfo; use crate::{ brightness_backend::BrightnessBackend, - utils::{get_max_volume, get_top_margin, volume_to_f64, KeysLocks, VolumeDeviceType}, + utils::{ + get_max_volume, get_show_percentage, get_top_margin, volume_to_f64, KeysLocks, + VolumeDeviceType, + }, }; const ICON_SIZE: i32 = 32; @@ -105,7 +108,9 @@ impl SwayosdWindow { self.container.add(&icon); self.container.add(&progress); - self.container.add(&label); + if get_show_percentage() { + self.container.add(&label); + } self.run_timeout(); } @@ -123,7 +128,9 @@ impl SwayosdWindow { self.container.add(&icon); self.container.add(&progress); - self.container.add(&label); + if get_show_percentage() { + self.container.add(&label); + } self.run_timeout(); } diff --git a/src/server/utils.rs b/src/server/utils.rs index 5e14eec..1e226b04 100644 --- a/src/server/utils.rs +++ b/src/server/utils.rs @@ -23,6 +23,7 @@ lazy_static! { static ref DEVICE_NAME: Mutex> = Mutex::new(None); pub static ref TOP_MARGIN_DEFAULT: f32 = 0.85_f32; static ref TOP_MARGIN: Mutex = Mutex::new(*TOP_MARGIN_DEFAULT); + pub static ref SHOW_PERCENTAGE: Mutex = Mutex::new(false); } pub enum KeysLocks { @@ -63,6 +64,15 @@ pub fn set_top_margin(margin: f32) { *margin_mut = margin; } +pub fn get_show_percentage() -> bool { + *SHOW_PERCENTAGE.lock().unwrap() +} + +pub fn set_show_percentage(show: bool) { + let mut show_mut = SHOW_PERCENTAGE.lock().unwrap(); + *show_mut = show; +} + pub fn get_device_name() -> Option { (*DEVICE_NAME.lock().unwrap()).clone() }