Spike Wayland compositor, hybrid MoonBit WM, SSD decorations, and wasmtime native host
This commit is contained in:
1 parent
880ecd4102
commit
17a8a46cfd
56 files changed
+8924
-11
No files matched your search
+889
@@ -0,0 +1,889 @@
|
||||
///| 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))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
///| Compute tiled rectangles for every leaf in a BSP tree within `area`.
|
||||
pub fn compute_bsp_rects(
|
||||
node : BspNode,
|
||||
area : Rect,
|
||||
inner_gap : Int,
|
||||
) -> Array[(UInt64, Rect)] {
|
||||
let out : Array[(UInt64, Rect)] = []
|
||||
fn walk(n : BspNode, r : Rect, gap : Int, acc : Array[(UInt64, Rect)]) -> Unit {
|
||||
match n {
|
||||
Leaf(id) => acc.push((id, r))
|
||||
Split(horizontal=horiz, ratio=ratio, left=l, right=rt) =>
|
||||
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 }
|
||||
walk(l, { x: r.x, y: r.y, w: lw, h: r.h }, gap, acc)
|
||||
walk(rt, { x: r.x + lw + gap, y: r.y, w: rw, h: r.h }, gap, acc)
|
||||
} else {
|
||||
let avail = r.h - gap
|
||||
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 }
|
||||
walk(l, { x: r.x, y: r.y, w: r.w, h: lh }, gap, acc)
|
||||
walk(rt, { x: r.x, y: r.y + lh + gap, w: r.w, h: rh }, gap, acc)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
walk(node, area, inner_gap, out)
|
||||
out
|
||||
}
|
||||
|
||||
///|
|
||||
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)
|
||||
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)
|
||||
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 },
|
||||
}
|
||||
win.geometry = clamped
|
||||
actions.push(SetGeometry(id, clamped))
|
||||
}
|
||||
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)
|
||||
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)
|
||||
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
|
||||
}
|
||||
|
||||
///|
|
||||
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
|
||||
}
|
||||
Reference in new issue
Block a user