Skip to content

Commit

Permalink
Add percentage labels to volume and brightness indicators (#69)
Browse files Browse the repository at this point in the history
* Add percentage labels to volume and brightness indicators

* Add config option to enable or disable percentage display
  • Loading branch information
Ferdi265 authored Sep 11, 2024
1 parent ebc9ebc commit c4bbb21
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 1 deletion.
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
3 changes: 3 additions & 0 deletions src/server/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,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| {
Expand Down
13 changes: 12 additions & 1 deletion 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 @@ -99,11 +102,15 @@ impl SwayosdWindow {

let icon = self.build_icon_widget(icon_name);
let progress = self.build_progress_widget(volume / max_volume);
let label = self.build_text_widget(Some(&format!("{}%", volume)));

progress.set_sensitive(!device.mute);

self.container.add(&icon);
self.container.add(&progress);
if get_show_percentage() {
self.container.add(&label);
}

self.run_timeout();
}
Expand All @@ -117,9 +124,13 @@ impl SwayosdWindow {
let brightness = brightness_backend.get_current() as f64;
let max = brightness_backend.get_max() as f64;
let progress = self.build_progress_widget(brightness / max);
let label = self.build_text_widget(Some(&format!("{}%", (brightness / max * 100.) as i32)));

self.container.add(&icon);
self.container.add(&progress);
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 c4bbb21

Please sign in to comment.