From 8a3ea16b1c4628e84da2ed99f67fe6282c98db1b Mon Sep 17 00:00:00 2001 From: nandithebull Date: Wed, 25 Nov 2026 00:42:38 -0800 Subject: [PATCH] Drag windows by their title bar A press on a title bar (outside the buttons) focuses the window, floats it if it was tiled (WmState::float_window) and starts a move drag; motion moves it, release ends it. Co-Authored-By: Claude Sonnet 5.5 --- compositor/host/compositor.mbt | 25 ++++++++++++++++++++++++- compositor/host/input_test.mbt | 24 ++++++++++++++++++++++++ lib/wm/wm.mbt | 23 +++++++++++++++++++++++ 3 files changed, 71 insertions(+), 1 deletion(-) diff --git a/compositor/host/compositor.mbt b/compositor/host/compositor.mbt index 8c06fbc..d541c11 100644 --- a/compositor/host/compositor.mbt +++ b/compositor/host/compositor.mbt @@ -119,7 +119,7 @@ fn MoonPlasmaCompositor::apply_actions( match self.windows.get(wid) { Some(win) => { win.geometry = @scene.Rect::new(r.x, r.y, r.w, r.h) - if self.split_drag is Some(_) { + if self.split_drag is Some(_) || self.wm.drag is Some(_) { // Dragging: send one configure per frame, not one per mouse event. win.cfg_dirty = true } else { @@ -810,6 +810,13 @@ pub fn MoonPlasmaCompositor::handle_input( self.shell.set_hover(id, xi, yi) return } + if self.wm.drag is Some(_) { + match self.wm.update_drag(xi, yi) { + Some(a) => self.apply_actions([a]) + None => () + } + return + } if self.shell.pointer_move(xi, yi) || self.shell.hit(xi, yi) { self.leave_pointer() return @@ -893,6 +900,11 @@ pub fn MoonPlasmaCompositor::handle_input( self.split_drag = None return } + if !pressed && self.wm.drag is Some(_) { + self.wm.end_drag() + self.flush_configures() + return + } // Right button over a swappable panel widget opens its alternatives menu. if button == 273U && pressed && self.shell.right_click(xi, yi) { return @@ -910,6 +922,14 @@ pub fn MoonPlasmaCompositor::handle_input( self.press_title_button(wid, btn) return } + if button == 272U { + // Drag the window by its title bar; a tiled window becomes floating first. + self.leave_pointer() + self.apply_actions([@wm.WmAction::Focus(wid), @wm.WmAction::Raise(wid)]) + self.apply_actions(self.wm.float_window(wid, self.work_area)) + ignore(self.wm.start_drag(wid, @wm.DragMode::Move, xi, yi)) + return + } } _ => () } @@ -1004,6 +1024,9 @@ pub fn MoonPlasmaCompositor::desired_cursor(self : MoonPlasmaCompositor) -> UInt if shell_shape != 0U { return shell_shape } + if self.wm.drag is Some(_) { + return 4U + } let handle = match self.split_drag { Some(h) => Some(h) None => self.handle_at(self.ptr_x.to_int(), self.ptr_y.to_int()) diff --git a/compositor/host/input_test.mbt b/compositor/host/input_test.mbt index bd23dd5..bde4d93 100644 --- a/compositor/host/input_test.mbt +++ b/compositor/host/input_test.mbt @@ -697,3 +697,27 @@ test "the dock module magnifies the tile under the pointer and shows nothing whe comp.handle_input(@nested.InputEvent::PointerMove(640.0, 300.0), 2U) assert_eq(comp.shell.hover_slot, "") } + +///| +test "dragging a title bar moves the window and floats it" { + let socket_path = "/tmp/wayland-moon-wdrag-" + + @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 g0 = comp.windows.get(2UL).unwrap().geometry + let sx = g0.x + g0.w / 3 + let sy = g0.y - 16 // inside the title bar, clear of the buttons + comp.handle_input(@nested.InputEvent::PointerMove(sx.to_double(), sy.to_double()), 1U) + comp.handle_input(@nested.InputEvent::PointerButton(272U, true), 2U) + comp.handle_input(@nested.InputEvent::PointerMove((sx - 120).to_double(), (sy + 60).to_double()), 3U) + comp.handle_input(@nested.InputEvent::PointerButton(272U, false), 4U) + let g1 = comp.windows.get(2UL).unwrap().geometry + assert_eq(g1.x, g0.x - 120) + assert_eq(g1.y, g0.y + 60) + assert_eq(g1.w, g0.w) + // The other window now has the whole tiling area. + assert_true(comp.windows.get(1UL).unwrap().geometry.w > g0.w) +} diff --git a/lib/wm/wm.mbt b/lib/wm/wm.mbt index 39e92ee..c4ae94b 100644 --- a/lib/wm/wm.mbt +++ b/lib/wm/wm.mbt @@ -880,6 +880,29 @@ pub fn WmState::hit_test( HitNone } +///| Take a tiled window out of the tiling tree so it can be moved freely; it keeps its +/// current geometry. No-op for windows that already float. +pub fn WmState::float_window( + self : WmState, + id : UInt64, + work_area : Rect, +) -> Array[WmAction] { + match self.windows.get(id) { + None => [] + Some(win) => + if win.is_floating || win.maximized || win.fullscreen { + [] + } else { + let ws = self.ensure_workspace(win.workspace) + ws.tree = remove_leaf(ws.tree, id) + win.is_floating = true + win.floating_geometry = win.geometry + push_top(ws.floating, id) + self.relayout(win.workspace, work_area) + } + } +} + ///| pub fn WmState::start_drag( self : WmState,