diff options
| author | user <user@clank> | 2026-08-08 20:58:17 +0200 |
|---|---|---|
| committer | user <user@clank> | 2026-08-08 20:58:17 +0200 |
| commit | c21f7324abdb6d83300a62b95e2af27bfcc75a1e (patch) | |
| tree | 4bf95a6a71c5c0b6e36c96d2a2d58d654b13052b /zellij-utils/src | |
| parent | sl0p: add fastdev build profile + SPEED.md (60x faster iteration) (diff) | |
| download | sl0ppij-c21f7324abdb6d83300a62b95e2af27bfcc75a1e.tar.gz sl0ppij-c21f7324abdb6d83300a62b95e2af27bfcc75a1e.tar.xz sl0ppij-c21f7324abdb6d83300a62b95e2af27bfcc75a1e.zip | |
sl0p: uniform pane gap + aspect-corrected horizontal gap
Two changes to how the gap is defined.
1. UNIFORM GAP. Previously every pane inset every side by `gap`, so two
neighbours each gave up a cell and the space between them was 2*gap while
the screen edge got only gap. Now a pane insets RIGHT and BOTTOM always,
and LEFT/TOP only where that edge is the viewport boundary. Two neighbours
contribute exactly one gap between them, and the outer screen edges get
the same gap. `gap 1` now means 1 everywhere.
The per-side offset is computed in tiled_panes, which is the layer that
knows the viewport, and pushed to panes via Pane::set_gap_offset before the
content-offset sweep. Floating panes keep a zero gap: they are positioned
explicitly by the user, so an inter-pane gap does not apply to them (they
still get padding).
2. ASPECT CORRECTION. Terminal cells are roughly twice as tall as they are
wide, so an equal number of rows and columns does not look like an equal
distance. `gap` and `padding` are now expressed in ROWS, and columns are
multiplied by `gap_aspect` (default 2, minimum 1). Style carries the
resolved pane_gap_x/y and pane_padding_x/y.
FrameConfig::Default is now hand-written rather than derived: gap_aspect must
default to 2 to match the KDL parser, otherwise a parsed config disagrees with
a default-constructed one and it surfaces as config round-trip diffs (this
broke can_define_ui_configuration_in_configfile and both
bare_config_from_default_assets_to_string tests).
Tests assert the actual invariants: stacked and side-by-side neighbours get
exactly one gap between them, screen edges get the same gap as between panes,
and inset_by refuses to collapse a pane too small to survive it.
Baselines held: zellij-server 1421/30, zellij-utils 433/5.
Diffstat (limited to 'zellij-utils/src')
| -rw-r--r-- | zellij-utils/src/data.rs | 10 | ||||
| -rw-r--r-- | zellij-utils/src/input/config.rs | 1 | ||||
| -rw-r--r-- | zellij-utils/src/input/theme.rs | 43 | ||||
| -rw-r--r-- | zellij-utils/src/kdl/mod.rs | 11 | ||||
| -rw-r--r-- | zellij-utils/src/pane_size.rs | 112 | ||||
| -rw-r--r-- | zellij-utils/src/plugin_api/event.rs | 6 | ||||
| -rw-r--r-- | zellij-utils/src/plugin_api/style.rs | 6 |
7 files changed, 180 insertions, 9 deletions
diff --git a/zellij-utils/src/data.rs b/zellij-utils/src/data.rs index c6603e76..aa455b35 100644 --- a/zellij-utils/src/data.rs +++ b/zellij-utils/src/data.rs @@ -1431,10 +1431,16 @@ pub struct Style { /// unaffected and clicks inside a gap still resolve to the owning pane. /// /// Like pane_title_alignment, not carried over the plugin protobuf boundary. - pub pane_gap: usize, + /// Resolved per axis, because terminal cells are about twice as tall as + /// they are wide: an equal number of rows and columns does NOT look like + /// an equal distance. See FrameConfig::gap_aspect. + pub pane_gap_x: usize, + pub pane_gap_y: usize, /// INNER padding: cells between a pane's frame and its content. Purely /// cosmetic breathing room inside the pane; does not move the frame. - pub pane_padding: usize, + /// Also aspect-corrected. + pub pane_padding_x: usize, + pub pane_padding_y: usize, } #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, Deserialize, Serialize)] diff --git a/zellij-utils/src/input/config.rs b/zellij-utils/src/input/config.rs index 546d39d0..be08adb9 100644 --- a/zellij-utils/src/input/config.rs +++ b/zellij-utils/src/input/config.rs @@ -1410,6 +1410,7 @@ mod config_test { title_alignment: Default::default(), gap: Default::default(), padding: Default::default(), + gap_aspect: 2, // parser default; see FrameConfig::default() }, }; assert_eq!(config.ui, expected_ui_config, "Ui config defined in config"); diff --git a/zellij-utils/src/input/theme.rs b/zellij-utils/src/input/theme.rs index 949a8752..f4f2ec80 100644 --- a/zellij-utils/src/input/theme.rs +++ b/zellij-utils/src/input/theme.rs @@ -22,15 +22,51 @@ impl UiConfig { } } -#[derive(Debug, Default, Clone, Copy, PartialEq, Deserialize, Serialize)] +#[derive(Debug, Clone, Copy, PartialEq, Deserialize, Serialize)] pub struct FrameConfig { pub rounded_corners: bool, pub hide_session_name: bool, pub title_alignment: crate::data::PaneTitleAlignment, - /// outer gap between panes, in cells (see Style::pane_gap) + /// outer gap between panes, expressed in ROWS (see Style::pane_gap_y). + /// Columns are scaled by `gap_aspect` so the gap looks square. pub gap: usize, - /// inner padding between a pane's frame and its content (see Style::pane_padding) + /// inner padding between a pane's frame and its content, in ROWS pub padding: usize, + /// Columns per row. Terminal cells are roughly twice as tall as they are + /// wide, so 1 row of gap needs ~2 columns to look like the same distance. + /// 0 is treated as 1 (no correction). + pub gap_aspect: usize, +} + +/// Hand-written rather than derived: `gap_aspect` must default to 2 to match +/// the KDL parser's default. A derived Default would give 0, which makes a +/// parsed config disagree with a default-constructed one and shows up as +/// config round-trip diffs. +impl Default for FrameConfig { + fn default() -> Self { + FrameConfig { + rounded_corners: false, + hide_session_name: false, + title_alignment: Default::default(), + gap: 0, + padding: 0, + gap_aspect: 2, + } + } +} + +impl FrameConfig { + fn aspect(&self) -> usize { + self.gap_aspect.max(1) + } + /// (columns, rows) of outer gap + pub fn resolved_gap(&self) -> (usize, usize) { + (self.gap * self.aspect(), self.gap) + } + /// (columns, rows) of inner padding + pub fn resolved_padding(&self) -> (usize, usize) { + (self.padding * self.aspect(), self.padding) + } } impl FrameConfig { @@ -41,6 +77,7 @@ impl FrameConfig { merged.title_alignment = other.title_alignment; merged.gap = other.gap; merged.padding = other.padding; + merged.gap_aspect = other.gap_aspect; merged } } diff --git a/zellij-utils/src/kdl/mod.rs b/zellij-utils/src/kdl/mod.rs index 6589d30f..c2500ebb 100644 --- a/zellij-utils/src/kdl/mod.rs +++ b/zellij-utils/src/kdl/mod.rs @@ -5554,12 +5554,17 @@ impl UiConfig { let padding = kdl_children_property_first_arg_as_i64!(pane_frames, "padding") .unwrap_or(0) .max(0) as usize; + // default 2: terminal cells are ~2x taller than wide + let gap_aspect = kdl_children_property_first_arg_as_i64!(pane_frames, "gap_aspect") + .unwrap_or(2) + .max(1) as usize; let frame_config = FrameConfig { rounded_corners, hide_session_name, title_alignment, gap, padding, + gap_aspect, }; ui_config.pane_frames = frame_config; } @@ -5608,6 +5613,12 @@ impl UiConfig { padding.push(KdlValue::Base10(self.pane_frames.padding as i64)); frame_config_children.nodes_mut().push(padding); } + if self.pane_frames.gap_aspect != 2 { + has_ui_config = true; + let mut gap_aspect = KdlNode::new("gap_aspect"); + gap_aspect.push(KdlValue::Base10(self.pane_frames.gap_aspect as i64)); + frame_config_children.nodes_mut().push(gap_aspect); + } if has_ui_config { frame_config.set_children(frame_config_children); ui_config_children.nodes_mut().push(frame_config); diff --git a/zellij-utils/src/pane_size.rs b/zellij-utils/src/pane_size.rs index 5afedd7a..fd59cc7a 100644 --- a/zellij-utils/src/pane_size.rs +++ b/zellij-utils/src/pane_size.rs @@ -64,6 +64,33 @@ impl Viewport { /// needs at least its two borders plus a cell of content in each axis, so /// below that we return the viewport untouched rather than emit a /// degenerate (or zero-sized) frame. + /// Shrink this viewport by a different amount on each side. + /// + /// Used for the pane gap: a pane only gives up space on a side where a gap + /// actually belongs, which is what makes the gap between two neighbours + /// equal to `gap` rather than `2 * gap`. + /// + /// Refuses (returns self) if the result would not leave a viable frame. + pub fn inset_by(&self, by: Offset) -> Viewport { + const MIN_FRAMED_EXTENT: usize = 3; + if by.top == 0 && by.bottom == 0 && by.left == 0 && by.right == 0 { + return *self; + } + let horizontal = by.left.saturating_add(by.right); + let vertical = by.top.saturating_add(by.bottom); + if self.cols < horizontal.saturating_add(MIN_FRAMED_EXTENT) + || self.rows < vertical.saturating_add(MIN_FRAMED_EXTENT) + { + return *self; + } + Viewport { + x: self.x.saturating_add(by.left), + y: self.y.saturating_add(by.top), + cols: self.cols.saturating_sub(horizontal), + rows: self.rows.saturating_sub(vertical), + } + } + pub fn inset(&self, by: usize) -> Viewport { const MIN_FRAMED_EXTENT: usize = 3; // left border + 1 content cell + right border if by == 0 { @@ -480,6 +507,27 @@ impl Offset { /// zero content rows, i.e. it silently disappears. Chrome panes are now /// excluded by their callers, but any pane can be resized down to nothing, /// so the geometry refuses to collapse itself regardless of caller. + /// As [`Offset::expanded_by_within`], but grows each side independently. + pub fn expanded_by_offset_within(&self, by: Offset, cols: usize, rows: usize) -> Self { + let expanded = Self { + top: self.top.saturating_add(by.top), + bottom: self.bottom.saturating_add(by.bottom), + left: self.left.saturating_add(by.left), + right: self.right.saturating_add(by.right), + }; + let fits = cols + .checked_sub(expanded.left + expanded.right) + .is_some_and(|c| c >= 1) + && rows + .checked_sub(expanded.top + expanded.bottom) + .is_some_and(|r| r >= 1); + if fits { + expanded + } else { + *self + } + } + pub fn expanded_by_within(&self, by: usize, cols: usize, rows: usize) -> Self { let expanded = self.expanded_by(by); let fits = cols @@ -636,6 +684,70 @@ mod gap_and_padding_tests { assert_eq!(vp(0, 0, 7, 7).inset(2), vp(2, 2, 3, 3)); } + /// Model of the per-side rule in tiled_panes: right/bottom always, left/top + /// only at the viewport edge. + fn gap_for(geom: Viewport, vp: Viewport, gx: usize, gy: usize) -> Offset { + Offset { + left: if geom.x <= vp.x { gx } else { 0 }, + top: if geom.y <= vp.y { gy } else { 0 }, + right: gx, + bottom: gy, + } + } + + #[test] + fn stacked_panes_get_exactly_one_gap_between_them() { + // THE regression this rule fixes: insetting every side on every pane + // made two neighbours produce 2*gap between them. + let vp = Viewport { x: 0, y: 0, cols: 80, rows: 24 }; + let (gx, gy) = (2usize, 1usize); + let top_alloc = Viewport { x: 0, y: 0, cols: 80, rows: 12 }; + let bottom_alloc = Viewport { x: 0, y: 12, cols: 80, rows: 12 }; + + let top = top_alloc.inset_by(gap_for(top_alloc, vp, gx, gy)); + let bottom = bottom_alloc.inset_by(gap_for(bottom_alloc, vp, gx, gy)); + + let between = bottom.y - (top.y + top.rows); + assert_eq!(between, gy, "vertical gap between neighbours must be exactly gy"); + } + + #[test] + fn side_by_side_panes_get_exactly_one_gap_between_them() { + let vp = Viewport { x: 0, y: 0, cols: 80, rows: 24 }; + let (gx, gy) = (2usize, 1usize); + let left_alloc = Viewport { x: 0, y: 0, cols: 40, rows: 24 }; + let right_alloc = Viewport { x: 40, y: 0, cols: 40, rows: 24 }; + + let left = left_alloc.inset_by(gap_for(left_alloc, vp, gx, gy)); + let right = right_alloc.inset_by(gap_for(right_alloc, vp, gx, gy)); + + let between = right.x - (left.x + left.cols); + assert_eq!(between, gx, "horizontal gap between neighbours must be exactly gx"); + } + + #[test] + fn screen_edges_get_the_same_gap_as_between_panes() { + let vp = Viewport { x: 0, y: 0, cols: 80, rows: 24 }; + let (gx, gy) = (2usize, 1usize); + let a = Viewport { x: 0, y: 0, cols: 40, rows: 24 }; + let b = Viewport { x: 40, y: 0, cols: 40, rows: 24 }; + let left = a.inset_by(gap_for(a, vp, gx, gy)); + let right = b.inset_by(gap_for(b, vp, gx, gy)); + + assert_eq!(left.x - vp.x, gx, "left screen margin"); + assert_eq!((vp.x + vp.cols) - (right.x + right.cols), gx, "right screen margin"); + assert_eq!(left.y - vp.y, gy, "top screen margin"); + assert_eq!((vp.y + vp.rows) - (left.y + left.rows), gy, "bottom screen margin"); + } + + #[test] + fn inset_by_zero_is_identity_and_refuses_to_collapse() { + let v = Viewport { x: 2, y: 3, cols: 20, rows: 10 }; + assert_eq!(v.inset_by(Offset::default()), v); + let tiny = Viewport { x: 0, y: 0, cols: 4, rows: 4 }; + assert_eq!(tiny.inset_by(Offset::frame(1)), tiny, "must not collapse a tiny pane"); + } + #[test] fn expanded_by_grows_every_side() { let o = Offset::frame(1).expanded_by(2); diff --git a/zellij-utils/src/plugin_api/event.rs b/zellij-utils/src/plugin_api/event.rs index f4bb3523..87af043e 100644 --- a/zellij-utils/src/plugin_api/event.rs +++ b/zellij-utils/src/plugin_api/event.rs @@ -2493,8 +2493,10 @@ fn serialize_mode_update_event_with_non_default_values() { rounded_corners: true, hide_session_name: false, pane_title_alignment: Default::default(), - pane_gap: Default::default(), - pane_padding: Default::default(), + pane_gap_x: Default::default(), + pane_gap_y: Default::default(), + pane_padding_x: Default::default(), + pane_padding_y: Default::default(), }, capabilities: PluginCapabilities { arrow_fonts: false }, session_name: Some("my awesome test session".to_owned()), diff --git a/zellij-utils/src/plugin_api/style.rs b/zellij-utils/src/plugin_api/style.rs index 46c3ce83..266ab7f8 100644 --- a/zellij-utils/src/plugin_api/style.rs +++ b/zellij-utils/src/plugin_api/style.rs @@ -26,8 +26,10 @@ impl TryFrom<ProtobufStyle> for Style { // alignment, and adding a proto field would mean regenerating the // committed prost assets. pane_title_alignment: Default::default(), - pane_gap: Default::default(), - pane_padding: Default::default(), + pane_gap_x: Default::default(), + pane_gap_y: Default::default(), + pane_padding_x: Default::default(), + pane_padding_y: Default::default(), }) } } |
