Nested window: follow the host's size instead of being cropped

The --window backend pinned its xdg_toplevel to a fixed size (min == max ==
1280x720 or --size) and ignored xdg_toplevel.configure. Whenever the host gave
the window less room than that (a tiling host, a maximized/snapped window, a
screen smaller than the default once its own panels and title bar are taken
off), the host cropped the 1280x720 buffer and the right/bottom of the desktop,
panel included, was cut off. Fullscreen only looked right when the screen
happened to be at least that big.

- nested: drop the max size (min 640x400), read the configure size and, on
  xdg_wm_base v4, configure_bounds to fit the first size on screen; report a
  new size through take_resize.
- compositor: MoonPlasmaCompositor::resize recomputes the work area, tells
  clients the new wl_output mode, reflows every workspace and pulls floating
  windows back inside (WmState::fit_work_area), and forces a full redraw.
- both main loops (wasm component and native) resize before presenting.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DPL5hkYa7dkDnotDeMSdX7
This commit is contained in:
Claude authored and V committed 2026-11-25 17:07:33 +00:00
1 parent 0f4c84b966
commit 7248e0ca6e
9 files changed
+220 -22

No files matched your search

+11
View File
@@ -244,6 +244,17 @@ pub fn run(args : Array[String]) -> Int {
if n.keymap.length() > 0 { if n.keymap.length() > 0 {
comp.server.set_keymap(n.keymap) 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() let now_ms = @sys.monotonic_ms().to_uint()
for ev in n.take_input() { for ev in n.take_input() {
comp.handle_input(ev, now_ms) comp.handle_input(ev, now_ms)
+5
View File
@@ -148,6 +148,11 @@ fn main {
if n.keymap.length() > 0 { if n.keymap.length() > 0 {
comp.server.set_keymap(n.keymap) 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() let now_ms = @env.now().to_uint()
for ev in n.take_input() { for ev in n.take_input() {
comp.handle_input(ev, now_ms) comp.handle_input(ev, now_ms)
+31 -9
View File
@@ -33,9 +33,9 @@ pub(all) struct MoonPlasmaCompositor {
server : @wayland.WaylandServer server : @wayland.WaylandServer
wm : @wm.WmState wm : @wm.WmState
theme : @scene.ThemeTokens theme : @scene.ThemeTokens
output_w : Int mut output_w : Int
output_h : Int mut output_h : Int
work_area : @wm.Rect mut work_area : @wm.Rect
windows : Map[UInt64, HostWindow] windows : Map[UInt64, HostWindow]
damage : @scene.DamageTracker damage : @scene.DamageTracker
shell : @shell.DesktopShell shell : @shell.DesktopShell
@@ -91,12 +91,7 @@ pub fn MoonPlasmaCompositor::new(
titlebar_h=32, titlebar_h=32,
border_w=2, border_w=2,
) )
let out_rect : @wm.Rect = { x: 0, y: 0, w: output_w, h: output_h } let work_area = work_area_for(output_w, output_h, theme)
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 shell = @shell.DesktopShell::new(socket_path) let shell = @shell.DesktopShell::new(socket_path)
shell.set_output(theme, output_w, output_h) shell.set_output(theme, output_w, output_h)
Some({ 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 { pub fn MoonPlasmaCompositor::close(self : MoonPlasmaCompositor) -> Unit {
for _, win in self.windows { for _, win in self.windows {
+16
View File
@@ -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()
}
+77 -11
View File
@@ -48,10 +48,29 @@ pub(all) struct NestedWindow {
mut closed : Bool mut closed : Bool
mut px : Double mut px : Double
mut py : Double mut py : Double
width : Int /// Current window size: what the compositor draws at and what each frame is attached as.
height : Int 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 { fn NestedWindow::alloc(self : NestedWindow) -> UInt {
let id = self.next_id let id = self.next_id
@@ -164,10 +183,39 @@ fn NestedWindow::dispatch(
w.u32(serial) w.u32(serial)
self.send(w.finish_msg(self.xdg_surf_id, 4U)) self.send(w.finish_msg(self.xdg_surf_id, 4U))
self.configured = true 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 { } 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 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 { } else if obj == self.seat_id {
if op == 0U { if op == 0U {
@@ -351,6 +399,12 @@ pub fn NestedWindow::open(
py: 0.0, py: 0.0,
width, width,
height, height,
cfg_w: 0,
cfg_h: 0,
bound_w: 0,
bound_h: 0,
resized: false,
mapped: false,
} }
let w = @wayland.WireWriter::new() let w = @wayland.WireWriter::new()
w.u32(2U) w.u32(2U)
@@ -358,7 +412,7 @@ pub fn NestedWindow::open(
self.roundtrip() self.roundtrip()
self.comp_id = self.bind("wl_compositor", 4U) self.comp_id = self.bind("wl_compositor", 4U)
self.shm_id = self.bind("wl_shm", 1U) 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.seat_id = self.bind("wl_seat", 5U)
self.shape_mgr_id = self.bind("wp_cursor_shape_manager_v1", 1U) 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 { 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() let w5 = @wayland.WireWriter::new()
w5.str(app_id) w5.str(app_id)
self.send(w5.finish_msg(self.toplevel_id, 3U)) self.send(w5.finish_msg(self.toplevel_id, 3U))
// Fixed size: min == max == output size. // Resizable: the host may give us any size from the minimum up (no max size), and the
let w6 = @wayland.WireWriter::new() // compositor follows it (see take_resize).
w6.i32(width)
w6.i32(height)
self.send(w6.finish_msg(self.toplevel_id, 7U)) // set_max_size
let w7 = @wayland.WireWriter::new() let w7 = @wayland.WireWriter::new()
w7.i32(width) w7.i32(min_width)
w7.i32(height) w7.i32(min_height)
self.send(w7.finish_msg(self.toplevel_id, 8U)) // set_min_size self.send(w7.finish_msg(self.toplevel_id, 8U)) // set_min_size
self.send(@wayland.WireWriter::new().finish_msg(self.surf_id, 6U)) // commit self.send(@wayland.WireWriter::new().finish_msg(self.surf_id, 6U)) // commit
let mut tries = 0 let mut tries = 0
@@ -414,6 +465,21 @@ pub fn NestedWindow::poll(self : NestedWindow) -> Bool {
!self.closed !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. ///| Take all input received since the last call.
pub fn NestedWindow::take_input(self : NestedWindow) -> Array[InputEvent] { pub fn NestedWindow::take_input(self : NestedWindow) -> Array[InputEvent] {
let out = self.input.copy() let out = self.input.copy()
+27 -2
View File
@@ -221,8 +221,8 @@ fn ClientConn::close_all(self : ClientConn) -> Unit {
pub(all) struct WaylandServer { pub(all) struct WaylandServer {
socket_path : String socket_path : String
listen_fd : Int listen_fd : Int
output_w : Int mut output_w : Int
output_h : Int mut output_h : Int
clients : Array[ClientConn] clients : Array[ClientConn]
win_to_client : Map[UInt64, UInt] win_to_client : Map[UInt64, UInt]
mut next_client_id : 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)) 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( fn emit_output_info(
c : ClientConn, c : ClientConn,
+2
View File
@@ -142,6 +142,8 @@ pub(all) struct WmState {
pub fn WmState::close_window(Self, UInt64, Rect) -> Array[WmAction] 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::drag_split(Self, SplitHandle, Int32, Int32, Rect) -> Array[WmAction]
pub fn WmState::end_drag(Self) -> Unit 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::handle_request(Self, UInt64, WindowRequest, Rect) -> Array[WmAction]
pub fn WmState::hit_test(Self, Int32, Int32, Rect?) -> InputHit 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 pub fn WmState::new(inner_gap? : Int32, outer_gap? : Int32, titlebar_h? : Int32, border_w? : Int32) -> Self
+36
View File
@@ -1088,3 +1088,39 @@ test "split handles: divider position, drag, and row gap leaves room for the tit
let w3 = wm.windows[3UL].geometry let w3 = wm.windows[3UL].geometry
assert_eq(w3.y - (w2.y + w2.h), 8 + 32) 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
}
+15
View File
@@ -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 }))) assert_eq(wm.update_drag(250, 140), Some(SetGeometry(5UL, { x: 150, y: 140, w: 200, h: 150 })))
wm.end_drag() 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 })
}