Skip to content

Commit 0244168

Browse files
committed
refactor: extract toolbar geometry helpers and add tests
1 parent ab8e795 commit 0244168

File tree

8 files changed

+523
-57
lines changed

8 files changed

+523
-57
lines changed

src/backend/wayland/state.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,10 @@ fn force_inline_env_enabled() -> bool {
307307
})
308308
}
309309

310+
fn force_inline_toolbars_requested_with_env(config: &Config, env_force_inline: bool) -> bool {
311+
config.ui.toolbar.force_inline || env_force_inline
312+
}
313+
310314
fn force_inline_toolbars_requested(config: &Config) -> bool {
311-
config.ui.toolbar.force_inline || force_inline_env_enabled()
315+
force_inline_toolbars_requested_with_env(config, force_inline_env_enabled())
312316
}

src/backend/wayland/state/tests.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,46 @@ fn debug_damage_logging_env_parses_falsey() {
8181
assert!(parse_debug_damage_env("1"));
8282
assert!(parse_debug_damage_env("true"));
8383
}
84+
85+
#[test]
86+
fn parse_boolish_env_handles_case_and_on() {
87+
assert!(parse_boolish_env("ON"));
88+
assert!(parse_boolish_env("yes"));
89+
assert!(!parse_boolish_env("Off"));
90+
assert!(!parse_boolish_env("0"));
91+
}
92+
93+
#[test]
94+
fn damage_summary_truncates_after_five_regions() {
95+
let mut regions = Vec::new();
96+
for i in 0..6 {
97+
regions.push(Rect {
98+
x: i,
99+
y: i,
100+
width: 1,
101+
height: 2,
102+
});
103+
}
104+
let summary = damage_summary(&regions);
105+
assert_eq!(
106+
summary,
107+
"(0,0) 1x2, (1,1) 1x2, (2,2) 1x2, (3,3) 1x2, (4,4) 1x2, ... +1 more"
108+
);
109+
}
110+
111+
#[test]
112+
fn resolve_then_scale_damage_regions_keeps_full_region() {
113+
let regions = resolve_damage_regions(100, 50, Vec::new());
114+
let scaled = scale_damage_regions(regions, 2);
115+
assert_eq!(scaled.len(), 1);
116+
assert_eq!(scaled[0], Rect::new(0, 0, 200, 100).unwrap());
117+
}
118+
119+
#[test]
120+
fn force_inline_toolbars_requested_uses_config_or_env() {
121+
let mut config = Config::default();
122+
assert!(!force_inline_toolbars_requested_with_env(&config, false));
123+
assert!(force_inline_toolbars_requested_with_env(&config, true));
124+
config.ui.toolbar.force_inline = true;
125+
assert!(force_inline_toolbars_requested_with_env(&config, false));
126+
}

src/backend/wayland/state/toolbar.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@ use super::*;
33

44
mod drag;
55
mod events;
6+
mod geometry;
67
mod inline;
78
mod visibility;

src/backend/wayland/state/toolbar/drag.rs

Lines changed: 52 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,19 @@ impl WaylandState {
1414
let top_size = top_size(snapshot);
1515
let side_start_y = Self::SIDE_BASE_MARGIN_TOP + self.data.toolbar_side_offset;
1616
let top_bottom_y = Self::INLINE_TOP_Y + self.data.toolbar_top_offset_y + top_size.1 as f64;
17-
let side_overlaps_top = side_visible && side_start_y < top_bottom_y;
1817
let base = Self::INLINE_SIDE_X + self.data.toolbar_side_offset_x;
1918
// When dragging the side toolbar, don't push the top bar; keep its base stable so it
2019
// doesn't shift while moving the side bar.
21-
if side_overlaps_top && self.active_move_drag_kind() != Some(MoveDragKind::Side) {
22-
base + side_size.0 as f64 + Self::INLINE_TOP_PUSH
23-
} else {
24-
base
25-
}
20+
let allow_push = self.active_move_drag_kind() != Some(MoveDragKind::Side);
21+
geometry::compute_inline_top_base_x(
22+
base,
23+
side_visible,
24+
side_size.0 as f64,
25+
side_start_y,
26+
top_bottom_y,
27+
Self::INLINE_TOP_PUSH,
28+
allow_push,
29+
)
2630
}
2731

2832
pub(super) fn inline_top_base_y(&self) -> f64 {
@@ -66,39 +70,36 @@ impl WaylandState {
6670
let top_base_x = self.inline_top_base_x(snapshot);
6771
let top_base_y = self.inline_top_base_y();
6872

69-
let mut max_top_x = (width - top_w as f64 - top_base_x - Self::TOP_MARGIN_RIGHT).max(0.0);
70-
let mut max_top_y = (height - top_h as f64 - top_base_y - Self::TOP_MARGIN_BOTTOM).max(0.0);
71-
let mut max_side_y =
72-
(height - side_h as f64 - Self::SIDE_BASE_MARGIN_TOP - Self::SIDE_MARGIN_BOTTOM)
73-
.max(0.0);
74-
let mut max_side_x =
75-
(width - side_w as f64 - Self::SIDE_BASE_MARGIN_LEFT - Self::SIDE_MARGIN_RIGHT)
76-
.max(0.0);
77-
78-
// Ensure max bounds remain non-negative.
79-
max_top_x = max_top_x.max(0.0);
80-
max_top_y = max_top_y.max(0.0);
81-
max_side_x = max_side_x.max(0.0);
82-
max_side_y = max_side_y.max(0.0);
83-
84-
// Allow negative offsets so toolbars can reach screen edges by cancelling base margins
85-
let min_top_x = -top_base_x;
86-
let min_top_y = -top_base_y;
87-
let min_side_x = -Self::SIDE_BASE_MARGIN_LEFT;
88-
let min_side_y = -Self::SIDE_BASE_MARGIN_TOP;
89-
9073
let before_top = (self.data.toolbar_top_offset, self.data.toolbar_top_offset_y);
9174
let before_side = (
9275
self.data.toolbar_side_offset_x,
9376
self.data.toolbar_side_offset,
9477
);
95-
self.data.toolbar_top_offset = self.data.toolbar_top_offset.clamp(min_top_x, max_top_x);
96-
self.data.toolbar_top_offset_y = self.data.toolbar_top_offset_y.clamp(min_top_y, max_top_y);
97-
self.data.toolbar_side_offset = self.data.toolbar_side_offset.clamp(min_side_y, max_side_y);
98-
self.data.toolbar_side_offset_x = self
99-
.data
100-
.toolbar_side_offset_x
101-
.clamp(min_side_x, max_side_x);
78+
let input = geometry::ToolbarClampInput {
79+
width,
80+
height,
81+
top_size: (top_w, top_h),
82+
side_size: (side_w, side_h),
83+
top_base_x,
84+
top_base_y,
85+
top_margin_right: Self::TOP_MARGIN_RIGHT,
86+
top_margin_bottom: Self::TOP_MARGIN_BOTTOM,
87+
side_base_margin_left: Self::SIDE_BASE_MARGIN_LEFT,
88+
side_base_margin_top: Self::SIDE_BASE_MARGIN_TOP,
89+
side_margin_right: Self::SIDE_MARGIN_RIGHT,
90+
side_margin_bottom: Self::SIDE_MARGIN_BOTTOM,
91+
};
92+
let offsets = geometry::ToolbarOffsets {
93+
top_x: self.data.toolbar_top_offset,
94+
top_y: self.data.toolbar_top_offset_y,
95+
side_x: self.data.toolbar_side_offset_x,
96+
side_y: self.data.toolbar_side_offset,
97+
};
98+
let (clamped, bounds) = geometry::clamp_toolbar_offsets(offsets, input);
99+
self.data.toolbar_top_offset = clamped.top_x;
100+
self.data.toolbar_top_offset_y = clamped.top_y;
101+
self.data.toolbar_side_offset_x = clamped.side_x;
102+
self.data.toolbar_side_offset = clamped.side_y;
102103
drag_log(format!(
103104
"clamp offsets: before=({:.3}, {:.3})/({:.3}, {:.3}), after=({:.3}, {:.3})/({:.3}, {:.3}), max=({:.3}, {:.3})/({:.3}, {:.3}), size=({}, {}), top_base_x={:.3}, top_base_y={:.3}",
104105
before_top.0,
@@ -109,10 +110,10 @@ impl WaylandState {
109110
self.data.toolbar_top_offset_y,
110111
self.data.toolbar_side_offset_x,
111112
self.data.toolbar_side_offset,
112-
max_top_x,
113-
max_top_y,
114-
max_side_x,
115-
max_side_y,
113+
bounds.max_top_x,
114+
bounds.max_top_y,
115+
bounds.max_side_x,
116+
bounds.max_side_y,
116117
width,
117118
height,
118119
top_base_x,
@@ -156,13 +157,19 @@ impl WaylandState {
156157
let _ = self.clamp_toolbar_offsets(snapshot);
157158
if self.layer_shell.is_some() {
158159
let top_base_x = self.inline_top_base_x(snapshot);
159-
let top_margin_left = (top_base_x + self.data.toolbar_top_offset).round() as i32;
160-
let top_margin_top =
161-
(Self::TOP_BASE_MARGIN_TOP + self.data.toolbar_top_offset_y).round() as i32;
162-
let side_margin_top =
163-
(Self::SIDE_BASE_MARGIN_TOP + self.data.toolbar_side_offset).round() as i32;
164-
let side_margin_left =
165-
(Self::SIDE_BASE_MARGIN_LEFT + self.data.toolbar_side_offset_x).round() as i32;
160+
let (top_margin_left, top_margin_top, side_margin_top, side_margin_left) =
161+
geometry::compute_layer_margins(
162+
top_base_x,
163+
Self::TOP_BASE_MARGIN_TOP,
164+
Self::SIDE_BASE_MARGIN_LEFT,
165+
Self::SIDE_BASE_MARGIN_TOP,
166+
geometry::ToolbarOffsets {
167+
top_x: self.data.toolbar_top_offset,
168+
top_y: self.data.toolbar_top_offset_y,
169+
side_x: self.data.toolbar_side_offset_x,
170+
side_y: self.data.toolbar_side_offset,
171+
},
172+
);
166173
drag_log(format!(
167174
"apply_toolbar_offsets: top_margin_left={}, top_margin_top={}, side_margin_top={}, side_margin_left={}, offsets=({}, {})/({}, {}), scale={}, top_base_x={}",
168175
top_margin_left,

0 commit comments

Comments
 (0)