Drag the divider between tiled windows to resize them
CI / test (push) Successful in 37s

- lib/wm: split handles, drag_split, vertical rows now leave room for the lower
  window's title bar (fixes overlapping title bars)
- compositor: pointer press/move/release on a divider resizes the split, with an
  ew/ns-resize cursor; button releases are only forwarded after a forwarded press

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
nandiandClaude Sonnet 5.5 committed 2026-11-24 20:12:13 -08:00
1 parent ee698c9846
commit 7154312919
4 files changed
+290 -32

No files matched your search

+192 -28
View File
@@ -229,45 +229,123 @@ pub fn remove_leaf(tree : BspNode?, id : UInt64) -> BspNode? {
}
}
///| Geometry of one split: (first child, second child, divider strip between them).
/// `gap` is the strip thickness; `vgap` is the (possibly larger) space a vertical split
/// reserves between rows, so the lower row's title bar fits below the upper window.
fn split_rects(
horiz : Bool,
ratio : Double,
r : Rect,
gap : Int,
vgap : Int,
) -> (Rect, Rect, Rect) {
if horiz {
let avail = r.w - gap
let lw = if avail > 2 {
clamp_int((avail.to_double() * ratio).to_int(), 1, avail - 1)
} else {
r.w / 2
}
let rw = if avail > lw { avail - lw } else { 1 }
(
{ x: r.x, y: r.y, w: lw, h: r.h },
{ x: r.x + lw + gap, y: r.y, w: rw, h: r.h },
{ x: r.x + lw, y: r.y, w: gap, h: r.h },
)
} else {
let avail = r.h - vgap
let lh = if avail > 2 {
clamp_int((avail.to_double() * ratio).to_int(), 1, avail - 1)
} else {
r.h / 2
}
let rh = if avail > lh { avail - lh } else { 1 }
(
{ x: r.x, y: r.y, w: r.w, h: lh },
{ x: r.x, y: r.y + lh + vgap, w: r.w, h: rh },
{ x: r.x, y: r.y + lh, w: r.w, h: gap },
)
}
}
///| Compute tiled rectangles for every leaf in a BSP tree within `area`.
pub fn compute_bsp_rects(
node : BspNode,
area : Rect,
inner_gap : Int,
vgap? : Int = -1,
) -> Array[(UInt64, Rect)] {
let vg = if vgap < 0 { inner_gap } else { vgap }
let out : Array[(UInt64, Rect)] = []
fn walk(n : BspNode, r : Rect, gap : Int, acc : Array[(UInt64, Rect)]) -> Unit {
fn walk(n : BspNode, r : Rect) -> Unit {
match n {
Leaf(id) => acc.push((id, r))
Split(horizontal=horiz, ratio=ratio, left=l, right=rt) =>
if horiz {
let avail = r.w - gap
let lw = if avail > 2 {
clamp_int((avail.to_double() * ratio).to_int(), 1, avail - 1)
} else {
r.w / 2
}
let rw = if avail > lw { avail - lw } else { 1 }
walk(l, { x: r.x, y: r.y, w: lw, h: r.h }, gap, acc)
walk(rt, { x: r.x + lw + gap, y: r.y, w: rw, h: r.h }, gap, acc)
} else {
let avail = r.h - gap
let lh = if avail > 2 {
clamp_int((avail.to_double() * ratio).to_int(), 1, avail - 1)
} else {
r.h / 2
}
let rh = if avail > lh { avail - lh } else { 1 }
walk(l, { x: r.x, y: r.y, w: r.w, h: lh }, gap, acc)
walk(rt, { x: r.x, y: r.y + lh + gap, w: r.w, h: rh }, gap, acc)
}
Leaf(id) => out.push((id, r))
Split(horizontal=horiz, ratio=ratio, left=l, right=rt) => {
let (lr, rr, _) = split_rects(horiz, ratio, r, inner_gap, vg)
walk(l, lr)
walk(rt, rr)
}
}
}
walk(node, area, inner_gap, out)
walk(node, area)
out
}
///| A draggable divider between two tiled subtrees.
pub(all) struct SplitHandle {
/// Route from the root: false = first child, true = second child.
path : Array[Bool]
horizontal : Bool
/// The strip between the two children.
rect : Rect
/// The area the split divides (fixed while this divider moves).
area : Rect
}
///| Every divider in the tree, with the strip it occupies.
pub fn compute_split_handles(
node : BspNode,
area : Rect,
inner_gap : Int,
vgap : Int,
) -> Array[SplitHandle] {
let out : Array[SplitHandle] = []
fn walk(n : BspNode, r : Rect, path : Array[Bool]) -> Unit {
match n {
Leaf(_) => ()
Split(horizontal=horiz, ratio=ratio, left=l, right=rt) => {
let (lr, rr, strip) = split_rects(horiz, ratio, r, inner_gap, vgap)
out.push({ path: path.copy(), horizontal: horiz, rect: strip, area: r })
let lp = path.copy()
lp.push(false)
walk(l, lr, lp)
let rp = path.copy()
rp.push(true)
walk(rt, rr, rp)
}
}
}
walk(node, area, [])
out
}
///|
fn set_ratio_at(node : BspNode, path : Array[Bool], i : Int, ratio : Double) -> BspNode {
match node {
Leaf(_) => node
Split(horizontal=h, ratio=r, left=l, right=rt) =>
if i >= path.length() {
Split(horizontal=h, ratio~, left=l, right=rt)
} else if path[i] {
Split(horizontal=h, ratio=r, left=l, right=set_ratio_at(rt, path, i + 1, ratio))
} else {
Split(horizontal=h, ratio=r, left=set_ratio_at(l, path, i + 1, ratio), right=rt)
}
}
}
///|
fn WmState::ensure_workspace(self : WmState, ws_id : UInt) -> Workspace {
for ws in self.workspaces {
@@ -341,7 +419,7 @@ pub fn WmState::relayout(
Monocle =>
match ws.tree {
Some(t) => {
let rects = compute_bsp_rects(t, padded, self.inner_gap)
let rects = compute_bsp_rects(t, padded, self.inner_gap, vgap=self.inner_gap + self.titlebar_h)
for pair in rects {
let (id, _) = pair
if ws.fullscreen == Some(id) {
@@ -362,7 +440,7 @@ pub fn WmState::relayout(
Tiling =>
match ws.tree {
Some(t) => {
let rects = compute_bsp_rects(t, padded, self.inner_gap)
let rects = compute_bsp_rects(t, padded, self.inner_gap, vgap=self.inner_gap + self.titlebar_h)
for pair in rects {
let (id, rect) = pair
if ws.fullscreen == Some(id) {
@@ -575,7 +653,12 @@ pub fn WmState::set_layout_mode(
// Move all tiled leaves into floating list preserving geometry.
match ws.tree {
Some(t) => {
let rects = compute_bsp_rects(t, self.inner_work_area(work_area), self.inner_gap)
let rects = compute_bsp_rects(
t,
self.inner_work_area(work_area),
self.inner_gap,
vgap=self.inner_gap + self.titlebar_h,
)
for pair in rects {
let (id, _) = pair
match self.windows.get(id) {
@@ -887,3 +970,84 @@ pub fn WmState::update_drag(
pub fn WmState::end_drag(self : WmState) -> Unit {
self.drag = None
}
///| Dividers of the active workspace's tiling layout (none unless tiling).
pub fn WmState::split_handles(self : WmState, work_area : Rect) -> Array[SplitHandle] {
let ws = self.ensure_workspace(self.active_workspace)
match (ws.mode, ws.tree) {
(Tiling, Some(t)) =>
compute_split_handles(
t,
self.inner_work_area(work_area),
self.inner_gap,
self.inner_gap + self.titlebar_h,
)
_ => []
}
}
///| Move a divider so it sits under pointer (px, py); reflows the tiled windows.
pub fn WmState::drag_split(
self : WmState,
h : SplitHandle,
px : Int,
py : Int,
work_area : Rect,
) -> Array[WmAction] {
let ws = self.ensure_workspace(self.active_workspace)
let ratio = if h.horizontal {
let avail = h.area.w - self.inner_gap
(px - h.area.x - self.inner_gap / 2).to_double() / avail.max(1).to_double()
} else {
let avail = h.area.h - self.inner_gap - self.titlebar_h
(py - h.area.y - self.inner_gap / 2).to_double() / avail.max(1).to_double()
}
let ratio = if ratio < 0.1 { 0.1 } else if ratio > 0.9 { 0.9 } else { ratio }
match ws.tree {
Some(t) => ws.tree = Some(set_ratio_at(t, h.path, 0, ratio))
None => ()
}
self.relayout(self.active_workspace, work_area)
}
///|
test "split handles: divider position, drag, and row gap leaves room for the title bar" {
let wm = WmState::new()
let area : Rect = { x: 0, y: 32, w: 1000, h: 600 }
let rec = fn(id : UInt64) -> WindowRecord {
{
id,
app_id: "a",
title: "t",
geometry: { x: 0, y: 0, w: 0, h: 0 },
floating_geometry: { x: 0, y: 0, w: 0, h: 0 },
min_size: { w: 0, h: 0 },
max_size: None,
focused: false,
is_floating: false,
fullscreen: false,
maximized: false,
minimized: false,
workspace: 0U,
can_move: true,
can_resize: true,
can_maximize: true,
can_minimize: true,
can_fullscreen: true,
}
}
ignore(wm.open_window(rec(1UL), area))
ignore(wm.open_window(rec(2UL), area))
let hs = wm.split_handles(area)
assert_eq(hs.length(), 1)
assert_true(hs[0].horizontal)
// Drag the divider to x=300: the left window becomes narrower.
ignore(wm.drag_split(hs[0], 300, 300, area))
assert_true(wm.windows[1UL].geometry.w < 330)
assert_true(wm.windows[1UL].geometry.w > 270)
// A third window splits the right one vertically; the lower row leaves 8 + title bar.
ignore(wm.open_window(rec(3UL), area))
let w2 = wm.windows[2UL].geometry
let w3 = wm.windows[3UL].geometry
assert_eq(w3.y - (w2.y + w2.h), 8 + 32)
}
+4 -3
View File
@@ -49,9 +49,10 @@ test "bsp tiling splits 2 and 3 windows with gaps" {
assert_eq(wm.windows[2UL].geometry, { x: 515, y: 10, w: 495, h: 680 })
ignore(wm.open_window(mk_win(3UL), wa))
// Right half splits vertically: 680 - 10 = 670 -> 335 + 335
assert_eq(wm.windows[2UL].geometry, { x: 515, y: 10, w: 495, h: 335 })
assert_eq(wm.windows[3UL].geometry, { x: 515, y: 355, w: 495, h: 335 })
// Right half splits vertically; rows are separated by the gap plus a 32px title bar:
// 680 - 42 = 638 -> 319 + 319
assert_eq(wm.windows[2UL].geometry, { x: 515, y: 10, w: 495, h: 319 })
assert_eq(wm.windows[3UL].geometry, { x: 515, y: 371, w: 495, h: 319 })
}
///|