Files
openidea/src-tauri/src/linux_window_state/geometry.rs
T
Vadim LazukandInf1mum a0c3249368 fix(linux): keep the first-map hint under Mutter's auto-maximize share (#2520)
Wayland has no primary monitor, so the first-map size hint fell back to scale 1 with no work-area limit and requested the saved physical size as logical. On a HiDPI output the first mapped area exceeded Mutter's 80% share and the window opened maximized before the saved geometry was restored. The hint now converts with the largest output scale and shrinks proportionally to stay within the tightest output's share; X11 keeps its primary-monitor path.

Fixes #2400

Co-authored-by: Inf1mum <8135225+Inf1mum@users.noreply.github.com>
2026-11-11 22:29:05 +08:00

349 lines
14 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
use serde::{Deserialize, Serialize};
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub(super) struct Size(pub(super) u32, pub(super) u32);
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub(super) struct Mode {
pub(super) maximized: bool,
pub(super) fullscreen: bool,
pub(super) minimized: bool,
}
impl Mode {
pub(super) fn normal(self) -> bool {
!self.maximized && !self.fullscreen && !self.minimized
}
}
#[derive(Clone, Copy, Debug, Default)]
pub(super) struct ModeHistory(pub(super) Mode);
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(super) enum NormalTransition {
Stable,
WaitForResize,
Restore,
}
impl ModeHistory {
pub(super) fn observe(&mut self, current: Mode, can_restore: bool) -> NormalTransition {
let returned = !self.0.normal() && current.normal();
if returned && !can_restore {
return NormalTransition::WaitForResize;
}
self.0 = current;
if returned { NormalTransition::Restore } else { NormalTransition::Stable }
}
}
#[derive(Clone, Copy, Debug, Default)]
pub(super) struct Limits {
pub(super) min: (f64, f64),
pub(super) max: (Option<f64>, Option<f64>),
}
impl Limits {
pub(super) fn in_work_area(self, area: Option<Size>, scale: f64) -> Self {
let Some(Size(width, height)) = area.filter(|s| s.0 > 0 && s.1 > 0) else { return self };
Self {
max: (
Some(self.max.0.unwrap_or(f64::INFINITY).min((f64::from(width) / scale).floor())),
Some(self.max.1.unwrap_or(f64::INFINITY).min((f64::from(height) / scale).floor())),
),
..self
}
}
}
pub(super) fn restore_size(saved: Size, scale: f64, limits: Limits) -> Size {
let dimension = |pixels: u32, min: f64, max: Option<f64>| {
let logical = (f64::from(pixels) / scale).max(min).max(1.0);
// gtk_window_resize takes integer logical dimensions.
(logical.min(max.unwrap_or(f64::INFINITY).max(min)).round() * scale).round() as u32
};
Size(dimension(saved.0, limits.min.0, limits.max.0), dimension(saved.1, limits.min.1, limits.max.1))
}
/// The share of the work area whose excess maximizes a window on its first map
/// (Mutter `window-private.h`, `MAX_UNMAXIMIZED_WINDOW_AREA`).
pub(super) const MAX_UNMAXIMIZED_AREA: f64 = 0.8;
/// The first-map size hint and the scale to request it at, for a window whose
/// output the compositor picks (Wayland has no primary monitor and assigns the
/// output, and its scale, only after mapping). Mutter maximizes a window whose
/// first mapped area exceeds `MAX_UNMAXIMIZED_AREA` of its work area, so convert
/// the saved pixels with the largest output scale — the fewest logical points —
/// and shrink them proportionally when their mapped area would exceed the
/// tightest output's share. The whole logical points keep the mapped area at or
/// under the share once GTK maps its integer size (rounding could tip a shrunk
/// hint back over it). `None` when no output is known: mapped geometry is
/// authoritative and the caller keeps its configured size.
pub(super) fn compositor_first_map_hint(saved: Size, limits: Limits, outputs: &[(f64, Size)]) -> Option<(Size, f64)> {
let scale = outputs.iter().map(|output| output.0).reduce(f64::max)?;
// The share compares areas, which are scale-free: one budget covers every output.
let budget = outputs
.iter()
.filter(|(_, area)| area.0 > 0 && area.1 > 0)
.map(|(output_scale, area)| {
MAX_UNMAXIMIZED_AREA * (f64::from(area.0) / output_scale) * (f64::from(area.1) / output_scale)
})
.reduce(f64::min);
let (mut width, mut height) = (f64::from(saved.0) / scale, f64::from(saved.1) / scale);
let area = width * height;
if let Some(budget) = budget
&& area > budget
{
let shrink = (budget / area).sqrt();
(width, height) = (width * shrink, height * shrink);
}
let (width, height) = (width.floor(), height.floor());
let hinted = Size((width * scale).round() as u32, (height * scale).round() as u32);
Some((restore_size(hinted, scale, limits), scale))
}
pub(super) fn remember_size(previous: Size, current: Size, restoring: bool, mode: Mode) -> Size {
if restoring || !mode.normal() || current.0 == 0 || current.1 == 0 { previous } else { current }
}
#[derive(Debug, PartialEq, Eq)]
pub(super) enum ModeAction {
Request,
Wait,
Track,
}
pub(super) fn mode_action(reached: bool, requested: bool, timed_out: bool) -> ModeAction {
if reached {
ModeAction::Track
} else if !requested {
ModeAction::Request
} else if timed_out {
ModeAction::Track
} else {
ModeAction::Wait
}
}
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(default)]
pub(super) struct Geometry {
pub(super) width: u32,
pub(super) height: u32,
pub(super) x: i32,
pub(super) y: i32,
pub(super) prev_x: i32,
pub(super) prev_y: i32,
pub(super) maximized: bool,
pub(super) fullscreen: bool,
pub(super) visible: bool,
pub(super) decorated: bool,
}
impl Geometry {
pub(super) fn set_normal_size(&mut self, size: Size) {
(self.width, self.height) = (size.0, size.1);
self.maximized = false;
self.fullscreen = false;
}
}
impl Default for Geometry {
fn default() -> Self {
Self {
width: 0,
height: 0,
x: 0,
y: 0,
prev_x: 0,
prev_y: 0,
maximized: false,
fullscreen: false,
visible: true,
decorated: true,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn constrained_size_does_not_skip_the_first_mode_request() {
assert_eq!(mode_action(false, false, true), ModeAction::Request);
assert_eq!(mode_action(false, false, false), ModeAction::Request);
assert_eq!(mode_action(false, true, false), ModeAction::Wait);
assert_eq!(mode_action(false, true, true), ModeAction::Track);
assert_eq!(mode_action(true, false, false), ModeAction::Track);
}
#[test]
fn physical_sizes_use_the_actual_output_scale() {
for scale in [1.0, 2.0] {
assert_eq!(restore_size(Size(2468, 1356), scale, Limits::default()), Size(2468, 1356));
}
// Saved at scale 1, mapped on scale 2: keep physical pixels, not twice that size.
assert_eq!(restore_size(Size(1234, 678), 2.0, Limits::default()), Size(1234, 678));
}
#[test]
fn gtk_limits_and_integer_logical_dimensions_are_applied_once() {
let limits = Limits { min: (800.0, 500.0), max: (Some(1600.0), Some(1000.0)) };
assert_eq!(restore_size(Size(1201, 801), 1.0, limits), Size(1201, 801));
assert_eq!(restore_size(Size(1201, 801), 2.0, limits), Size(1600, 1000));
assert_eq!(restore_size(Size(2001, 1401), 2.0, limits), Size(2002, 1402));
assert_eq!(restore_size(Size(9000, 6000), 2.0, limits), Size(3200, 2000));
}
#[test]
fn a_hidpi_saved_size_maps_without_crossing_the_auto_maximize_share() {
// Issue #2400: 2468×1356 physical saved on one 3840×2160 output at scale 2.
// Requesting it as logical (the old scale-1 fallback) maps 4936×2712 — over
// the work area, so Mutter maximizes the window before the restore runs.
let outputs = [(2.0, Size(3840, 2160))];
let (hint, scale) = compositor_first_map_hint(Size(2468, 1356), Limits::default(), &outputs).unwrap();
assert_eq!((hint, scale), (Size(2468, 1356), 2.0));
let work_area = 3840.0 * 2160.0;
let mapped = f64::from(hint.0) * f64::from(hint.1);
assert!(mapped / work_area <= MAX_UNMAXIMIZED_AREA);
let fallback_mapped = 2468.0 * 2.0 * 1356.0 * 2.0;
assert!(fallback_mapped / work_area > MAX_UNMAXIMIZED_AREA);
}
#[test]
fn damaged_sizes_are_bounded_in_the_actual_outputs_units() {
let limits = Limits { min: (800.0, 500.0), ..Default::default() };
for scale in [1.0, 2.0] {
let bounded = limits.in_work_area(Some(Size(3840, 2160)), scale);
assert_eq!(restore_size(Size(45068, 30000), scale, bounded), Size(3840, 2160));
assert_eq!(restore_size(Size(2468, 1356), scale, bounded), Size(2468, 1356));
}
}
#[test]
fn a_saved_size_over_the_share_is_shrunk_proportionally_to_the_tightest_output() {
// 2560×1440 at scale 2 allows 1152×640 logical (80% of the area exactly);
// 2304×1280 physical is twice that, so the hint halves to stay under it.
let outputs = [(2.0, Size(2560, 1440))];
let (hint, scale) = compositor_first_map_hint(Size(4608, 2560), Limits::default(), &outputs).unwrap();
assert_eq!((hint, scale), (Size(2304, 1280), 2.0));
assert!(f64::from(hint.0) * f64::from(hint.1) / (2560.0 * 1440.0) <= MAX_UNMAXIMIZED_AREA);
// A large output does not relax the smallest one's budget.
let outputs = [(1.0, Size(3840, 2160)), (2.0, Size(2560, 1440))];
let (hint, scale) = compositor_first_map_hint(Size(4608, 2560), Limits::default(), &outputs).unwrap();
assert_eq!((hint, scale), (Size(2304, 1280), 2.0));
}
#[test]
fn an_area_exactly_at_the_share_is_left_alone() {
// Mutter maximizes only windows over the share (`window_area > work_area * 0.8`).
let outputs = [(2.0, Size(2560, 1440))];
let (hint, _) = compositor_first_map_hint(Size(2304, 1280), Limits::default(), &outputs).unwrap();
assert_eq!(hint, Size(2304, 1280));
}
#[test]
fn a_fractional_logical_hint_does_not_round_up_over_the_share() {
// 2305×1279 physical is 1152.5×639.5 logical, under the share — but mapped
// as whole GTK points that rounds to 1153×640, over it. The whole logical
// points keep the hint at or under the share, so the exact hint matters.
let outputs = [(2.0, Size(2560, 1440))];
let (hint, scale) = compositor_first_map_hint(Size(2305, 1279), Limits::default(), &outputs).unwrap();
assert_eq!((hint, scale), (Size(2304, 1278), 2.0));
assert!(f64::from(hint.0) * f64::from(hint.1) / (2560.0 * 1440.0) <= MAX_UNMAXIMIZED_AREA);
}
#[test]
fn the_hint_preserves_physical_pixels_across_scales() {
// Saved at scale 1, mapped on scale 2: like restore_size, the largest scale
// keeps the physical pixels instead of doubling them.
let outputs = [(1.0, Size(1920, 1080)), (2.0, Size(1920, 1080))];
let (hint, scale) = compositor_first_map_hint(Size(1234, 678), Limits::default(), &outputs).unwrap();
assert_eq!((hint, scale), (Size(1234, 678), 2.0));
}
#[test]
fn the_hint_keeps_the_configured_minimum() {
// min_size is a GTK constraint; it wins over the share, as in the mapped restore.
let limits = Limits { min: (800.0, 500.0), ..Default::default() };
let (hint, scale) = compositor_first_map_hint(Size(400, 200), limits, &[(2.0, Size(3840, 2160))]).unwrap();
assert_eq!((hint, scale), (Size(1600, 1000), 2.0));
}
#[test]
fn without_output_information_the_hint_is_not_set() {
assert!(compositor_first_map_hint(Size(2468, 1356), Limits::default(), &[]).is_none());
// Outputs without a usable work area still give the conversion scale.
let (hint, scale) =
compositor_first_map_hint(Size(2468, 1356), Limits::default(), &[(2.0, Size(0, 0))]).unwrap();
assert_eq!((hint, scale), (Size(2468, 1356), 2.0));
}
#[test]
fn restoration_and_non_normal_events_preserve_the_normal_size() {
let normal = Size(1234, 678);
for current in [Size(2468, 1356), Size(2648, 1602), Size(5476, 3204)] {
assert_eq!(remember_size(normal, current, true, Mode::default()), normal);
for mode in [
Mode { maximized: true, ..Default::default() },
Mode { fullscreen: true, ..Default::default() },
Mode { minimized: true, ..Default::default() },
] {
assert_eq!(remember_size(normal, current, false, mode), normal);
}
}
assert_eq!(remember_size(normal, Size(1201, 801), false, Mode::default()), Size(1201, 801));
assert_eq!(remember_size(normal, Size(0, 678), false, Mode::default()), normal);
}
#[test]
fn a_mixed_dpi_maximized_launch_does_not_compound() {
let mut saved = Size(2468, 1356);
for _ in 0..3 {
saved = remember_size(saved, Size(saved.0 * 2, saved.1 * 2), true, Mode::default());
let restored = restore_size(saved, 2.0, Limits::default());
saved = remember_size(saved, restored, false, Mode::default());
saved = remember_size(saved, Size(3840, 2160), false, Mode { maximized: true, ..Default::default() });
assert_eq!(saved, Size(2468, 1356));
}
}
#[test]
fn moved_does_not_consume_the_unmaximize_resize() {
for mode in [
Mode { maximized: true, ..Default::default() },
Mode { fullscreen: true, ..Default::default() },
Mode { minimized: true, ..Default::default() },
] {
let mut history = ModeHistory(mode);
assert_eq!(history.observe(Mode::default(), false), NormalTransition::WaitForResize);
assert_eq!(history.0, mode);
assert_eq!(history.observe(Mode::default(), true), NormalTransition::Restore);
assert_eq!(history.observe(Mode::default(), true), NormalTransition::Stable);
}
}
#[test]
fn an_explicit_normal_frame_overrides_old_window_modes() {
let mut saved = Geometry { maximized: true, fullscreen: true, x: 123, y: 456, ..Default::default() };
saved.set_normal_size(Size(1100, 700));
assert_eq!((saved.width, saved.height), (1100, 700));
assert!(!saved.maximized && !saved.fullscreen);
assert_eq!((saved.x, saved.y), (123, 456));
}
#[test]
fn state_files_remain_compatible_and_keep_closed_windows() {
let saved: std::collections::HashMap<String, Geometry> = serde_json::from_str(
r#"{"main":{"width":1234,"height":678,"maximized":true},"settings":{"width":1001,"height":701}}"#,
)
.unwrap();
let copy: std::collections::HashMap<String, Geometry> =
serde_json::from_value(serde_json::to_value(&saved).unwrap()).unwrap();
assert_eq!(copy["settings"].height, 701);
assert!(copy["main"].maximized);
assert_eq!(copy["main"].width, 1234);
}
}