Skip to content

Commit

Permalink
fix: popups not accounting for monitor scaling
Browse files Browse the repository at this point in the history
Fixes #657
  • Loading branch information
JakeStanger committed Aug 9, 2024
1 parent eaf13c5 commit 45d5bf5
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 23 deletions.
17 changes: 13 additions & 4 deletions src/bar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ enum Inner {
pub struct Bar {
name: String,
monitor_name: String,
monitor_size: (i32, i32),
position: BarPosition,

ironbar: Rc<Ironbar>,
Expand All @@ -41,6 +42,7 @@ impl Bar {
pub fn new(
app: &Application,
monitor_name: String,
monitor_size: (i32, i32),
config: BarConfig,
ironbar: Rc<Ironbar>,
) -> Self {
Expand Down Expand Up @@ -89,6 +91,7 @@ impl Bar {
Self {
name,
monitor_name,
monitor_size,
position,
ironbar,
window,
Expand Down Expand Up @@ -146,7 +149,7 @@ impl Bar {
}
}

let load_result = self.load_modules(config, monitor)?;
let load_result = self.load_modules(config, monitor, self.monitor_size)?;

self.show(!start_hidden);

Expand Down Expand Up @@ -243,7 +246,12 @@ impl Bar {
}

/// Loads the configured modules onto a bar.
fn load_modules(&self, config: BarConfig, monitor: &Monitor) -> Result<BarLoadResult> {
fn load_modules(
&self,
config: BarConfig,
monitor: &Monitor,
output_size: (i32, i32),
) -> Result<BarLoadResult> {
let icon_theme = IconTheme::new();
if let Some(ref theme) = config.icon_theme {
icon_theme.set_custom_theme(Some(theme));
Expand All @@ -265,7 +273,7 @@ impl Bar {
}

// popup ignores module location so can bodge this for now
let popup = Popup::new(&info!(ModuleLocation::Left), config.popup_gap);
let popup = Popup::new(&info!(ModuleLocation::Left), output_size, config.popup_gap);
let popup = Rc::new(popup);

if let Some(modules) = config.start {
Expand Down Expand Up @@ -384,9 +392,10 @@ pub fn create_bar(
app: &Application,
monitor: &Monitor,
monitor_name: String,
monitor_size: (i32, i32),
config: BarConfig,
ironbar: Rc<Ironbar>,
) -> Result<Bar> {
let bar = Bar::new(app, monitor_name, config, ironbar);
let bar = Bar::new(app, monitor_name, monitor_size, config, ironbar);
bar.init(monitor)
}
5 changes: 5 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,8 @@ fn load_output_bars(
// We also need this static to ensure hot-reloading continues to work as best we can.
static INDEX_MAP: OnceLock<Mutex<Vec<String>>> = OnceLock::new();

let output_size = output.logical_size.unwrap_or_default();

let Some(monitor_name) = &output.name else {
return Err(Report::msg("Output missing monitor name"));
};
Expand Down Expand Up @@ -401,6 +403,7 @@ fn load_output_bars(
app,
&monitor,
monitor_name.to_string(),
output_size,
config.clone(),
ironbar.clone(),
)?]
Expand All @@ -412,6 +415,7 @@ fn load_output_bars(
app,
&monitor,
monitor_name.to_string(),
output_size,
config.clone(),
ironbar.clone(),
)
Expand All @@ -421,6 +425,7 @@ fn load_output_bars(
app,
&monitor,
monitor_name.to_string(),
output_size,
config.bar.clone(),
ironbar.clone(),
)?],
Expand Down
36 changes: 17 additions & 19 deletions src/popup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,14 @@ use std::cell::RefCell;
use std::collections::HashMap;
use std::rc::Rc;

use gtk::gdk::Monitor;
use gtk::prelude::*;
use gtk::{ApplicationWindow, Button, Orientation};
use gtk_layer_shell::LayerShell;
use tracing::{debug, trace};

use crate::config::BarPosition;
use crate::gtk_helpers::{IronbarGtkExt, WidgetGeometry};
use crate::modules::{ModuleInfo, ModulePopupParts, PopupButton};
use crate::rc_mut;
use gtk::prelude::*;
use gtk::{ApplicationWindow, Button, Orientation};
use gtk_layer_shell::LayerShell;
use tracing::{debug, trace};

#[derive(Debug, Clone)]
pub struct PopupCacheValue {
Expand All @@ -25,16 +23,16 @@ pub struct Popup {
pub window: ApplicationWindow,
pub container_cache: Rc<RefCell<HashMap<usize, PopupCacheValue>>>,
pub button_cache: Rc<RefCell<Vec<Button>>>,
monitor: Monitor,
pos: BarPosition,
current_widget: Rc<RefCell<Option<(usize, usize)>>>,
output_size: (i32, i32),
}

impl Popup {
/// Creates a new popup window.
/// This includes setting up gtk-layer-shell
/// and an empty `gtk::Box` container.
pub fn new(module_info: &ModuleInfo, gap: i32) -> Self {
pub fn new(module_info: &ModuleInfo, output_size: (i32, i32), gap: i32) -> Self {
let pos = module_info.bar_position;
let orientation = pos.orientation();

Expand Down Expand Up @@ -109,9 +107,9 @@ impl Popup {
window: win,
container_cache: rc_mut!(HashMap::new()),
button_cache: rc_mut!(vec![]),
monitor: module_info.monitor.clone(),
pos,
current_widget: rc_mut!(None),
output_size,
}
}

Expand All @@ -123,13 +121,14 @@ impl Popup {
}

let orientation = self.pos.orientation();
let monitor = self.monitor.clone();
let window = self.window.clone();

let current_widget = self.current_widget.clone();
let cache = self.container_cache.clone();
let button_cache = self.button_cache.clone();

let output_size = self.output_size;

content
.container
.connect_size_allocate(move |container, rect| {
Expand All @@ -142,8 +141,8 @@ impl Popup {
&button_cache.borrow(),
button_id,
orientation,
&monitor,
&window,
output_size,
);
}
}
Expand Down Expand Up @@ -175,8 +174,8 @@ impl Popup {
&self.button_cache.borrow(),
button_id,
self.pos.orientation(),
&self.monitor,
&self.window,
self.output_size,
);
}
}
Expand All @@ -193,8 +192,8 @@ impl Popup {
Self::set_pos(
geometry,
self.pos.orientation(),
&self.monitor,
&self.window,
self.output_size,
);
}
}
Expand All @@ -203,16 +202,16 @@ impl Popup {
buttons: &[Button],
button_id: usize,
orientation: Orientation,
monitor: &Monitor,
window: &ApplicationWindow,
output_size: (i32, i32),
) {
let button = buttons
.iter()
.find(|b| b.popup_id() == button_id)
.expect("to find valid button");

let geometry = button.geometry(orientation);
Self::set_pos(geometry, orientation, monitor, window);
Self::set_pos(geometry, orientation, window, output_size);
}

fn clear_window(&self) {
Expand Down Expand Up @@ -242,14 +241,13 @@ impl Popup {
fn set_pos(
geometry: WidgetGeometry,
orientation: Orientation,
monitor: &Monitor,
window: &ApplicationWindow,
output_size: (i32, i32),
) {
let mon_workarea = monitor.workarea();
let screen_size = if orientation == Orientation::Horizontal {
mon_workarea.width()
output_size.0
} else {
mon_workarea.height()
output_size.1
};

let (popup_width, popup_height) = window.size();
Expand Down

0 comments on commit 45d5bf5

Please sign in to comment.