diff --git a/compositor/host/compositor.mbt b/compositor/host/compositor.mbt index d8be9b5..f19992e 100644 --- a/compositor/host/compositor.mbt +++ b/compositor/host/compositor.mbt @@ -26,6 +26,10 @@ pub(all) struct MoonPlasmaCompositor { mut ptr_x : Double mut ptr_y : Double mut ptr_win : UInt64? + /// Divider being dragged between tiled windows. + mut split_drag : @wm.SplitHandle? + /// True while a button press was forwarded to a client (so its release is too). + mut press_fwd : Bool mut kbd_win : UInt64? mut mods : UInt mut layout_mode : @wm.LayoutMode @@ -67,6 +71,8 @@ pub fn MoonPlasmaCompositor::new( ptr_x: 0.0, ptr_y: 0.0, ptr_win: None, + split_drag: None, + press_fwd: false, kbd_win: None, mods: 0U, layout_mode: @wm.LayoutMode::Tiling, @@ -384,6 +390,28 @@ fn MoonPlasmaCompositor::window_at(self : MoonPlasmaCompositor, x : Int, y : Int None } +///| Tiling divider under (x, y), with a few pixels of slack either side. +fn MoonPlasmaCompositor::handle_at( + self : MoonPlasmaCompositor, + x : Int, + y : Int, +) -> @wm.SplitHandle? { + let slop = 3 + for h in self.wm.split_handles(self.work_area) { + let r = h.rect + // Title bars sit 32px above the windows, so the hit area reaches up over them. + let hit = if h.horizontal { + x >= r.x - slop && x < r.x + r.w + slop && y >= r.y - 32 && y < r.y + r.h + } else { + y >= r.y - slop && y < r.y + r.h + slop && x >= r.x && x < r.x + r.w + } + if hit { + return Some(h) + } + } + None +} + ///| fn MoonPlasmaCompositor::leave_pointer(self : MoonPlasmaCompositor) -> Unit { match self.ptr_win { @@ -443,6 +471,17 @@ pub fn MoonPlasmaCompositor::handle_input( self.leave_pointer() return } + match self.split_drag { + Some(h) => { + self.apply_actions(self.wm.drag_split(h, xi, yi, self.work_area)) + return + } + None => () + } + if self.handle_at(xi, yi) is Some(_) { + self.leave_pointer() + return + } match self.window_at(xi, yi) { Some((wid, true)) => match self.windows.get(wid) { @@ -464,10 +503,22 @@ pub fn MoonPlasmaCompositor::handle_input( PointerButton(button, pressed) => { let xi = self.ptr_x.to_int() let yi = self.ptr_y.to_int() + if !pressed && self.split_drag is Some(_) { + self.split_drag = None + return + } if pressed { if self.shell.pointer_down(xi, yi) { return } + match self.handle_at(xi, yi) { + Some(h) => { + self.split_drag = Some(h) + self.leave_pointer() + return + } + None => () + } match self.window_at(xi, yi) { Some((wid, _)) => { self.apply_actions([ @@ -479,7 +530,14 @@ pub fn MoonPlasmaCompositor::handle_input( } } match self.ptr_win { - Some(w) => self.server.pointer_button(w, time_ms, button, pressed) + Some(w) => + if pressed { + self.press_fwd = true + self.server.pointer_button(w, time_ms, button, pressed) + } else if self.press_fwd { + self.press_fwd = false + self.server.pointer_button(w, time_ms, button, pressed) + } None => () } } @@ -514,6 +572,14 @@ pub fn MoonPlasmaCompositor::desired_cursor(self : MoonPlasmaCompositor) -> UInt if shell_shape != 0U { return shell_shape } + let handle = match self.split_drag { + Some(h) => Some(h) + None => self.handle_at(self.ptr_x.to_int(), self.ptr_y.to_int()) + } + match handle { + Some(h) => return if h.horizontal { 26U } else { 27U } // ew-resize / ns-resize + None => () + } match self.ptr_win { Some(w) => self.server.client_cursor_shape(w) None => 1U diff --git a/compositor/host/input_test.mbt b/compositor/host/input_test.mbt index 74187b3..8a7afbd 100644 --- a/compositor/host/input_test.mbt +++ b/compositor/host/input_test.mbt @@ -119,3 +119,30 @@ test "host input reaches the focused client and the launcher popup opens" { assert_false(comp.shell.popup_open()) comp.close() } + +///| +test "dragging the divider between two tiled windows resizes them" { + let socket_path = "/tmp/wayland-moon-split-" + + @sys.getpid().to_string() + + ".sock" + let theme = @scene.ThemeTokens::moon_night() + let comp = @host.MoonPlasmaCompositor::new(socket_path, theme, 1280, 720).unwrap() + let _a = open_and_configure_client(comp, socket_path, "A", "a") + let _b = open_and_configure_client(comp, socket_path, "B", "b") + let left = comp.windows.get(1UL).unwrap().geometry + let right = comp.windows.get(2UL).unwrap().geometry + // The divider is the gap between the two windows. + let gx = left.x + left.w + 2 + let gy = left.y + left.h / 2 + assert_true(right.x > left.x + left.w) + comp.handle_input(@nested.InputEvent::PointerMove(gx.to_double(), gy.to_double()), 1U) + assert_eq(comp.desired_cursor(), 26U) // ew-resize over the divider + comp.handle_input(@nested.InputEvent::PointerButton(272U, true), 2U) + comp.handle_input(@nested.InputEvent::PointerMove(400.0, gy.to_double()), 3U) + comp.handle_input(@nested.InputEvent::PointerButton(272U, false), 4U) + let left2 = comp.windows.get(1UL).unwrap().geometry + let right2 = comp.windows.get(2UL).unwrap().geometry + assert_true(left2.w < left.w - 200) + assert_true(right2.w > right.w + 200) + assert_true(right2.x > left2.x + left2.w) +} diff --git a/compositor/scene/scene.mbt b/compositor/scene/scene.mbt index 162add7..616f73c 100644 --- a/compositor/scene/scene.mbt +++ b/compositor/scene/scene.mbt @@ -353,19 +353,6 @@ pub fn build_breeze_decoration( Line(border, title_h - 1, outer_w - border, title_h - 1, p.border, 1), ] - let label = if title.length() > 0 { title } else { app_id } - ops.push( - Text( - 14, - title_h / 2, - label, - tokens.font_size, - title_fg, - Left, - focused, - ), - ) - // Window control buttons on the right: [ _ ] [ [] ] [ X ] let btn_size = 18 let btn_y = (title_h - btn_size) / 2 @@ -373,6 +360,21 @@ pub fn build_breeze_decoration( let max_x = close_x - 8 - btn_size let min_x = max_x - 8 - btn_size + // Title: truncated so it never runs under the buttons or past the window edge. + let label = if title.length() > 0 { title } else { app_id } + let char_w = if tokens.font_size >= 14 { 12 } else { 6 } + let max_chars = (min_x - 8 - 14) / char_w + let shown = if max_chars < 3 { + "" + } else if label.length() <= max_chars { + label + } else { + label[:max_chars - 2].to_string() + ".." + } + ops.push( + Text(14, title_h / 2, shown, tokens.font_size, title_fg, Left, focused), + ) + let close_bg = if focused { Rgba::new(218, 68, 83, 255) } else { diff --git a/lib/wm/wm.mbt b/lib/wm/wm.mbt index 107b981..2f07657 100644 --- a/lib/wm/wm.mbt +++ b/lib/wm/wm.mbt @@ -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) +} diff --git a/lib/wm/wm_test.mbt b/lib/wm/wm_test.mbt index f812452..28bae71 100644 --- a/lib/wm/wm_test.mbt +++ b/lib/wm/wm_test.mbt @@ -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 }) } ///|