Skip to content

Commit

Permalink
Add option to enable or disable percentage display
Browse files Browse the repository at this point in the history
  • Loading branch information
Ferdi265 committed May 6, 2024
1 parent 1a4a90b commit 07f7107
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 3 deletions.
3 changes: 3 additions & 0 deletions src/argtypes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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",
Expand Down Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions src/config/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub struct ServerConfig {
pub style: Option<PathBuf>,
pub top_margin: Option<f32>,
pub max_volume: Option<u8>,
pub show_percentage: Option<bool>,
}

#[derive(Deserialize, Default, Debug)]
Expand Down
1 change: 1 addition & 0 deletions src/global_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ pub(crate) fn handle_application_args(
}
}
}
"show-percentage" => (ArgTypes::ShowPercentage, None),
"style" => continue,
"config" => continue,
e => {
Expand Down
15 changes: 15 additions & 0 deletions src/server/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,15 @@ impl SwayOSDApplication {
Some("<from 0.0 to 1.0>"),
);

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())),
Expand All @@ -70,6 +79,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| {
Expand All @@ -96,6 +108,9 @@ impl SwayOSDApplication {
set_default_max_volume(max);
}
},
(ArgTypes::ShowPercentage, _) => {
set_show_percentage(true);
},
(arg_type, data) => Self::action_activated(&osd_app, arg_type, data),
}
}
Expand Down
13 changes: 10 additions & 3 deletions src/server/osd_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
Expand All @@ -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();
}
Expand Down
10 changes: 10 additions & 0 deletions src/server/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ lazy_static! {
static ref DEVICE_NAME: Mutex<Option<String>> = Mutex::new(None);
pub static ref TOP_MARGIN_DEFAULT: f32 = 0.85_f32;
static ref TOP_MARGIN: Mutex<f32> = Mutex::new(*TOP_MARGIN_DEFAULT);
pub static ref SHOW_PERCENTAGE: Mutex<bool> = Mutex::new(false);
}

pub enum KeysLocks {
Expand Down Expand Up @@ -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<String> {
(*DEVICE_NAME.lock().unwrap()).clone()
}
Expand Down

0 comments on commit 07f7107

Please sign in to comment.