Files
chandra/compositor/shell/dock.mbt
T
Claude 01bd8281d5
CI / test (push) Failing after 2m33s
Dock: plain icons always come before the task pills
With window titles on, a pinned app that was running drew as a pill in its
pin slot, so idle pinned icons ended up between pills. Now the dock shows
the pinned apps that aren't running first, then every task: running pinned
apps in pin order, then the other running apps. The divider sits between
the two groups. Squeezing the pills to icons keeps the order; with titles
off the plain pinned order stays. Move left / right in the menu follow the
pin list, not the shown position.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DPL5hiPCFHRxaJQvwex6ry
2026-11-26 01:53:43 +00:00

480 lines
16 KiB
MoonBit

///| The shell's half of the dock (components/dock, logic in lib/dock). The module draws the icons
/// and handles clicks; the shell, which sees right-clicks and can draw outside the panel, adds:
/// - the per-app right-click menu: Open / New window, Pin to dock / Unpin, Move left / right,
/// Quit (every window of the app) and Dock settings;
/// - the app's name in a label above the icon under the pointer;
/// - the dock's width as its apps come and go (a module's preferred width is read once);
/// - the icons of pinned apps that aren't running (the icons table otherwise has windows' apps);
/// - the first pinned list: until one is saved the dock pins the catalog's first apps, and the
/// shell saves that choice once so later launches (which reorder the catalog) don't move them.
/// The pinned list is the dock's hidden `pinned` setting, kept in state.edn's :module-config
/// like any module setting. Both sides call lib/dock's `items` / `item_at`, so they agree on
/// which app is where.
///| The dock module's id.
let dock_module_id : String = "org.chandra.dock"
///|
fn Widget::is_dock(self : Widget) -> Bool {
self.pl.id == dock_module_id
}
///| The dock's settings as the module holds them now.
fn dock_config(w : Widget) -> @dock.DockConfig {
@dock.DockConfig::from_pairs(w.pl.config_items().map(it => (it.key, it.value)))
}
///| The windows as the dock sees them.
fn DesktopShell::dock_windows(self : DesktopShell) -> Array[@dock.DockWindow] {
self.win_list.map(e => {
id: e.id,
app_id: e.app_id,
focused: e.focused,
title: e.title,
minimized: self.win_info(e.id).map(i => i.minimized).unwrap_or(false),
})
}
///| The window list entry (minimized, maximized) of window `id`.
fn DesktopShell::win_info(self : DesktopShell, id : UInt64) -> WinInfo? {
for w in self.windows {
if w.id == id {
return Some(w)
}
}
None
}
///| The dock in `w`'s settings and icons, as the module computes them.
fn DesktopShell::dock_state(self : DesktopShell, w : Widget) -> (@dock.DockConfig, Array[@dock.DockItem]) {
let cfg = dock_config(w)
(cfg, @dock.items(cfg, self.apps, self.dock_windows()))
}
///| Height the dock is drawn at (before its first layout, the height it asked for).
fn dock_height(w : Widget) -> Int {
if w.rect.h > 0 {
w.rect.h
} else {
w.pl.pref_h
}
}
///| The width the dock in `w` needs for its icons now, or None when `w` isn't the dock.
fn DesktopShell::dock_width(self : DesktopShell, w : Widget) -> Int? {
if !w.is_dock() {
return None
}
let (cfg, items) = self.dock_state(w)
Some(@dock.preferred_width(items, cfg, dock_height(w)))
}
///| The dock's icons and the index of the one under (x, y), when `w` is the dock and one is.
fn DesktopShell::dock_item_at(
self : DesktopShell,
w : Widget,
x : Int,
y : Int,
) -> (Array[@dock.DockItem], Int)? {
if !w.is_dock() || !inside(w.rect, x, y) {
return None
}
let (cfg, items) = self.dock_state(w)
let i = @dock.item_at(items, cfg, w.rect.w, w.rect.h, x - w.rect.x, y - w.rect.y)
if i >= 0 {
Some((items, i))
} else {
None
}
}
///| App ids of every dock's icons (for the icons table).
fn DesktopShell::dock_app_ids(self : DesktopShell) -> Array[String] {
let out : Array[String] = []
for w in self.widgets {
if w.is_dock() {
let (_, items) = self.dock_state(w)
for it in items {
out.push(it.id)
}
}
}
out
}
///| The dock's first pins: the default web browser, file manager, terminal and text editor (as
/// Settings > Apps has them, `defaults` being each category's current app id or "") that are
/// installed, topped up from the catalog's first apps when that gives fewer
/// than three. Only apps in `apps` (the installed ones) are pinned.
fn default_dock_pins(defaults : Array[String], apps : Array[@types.AppEntry]) -> Array[String] {
let installed = apps.map(a => a.id)
let out : Array[String] = []
let add = fn(id : String) {
if id.length() > 0 && installed.contains(id) && !out.contains(id) && out.length() < @dock.default_pin_count {
out.push(id)
}
}
for cat in ["browser", "files", "terminal", "editor"] {
for i, c in app_categories {
if c.id == cat && i < defaults.length() {
add(defaults[i])
}
}
}
if out.length() < 3 {
for a in apps {
add(a.id)
}
}
out
}
///|
test "the dock's first pins are the installed default apps" {
let app = fn(id : String, cat : String) -> @types.AppEntry {
{ id, name: id, comment: "", category: cat, glyph: "X" }
}
let apps = [
app("org.kde.kate", "Utility"), app("org.kde.konsole", "System"), app("org.kde.dolphin", "System"),
app("firefox", "Network"), app("systemsettings", "Settings"), app("org.kde.kwrite", "Utility"),
]
// browser, email, files, terminal, editor, ...; the email client isn't pinned, a missing editor
// is skipped.
let defaults = ["firefox", "thunderbird", "org.kde.dolphin", "org.kde.konsole", "gone-editor"]
assert_eq(default_dock_pins(defaults, apps), ["firefox", "org.kde.dolphin", "org.kde.konsole"])
// Nothing configured: the catalog's first apps.
assert_eq(default_dock_pins([], apps[:2].to_array()), ["org.kde.kate", "org.kde.konsole"])
}
///| Save the dock's first pinned list the first time it is seen without one (see the top).
fn DesktopShell::settle_dock_pins(self : DesktopShell) -> Unit {
if self.apps.length() == 0 {
return
}
for w in self.widgets.copy() {
if w.is_dock() {
let cfg = dock_config(w)
if cfg.pinned is None {
let defaults = self.default_app_choices().map(c => c.current_id)
self.set_module_setting(w.slot, "pinned", @dock.encode_pins(default_dock_pins(defaults, self.apps)))
}
}
}
}
///| A dock menu row: `kind` 7 starts the app, 8 pins or unpins it, 9 moves its pin by `value`
/// places, 10 closes its windows. `arg` is the app id.
fn dock_row(label : String, kind : Int, id : String, value? : String = "") -> MenuRow {
{ ..menu_row(label, kind, arg=id), value, }
}
///| The right-click menu of dock icon `i`, titled with the app's name.
fn DesktopShell::open_dock_menu(
self : DesktopShell,
w : Widget,
items : Array[@dock.DockItem],
i : Int,
x : Int,
) -> Unit {
let it = items[i]
let rows : Array[MenuRow] = []
let n = it.windows.length()
if self.command_for(it.id) is Some(_) {
rows.push(dock_row(if n == 0 { "Open" } else { "New window" }, 7, it.id))
}
if it.pinned {
rows.push(dock_row("Unpin from dock", 8, it.id))
// Its place among the pins (the dock may show running pins after the idle ones).
let pins = dock_config(w).pins(self.apps)
let at = pins.search(it.id).unwrap_or(i)
if at > 0 {
rows.push(dock_row("Move left", 9, it.id, value="-1"))
}
if at < pins.length() - 1 {
rows.push(dock_row("Move right", 9, it.id, value="1"))
}
} else {
rows.push(dock_row("Pin to dock", 8, it.id))
}
if n == 1 {
// The window's own commands, as the task list had them.
match self.win_info(it.windows[0]) {
Some(win) => {
rows.push(
if win.minimized {
window_row("Restore", win.id, Restore)
} else {
window_row("Minimize", win.id, Minimize)
},
)
rows.push(window_row(if win.maximized { "Restore size" } else { "Maximize" }, win.id, ToggleMaximize))
}
None => ()
}
} else if n > 1 {
// Its windows by title, so one can be picked straight away (a click cycles through them).
rows.push(menu_row("Windows", 5))
for win in it.windows {
let min = self.win_info(win).map(i => i.minimized).unwrap_or(false)
let label = self.window_title(win) + (if min { " (minimized)" } else { "" })
rows.push({ ..menu_row(label, 6), window_id: win, window_cmd: Restore })
}
}
if n == 1 {
rows.push(dock_row("Quit", 10, it.id))
} else if n > 1 {
rows.push(dock_row("Close " + n.to_string() + " windows", 10, it.id))
}
rows.push(menu_row("Dock settings...", 3))
self.open_menu(it.name, rows, w.slot, x)
}
///| A dock menu row was chosen for the dock in `slot`. "Pin to dock" from the Applications menu
/// when there is no dock yet (`slot` "") puts one in the panel first.
fn DesktopShell::dock_menu_choice(self : DesktopShell, slot : String, row : MenuRow) -> Unit {
let dock = match self.widget_by_slot(slot) {
Some(w) => Some(w)
None if row.kind == 8 => self.add_dock()
None => None
}
if row.kind == 7 {
self.launch_catalog_app(row.arg)
return
}
match dock {
None => ()
Some(w) => {
let slot = w.slot
let (cfg, items) = self.dock_state(w)
let pins = cfg.pins(self.apps)
match row.kind {
8 => self.set_module_setting(slot, "pinned", @dock.encode_pins(@dock.toggle_pin(pins, row.arg)))
9 => {
let delta = if row.value == "-1" { -1 } else { 1 }
self.set_module_setting(slot, "pinned", @dock.encode_pins(@dock.move_pin(pins, row.arg, delta)))
}
10 =>
for it in items {
if it.id == row.arg {
for win in it.windows {
self.actions.push(Window(win, Close))
}
}
}
_ => ()
}
}
}
}
///| The name of the dock icon under the pointer and that icon's centre x on screen, if any.
fn DesktopShell::dock_label(self : DesktopShell) -> (String, Int)? {
match self.widget_by_slot(self.hover_slot) {
Some(w) if w.is_dock() =>
match self.dock_item_at(w, self.ptr_x, self.ptr_y) {
Some((items, i)) => {
let cfg = dock_config(w)
if @dock.titled_at(items, cfg, w.rect.w, w.rect.h, i) {
return None
}
Some((items[i].name, w.rect.x + @dock.item_center(items, cfg, w.rect.w, w.rect.h, i)))
}
None => None
}
_ => None
}
}
///| The name the dock's hover label shows now ("" when none shows).
pub fn DesktopShell::dock_hover_name(self : DesktopShell) -> String {
if self.menu_open || self.drag_active {
return ""
}
match self.dock_label() {
Some((name, _)) => name
None => ""
}
}
///| The name of the dock icon under the pointer, in a label above it.
fn DesktopShell::render_dock_label(self : DesktopShell, tokens : @theme.Tokens, out : Array[@scene.DrawOp]) -> Unit {
match self.dock_label() {
Some((name, cx)) => {
let fs = tokens.font_size.reinterpret_as_int() - 2
let lw = @ui.text_width(name, fs) + 20
let lh = 24
let lx = clamp_menu(cx - lw / 2, 4, self.screen_w - lw - 4)
let ly = self.panel_rect.y - lh - 6
let r = tokens.radius.reinterpret_as_int()
translate(
[
@ui.fill_rect(@ui.rect(0, 0, lw, lh), tokens.palette.surface_popup, radius=r),
@ui.stroke_rect(@ui.rect(0, 0, lw, lh), tokens.palette.border, radius=r),
@ui.text(lw / 2, lh / 2, name, fs, tokens.palette.foreground, align=CENTER),
],
lx,
ly,
out,
)
}
None => ()
}
}
// ---------------------------------------------------------------- pinning from elsewhere
///| The first dock in the panel, if any.
fn DesktopShell::dock_widget(self : DesktopShell) -> Widget? {
for w in self.widgets {
if w.is_dock() {
return Some(w)
}
}
None
}
///| A window's title ("Window" when it has none).
fn DesktopShell::window_title(self : DesktopShell, id : UInt64) -> String {
for e in self.win_list {
if e.id == id && e.title.length() > 0 {
return e.title
}
}
"Window"
}
///| Put a dock into the panel, just after the Applications button (or first), with its
/// first pins settled. None when the dock module can't be found or loaded.
fn DesktopShell::add_dock(self : DesktopShell) -> Widget? {
self.name_catalog()
match self.find_module("dock") {
None => {
self.say("Dock module not found (build it with scripts/build-modules.sh)")
None
}
Some(c) =>
match self.load_path(c.path) {
None => None
Some(w) => {
let mut at = 0
for i = 0; i < self.widgets.length(); i = i + 1 {
if self.widgets[i].module_id == launcher_module {
at = i + 1
}
}
self.widgets.retain(x => x.slot != w.slot)
self.widgets.insert(at, w)
self.settle_dock_pins()
self.save_panel_config()
Some(w)
}
}
}
}
///| "Pin to dock" or "Unpin from dock" for app `id` (a catalog or window app id) as a row of
/// another menu, and the dock's slot that row acts on ("" when there is no dock yet: pinning
/// then adds one).
fn DesktopShell::pin_row(self : DesktopShell, id : String) -> (MenuRow, String) {
match self.dock_widget() {
Some(w) => {
let pinned = dock_config(w).pins(self.apps).contains(id)
(dock_row(if pinned { "Unpin from dock" } else { "Pin to dock" }, 8, id), w.slot)
}
None => (dock_row("Pin to dock", 8, id), "")
}
}
///| The right-click menu of an app in the Applications menu: Open, and Pin to dock / Unpin.
fn DesktopShell::open_app_menu(self : DesktopShell, app : @types.AppEntry, x : Int) -> Unit {
let (pin, slot) = self.pin_row(app.id)
self.open_menu(app.name, [dock_row("Open", 7, app.id), pin], slot, x)
}
///| The app under (x, y) in the Applications menu's popup, if it is open there.
fn DesktopShell::launcher_app_at(self : DesktopShell, x : Int, y : Int) -> @types.AppEntry? {
match self.popup_at(x, y) {
Some(w) if w.module_id == launcher_module => {
let shown = @launcher.filter(self.apps, self.launcher_state.query)
let i = @launcher.row_at(self.apps, self.launcher_state, { x: x - w.popup_rect.x, y: y - w.popup_rect.y })
if i >= 0 && i < shown.length() {
Some(shown[i])
} else {
None
}
}
_ => None
}
}
///| Follow an event the launcher module was handed, so the shell knows what its popup lists
/// (its search text) for the right-click menu. The module's own state stays the truth for
/// whether it is open.
fn DesktopShell::follow_launcher(self : DesktopShell, w : Widget, popup : Bool, ev : @wasm.PlasmoidEvent) -> Unit {
let st = self.launcher_state
let e : @types.InputEvent = match ev {
PointerMove(x, y) => PointerMove({ x, y })
PointerDown(x, y) => PointerDown({ x, y })
PointerUp(x, y) => PointerUp({ x, y })
PointerLeave => PointerLeave
FocusLost => FocusLost
KeyDown(k) => KeyDown(k)
}
ignore(@launcher.handle_event(self.apps, st, if popup { Popup } else { Widget }, e, _ => Ok(())))
if w.popup_rect.w == 0 {
@launcher.close(st)
} else if !st.open {
// Open in the module (its launch failed and kept it open): open here too.
ignore(@launcher.handle_event(self.apps, st, Widget, PointerDown({ x: 0, y: 0 }), _ => Ok(())))
}
}
///| Super+`n` (1-based): what a click on the dock's `n`-th icon does, start the app or bring its
/// windows forward. False when there is no dock or no such icon.
pub fn DesktopShell::open_dock_item(self : DesktopShell, n : Int) -> Bool {
self.ensure_booted()
match self.dock_widget() {
None => false
Some(w) => {
let (_, items) = self.dock_state(w)
if n < 1 || n > items.length() {
return false
}
let mut focused : UInt64? = None
for e in self.win_list {
if e.focused {
focused = Some(e.id)
}
}
match @dock.activate(items[n - 1], focused) {
Launch(id) => self.launch_catalog_app(id)
Focus(win) => self.actions.push(FocusWindow(win))
Nothing => ()
}
true
}
}
}
///| A left click on the dock in `w` at (x, y): the empty dock's "+" opens the Applications menu,
/// where right-clicking an app pins it. True when the shell took the click.
fn DesktopShell::dock_click(self : DesktopShell, w : Widget) -> Bool {
if !w.is_dock() {
return false
}
let (_, items) = self.dock_state(w)
if items.length() > 0 {
return false
}
self.toggle_launcher()
true
}
///| The app ids of the first dock's items, in the order it shows them.
pub fn DesktopShell::dock_item_ids(self : DesktopShell) -> Array[String] {
match self.dock_widget() {
Some(w) => self.dock_state(w).1.map(it => it.id)
None => []
}
}