diff --git a/compositor/host/compositor.mbt b/compositor/host/compositor.mbt index 413868d..2543116 100644 --- a/compositor/host/compositor.mbt +++ b/compositor/host/compositor.mbt @@ -49,6 +49,8 @@ pub(all) struct ChandraCompositor { mut ptr_win : UInt64? /// Divider being dragged between tiled windows. mut split_drag : @wm.SplitHandle? + /// The last left press on a title bar (titleclick.mbt): drag to move, double-click to maximize. + mut title_press : TitlePress? /// True while a button press was forwarded to a client (so its release is too). mut press_fwd : Bool mut kbd_win : UInt64? @@ -126,6 +128,7 @@ pub fn ChandraCompositor::new( popups: [], ptr_win: None, split_drag: None, + title_press: None, press_fwd: false, kbd_win: None, mods: 0U, @@ -1098,6 +1101,9 @@ pub fn ChandraCompositor::handle_input( self.shell.set_hover(id, xi, yi) return } + if self.title_bar_motion(xi, yi) { + return + } if self.wm.drag is Some(_) { match self.wm.update_drag(xi, yi) { Some(a) => self.apply_actions([a]) @@ -1208,6 +1214,9 @@ pub fn ChandraCompositor::handle_input( } return } + if !pressed && button == 272U { + self.title_bar_release() + } if !pressed && self.shell.pointer_up() { self.run_shell_actions() return @@ -1248,11 +1257,8 @@ pub fn ChandraCompositor::handle_input( return } if button == 272U && self.resize_at(xi, yi) is None { - // Drag the window by its title bar; a tiled window becomes floating first. - self.leave_pointer() - self.activate_window(wid) - self.apply_actions(self.wm.float_window(wid, self.work_area)) - ignore(self.wm.start_drag(wid, @wm.DragMode::Move, xi, yi)) + // Drag the window by its title bar, or double-click it to maximize / restore. + self.title_bar_press(wid, xi, yi, time_ms) return } } diff --git a/compositor/host/titleclick.mbt b/compositor/host/titleclick.mbt new file mode 100644 index 0000000..8c2b768 --- /dev/null +++ b/compositor/host/titleclick.mbt @@ -0,0 +1,129 @@ +///| Title-bar presses: drag to move, double-click to maximize or restore. +/// +/// A left press on a title bar (away from its buttons) focuses the window and starts a move. +/// A tiled window is only turned floating once the pointer actually moves, so a plain click +/// (or double-click) leaves it in the tiling layout. Two presses on the same window's title +/// bar count as a double-click when the second comes within `double_click_ms` of the first, +/// lands within `double_click_px` of it, and the first press did not turn into a drag. The +/// double-click toggles maximize like the title-bar button: restoring brings back the size +/// and position the window had before (its tile, or where it floated). + +///| +/// Longest gap between the two presses of a double-click, in milliseconds. +pub let double_click_ms : UInt32 = 400 + +///| +/// How far (in pixels, along either axis) the pointer may wander between the two presses, +/// and how far it may move while held before a press becomes a drag. +pub let double_click_px : Int32 = 4 + +///| +/// The last left press on a title bar. +struct TitlePress { + wid : UInt64 + x : Int32 + y : Int32 + time : UInt32 + /// The button is still down. + mut held : Bool + /// The pointer moved past `double_click_px` while held (a drag, not a click). + mut moved : Bool + /// A tiled window waiting for the pointer to move before it floats and starts moving. + mut float_pending : Bool +} + +///| +fn abs_int(v : Int32) -> Int32 { + if v < 0 { + -v + } else { + v + } +} + +///| +/// Left press at (x, y) on the title bar of `wid` (not on one of its buttons). +fn ChandraCompositor::title_bar_press( + self : ChandraCompositor, + wid : UInt64, + x : Int32, + y : Int32, + time_ms : UInt32, +) -> Unit { + self.leave_pointer() + match self.title_press { + Some(p) => + if p.wid == wid && + !p.moved && + time_ms >= p.time && + time_ms - p.time <= double_click_ms && + abs_int(x - p.x) <= double_click_px && + abs_int(y - p.y) <= double_click_px { + self.title_press = None + self.activate_window(wid) + self.press_title_button(wid, 2) + return + } + None => () + } + self.activate_window(wid) + let tiled = match self.wm.windows.get(wid) { + Some(r) => !r.is_floating && !r.maximized && !r.fullscreen + None => false + } + self.title_press = Some({ + wid, + x, + y, + time: time_ms, + held: true, + moved: false, + float_pending: tiled, + }) + if !tiled { + ignore(self.wm.start_drag(wid, @wm.DragMode::Move, x, y)) + } +} + +///| +/// Pointer motion to (x, y) while a title-bar press may be held. Returns true when the motion +/// is consumed (a tiled window held but not yet dragged far enough to float). +fn ChandraCompositor::title_bar_motion( + self : ChandraCompositor, + x : Int32, + y : Int32, +) -> Bool { + match self.title_press { + Some(p) => + if p.held { + if abs_int(x - p.x) > double_click_px || + abs_int(y - p.y) > double_click_px { + p.moved = true + if p.float_pending { + // Drag the window by its title bar; a tiled window becomes floating first. + p.float_pending = false + self.apply_actions(self.wm.float_window(p.wid, self.work_area)) + ignore(self.wm.start_drag(p.wid, @wm.DragMode::Move, p.x, p.y)) + } + false + } else { + p.float_pending + } + } else { + false + } + None => false + } +} + +///| +/// The left button was released. +fn ChandraCompositor::title_bar_release(self : ChandraCompositor) -> Unit { + match self.title_press { + Some(p) => { + p.held = false + p.float_pending = false + } + None => () + } +} diff --git a/compositor/host/titleclick_test.mbt b/compositor/host/titleclick_test.mbt new file mode 100644 index 0000000..4b63dc4 --- /dev/null +++ b/compositor/host/titleclick_test.mbt @@ -0,0 +1,131 @@ +///| Double-clicking a title bar toggles maximize; single clicks and drags keep working. + +///| +fn two_windows(tag : String) -> @host.ChandraCompositor { + let socket_path = "/tmp/wayland-moon-" + + tag + + "-" + + @sys.getpid().to_string() + + ".sock" + let theme = @scene.ThemeTokens::moon_night() + let comp = @host.ChandraCompositor::new(socket_path, theme, 1280, 720).unwrap() + ignore(open_and_configure_client(comp, socket_path, "A", "a")) + ignore(open_and_configure_client(comp, socket_path, "B", "b")) + ignore(comp.compose_frame()) + comp +} + +///| +/// Press and release the left button at (x, y), at time t (press) and t + 1 (release). +fn title_click( + comp : @host.ChandraCompositor, + x : Int32, + y : Int32, + t : UInt32, +) -> Unit { + comp.handle_input( + @nested.InputEvent::PointerMove(x.to_double(), y.to_double()), + t, + ) + comp.handle_input(@nested.InputEvent::PointerButton(272U, true), t) + comp.handle_input(@nested.InputEvent::PointerButton(272U, false), t + 1U) +} + +///| +fn maximized(comp : @host.ChandraCompositor, wid : UInt64) -> Bool { + comp.wm.windows.get(wid).unwrap().maximized +} + +///| +fn floating(comp : @host.ChandraCompositor, wid : UInt64) -> Bool { + comp.wm.windows.get(wid).unwrap().is_floating +} + +///| +test "double-clicking a tiled window's title bar maximizes it, and again restores its tile" { + let comp = two_windows("dbl-tiled") + let g0 = comp.windows.get(2UL).unwrap().geometry + let (x, y) = (g0.x + g0.w / 3, g0.y - 16) + // A single click focuses but neither floats nor maximizes the window. + title_click(comp, x, y, 100U) + assert_false(floating(comp, 2UL)) + assert_false(maximized(comp, 2UL)) + assert_eq(comp.windows.get(2UL).unwrap().geometry, g0) + // The second click, 200ms later and 2px off, makes it a double-click. + title_click(comp, x + 2, y + 1, 300U) + assert_true(maximized(comp, 2UL)) + assert_false(floating(comp, 2UL)) + let gm = comp.windows.get(2UL).unwrap().geometry + assert_true(gm.w > g0.w) + // Double-click the maximized title bar: back in its tile. + let (mx, my) = (gm.x + gm.w / 3, gm.y - 16) + title_click(comp, mx, my, 2000U) + title_click(comp, mx, my, 2100U) + assert_false(maximized(comp, 2UL)) + assert_eq(comp.windows.get(2UL).unwrap().geometry, g0) +} + +///| +test "double-clicking a floating window's title bar restores its old size and place" { + let comp = two_windows("dbl-float") + let g = comp.windows.get(2UL).unwrap().geometry + // Float it by dragging its title bar 100px left and 30px down. + comp.handle_input( + @nested.InputEvent::PointerMove( + (g.x + 60).to_double(), + (g.y - 16).to_double(), + ), + 10U, + ) + comp.handle_input(@nested.InputEvent::PointerButton(272U, true), 11U) + comp.handle_input( + @nested.InputEvent::PointerMove( + (g.x - 40).to_double(), + (g.y + 14).to_double(), + ), + 12U, + ) + comp.handle_input(@nested.InputEvent::PointerButton(272U, false), 13U) + assert_true(floating(comp, 2UL)) + let g0 = comp.windows.get(2UL).unwrap().geometry + assert_eq(g0.x, g.x - 100) + assert_eq(g0.y, g.y + 30) + // A click right after the drag, on the spot where it was dropped, is not a double-click. + title_click(comp, g.x - 40, g.y + 14, 14U) + assert_false(maximized(comp, 2UL)) + // A real double-click maximizes it… + let (x, y) = (g0.x + 50, g0.y - 16) + title_click(comp, x, y, 1000U) + title_click(comp, x, y, 1150U) + assert_true(maximized(comp, 2UL)) + // …and another one puts it back where and as big as it was. + let gm = comp.windows.get(2UL).unwrap().geometry + title_click(comp, gm.x + 50, gm.y - 16, 3000U) + title_click(comp, gm.x + 50, gm.y - 16, 3100U) + assert_false(maximized(comp, 2UL)) + assert_eq(comp.windows.get(2UL).unwrap().geometry, g0) +} + +///| +test "slow or far-apart title-bar clicks are not a double-click" { + let comp = two_windows("dbl-slow") + let g0 = comp.windows.get(2UL).unwrap().geometry + let (x, y) = (g0.x + g0.w / 3, g0.y - 16) + // Too slow: 500ms between the presses. + title_click(comp, x, y, 100U) + title_click(comp, x, y, 600U) + assert_false(maximized(comp, 2UL)) + // Too far apart: 20px. + title_click(comp, x, y, 2000U) + title_click(comp, x + 20, y, 2100U) + assert_false(maximized(comp, 2UL)) + // On two different windows. + let g1 = comp.windows.get(1UL).unwrap().geometry + title_click(comp, g1.x + 40, g1.y - 16, 4000U) + title_click(comp, x, y, 4100U) + assert_false(maximized(comp, 1UL)) + assert_false(maximized(comp, 2UL)) + // Nothing above floated a window. + assert_false(floating(comp, 1UL)) + assert_false(floating(comp, 2UL)) +}