Files
Claude 7248e0ca6e 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
2026-11-25 17:07:33 +00:00

1127 lines
29 KiB
MoonBit

///| Pure MoonBit Window Manager & hybrid layout engine (no WIT dependencies).
///|
pub(all) struct Rect {
x : Int
y : Int
w : Int
h : Int
} derive(Eq, Show, Debug)
///|
pub(all) struct Size {
w : Int
h : Int
} derive(Eq, Show, Debug)
///|
pub fn Rect::contains(self : Rect, px : Int, py : Int) -> Bool {
px >= self.x && py >= self.y && px < self.x + self.w && py < self.y + self.h
}
///|
pub fn clamp_int(v : Int, lo : Int, hi : Int) -> Int {
if v < lo {
lo
} else if v > hi {
hi
} else {
v
}
}
///|
pub(all) enum LayoutMode {
Floating
Tiling
Monocle
} derive(Eq, Show, Debug)
///|
pub(all) enum BspNode {
Leaf(UInt64)
Split(horizontal~ : Bool, ratio~ : Double, left~ : BspNode, right~ : BspNode)
} derive(Eq, Show, Debug)
///|
pub(all) struct WindowRecord {
id : UInt64
app_id : String
title : String
mut geometry : Rect
mut floating_geometry : Rect
min_size : Size
max_size : Size?
mut focused : Bool
mut maximized : Bool
mut fullscreen : Bool
mut minimized : Bool
mut is_floating : Bool
workspace : UInt
can_move : Bool
can_resize : Bool
can_maximize : Bool
can_minimize : Bool
can_fullscreen : Bool
}
///|
pub(all) enum WindowRequest {
Maximize(Bool)
Fullscreen(Bool)
Minimize(Bool)
Close
Activate
MoveTo(Int, Int)
ResizeTo(Int, Int)
} derive(Eq, Show, Debug)
///|
pub(all) enum WmAction {
SetGeometry(UInt64, Rect)
Focus(UInt64)
Raise(UInt64)
Close(UInt64)
SetMaximized(UInt64, Bool)
SetFullscreen(UInt64, Bool)
} derive(Eq, Show, Debug)
///|
pub(all) enum InputHit {
Client(UInt64, Int, Int)
Decoration(UInt64, Int, Int)
ShellPanel(Int, Int)
HitNone
} derive(Eq, Show, Debug)
///|
pub(all) enum DragMode {
Move
ResizeN
ResizeS
ResizeE
ResizeW
ResizeNW
ResizeNE
ResizeSW
ResizeSE
} derive(Eq, Show)
///|
pub(all) struct DragSession {
win_id : UInt64
mode : DragMode
start_ptr_x : Int
start_ptr_y : Int
start_rect : Rect
}
///|
pub(all) struct Workspace {
id : UInt
mut mode : LayoutMode
mut tree : BspNode?
floating : Array[UInt64]
mru : Array[UInt64]
mut fullscreen : UInt64?
}
///|
pub fn Workspace::new(id : UInt, mode~ : LayoutMode = Tiling) -> Workspace {
{ id, mode, tree: None, floating: [], mru: [], fullscreen: None }
}
///|
pub(all) struct WmState {
workspaces : Array[Workspace]
windows : Map[UInt64, WindowRecord]
mut active_workspace : UInt
mut focused : UInt64?
mut inner_gap : Int
mut outer_gap : Int
mut titlebar_h : Int
mut border_w : Int
mut cascade_step : Int
mut drag : DragSession?
}
///|
pub fn WmState::new(
inner_gap~ : Int = 8,
outer_gap~ : Int = 8,
titlebar_h~ : Int = 32,
border_w~ : Int = 2,
) -> WmState {
{
workspaces: [Workspace::new(0U, mode=Tiling)],
windows: {},
active_workspace: 0U,
focused: None,
inner_gap,
outer_gap,
titlebar_h,
border_w,
cascade_step: 0,
drag: None,
}
}
///| Compute usable work area by subtracting exclusive zones (e.g. top/bottom panel).
pub fn compute_work_area(
output : Rect,
excl_top~ : Int = 0,
excl_right~ : Int = 0,
excl_bottom~ : Int = 0,
excl_left~ : Int = 0,
) -> Rect {
let x = output.x + excl_left
let y = output.y + excl_top
let w = if output.w > excl_left + excl_right {
output.w - excl_left - excl_right
} else {
1
}
let h = if output.h > excl_top + excl_bottom {
output.h - excl_top - excl_bottom
} else {
1
}
{ x, y, w, h }
}
///| Insert a window leaf into a BSP tree, splitting at the deepest rightmost leaf
/// (alternating horizontal/vertical split).
pub fn insert_leaf(
tree : BspNode?,
id : UInt64,
horizontal~ : Bool = true,
) -> BspNode {
match tree {
None => Leaf(id)
Some(Leaf(existing)) =>
Split(horizontal~, ratio=0.5, left=Leaf(existing), right=Leaf(id))
Some(Split(horizontal=h, ratio=r, left=l, right=rt)) =>
Split(
horizontal=h,
ratio=r,
left=l,
right=insert_leaf(Some(rt), id, horizontal=!h),
)
}
}
///| Remove a window leaf from a BSP tree, collapsing its parent split.
pub fn remove_leaf(tree : BspNode?, id : UInt64) -> BspNode? {
match tree {
None => None
Some(Leaf(leaf_id)) => if leaf_id == id { None } else { Some(Leaf(leaf_id)) }
Some(Split(horizontal=h, ratio=r, left=l, right=rt)) => {
let nl = remove_leaf(Some(l), id)
let nr = remove_leaf(Some(rt), id)
match (nl, nr) {
(None, None) => None
(Some(only), None) => Some(only)
(None, Some(only)) => Some(only)
(Some(new_l), Some(new_r)) =>
Some(Split(horizontal=h, ratio=r, left=new_l, right=new_r))
}
}
}
}
///| 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) -> Unit {
match n {
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)
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 {
if ws.id == ws_id {
return ws
}
}
let ws = Workspace::new(ws_id, mode=Tiling)
self.workspaces.push(ws)
ws
}
///|
fn remove_id(arr : Array[UInt64], id : UInt64) -> Unit {
let mut idx = -1
for i = 0; i < arr.length(); i = i + 1 {
if arr[i] == id {
idx = i
break
}
}
if idx >= 0 {
ignore(arr.remove(idx))
}
}
///|
fn push_mru_front(mru : Array[UInt64], id : UInt64) -> Unit {
remove_id(mru, id)
mru.insert(0, id)
}
///|
fn push_top(arr : Array[UInt64], id : UInt64) -> Unit {
remove_id(arr, id)
arr.push(id)
}
///|
fn WmState::inner_work_area(self : WmState, work_area : Rect) -> Rect {
let g = self.outer_gap
let w = if work_area.w > 2 * g { work_area.w - 2 * g } else { work_area.w }
let h = if work_area.h > 2 * g { work_area.h - 2 * g } else { work_area.h }
{ x: work_area.x + g, y: work_area.y + g, w, h }
}
///| Reflow tiled/monocle/fullscreen windows on a workspace and emit geometry actions.
pub fn WmState::relayout(
self : WmState,
ws_id : UInt,
work_area : Rect,
) -> Array[WmAction] {
let ws = self.ensure_workspace(ws_id)
let actions : Array[WmAction] = []
// Fullscreen window overrides work_area.
match ws.fullscreen {
Some(fs_id) =>
match self.windows.get(fs_id) {
Some(win) =>
if !win.minimized {
win.geometry = work_area
actions.push(SetGeometry(fs_id, work_area))
}
None => ()
}
None => ()
}
let padded = self.inner_work_area(work_area)
match ws.mode {
Floating => ()
Monocle =>
match ws.tree {
Some(t) => {
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) {
continue
}
match self.windows.get(id) {
Some(win) =>
if !win.minimized {
win.geometry = padded
actions.push(SetGeometry(id, padded))
}
None => ()
}
}
}
None => ()
}
Tiling =>
match ws.tree {
Some(t) => {
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) {
continue
}
match self.windows.get(id) {
Some(win) =>
if !win.minimized {
let min_w = if win.min_size.w > 0 { win.min_size.w } else { 64 }
let min_h = if win.min_size.h > 0 { win.min_size.h } else { 48 }
let clamped : Rect = {
x: rect.x,
y: rect.y,
w: if rect.w < min_w { min_w } else { rect.w },
h: if rect.h < min_h { min_h } else { rect.h },
}
// A maximized tiled window covers the whole padded work area.
let g = if win.maximized { padded } else { clamped }
win.geometry = g
actions.push(SetGeometry(id, g))
}
None => ()
}
}
}
None => ()
}
}
// Maximized floating windows also fill padded work_area.
for id in ws.floating {
if ws.fullscreen == Some(id) {
continue
}
match self.windows.get(id) {
Some(win) =>
if win.maximized && !win.minimized {
win.geometry = padded
actions.push(SetGeometry(id, padded))
}
None => ()
}
}
actions
}
///|
fn WmState::set_focus(self : WmState, id : UInt64?) -> Array[WmAction] {
let actions : Array[WmAction] = []
match self.focused {
Some(prev) =>
if Some(prev) != id {
match self.windows.get(prev) {
Some(w) => w.focused = false
None => ()
}
}
None => ()
}
self.focused = id
match id {
Some(nid) =>
match self.windows.get(nid) {
Some(w) => {
w.focused = true
let ws = self.ensure_workspace(w.workspace)
push_mru_front(ws.mru, nid)
if w.is_floating {
push_top(ws.floating, nid)
}
actions.push(Focus(nid))
actions.push(Raise(nid))
}
None => ()
}
None => ()
}
actions
}
///|
fn should_auto_float(win : WindowRecord) -> Bool {
if !win.can_resize {
return true
}
match win.max_size {
Some(mx) => mx.w == win.min_size.w && mx.h == win.min_size.h
None => false
}
}
///|
pub fn WmState::open_window(
self : WmState,
win : WindowRecord,
work_area : Rect,
) -> Array[WmAction] {
let ws = self.ensure_workspace(win.workspace)
let auto_float = win.is_floating || ws.mode == Floating || should_auto_float(win)
win.is_floating = auto_float
if auto_float && (win.geometry.w == 0 || win.geometry.h == 0) {
let dw = clamp_int(work_area.w * 2 / 3, 240, work_area.w - 2 * self.outer_gap)
let dh = clamp_int(work_area.h * 2 / 3, 180, work_area.h - 2 * self.outer_gap)
let offset = (self.cascade_step % 6) * 28
self.cascade_step += 1
let gx = work_area.x + self.outer_gap + 32 + offset
let gy = work_area.y + self.outer_gap + 24 + offset
let r : Rect = { x: gx, y: gy, w: dw, h: dh }
win.geometry = r
win.floating_geometry = r
} else if auto_float {
win.floating_geometry = win.geometry
}
self.windows[win.id] = win
if auto_float {
push_top(ws.floating, win.id)
} else {
ws.tree = Some(insert_leaf(ws.tree, win.id))
}
push_mru_front(ws.mru, win.id)
let actions : Array[WmAction] = []
if auto_float {
actions.push(SetGeometry(win.id, win.geometry))
}
for a in self.relayout(win.workspace, work_area) {
actions.push(a)
}
for a in self.set_focus(Some(win.id)) {
actions.push(a)
}
actions
}
///|
pub fn WmState::close_window(
self : WmState,
id : UInt64,
work_area : Rect,
) -> Array[WmAction] {
let actions : Array[WmAction] = []
match self.windows.get(id) {
None => return actions
Some(win) => {
let ws_id = win.workspace
let ws = self.ensure_workspace(ws_id)
ignore(self.windows.remove(id))
ws.tree = remove_leaf(ws.tree, id)
remove_id(ws.floating, id)
remove_id(ws.mru, id)
if ws.fullscreen == Some(id) {
ws.fullscreen = None
}
for a in self.relayout(ws_id, work_area) {
actions.push(a)
}
if self.focused == Some(id) {
let next_focus : UInt64? = if ws.mru.length() > 0 {
Some(ws.mru[0])
} else {
None
}
for a in self.set_focus(next_focus) {
actions.push(a)
}
}
}
}
actions
}
///| Synchronize state on startup or WM hot-swap.
pub fn WmState::sync_windows(
self : WmState,
work_area : Rect,
wins : Array[WindowRecord],
) -> Array[WmAction] {
self.workspaces.clear()
ignore(self.workspaces.push(Workspace::new(0U, mode=Tiling)))
self.windows.clear()
self.focused = None
let actions : Array[WmAction] = []
let mut last_focused : UInt64? = None
for w in wins {
if w.focused {
last_focused = Some(w.id)
}
for a in self.open_window(w, work_area) {
actions.push(a)
}
}
match last_focused {
Some(fid) =>
for a in self.set_focus(Some(fid)) {
actions.push(a)
}
None => ()
}
actions
}
///|
pub fn WmState::set_layout_mode(
self : WmState,
mode : LayoutMode,
work_area : Rect,
) -> Array[WmAction] {
let ws = self.ensure_workspace(self.active_workspace)
if ws.mode == mode {
return []
}
ws.mode = mode
if mode == Floating {
// 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,
vgap=self.inner_gap + self.titlebar_h,
)
for pair in rects {
let (id, _) = pair
match self.windows.get(id) {
Some(win) => {
win.is_floating = true
win.floating_geometry = win.geometry
push_top(ws.floating, id)
}
None => ()
}
}
ws.tree = None
}
None => ()
}
} else {
// Re-insert floating windows (except fixed-size dialogs) into BSP tree.
let keep_floating : Array[UInt64] = []
for id in ws.floating {
match self.windows.get(id) {
Some(win) =>
if should_auto_float(win) {
keep_floating.push(id)
} else {
win.is_floating = false
ws.tree = Some(insert_leaf(ws.tree, id))
}
None => ()
}
}
ws.floating.clear()
for id in keep_floating {
ws.floating.push(id)
}
}
self.relayout(self.active_workspace, work_area)
}
///|
pub fn WmState::handle_request(
self : WmState,
id : UInt64,
req : WindowRequest,
work_area : Rect,
) -> Array[WmAction] {
let actions : Array[WmAction] = []
match self.windows.get(id) {
None => return actions
Some(win) => {
let ws = self.ensure_workspace(win.workspace)
match req {
Activate =>
for a in self.set_focus(Some(id)) {
actions.push(a)
}
Close => actions.push(Close(id))
Maximize(max) =>
if win.can_maximize {
win.maximized = max
actions.push(SetMaximized(id, max))
if !max && win.is_floating {
win.geometry = win.floating_geometry
actions.push(SetGeometry(id, win.geometry))
}
for a in self.relayout(win.workspace, work_area) {
actions.push(a)
}
}
Fullscreen(fs) =>
if win.can_fullscreen {
win.fullscreen = fs
if fs {
ws.fullscreen = Some(id)
} else if ws.fullscreen == Some(id) {
ws.fullscreen = None
}
actions.push(SetFullscreen(id, fs))
if !fs && win.is_floating && !win.maximized {
win.geometry = win.floating_geometry
actions.push(SetGeometry(id, win.geometry))
}
for a in self.relayout(win.workspace, work_area) {
actions.push(a)
}
}
Minimize(min) =>
if win.can_minimize {
win.minimized = min
for a in self.relayout(win.workspace, work_area) {
actions.push(a)
}
}
MoveTo(nx, ny) =>
if win.can_move {
let r : Rect = { x: nx, y: ny, w: win.geometry.w, h: win.geometry.h }
win.geometry = r
if win.is_floating {
win.floating_geometry = r
}
actions.push(SetGeometry(id, r))
}
ResizeTo(nw, nh) =>
if win.can_resize {
let min_w = if win.min_size.w > 0 { win.min_size.w } else { 64 }
let min_h = if win.min_size.h > 0 { win.min_size.h } else { 48 }
let cw = if nw < min_w { min_w } else { nw }
let ch = if nh < min_h { min_h } else { nh }
let r : Rect = { x: win.geometry.x, y: win.geometry.y, w: cw, h: ch }
win.geometry = r
if win.is_floating {
win.floating_geometry = r
}
actions.push(SetGeometry(id, r))
}
}
}
}
actions
}
///| Bottom-to-top Z-order: Tiled -> Floating -> Fullscreen.
pub fn WmState::stacking_order(self : WmState) -> Array[UInt64] {
let ws = self.ensure_workspace(self.active_workspace)
let order : Array[UInt64] = []
fn collect_tree(n : BspNode?, acc : Array[UInt64], fs : UInt64?, wins : Map[UInt64, WindowRecord]) -> Unit {
match n {
None => ()
Some(Leaf(id)) =>
if fs != Some(id) {
match wins.get(id) {
Some(w) => if !w.minimized { acc.push(id) }
None => ()
}
}
Some(Split(horizontal=_, ratio=_, left=l, right=rt)) => {
collect_tree(Some(l), acc, fs, wins)
collect_tree(Some(rt), acc, fs, wins)
}
}
}
collect_tree(ws.tree, order, ws.fullscreen, self.windows)
// Maximized tiled windows are drawn above their neighbours.
let below : Array[UInt64] = []
let above : Array[UInt64] = []
for id in order {
match self.windows.get(id) {
Some(w) => if w.maximized { above.push(id) } else { below.push(id) }
None => below.push(id)
}
}
order.clear()
order.append(below)
order.append(above)
for id in ws.floating {
if ws.fullscreen != Some(id) {
match self.windows.get(id) {
Some(w) => if !w.minimized { order.push(id) }
None => ()
}
}
}
match ws.fullscreen {
Some(fs_id) =>
match self.windows.get(fs_id) {
Some(w) => if !w.minimized { order.push(fs_id) }
None => ()
}
None => ()
}
order
}
///| Hit-test pointer position against panel, decorations, and client surfaces.
pub fn WmState::hit_test(
self : WmState,
px : Int,
py : Int,
panel_rect : Rect?,
) -> InputHit {
match panel_rect {
Some(pr) =>
if pr.contains(px, py) {
return ShellPanel(px - pr.x, py - pr.y)
}
None => ()
}
let order = self.stacking_order()
for i = order.length() - 1; i >= 0; i = i - 1 {
let id = order[i]
match self.windows.get(id) {
Some(win) => {
let g = win.geometry
if win.fullscreen {
if g.contains(px, py) {
return Client(id, px - g.x, py - g.y)
}
} else {
// Outer decoration rect surrounds client geometry by titlebar_h on top, border_w on sides/bottom.
let outer : Rect = {
x: g.x - self.border_w,
y: g.y - self.titlebar_h,
w: g.w + 2 * self.border_w,
h: g.h + self.titlebar_h + self.border_w,
}
if g.contains(px, py) {
return Client(id, px - g.x, py - g.y)
}
if outer.contains(px, py) {
return Decoration(id, px - outer.x, py - outer.y)
}
}
}
None => ()
}
}
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,
win_id : UInt64,
mode : DragMode,
ptr_x : Int,
ptr_y : Int,
) -> Bool {
match self.windows.get(win_id) {
Some(win) =>
if !win.fullscreen {
self.drag = Some({
win_id,
mode,
start_ptr_x: ptr_x,
start_ptr_y: ptr_y,
start_rect: win.geometry,
})
true
} else {
false
}
None => false
}
}
///|
pub fn WmState::update_drag(
self : WmState,
ptr_x : Int,
ptr_y : Int,
) -> WmAction? {
match self.drag {
None => None
Some(d) =>
match self.windows.get(d.win_id) {
None => {
self.drag = None
None
}
Some(win) => {
let dx = ptr_x - d.start_ptr_x
let dy = ptr_y - d.start_ptr_y
let min_w = if win.min_size.w > 0 { win.min_size.w } else { 120 }
let min_h = if win.min_size.h > 0 { win.min_size.h } else { 80 }
let sr = d.start_rect
let next : Rect = match d.mode {
Move => { x: sr.x + dx, y: sr.y + dy, w: sr.w, h: sr.h }
ResizeE => {
x: sr.x,
y: sr.y,
w: if sr.w + dx < min_w { min_w } else { sr.w + dx },
h: sr.h,
}
ResizeS => {
x: sr.x,
y: sr.y,
w: sr.w,
h: if sr.h + dy < min_h { min_h } else { sr.h + dy },
}
ResizeSE => {
x: sr.x,
y: sr.y,
w: if sr.w + dx < min_w { min_w } else { sr.w + dx },
h: if sr.h + dy < min_h { min_h } else { sr.h + dy },
}
ResizeW => {
let nw = if sr.w - dx < min_w { min_w } else { sr.w - dx }
{ x: sr.x + (sr.w - nw), y: sr.y, w: nw, h: sr.h }
}
ResizeN => {
let nh = if sr.h - dy < min_h { min_h } else { sr.h - dy }
{ x: sr.x, y: sr.y + (sr.h - nh), w: sr.w, h: nh }
}
ResizeNW => {
let nw = if sr.w - dx < min_w { min_w } else { sr.w - dx }
let nh = if sr.h - dy < min_h { min_h } else { sr.h - dy }
{ x: sr.x + (sr.w - nw), y: sr.y + (sr.h - nh), w: nw, h: nh }
}
ResizeNE => {
let nw = if sr.w + dx < min_w { min_w } else { sr.w + dx }
let nh = if sr.h - dy < min_h { min_h } else { sr.h - dy }
{ x: sr.x, y: sr.y + (sr.h - nh), w: nw, h: nh }
}
ResizeSW => {
let nw = if sr.w - dx < min_w { min_w } else { sr.w - dx }
let nh = if sr.h + dy < min_h { min_h } else { sr.h + dy }
{ x: sr.x + (sr.w - nw), y: sr.y, w: nw, h: nh }
}
}
win.geometry = next
if win.is_floating {
win.floating_geometry = next
}
Some(SetGeometry(d.win_id, next))
}
}
}
}
///|
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)
}
///| 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
}