Skip to content

Commit

Permalink
Take gtk scaling into account when setting struts
Browse files Browse the repository at this point in the history
  • Loading branch information
WilfSilver committed Jul 2, 2023
1 parent 9a7551c commit d1212e2
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 14 deletions.
14 changes: 8 additions & 6 deletions crates/eww/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use crate::{
use anyhow::anyhow;
use codespan_reporting::files::Files;
use eww_shared_util::{Span, VarName};
use gdk::Monitor;
use glib::ObjectExt;
use itertools::Itertools;
use once_cell::sync::Lazy;
Expand Down Expand Up @@ -398,8 +399,8 @@ impl<B: DisplayBackend> App<B> {

root_widget.style_context().add_class(window_name);

let monitor_geometry = get_monitor_geometry(initiator.monitor.clone())?;
let mut eww_window = initialize_window::<B>(&initiator, monitor_geometry, root_widget, window_scope)?;
let monitor = get_monitor(initiator.monitor.clone())?;
let mut eww_window = initialize_window::<B>(&initiator, monitor, root_widget, window_scope)?;
eww_window.gtk_window.style_context().add_class(window_name);

// initialize script var handlers for variables. As starting a scriptvar with the script_var_handler is idempodent,
Expand Down Expand Up @@ -492,10 +493,11 @@ impl<B: DisplayBackend> App<B> {

fn initialize_window<B: DisplayBackend>(
window_init: &WindowInitiator,
monitor_geometry: gdk::Rectangle,
monitor: Monitor,
root_widget: gtk::Widget,
window_scope: ScopeIndex,
) -> Result<EwwWindow> {
let monitor_geometry = monitor.geometry();
let window = B::initialize_window(window_init, monitor_geometry)
.with_context(|| format!("monitor {} is unavailable", window_init.monitor.clone().unwrap()))?;

Expand Down Expand Up @@ -531,7 +533,7 @@ fn initialize_window<B: DisplayBackend>(
});
}
}
display_backend::set_xprops(&window, monitor_geometry, window_init)?;
display_backend::set_xprops(&window, monitor, window_init)?;
}

window.show_all();
Expand Down Expand Up @@ -573,7 +575,7 @@ fn on_screen_changed(window: &gtk::Window, _old_screen: Option<&gdk::Screen>) {
}

/// Get the monitor geometry of a given monitor, or the default if none is given
fn get_monitor_geometry(identifier: Option<MonitorIdentifier>) -> Result<gdk::Rectangle> {
fn get_monitor(identifier: Option<MonitorIdentifier>) -> Result<Monitor> {
let display = gdk::Display::default().expect("could not get default display");
let monitor = match identifier {
Some(ident) => {
Expand Down Expand Up @@ -608,7 +610,7 @@ fn get_monitor_geometry(identifier: Option<MonitorIdentifier>) -> Result<gdk::Re
.primary_monitor()
.context("Failed to get primary monitor from GTK. Try explicitly specifying the monitor on your window.")?,
};
Ok(monitor.geometry())
Ok(monitor)
}

/// Returns the [Monitor][gdk::Monitor] structure corresponding to the identifer.
Expand Down
21 changes: 13 additions & 8 deletions crates/eww/src/display_backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ mod platform_wayland {
mod platform_x11 {
use crate::window_initiator::WindowInitiator;
use anyhow::{Context, Result};
use gdk::Monitor;
use gtk::{self, prelude::*};
use x11rb::protocol::xproto::ConnectionExt;

Expand Down Expand Up @@ -145,7 +146,7 @@ mod platform_x11 {
}
}

pub fn set_xprops(window: &gtk::Window, monitor: gdk::Rectangle, window_init: &WindowInitiator) -> Result<()> {
pub fn set_xprops(window: &gtk::Window, monitor: Monitor, window_init: &WindowInitiator) -> Result<()> {
let backend = X11BackendConnection::new()?;
backend.set_xprops_for(window, monitor, window_init)?;
Ok(())
Expand All @@ -168,17 +169,21 @@ mod platform_x11 {
fn set_xprops_for(
&self,
window: &gtk::Window,
monitor_rect: gdk::Rectangle,
monitor: Monitor,
window_init: &WindowInitiator,
) -> Result<()> {
let monitor_rect = monitor.geometry();
let scale_factor = monitor.scale_factor() as u32;
let gdk_window = window.window().context("Couldn't get gdk window from gtk window")?;
let win_id =
gdk_window.downcast_ref::<gdkx11::X11Window>().context("Failed to get x11 window for gtk window")?.xid() as u32;
let strut_def = window_init.backend_options.x11.struts;
let root_window_geometry = self.conn.get_geometry(self.root_window)?.reply()?;

let mon_end_x = (monitor_rect.x() + monitor_rect.width()) as u32 - 1u32;
let mon_end_y = (monitor_rect.y() + monitor_rect.height()) as u32 - 1u32;
let mon_x = scale_factor * monitor_rect.x() as u32;
let mon_y = scale_factor * monitor_rect.y() as u32;
let mon_end_x = scale_factor * (monitor_rect.x() + monitor_rect.width()) as u32 - 1u32;
let mon_end_y = scale_factor * (monitor_rect.y() + monitor_rect.height()) as u32 - 1u32;

let dist = match strut_def.side {
Side::Left | Side::Right => strut_def.dist.pixels_relative_to(monitor_rect.width()) as u32,
Expand All @@ -190,10 +195,10 @@ mod platform_x11 {
// left, right, top, bottom, left_start_y, left_end_y, right_start_y, right_end_y, top_start_x, top_end_x, bottom_start_x, bottom_end_x
#[rustfmt::skip]
let strut_list: Vec<u8> = match strut_def.side {
Side::Left => vec![dist + monitor_rect.x() as u32, 0, 0, 0, monitor_rect.y() as u32, mon_end_y, 0, 0, 0, 0, 0, 0],
Side::Right => vec![0, root_window_geometry.width as u32 - mon_end_x + dist, 0, 0, 0, 0, monitor_rect.y() as u32, mon_end_y, 0, 0, 0, 0],
Side::Top => vec![0, 0, dist + monitor_rect.y() as u32, 0, 0, 0, 0, 0, monitor_rect.x() as u32, mon_end_x, 0, 0],
Side::Bottom => vec![0, 0, 0, root_window_geometry.height as u32 - mon_end_y + dist, 0, 0, 0, 0, 0, 0, monitor_rect.x() as u32, mon_end_x],
Side::Left => vec![dist + mon_x, 0, 0, 0, mon_x, mon_end_y, 0, 0, 0, 0, 0, 0],
Side::Right => vec![0, root_window_geometry.width as u32 - mon_end_x + dist, 0, 0, 0, 0, mon_x, mon_end_y, 0, 0, 0, 0],
Side::Top => vec![0, 0, dist + mon_y as u32, 0, 0, 0, 0, 0, mon_x, mon_end_x, 0, 0],
Side::Bottom => vec![0, 0, 0, root_window_geometry.height as u32 - mon_end_y + dist, 0, 0, 0, 0, 0, 0, mon_x as u32, mon_end_x],
// This should never happen but if it does the window will be anchored on the
// right of the screen
}.iter().flat_map(|x| x.to_le_bytes().to_vec()).collect();
Expand Down

0 comments on commit d1212e2

Please sign in to comment.