Skip to content

Commit

Permalink
server/osd_window: update window margin when parameters change
Browse files Browse the repository at this point in the history
This ensures the window margin is always correct. This also fixes an
annoying issue where the monitor scale factor is not always correct (it
never changes back to scale 1 if it was scale 2 at some point) by
transforming the monitor height into window coordinates manually.
  • Loading branch information
Ferdi265 committed Sep 20, 2024
1 parent 253f633 commit c8c4035
Showing 1 changed file with 27 additions and 9 deletions.
36 changes: 27 additions & 9 deletions src/server/osd_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,34 @@ impl SwayosdWindow {

window.set_child(Some(&container));

let update_margins = |window: &gtk::ApplicationWindow, monitor: &gdk::Monitor| {
// Monitor scale factor is not always correct
// Transform monitor height into coordinate system of window
let mon_height = monitor.geometry().height() * monitor.scale_factor() / window.scale_factor();
// Calculate new margin
let bottom = mon_height - window.allocated_height();
let margin = (bottom as f32 * get_top_margin()).round() as i32;
window.set_margin(gtk_layer_shell::Edge::Top, margin);
};

// Set the window margin
window.connect_realize(clone!(
#[strong]
monitor,
move |win| {
let bottom = monitor.geometry().height() - win.allocated_height();
let margin = (bottom as f32 * get_top_margin()).round() as i32;
win.set_margin(gtk_layer_shell::Edge::Top, margin);
}
));
update_margins(&window, monitor);
// Ensure window margin is updated when necessary
window.connect_scale_factor_notify(clone!(
#[weak]
monitor,
move |window| update_margins(window, &monitor)
));
monitor.connect_scale_factor_notify(clone!(
#[weak]
window,
move |monitor| update_margins(&window, monitor)
));
monitor.connect_geometry_notify(clone!(
#[weak]
window,
move |monitor| update_margins(&window, monitor)
));

Self {
window,
Expand Down

0 comments on commit c8c4035

Please sign in to comment.