Skip to content

Commit

Permalink
add parameter --top-margin (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
bits0rcerer committed Jul 18, 2023
1 parent 446dcdd commit b14c838
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 3 deletions.
22 changes: 22 additions & 0 deletions src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ pub enum ArgTypes {
ScrollLock = 12,
// should always be first to set a global variable before executing related functions
DeviceName = isize::MIN,
TopMargin = isize::MIN + 1
}

impl fmt::Display for ArgTypes {
Expand All @@ -55,6 +56,7 @@ impl fmt::Display for ArgTypes {
ArgTypes::NumLock => "NUM-LOCK",
ArgTypes::ScrollLock => "SCROLL-LOCK",
ArgTypes::DeviceName => "DEVICE-NAME",
ArgTypes::TopMargin => "TOP-MARGIN",
};
return write!(f, "{}", string);
}
Expand All @@ -78,6 +80,7 @@ impl str::FromStr for ArgTypes {
"NUM-LOCK" => ArgTypes::NumLock,
"SCROLL-LOCK" => ArgTypes::ScrollLock,
"DEVICE-NAME" => ArgTypes::DeviceName,
"TOP-MARGIN" => ArgTypes::TopMargin,
_ => ArgTypes::None,
};
Ok(result)
Expand Down Expand Up @@ -191,6 +194,14 @@ impl SwayOSDApplication {
"For which device to increase/decrease audio",
Some("Pulseaudio device name (pactl list short sinks|sources)"),
);
app.add_main_option(
"top-margin",
glib::Char::from(0),
OptionFlags::NONE,
OptionArg::String,
"OSD margin from top edge (0.5 would be screen center)",
Some("from 0.0 to 1.0"),
);

// Parse args
app.connect_handle_local_options(|app, args| -> i32 {
Expand Down Expand Up @@ -290,6 +301,16 @@ impl SwayOSDApplication {
};
(ArgTypes::DeviceName, Some(value))
}
"top-margin" => {
let value = child.value().str().unwrap_or("").trim();
match value.parse::<f32>() {
Ok(top_margin) if (0.0f32..=1.0f32).contains(&top_margin) => (ArgTypes::TopMargin, Some(value.to_string())),
_ => {
eprintln!("{} is not a number between 0.0 and 1.0!", value);
return 1;
}
}
}
e => {
eprintln!("Unknown Variant Key: \"{}\"!...", e);
return 1;
Expand Down Expand Up @@ -572,6 +593,7 @@ impl SwayOSDApplication {
(Ok(ArgTypes::DeviceName), name) => {
set_device_name(name.unwrap_or("default".to_owned()))
}
(Ok(ArgTypes::TopMargin), max) => set_top_margin(max),
(_, _) => {
eprintln!("Failed to parse variant: {}!...", variant.print(true))
}
Expand Down
7 changes: 4 additions & 3 deletions src/osd_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use gtk::{
};
use pulsectl::controllers::types::DeviceInfo;

use crate::utils::{volume_to_f64, KeysLocks, VolumeDeviceType};
use crate::utils::{volume_to_f64, KeysLocks, VolumeDeviceType, get_top_margin};
use blight::Device;

const DISABLED_OPACITY: f64 = 0.5;
Expand Down Expand Up @@ -40,6 +40,7 @@ impl SwayosdWindow {
gtk_layer_shell::set_monitor(&window, monitor);
gtk_layer_shell::set_namespace(&window, "swayosd");

gtk_layer_shell::set_exclusive_zone(&window, -1);
gtk_layer_shell::set_layer(&window, gtk_layer_shell::Layer::Overlay);
gtk_layer_shell::set_anchor(&window, gtk_layer_shell::Edge::Top, true);

Expand Down Expand Up @@ -108,8 +109,8 @@ impl SwayosdWindow {

// Set the window margin
window.connect_map(clone!(@strong monitor => move |win| {
let bottom = monitor.workarea().height() - win.height_request();
let margin = (bottom as f32 * 0.75).round() as i32;
let bottom = monitor.workarea().height() - win.allocated_height();
let margin = (bottom as f32 * get_top_margin()).round() as i32;
gtk_layer_shell::set_margin(win, gtk_layer_shell::Edge::Top, margin);
}));

Expand Down
12 changes: 12 additions & 0 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use crate::application::ArgTypes;
lazy_static! {
static ref MAX_VOLUME: Mutex<u8> = Mutex::new(100_u8);
static ref DEVICE_NAME: Mutex<String> = Mutex::new("default".to_string());
static ref TOP_MARGIN: Mutex<f32> = Mutex::new(0.85_f32);
}

pub enum KeysLocks {
Expand Down Expand Up @@ -67,6 +68,17 @@ pub fn set_max_volume(volume: Option<String>) {
*vol = setter;
}

pub fn get_top_margin() -> f32 {
*TOP_MARGIN.lock().unwrap()
}

pub fn set_top_margin(volume: Option<String>) {
let setter: f32 = volume.unwrap().parse().unwrap();

let mut vol = TOP_MARGIN.lock().unwrap();
*vol = setter;
}

pub fn get_device_name() -> String {
(*DEVICE_NAME.lock().unwrap()).clone()
}
Expand Down

0 comments on commit b14c838

Please sign in to comment.