///| The dock: pinned apps first, then (after a thin divider) apps that are running but not pinned, /// each drawn with its theme icon (or a coloured letter tile when the theme has none) and a dot /// under it per open window, up to three, lit when the app has focus. The icon under the pointer /// grows, its neighbours a little. A click starts an app that isn't running, brings a running /// one's newest window forward, and when the app already has focus steps to its next window. /// /// Pure: the component (components/dock) hands in the app catalog, the windows and an icon lookup. /// The shell runs the same `items` / `item_at` to draw the hover label and the per-app right-click /// menu (Pin, Move, Quit), so both always agree on which app is where. // ---------------------------------------------------------------- settings ///| One item of the right-click "Module settings" menu (the `module-config` record). An item /// with an empty label is kept by the shell but not listed (the pinned apps). #derive(Eq, Debug) pub(all) struct ConfigEntry { key : String label : String choices : Array[String] value : String } ///| Icon sizes offered in the dock's settings. pub let size_choices : Array[String] = ["Small", "Medium", "Large"] ///| The pinned apps when nobody has chosen yet: the first this many of the catalog (its default /// apps come first). The shell saves that choice the first time it sees the dock. pub let default_pin_count : Int = 5 ///| What the user picked for the dock. pub(all) struct DockConfig { /// Pinned app ids in dock order; None until chosen (then the catalog's first few). mut pinned : Array[String]? /// One of `size_choices`. mut size : String mut magnify : Bool /// Show apps that run but aren't pinned. mut running : Bool /// Draw apps that have windows as pills with a title, like a task list, when there is room. mut titles : Bool } ///| pub fn DockConfig::new() -> DockConfig { { pinned: None, size: "Medium", magnify: true, running: true, titles: true } } ///| A config built from saved key/value pairs (what the shell reads back from the module). pub fn DockConfig::from_pairs(pairs : Array[(String, String)]) -> DockConfig { let c = DockConfig::new() for p in pairs { ignore(c.set(p.0, p.1)) } c } ///| fn on_off(b : Bool) -> String { if b { "on" } else { "off" } } ///| The pinned list as the `pinned` setting's value: ids joined by ";", and ";" alone for an /// empty list (the empty string means "not chosen yet"). pub fn encode_pins(ids : Array[String]) -> String { if ids.length() == 0 { return ";" } let sb = StringBuilder::new() for i = 0; i < ids.length(); i = i + 1 { if i > 0 { sb.write_char(';') } sb.write_string(ids[i]) } sb.to_string() } ///| pub fn decode_pins(s : String) -> Array[String]? { if s.length() == 0 { return None } let out : Array[String] = [] for part in s.split(";") { let id = part.to_string() if id.length() > 0 && !out.contains(id) { out.push(id) } } Some(out) } ///| pub fn DockConfig::items(self : DockConfig) -> Array[ConfigEntry] { [ { key: "icon-size", label: "Icon size", choices: size_choices, value: self.size }, { key: "magnify", label: "Magnify icons under the pointer", choices: [], value: on_off(self.magnify) }, { key: "running", label: "Show running apps that aren't pinned", choices: [], value: on_off(self.running) }, { key: "titles", label: "Show window titles", choices: [], value: on_off(self.titles) }, { key: "pinned", label: "", choices: [], value: match self.pinned { Some(p) => encode_pins(p) None => "" }, }, ] } ///| Apply one setting; true when it changed something. Unknown keys and values are ignored. pub fn DockConfig::set(self : DockConfig, key : String, value : String) -> Bool { let flag = fn(cur : Bool, set : (Bool) -> Unit) -> Bool { match value { "on" if !cur => { set(true) true } "off" if cur => { set(false) true } _ => false } } match key { "icon-size" => if size_choices.contains(value) && value != self.size { self.size = value true } else { false } "magnify" => flag(self.magnify, b => self.magnify = b) "running" => flag(self.running, b => self.running = b) "titles" => flag(self.titles, b => self.titles = b) "pinned" => { let p = decode_pins(value) if p != self.pinned { self.pinned = p true } else { false } } _ => false } } ///| The pins in use: the chosen ones, or the catalog's first `default_pin_count`. pub fn DockConfig::pins(self : DockConfig, apps : Array[@types.AppEntry]) -> Array[String] { match self.pinned { Some(p) => p None => { let out : Array[String] = [] for a in apps { if out.length() < default_pin_count { out.push(a.id) } } out } } } ///| The module's settings (one module instance per wasm instance). pub let config : DockConfig = DockConfig::new() // ---------------------------------------------------------------- items ///| A window as the dock sees it. pub(all) struct DockWindow { id : UInt64 app_id : String focused : Bool title : String minimized : Bool } ///| One icon in the dock. #derive(Debug) pub(all) struct DockItem { /// The catalog id (desktop file id) when known, else the windows' app id. id : String name : String glyph : String pinned : Bool /// Its open windows, oldest first. windows : Array[UInt64] /// One of its windows has focus. focused : Bool /// The title of its newest window ("" without windows). title : String /// How many of its windows are minimized. minimized : Int } ///| The id among `ids` of the app whose windows carry `app_id`: the same id, the same ignoring /// case, or the id's last dotted part (`org.gnome.Nautilus` for windows of `nautilus`), or the /// window's last dotted part (`firefox` for windows of `org.mozilla.firefox`). pub fn match_app(app_id : String, ids : Array[String]) -> String? { if app_id.length() == 0 { return None } if ids.contains(app_id) { return Some(app_id) } let low = app_id.to_lower() for id in ids { if id.to_lower() == low { return Some(id) } } let tail = fn(s : String) -> String { let parts = s.split(".").to_array() parts[parts.length() - 1].to_string().to_lower() } for id in ids { if id.contains(".") && tail(id) == low { return Some(id) } } if app_id.contains(".") { let t = tail(app_id) for id in ids { if id.to_lower() == t { return Some(id) } } } None } ///| The catalog app whose name, without spaces and ignoring case, is `app_id` (windows of /// `xterm` belong to `debian-xterm.desktop`, named "XTerm"). pub fn match_name(app_id : String, apps : Array[@types.AppEntry]) -> String? { if app_id.length() == 0 { return None } let low = app_id.to_lower() for a in apps { if a.name.to_lower().replace_all(old=" ", new="") == low { return Some(a.id) } } None } ///| The dock id of the app whose windows carry `app_id`: a pinned app (`pins`), else a catalog /// app, by id or by name, else `app_id` itself. None for windows without an app id. The shell /// uses it to pin a window's app from the task list's menu. pub fn owner_of( app_id : String, pins : Array[String], apps : Array[@types.AppEntry], catalog_ids? : Array[String] = apps.map(a => a.id), ) -> String? { match match_app(app_id, pins) { Some(id) => Some(id) None => match match_app(app_id, catalog_ids) { Some(id) => Some(id) None => match match_name(app_id, apps) { Some(id) => Some(id) None => if app_id.length() > 0 { Some(app_id) } else { None } } } } } ///| The dock's icons: the pinned apps in their order, then the other running apps in the order /// they first opened a window (when `cfg.running`). With window titles on, the pinned apps that /// aren't running come first, so plain icons never sit after a task pill. Windows without an app id aren't shown. pub fn items( cfg : DockConfig, apps : Array[@types.AppEntry], wins : Array[DockWindow], ) -> Array[DockItem] { let by_id : Map[String, @types.AppEntry] = Map([]) for a in apps { by_id.set(a.id, a) } let catalog_ids = apps.map(a => a.id) let pins = cfg.pins(apps) let sorted = wins.copy() sorted.sort_by((a, b) => a.id.compare(b.id)) let out : Array[DockItem] = [] let index : Map[String, Int] = Map([]) let make = fn(id : String, pinned : Bool) -> DockItem { match by_id.get(id) { Some(a) => { id, name: a.name, glyph: a.glyph, pinned, windows: [], focused: false, title: "", minimized: 0 } None => { let parts = id.split(".").to_array() let name = parts[parts.length() - 1].to_string() { id, name, glyph: app_glyph(id, id), pinned, windows: [], focused: false, title: "", minimized: 0 } } } } for p in pins { if !index.contains(p) { index.set(p, out.length()) out.push(make(p, true)) } } for w in sorted { match owner_of(w.app_id, pins, apps, catalog_ids~) { None => () Some(id) => { let i = match index.get(id) { Some(i) => i None => { if !cfg.running { continue } index.set(id, out.length()) out.push(make(id, false)) out.length() - 1 } } let it = out[i] it.windows.push(w.id) out[i] = { ..it, focused: it.focused || w.focused, title: w.title, minimized: it.minimized + (if w.minimized { 1 } else { 0 }), } } } } if !cfg.titles { return out } // With window titles the running apps are task pills: the plain icons (pinned apps that // aren't running) come first, then every task, pinned ones in pin order, then the others. out.filter(it => it.windows.length() == 0) + out.filter(it => it.windows.length() > 0) } ///| Index of the first unpinned item (items.length() when all are pinned). /// With window titles on the divider sits between the plain icons and the tasks instead. fn first_running(items : Array[DockItem], cfg : DockConfig) -> Int { let mut i = 0 while i < items.length() && items[i].pinned && !(cfg.titles && items[i].windows.length() > 0) { i = i + 1 } i } // ---------------------------------------------------------------- geometry // // Icons sit in a row. With window titles on, an app that has windows gets a pill: its icon and a // title (its window's, or its name and window count), like the task list's pills. When the row // doesn't fit the pills shrink, down to `label_min`, then drop their titles. ///| Side padding of the whole dock, and the room between two icons. let pad : Int = 6 ///| let gap : Int = 6 ///| Width of the divider between pinned and running apps. let divider_w : Int = 11 ///| Rows of pixels kept free under the icons for the window dots. let dots_h : Int = 6 ///| Widest and narrowest a titled pill gets (narrower than that: just the icon). pub let label_max : Int = 150 ///| pub let label_min : Int = 84 ///| Icon edge for the chosen size in a dock `h` pixels tall. pub fn icon_size(cfg : DockConfig, h : Int) -> Int { let want = match cfg.size { "Small" => 20 "Large" => 28 _ => 24 } let most = h - dots_h - 2 let s = if want > most { most } else { want } if s < 12 { 12 } else { s } } ///| Width of every item's slot in a dock `w` wide (`w` < 0: as wide as they like). fn widths(items : Array[DockItem], cfg : DockConfig, w : Int, h : Int) -> Array[Int] { let base = icon_size(cfg, h) let titled = items.map(it => cfg.titles && it.windows.length() > 0) let n = items.length() let split = first_running(items, cfg) let fixed = 2 * pad + (if n > 1 { (n - 1) * gap } else { 0 }) + (if split > 0 && split < n { divider_w } else { 0 }) let mut nt = 0 for t in titled { if t { nt = nt + 1 } } let icons_w = (n - nt) * base let lw = if nt == 0 || w < 0 { label_max } else { let fit = (w - fixed - icons_w) / nt if fit > label_max { label_max } else { fit } } let lw = if lw < label_min { base } else { lw } titled.map(t => if t { lw } else { base }) } ///| Whether item `i` is drawn as a titled pill in a dock `w` wide (the shell then shows no /// hover label over it: the pill already says what it is). pub fn titled_at(items : Array[DockItem], cfg : DockConfig, w : Int, h : Int, i : Int) -> Bool { widths(items, cfg, w, h)[i] > icon_size(cfg, h) } ///| Left edge of item `i`'s slot. fn slot_x(items : Array[DockItem], cfg : DockConfig, w : Int, h : Int, i : Int) -> Int { let ws = widths(items, cfg, w, h) let split = first_running(items, cfg) let mut x = pad for j = 0; j < i; j = j + 1 { x = x + ws[j] + gap } if i >= split && split > 0 { x = x + divider_w } x } ///| Width that shows every item (titles at their widest). pub fn preferred_width(items : Array[DockItem], cfg : DockConfig, h : Int) -> Int { let n = items.length() if n == 0 { return empty_width } slot_x(items, cfg, -1, h, n - 1) + widths(items, cfg, -1, h)[n - 1] + pad } ///| Width of the empty dock: its "+" tile and hint. let empty_width : Int = 150 ///| How many items fit in a dock `w` wide. fn visible(items : Array[DockItem], cfg : DockConfig, w : Int, h : Int) -> Int { let ws = widths(items, cfg, w, h) let mut n = 0 while n < items.length() && slot_x(items, cfg, w, h, n) + ws[n] + pad <= w { n = n + 1 } n } ///| The width the row of items takes in a dock `w` wide (its backing is drawn that wide). fn used_width(items : Array[DockItem], cfg : DockConfig, w : Int, h : Int) -> Int { let n = visible(items, cfg, w, h) if n == 0 { return if items.length() == 0 { empty_width } else { 0 } } slot_x(items, cfg, w, h, n - 1) + widths(items, cfg, w, h)[n - 1] + pad } ///| The item under dock-local (x, y), or -1. The gap after an item belongs to it, so the pointer /// never drops off the dock between two icons. pub fn item_at(items : Array[DockItem], cfg : DockConfig, w : Int, h : Int, x : Int, y : Int) -> Int { if y < 0 || y >= h || x < 0 || x >= w { return -1 } let n = visible(items, cfg, w, h) let ws = widths(items, cfg, w, h) for i = 0; i < n; i = i + 1 { let x0 = slot_x(items, cfg, w, h, i) if x >= x0 - gap / 2 && x < x0 + ws[i] + gap - gap / 2 { return i } } -1 } ///| Centre x of item `i` in a dock `w` wide, dock-local (for the shell's hover label and menu). pub fn item_center(items : Array[DockItem], cfg : DockConfig, w : Int, h : Int, i : Int) -> Int { slot_x(items, cfg, w, h, i) + widths(items, cfg, w, h)[i] / 2 } ///| Edge of item `i`'s icon with `hover` the item under the pointer (-1: none): the hovered icon /// grows most and its neighbours half as much, when magnifying. pub fn item_size(cfg : DockConfig, i : Int, hover : Int, h : Int) -> Int { let base = icon_size(cfg, h) if !cfg.magnify || hover < 0 { return base } let room = h - dots_h - base let extra = if room > 8 { 8 } else if room < 0 { 0 } else { room } let d = if i > hover { i - hover } else { hover - i } if d == 0 { base + extra } else if d == 1 { base + extra / 2 } else { base } } // ---------------------------------------------------------------- letter tiles ///| Tile colours: fixed saturated hues picked from the app id so an app keeps its colour. let hues : Array[(Int, Int, Int)] = [ (0xE0, 0x5A, 0x4F), (0xE8, 0x92, 0x3A), (0xD9, 0xB5, 0x2E), (0x4F, 0xAE, 0x6B), (0x2F, 0xA8, 0xA0), (0x3D, 0x8B, 0xE0), (0x7C, 0x5C, 0xD6), (0xC4, 0x55, 0xA8), ] ///| fn hash(s : String) -> Int { let mut h = 5381 for c in s { h = (h * 33 + c.to_int()) & 0x7FFFFFF } h } ///| Colour of an app's tile. pub fn tile_color(id : String) -> @types.Color { let (r, g, b) = hues[hash(id) % hues.length()] @ui.color(r, g, b, 255) } ///| The letter on an app's icon tile when the app catalog's glyph isn't at hand (the task manager /// only knows a window's app id): the first letter of the id's last dotted part, so /// `org.gnome.Nautilus` -> "N" and `foot` -> "F". Falls back to `fallback`'s first letter, then "?". pub fn app_glyph(app_id : String, fallback : String) -> String { let parts = app_id.split(".").to_array() let mut i = parts.length() - 1 while i >= 0 { for c in parts[i] { if c.is_ascii_alphabetic() || c.is_ascii_digit() { return c.to_string().to_upper() } } i = i - 1 } for c in fallback { if c != ' ' { return c.to_string().to_upper() } } "?" } ///| An app's stand-in icon: a rounded tile `s` pixels square centred on (cx, cy) in the app's /// colour, with its glyph in white. Shared by the dock and the task manager so an app looks the /// same in both when its icon theme has nothing for it. pub fn app_icon( cx : Int, cy : Int, s : Int, id : String, glyph : String, hover~ : Bool = false, ) -> Array[@types.DrawOp] { let white = @ui.color(255, 255, 255, 255) let c = tile_color(id) let fill = if hover { @ui.lerp_color(c, white, 1, 5) } else { c } [ @ui.fill_rect(@ui.rect(cx - s / 2, cy - s / 2, s, s), fill, radius=s / 4), @ui.text(cx, cy, glyph, s * 11 / 20, white, align=CENTER, bold=true), ] } // ---------------------------------------------------------------- drawing ///| What the empty dock shows: a "+" tile and a hint. A click on it opens the Applications menu /// (the shell does that: modules can't open each other), whose right-click menu pins apps. fn render_empty(t : @theme.Tokens, cfg : DockConfig, h : Int, hot : Bool) -> Array[@types.DrawOp] { let s = icon_size(cfg, h) let x = pad let y = (h - s) / 2 let fg = if hot { t.palette.foreground } else { t.palette.foreground_muted } let r = t.radius.reinterpret_as_int() let cx = x + s / 2 let cy = y + s / 2 let arm = s / 4 [ @ui.fill_rect(@ui.rect(x, y, s, s), @ui.with_alpha(t.palette.foreground, if hot { 40 } else { 18 }), radius=r), @ui.stroke_rect(@ui.rect(x, y, s, s), fg, radius=r), @ui.line(cx - arm, cy, cx + arm, cy, fg, width=2), @ui.line(cx, cy - arm, cx, cy + arm, fg, width=2), @ui.text(x + s + 8, h / 2, "Add apps", t.font_size.reinterpret_as_int() - 2, fg), ] } ///| Draw the dock into `size`. `hover` is the item under the pointer or -1; `icon` gives an app /// id's theme icon handle (0: none, a letter tile is drawn). pub fn render( size : @types.Size, t : @theme.Tokens, cfg : DockConfig, items : Array[DockItem], hover : Int, icon : (String) -> UInt, ) -> Array[@types.DrawOp] { let w = size.w.reinterpret_as_int() let h = size.h.reinterpret_as_int() let r = t.radius.reinterpret_as_int() // The backing hugs the items, so the rest of the panel stays clear when the dock stretches. let bw = used_width(items, cfg, w, h) let ops : Array[@types.DrawOp] = [ @ui.fill_rect(@ui.rect(0, 0, if bw < w { bw } else { w }, h), @ui.with_alpha(t.palette.surface_hover, 150), radius=r + 4), ] if items.length() == 0 { ops.append(render_empty(t, cfg, h, hover >= 0)) return ops } let n = visible(items, cfg, w, h) let ws = widths(items, cfg, w, h) let base = icon_size(cfg, h) let cy = (h - dots_h) / 2 + 2 let fs = t.font_size.reinterpret_as_int() - 3 let split = first_running(items, cfg) if split > 0 && split < n { let x = slot_x(items, cfg, w, h, split) - gap / 2 - divider_w / 2 - 1 ops.push(@ui.fill_rect(@ui.rect(x, 6, 1, h - 12), t.palette.border)) } for i = 0; i < n; i = i + 1 { let it = items[i] let x0 = slot_x(items, cfg, w, h, i) let titled = ws[i] > base let all_min = it.windows.length() > 0 && it.minimized == it.windows.length() let mut cx = x0 + base / 2 let mut icy = cy let mut s = base if titled { // A pill like the task list's: lit when the app has focus, dim when all its windows are // minimized. The icon doesn't grow in a pill. let bg = if it.focused { @ui.with_alpha(t.palette.accent, 90) } else if i == hover { @ui.with_alpha(t.palette.foreground, 36) } else { @ui.with_alpha(t.palette.foreground, 14) } ops.push(@ui.fill_rect(@ui.rect(x0, 3, ws[i], h - 6), bg, radius=r)) cx = x0 + 6 + base / 2 icy = h / 2 let s2 = h - 14 s = if s2 < base { s2 } else { base } let tx = cx + s / 2 + 6 let col = if all_min { t.palette.foreground_muted } else { t.palette.foreground } ops.push(@ui.text(tx, h / 2, fit_text(item_title(it), fs, x0 + ws[i] - 6 - tx), fs, col)) } else { s = item_size(cfg, i, hover, h) // Grow upwards from the icons' common baseline so the row doesn't wobble. icy = cy + base / 2 - s / 2 if i == hover { ops.push( @ui.fill_rect(@ui.rect(cx - s / 2 - 3, icy - s / 2 - 3, s + 6, s + 6), @ui.with_alpha(t.palette.foreground, 36), radius=r), ) } } let handle = icon(it.id) if handle != 0U { ops.push(Image({ rect: @ui.rect(cx - s / 2, icy - s / 2, s, s), image: handle })) } else { ops.append(app_icon(cx, icy, s, it.id, it.glyph, hover=i == hover)) } // One dot per window, up to three, under the icon; lit when the app has focus. let dots = if it.windows.length() > 3 { 3 } else { it.windows.length() } let col = if it.focused { t.palette.accent } else { t.palette.foreground_muted } let dw = if it.focused && dots == 1 { 10 } else { 4 } let total = dots * dw + (dots - 1) * 3 for d = 0; d < dots; d = d + 1 { let dx = cx - total / 2 + d * (dw + 3) ops.push(@ui.fill_rect(@ui.rect(dx, h - 5, dw, 3), col, radius=1)) } } ops } ///| The title on an app's pill: its one window's title, else its name and how many windows. pub fn item_title(it : DockItem) -> String { if it.windows.length() == 1 && it.title.length() > 0 { it.title } else if it.windows.length() > 1 { it.name + " (" + it.windows.length().to_string() + ")" } else { it.name } } ///| `s` shortened with an ellipsis until it fits `max_w` pixels (at about 0.55em a character). fn fit_text(s : String, size : Int, max_w : Int) -> String { let cw = (size * 11 + 19) / 20 if s.length() * cw <= max_w { return s } let mut n = s.length() while n > 1 && (n + 1) * cw > max_w { n = n - 1 } s[:n].to_string() + "…" } // ---------------------------------------------------------------- input ///| What a pointer event asks the host to do. #derive(Eq, Debug) pub(all) enum DockAction { Nothing /// Start the app with this catalog id. Launch(String) /// Bring this window forward (restoring it if minimized). Focus(UInt64) } ///| The window a click on `it` brings forward: the newest when the app isn't focused, the next /// one after the focused window when it is (so repeated clicks walk through its windows). None /// when there is nothing to do (one window, already focused). pub fn click_target(it : DockItem, focused_window : UInt64?) -> UInt64? { let n = it.windows.length() if n == 0 { return None } match focused_window { Some(f) if it.focused => { let mut at = -1 for i = 0; i < n; i = i + 1 { if it.windows[i] == f { at = i } } if n == 1 { None } else { Some(it.windows[(at + 1) % n]) } } _ => Some(it.windows[n - 1]) } } ///| What clicking `it` (or its Super+number shortcut) does: start it when it has no windows, /// else bring its newest window forward, or step to its next one when it already has focus. pub fn activate(it : DockItem, focused_window : UInt64?) -> DockAction { if it.windows.length() == 0 { Launch(it.id) } else { match click_target(it, focused_window) { Some(win) => Focus(win) None => Nothing } } } ///| Pointer events -> (new hover, redraw?, action). Pure so it can be tested. `focused_window` /// is the window with keyboard focus, if any. pub fn handle( items : Array[DockItem], cfg : DockConfig, hover : Int, w : Int, h : Int, focused_window : UInt64?, event : @types.InputEvent, ) -> (Int, Bool, DockAction) { match event { PointerMove(p) => { // The empty dock is one target, its "+" tile (hover 0). let i = if items.length() == 0 { 0 } else { item_at(items, cfg, w, h, p.x, p.y) } (i, i != hover, Nothing) } PointerLeave => (-1, hover != -1, Nothing) PointerDown(p) => { let i = item_at(items, cfg, w, h, p.x, p.y) if i < 0 { return (hover, false, Nothing) } match activate(items[i], focused_window) { Nothing => (i, false, Nothing) a => (i, true, a) } } _ => (hover, false, Nothing) } } // ---------------------------------------------------------------- pins ///| `pins` with `id` added at the end, or taken out when it is there. pub fn toggle_pin(pins : Array[String], id : String) -> Array[String] { if pins.contains(id) { pins.filter(p => p != id) } else { let out = pins.copy() out.push(id) out } } ///| `pins` with `id` moved `delta` places (-1 left, 1 right), clamped to the ends. pub fn move_pin(pins : Array[String], id : String, delta : Int) -> Array[String] { let out = pins.copy() let mut at = -1 for i = 0; i < out.length(); i = i + 1 { if out[i] == id { at = i } } let to = at + delta if at < 0 || to < 0 || to >= out.length() { return out } out[at] = out[to] out[to] = id out }