diff --git a/compositor/app/app.mbt b/compositor/app/app.mbt index fbbfc8c..7b7a224 100644 --- a/compositor/app/app.mbt +++ b/compositor/app/app.mbt @@ -244,6 +244,17 @@ pub fn run(args : Array[String]) -> Int { if n.keymap.length() > 0 { comp.server.set_keymap(n.keymap) } + // The window was resized (or maximized / made fullscreen): follow it, or the + // desktop stays drawn at the old size and is cropped by the smaller window. + match n.take_resize() { + Some((nw, nh)) => { + width = nw + height = nh + comp.resize(nw, nh) + need_present = true + } + None => () + } let now_ms = @sys.monotonic_ms().to_uint() for ev in n.take_input() { comp.handle_input(ev, now_ms) diff --git a/compositor/bin/main.mbt b/compositor/bin/main.mbt index eb8916e..871dbbc 100644 --- a/compositor/bin/main.mbt +++ b/compositor/bin/main.mbt @@ -148,6 +148,11 @@ fn main { if n.keymap.length() > 0 { comp.server.set_keymap(n.keymap) } + // Follow the window's size, or the desktop is cropped when the window is smaller. + match n.take_resize() { + Some((nw, nh)) => comp.resize(nw, nh) + None => () + } let now_ms = @env.now().to_uint() for ev in n.take_input() { comp.handle_input(ev, now_ms) diff --git a/compositor/host/compositor.mbt b/compositor/host/compositor.mbt index 55e396f..581c692 100644 --- a/compositor/host/compositor.mbt +++ b/compositor/host/compositor.mbt @@ -33,9 +33,9 @@ pub(all) struct MoonPlasmaCompositor { server : @wayland.WaylandServer wm : @wm.WmState theme : @scene.ThemeTokens - output_w : Int - output_h : Int - work_area : @wm.Rect + mut output_w : Int + mut output_h : Int + mut work_area : @wm.Rect windows : Map[UInt64, HostWindow] damage : @scene.DamageTracker shell : @shell.DesktopShell @@ -91,12 +91,7 @@ pub fn MoonPlasmaCompositor::new( titlebar_h=32, border_w=2, ) - let out_rect : @wm.Rect = { x: 0, y: 0, w: output_w, h: output_h } - let work_area = @wm.compute_work_area( - out_rect, - excl_top=32, // room for the server-side title bar drawn above each client - excl_bottom=theme.panel_height, - ) + let work_area = work_area_for(output_w, output_h, theme) let shell = @shell.DesktopShell::new(socket_path) shell.set_output(theme, output_w, output_h) Some({ @@ -135,6 +130,33 @@ pub fn MoonPlasmaCompositor::new( }) } +///| The part of a `w` x `h` output windows are laid out in. +fn work_area_for(w : Int, h : Int, theme : @scene.ThemeTokens) -> @wm.Rect { + @wm.compute_work_area( + { x: 0, y: 0, w, h }, + excl_top=32, // room for the server-side title bar drawn above each client + excl_bottom=theme.panel_height, + ) +} + +///| The output changed size (the nested window was resized, maximized or made fullscreen): +/// lay the desktop out for the new size, so nothing is drawn past the edge and cropped away. +pub fn MoonPlasmaCompositor::resize(self : MoonPlasmaCompositor, w : Int, h : Int) -> Unit { + if w <= 0 || h <= 0 || (w == self.output_w && h == self.output_h) { + return + } + self.output_w = w + self.output_h = h + self.work_area = work_area_for(w, h, self.theme) + self.server.set_output_size(w, h) + self.shell.set_output(self.theme, w, h) + self.apply_actions(self.wm.fit_work_area(self.work_area)) + self.last_frame = None + self.last_sig = None + self.frame_changed = true + self.damage.mark_full() +} + ///| pub fn MoonPlasmaCompositor::close(self : MoonPlasmaCompositor) -> Unit { for _, win in self.windows { diff --git a/compositor/host/resize_test.mbt b/compositor/host/resize_test.mbt new file mode 100644 index 0000000..0fe33df --- /dev/null +++ b/compositor/host/resize_test.mbt @@ -0,0 +1,16 @@ +///| Resizing the output (the nested window was resized): the desktop is drawn at the new size. + +///| +test "resize redraws the desktop at the new output size" { + let socket_path = "/tmp/wayland-moon-resize-" + @sys.getpid().to_string() + ".sock" + let comp = @host.MoonPlasmaCompositor::new(socket_path, @scene.ThemeTokens::moon_night(), 1280, 720).unwrap() + let before = comp.compose_frame() + assert_eq((before.width, before.height), (1280, 720)) + comp.resize(900, 560) + let after = comp.compose_frame() + assert_eq((after.width, after.height), (900, 560)) + assert_eq((comp.output_w, comp.output_h), (900, 560)) + assert_eq(comp.work_area.h, 560 - 32 - comp.theme.panel_height) + assert_eq(comp.server.output_w, 900) + comp.close() +} diff --git a/compositor/nested/nested.mbt b/compositor/nested/nested.mbt index fc1a5ef..c391e9f 100644 --- a/compositor/nested/nested.mbt +++ b/compositor/nested/nested.mbt @@ -48,10 +48,29 @@ pub(all) struct NestedWindow { mut closed : Bool mut px : Double mut py : Double - width : Int - height : Int + /// Current window size: what the compositor draws at and what each frame is attached as. + mut width : Int + mut height : Int + /// Size from the latest xdg_toplevel.configure (0 = the host leaves it to us), applied when + /// the xdg_surface.configure that ends the sequence arrives. + mut cfg_w : Int + mut cfg_h : Int + /// Largest size the host suggests for a window (xdg_toplevel.configure_bounds: the screen + /// minus its panels); 0 = unknown. The first size is fitted inside it. + mut bound_w : Int + mut bound_h : Int + /// Set when the host resized the window; cleared by take_resize. + mut resized : Bool + /// The first configure was handled. + mut mapped : Bool } +///| Smallest size the window may be resized to (the panel and a window or two still fit). +pub let min_width : Int = 640 + +///| +pub let min_height : Int = 400 + ///| fn NestedWindow::alloc(self : NestedWindow) -> UInt { let id = self.next_id @@ -164,10 +183,39 @@ fn NestedWindow::dispatch( w.u32(serial) self.send(w.finish_msg(self.xdg_surf_id, 4U)) self.configured = true + // The host chose a size (resized, maximized, tiled or fullscreen): draw at that size, so + // the desktop is never cropped to a window smaller than our buffer. + // Left to us, the first size still has to fit the screen the host has room for. + let fit_w = if !self.mapped && self.bound_w > 0 && self.width > self.bound_w { + clamp_min(self.bound_w, min_width) + } else { + self.width + } + let fit_h = if !self.mapped && self.bound_h > 0 && self.height > self.bound_h { + clamp_min(self.bound_h, min_height) + } else { + self.height + } + self.mapped = true + let nw = if self.cfg_w > 0 { clamp_min(self.cfg_w, min_width) } else { fit_w } + let nh = if self.cfg_h > 0 { clamp_min(self.cfg_h, min_height) } else { fit_h } + if nw != self.width || nh != self.height { + self.width = nw + self.height = nh + self.resized = true + } } } else if obj == self.toplevel_id { - if op == 1U { + if op == 0U { + // configure(width, height, states): 0 x 0 means "pick your own size". + self.cfg_w = r.i32() + self.cfg_h = r.i32() + } else if op == 1U { self.closed = true + } else if op == 2U { + // configure_bounds(width, height), xdg_wm_base v4+ + self.bound_w = r.i32() + self.bound_h = r.i32() } } else if obj == self.seat_id { if op == 0U { @@ -351,6 +399,12 @@ pub fn NestedWindow::open( py: 0.0, width, height, + cfg_w: 0, + cfg_h: 0, + bound_w: 0, + bound_h: 0, + resized: false, + mapped: false, } let w = @wayland.WireWriter::new() w.u32(2U) @@ -358,7 +412,7 @@ pub fn NestedWindow::open( self.roundtrip() self.comp_id = self.bind("wl_compositor", 4U) self.shm_id = self.bind("wl_shm", 1U) - self.wm_id = self.bind("xdg_wm_base", 2U) + self.wm_id = self.bind("xdg_wm_base", 4U) // v4: configure_bounds self.seat_id = self.bind("wl_seat", 5U) self.shape_mgr_id = self.bind("wp_cursor_shape_manager_v1", 1U) if self.comp_id == 0U || self.shm_id == 0U || self.wm_id == 0U { @@ -384,14 +438,11 @@ pub fn NestedWindow::open( let w5 = @wayland.WireWriter::new() w5.str(app_id) self.send(w5.finish_msg(self.toplevel_id, 3U)) - // Fixed size: min == max == output size. - let w6 = @wayland.WireWriter::new() - w6.i32(width) - w6.i32(height) - self.send(w6.finish_msg(self.toplevel_id, 7U)) // set_max_size + // Resizable: the host may give us any size from the minimum up (no max size), and the + // compositor follows it (see take_resize). let w7 = @wayland.WireWriter::new() - w7.i32(width) - w7.i32(height) + w7.i32(min_width) + w7.i32(min_height) self.send(w7.finish_msg(self.toplevel_id, 8U)) // set_min_size self.send(@wayland.WireWriter::new().finish_msg(self.surf_id, 6U)) // commit let mut tries = 0 @@ -414,6 +465,21 @@ pub fn NestedWindow::poll(self : NestedWindow) -> Bool { !self.closed } +///| +fn clamp_min(v : Int, lo : Int) -> Int { + if v < lo { lo } else { v } +} + +///| The new window size if the host resized the window since the last call. The caller resizes +/// the desktop to it before presenting the next frame. +pub fn NestedWindow::take_resize(self : NestedWindow) -> (Int, Int)? { + if !self.resized { + return None + } + self.resized = false + Some((self.width, self.height)) +} + ///| Take all input received since the last call. pub fn NestedWindow::take_input(self : NestedWindow) -> Array[InputEvent] { let out = self.input.copy() diff --git a/compositor/wayland/server.mbt b/compositor/wayland/server.mbt index eee1a49..25b657e 100644 --- a/compositor/wayland/server.mbt +++ b/compositor/wayland/server.mbt @@ -221,8 +221,8 @@ fn ClientConn::close_all(self : ClientConn) -> Unit { pub(all) struct WaylandServer { socket_path : String listen_fd : Int - output_w : Int - output_h : Int + mut output_w : Int + mut output_h : Int clients : Array[ClientConn] win_to_client : Map[UInt64, UInt] mut next_client_id : UInt @@ -321,6 +321,31 @@ fn emit_seat_info(c : ClientConn, seat_id : UInt) -> Unit { c.send_msg(w2.finish_msg(seat_id, 1U)) } +///| The output changed size (the nested window was resized): tell every bound wl_output. +pub fn WaylandServer::set_output_size(self : WaylandServer, w : Int, h : Int) -> Unit { + if w == self.output_w && h == self.output_h { + return + } + self.output_w = w + self.output_h = h + for c in self.clients { + if !c.alive { + continue + } + for id, kind in c.objects { + if kind is ObjOutput { + let m = WireWriter::new() + m.u32(3U) // current | preferred + m.i32(w) + m.i32(h) + m.i32(60000) + c.send_msg(m.finish_msg(id, 1U)) + c.send_msg(WireWriter::new().finish_msg(id, 2U)) // done + } + } + } +} + ///| fn emit_output_info( c : ClientConn, diff --git a/lib/wm/pkg.generated.mbti b/lib/wm/pkg.generated.mbti index 241e088..9876888 100644 --- a/lib/wm/pkg.generated.mbti +++ b/lib/wm/pkg.generated.mbti @@ -142,6 +142,8 @@ pub(all) struct WmState { pub fn WmState::close_window(Self, UInt64, Rect) -> Array[WmAction] pub fn WmState::drag_split(Self, SplitHandle, Int32, Int32, Rect) -> Array[WmAction] pub fn WmState::end_drag(Self) -> Unit +pub fn WmState::fit_work_area(Self, Rect) -> Array[WmAction] +pub fn WmState::float_window(Self, UInt64, Rect) -> Array[WmAction] pub fn WmState::handle_request(Self, UInt64, WindowRequest, Rect) -> Array[WmAction] pub fn WmState::hit_test(Self, Int32, Int32, Rect?) -> InputHit pub fn WmState::new(inner_gap? : Int32, outer_gap? : Int32, titlebar_h? : Int32, border_w? : Int32) -> Self diff --git a/lib/wm/wm.mbt b/lib/wm/wm.mbt index c4ae94b..ab7c8e2 100644 --- a/lib/wm/wm.mbt +++ b/lib/wm/wm.mbt @@ -1088,3 +1088,39 @@ test "split handles: divider position, drag, and row gap leaves room for the tit let w3 = wm.windows[3UL].geometry assert_eq(w3.y - (w2.y + w2.h), 8 + 32) } + +///| Keep a floating rectangle inside `area`: shrunk to fit, then moved back on screen. +fn fit_rect(r : Rect, area : Rect) -> Rect { + let w = if r.w > area.w { area.w } else { r.w } + let h = if r.h > area.h { area.h } else { r.h } + let x = clamp_int(r.x, area.x, area.x + area.w - w) + let y = clamp_int(r.y, area.y, area.y + area.h - h) + { x, y, w, h } +} + +///| The output changed size: reflow every workspace into the new work area and pull floating +/// windows back inside it, so no window is left cut off past the new edge. +pub fn WmState::fit_work_area(self : WmState, work_area : Rect) -> Array[WmAction] { + let actions : Array[WmAction] = [] + for ws in self.workspaces { + for id in ws.floating { + match self.windows.get(id) { + Some(win) => { + win.floating_geometry = fit_rect(win.floating_geometry, work_area) + if !win.maximized && !win.fullscreen && !win.minimized { + let g = fit_rect(win.geometry, work_area) + if g != win.geometry { + win.geometry = g + actions.push(SetGeometry(id, g)) + } + } + } + None => () + } + } + for a in self.relayout(ws.id, work_area) { + actions.push(a) + } + } + actions +} diff --git a/lib/wm/wm_test.mbt b/lib/wm/wm_test.mbt index 28bae71..48f1643 100644 --- a/lib/wm/wm_test.mbt +++ b/lib/wm/wm_test.mbt @@ -135,3 +135,18 @@ test "interactive drag move and resize clamps to min_size" { assert_eq(wm.update_drag(250, 140), Some(SetGeometry(5UL, { x: 150, y: 140, w: 200, h: 150 }))) wm.end_drag() } + +///| +test "shrinking the work area reflows tiles and pulls floating windows back on screen" { + let wm = WmState::new(inner_gap=10, outer_gap=10) + let big : Rect = { x: 0, y: 0, w: 1280, h: 700 } + ignore(wm.open_window(mk_win(1UL), big)) + ignore(wm.open_window(mk_win(2UL, floating=true), big)) + wm.windows[2UL].geometry = { x: 900, y: 400, w: 360, h: 280 } + let small : Rect = { x: 0, y: 0, w: 800, h: 500 } + ignore(wm.fit_work_area(small)) + // The tile fills the new padded area. + assert_eq(wm.windows[1UL].geometry, { x: 10, y: 10, w: 780, h: 480 }) + // The floating window keeps its size but moves back inside. + assert_eq(wm.windows[2UL].geometry, { x: 440, y: 220, w: 360, h: 280 }) +}