///| The Gamerig Library desktop: gamerig's library window (desktop/ui, the WebKit page) drawn as /// Chandra draw ops. The layout follows it: a top bar with the Gamerig mark, the title and the /// search box; a sidebar with the categories and their counts, the running games and the rig's /// status; and a grid of cards, featured games two columns wide, each with its icon, name, /// category and status badges, description, install hint and buttons (Launch, the extra modes such /// as Big Picture; Switch to and Stop while it runs). /// /// Input: a click on a button acts on its card; a click on a card selects it. Typing filters /// (while the desktop has the keyboard), Backspace and Escape edit the search, arrows move the /// selection, Enter launches it (or switches to it when it runs), the wheel scrolls the grid. /// /// Pure: the component hands in the data, the size, the theme and the events, and carries out the /// `Action` this returns. ///| What the module asks of the host after an event. #derive(Eq, Debug) pub(all) enum Action { NoAction Launch(String, String) Stop(String) Focus(String) Refresh } ///| What is under the pointer. #derive(Eq, Debug) pub(all) enum Hit { NoHit Cat(Category) Search ClearSearch RefreshButton Card(String) Button(String, String) StopButton(String) FocusButton(String) RunningRow(String) RunningStop(String) } ///| One item of the module's right-click settings (the `module-config` record). #derive(Eq, Debug) pub(all) struct ConfigEntry { key : String label : String choices : Array[String] value : String } ///| pub let colors_choices : Array[String] = ["Follow desktop", "Dark", "Light"] ///| Behind the library: the desktop's wallpaper through glassy panels, or gamerig's solid colour. pub let background_choices : Array[String] = ["Wallpaper", "Solid"] ///| Gamerig's colours. pub(all) struct Pal { bg : @types.Color panel : @types.Color card : @types.Color card_hover : @types.Color row_sel : @types.Color border : @types.Color fg : @types.Color muted : @types.Color faint : @types.Color accent : @types.Color accent_fg : @types.Color ok : @types.Color ok_bg : @types.Color badge_bg : @types.Color badge_fg : @types.Color warn : @types.Color dark : Bool } ///| fn c(r : Int, g : Int, b : Int) -> @types.Color { @ui.color(r, g, b, 255) } ///| pub fn Pal::dark() -> Pal { { bg: c(24, 26, 35), panel: c(33, 35, 47), card: c(41, 43, 58), card_hover: c(48, 51, 68), row_sel: c(49, 52, 70), border: c(52, 55, 74), fg: c(234, 235, 241), muted: c(156, 159, 178), faint: c(118, 121, 140), accent: c(246, 183, 46), accent_fg: c(27, 29, 38), ok: c(74, 222, 128), ok_bg: c(30, 58, 46), badge_bg: c(54, 57, 75), badge_fg: c(188, 191, 206), warn: c(251, 146, 60), dark: true, } } ///| pub fn Pal::light() -> Pal { { bg: c(236, 238, 244), panel: c(247, 248, 251), card: c(255, 255, 255), card_hover: c(244, 246, 251), row_sel: c(228, 231, 240), border: c(221, 224, 233), fg: c(29, 32, 46), muted: c(103, 108, 126), faint: c(140, 145, 160), accent: c(246, 183, 46), accent_fg: c(27, 29, 38), ok: c(21, 128, 61), ok_bg: c(220, 252, 231), badge_bg: c(236, 238, 243), badge_fg: c(75, 80, 96), warn: c(194, 65, 12), dark: false, } } ///| The library's state between calls. pub(all) struct Library { mut category : Category mut query : String /// Pixels the grid is scrolled down. mut scroll : Int mut hover : Hit mut press : Hit /// The selected card (keyboard), by game id; "" none. mut selected : String mut focused : Bool mut colors : String mut background : String /// From the last render: where things are, the cards in order with their columns, and the /// grid's height and viewport (for the wheel). regions : Array[(@types.Rect, Hit)] mut shown : Array[(String, Int, Int)] mut content_h : Int mut view_h : Int mut games : Array[Game] } ///| pub fn Library::new() -> Library { { category: All, query: "", scroll: 0, hover: NoHit, press: NoHit, selected: "", focused: false, colors: "Follow desktop", background: "Wallpaper", regions: [], shown: [], content_h: 0, view_h: 0, games: [], } } ///| The one instance the component drives. pub let state : Library = Library::new() ///| Text sizes. let fs : Int = 14 ///| let fs_small : Int = 12 ///| let top_h : Int = 56 ///| let card_min_w : Int = 280 ///| let gap : Int = 16 ///| Roughly how wide `s` is in the desktop's proportional UI font: about 0.53 em a character /// (the 0.6 em of `@ui.text_width` is the monospace upper bound and leaves lines half empty). pub fn approx_width(s : String, size : Int) -> Int { s.length() * size * 53 / 100 } ///| Narrower than `w` at `size`? fn fits(s : String, w : Int, size : Int) -> Bool { approx_width(s, size) <= w } ///| `s` cut to `w` pixels with an ellipsis. pub fn ellipsize(s : String, w : Int, size : Int) -> String { if fits(s, w, size) { return s } let chars = s.to_array() let mut n = chars.length() while n > 0 && !fits(String::from_array(chars[:n].to_owned()) + "…", w, size) { n = n - 1 } String::from_array(chars[:n].to_owned()).trim_end().to_owned() + "…" } ///| `s` broken into lines of at most `w` pixels, at most `max` lines (the last one ellipsized). pub fn wrap(s : String, w : Int, size : Int, max : Int) -> Array[String] { let out : Array[String] = [] let mut line = "" let words = s.split(" ").map(x => x.to_owned()).filter(x => x != "").collect() let mut i = 0 while i < words.length() { let word = words[i] let cand = if line == "" { word } else { line + " " + word } if fits(cand, w, size) || line == "" { line = cand i = i + 1 } else { out.push(line) line = "" if out.length() == max { break } } } if line != "" && out.length() < max { out.push(line) } else if out.length() == max && i < words.length() { // More left: say so on the last line. out[max - 1] = ellipsize(out[max - 1] + " " + words[i], w, size) } out.map(l => ellipsize(l, w, size)) } ///| The palette for the colours setting and the desktop theme. fn Library::pal(self : Library, tokens : @theme.Tokens) -> Pal { let p = self.solid_pal(tokens) if self.background == "Solid" { return p } // Over the wallpaper: the panels and cards let it show through, the wallpaper itself is the // background. let a = if p.dark { 0 } else { 30 } { ..p, bg: @ui.with_alpha(p.bg, 0), panel: @ui.with_alpha(p.panel, 170 + a), card: @ui.with_alpha(p.card, 215 + a / 2), card_hover: @ui.with_alpha(p.card_hover, 235), row_sel: @ui.with_alpha(p.row_sel, 210), } } ///| fn Library::solid_pal(self : Library, tokens : @theme.Tokens) -> Pal { match self.colors { "Dark" => Pal::dark() "Light" => Pal::light() _ => { let f = tokens.palette.foreground if f.r.to_int() + f.g.to_int() + f.b.to_int() > 384 { Pal::dark() } else { Pal::light() } } } } ///| (background, glyph colour) of an icon tile, after gamerig's tinted tiles. fn tile_colors(icon : String, p : Pal) -> (@types.Color, @types.Color) { let (r, g, b) = match icon { "Play" => (251, 113, 133) "Sparkles" | "Star" | "ExternalLink" => (167, 139, 250) "Settings" => (96, 165, 250) "Folder" | "Square" | "Terminal" => (251, 146, 60) _ => (45, 212, 191) } if p.dark { (@ui.color(r / 4 + 22, g / 4 + 22, b / 4 + 30, 255), c(r, g, b)) } else { (@ui.color(255 - (255 - r) / 6, 255 - (255 - g) / 6, 255 - (255 - b) / 6, 255), @ui.color(r * 3 / 4, g * 3 / 4, b * 3 / 4, 255)) } } ///| A line-drawn icon in the square at (x, y) of side `s`, after the lucide icons gamerig uses. pub fn draw_glyph(ops : Array[@types.DrawOp], name : String, x : Int, y : Int, s : Int, col : @types.Color, letter~ : String = "") -> Unit { let w = if s >= 24 { 2 } else { 1 } let l = (x0, y0, x1, y1) => ops.push(@ui.line(x + x0 * s / 24, y + y0 * s / 24, x + x1 * s / 24, y + y1 * s / 24, col, width=w)) match name { "Play" => { l(7, 4, 7, 20) l(7, 4, 19, 12) l(7, 20, 19, 12) } "Sparkles" => { l(10, 3, 10, 17) l(3, 10, 17, 10) l(10, 3, 12, 8) l(12, 8, 17, 10) l(17, 10, 12, 12) l(12, 12, 10, 17) l(10, 17, 8, 12) l(8, 12, 3, 10) l(3, 10, 8, 8) l(8, 8, 10, 3) l(19, 3, 19, 7) l(17, 5, 21, 5) l(5, 18, 5, 21) } "Folder" => { l(3, 6, 3, 19) l(3, 19, 21, 19) l(21, 19, 21, 8) l(21, 8, 12, 8) l(12, 8, 10, 5) l(10, 5, 3, 5) l(3, 5, 3, 6) } "Star" => { let pts = [(12, 2), (14, 9), (22, 9), (16, 14), (18, 21), (12, 17), (6, 21), (8, 14), (2, 9), (10, 9)] for i in 0.. ops.push(@ui.stroke_rect(@ui.rect(x + s * 4 / 24, y + s * 4 / 24, s * 16 / 24, s * 16 / 24), col, width=w, radius=s / 10)) "Terminal" => { l(4, 7, 10, 12) l(10, 12, 4, 17) l(12, 18, 20, 18) } "Settings" => { ops.push(@ui.circle(x + s / 2, y + s / 2, s * 8 / 24, col, filled=false)) ops.push(@ui.circle(x + s / 2, y + s / 2, s * 3 / 24, col, filled=false)) for d in [(12, 1, 12, 4), (12, 20, 12, 23), (1, 12, 4, 12), (20, 12, 23, 12)] { l(d.0, d.1, d.2, d.3) } } "ExternalLink" => { l(4, 7, 4, 20) l(4, 20, 17, 20) l(17, 20, 17, 13) l(4, 7, 11, 7) l(13, 4, 20, 4) l(20, 4, 20, 11) l(20, 4, 10, 14) } "Home" => { l(3, 11, 12, 3) l(12, 3, 21, 11) l(5, 9, 5, 21) l(5, 21, 19, 21) l(19, 21, 19, 9) l(10, 21, 10, 14) l(10, 14, 14, 14) l(14, 14, 14, 21) } "Wrench" => { ops.push(@ui.circle(x + s * 16 / 24, y + s * 8 / 24, s * 5 / 24, col, filled=false)) l(13, 11, 4, 20) l(4, 20, 6, 22) l(6, 22, 15, 13) } "Running" => { ops.push(@ui.circle(x + s / 2, y + s / 2, s * 9 / 24, col, filled=false)) ops.push(@ui.circle(x + s / 2, y + s / 2, s * 3 / 24, col)) } "Search" => { ops.push(@ui.circle(x + s * 10 / 24, y + s * 10 / 24, s * 7 / 24, col, filled=false)) l(15, 15, 21, 21) } "Refresh" => { ops.push(@ui.circle(x + s / 2, y + s / 2, s * 8 / 24, col, filled=false)) l(16, 3, 20, 5) l(20, 5, 18, 9) } "Monitor" => { ops.push(@ui.stroke_rect(@ui.rect(x + s * 2 / 24, y + s * 3 / 24, s * 20 / 24, s * 14 / 24), col, width=w, radius=s / 12)) l(8, 21, 16, 21) l(12, 17, 12, 21) } "Speaker" => { l(3, 9, 3, 15) l(3, 9, 7, 9) l(3, 15, 7, 15) l(7, 9, 12, 4) l(7, 15, 12, 20) l(12, 4, 12, 20) l(16, 9, 18, 12) l(18, 12, 16, 15) } "Info" => { ops.push(@ui.circle(x + s / 2, y + s / 2, s * 9 / 24, col, filled=false)) l(12, 11, 12, 17) ops.push(@ui.circle(x + s / 2, y + s * 8 / 24, 1, col)) } "Stop" => ops.push(@ui.fill_rect(@ui.rect(x + s * 6 / 24, y + s * 6 / 24, s * 12 / 24, s * 12 / 24), col, radius=2)) "Close" => { l(6, 6, 18, 18) l(18, 6, 6, 18) } _ => { let t = if letter == "" { "?" } else { letter } ops.push(@ui.text(x + s / 2, y + s / 2, t, s * 2 / 3, col, align=CENTER, bold=true)) } } } ///| A rounded pill badge at (x, y) (its left edge, top); returns its width. fn badge(ops : Array[@types.DrawOp], x : Int, y : Int, label : String, bg : @types.Color, fg : @types.Color) -> Int { let w = approx_width(label, fs_small) * 12 / 10 + 18 ops.push(@ui.fill_rect(@ui.rect(x, y, w, 22), bg, radius=11)) ops.push(@ui.text(x + 8, y + 11, label, fs_small, fg, bold=true)) w } ///| A button: (x, y) its top-left; returns its width. `primary` is gamerig's gold Launch. fn Library::button( self : Library, ops : Array[@types.DrawOp], x : Int, y : Int, label : String, glyph : String, primary : Bool, hit : Hit, p : Pal, ) -> Int { let w = approx_width(label, fs_small + 1) * 11 / 10 + 42 let r = @ui.rect(x, y, w, 30) let hov = self.hover == hit let (bg, fg) = if primary { (if hov { @ui.lerp_color(p.accent, c(255, 255, 255), 2, 10) } else { p.accent }, p.accent_fg) } else { (if hov { p.row_sel } else { @ui.with_alpha(p.badge_bg, 0) }, p.fg) } ops.push(@ui.fill_rect(r, bg, radius=15)) if !primary { ops.push(@ui.stroke_rect(r, p.border, radius=15)) } draw_glyph(ops, glyph, x + 10, y + 7, 16, fg) ops.push(@ui.text(x + 32, y + 15, label, fs_small + 1, fg, bold=true)) self.regions.push((r, hit)) w } ///| How much a card says at density `lv` (0 the most): (description lines, hint lines, padding). /// The grid picks the roomiest density that fits the screen, so the library doesn't scroll. fn dense(lv : Int) -> (Int, Int, Int) { match lv { 0 => (3, 3, 20) 1 => (2, 2, 18) 2 => (2, 1, 16) 3 => (1, 0, 14) _ => (0, 0, 12) } } ///| Densities and narrowest card widths tried, in order, until the grid fits. let fit_steps : Array[(Int, Int)] = [ (0, 280), (1, 280), (1, 240), (2, 240), (2, 210), (3, 240), (3, 210), (3, 180), (4, 210), (4, 180), (4, 150), ] ///| Height of a card `w` wide at density `lv`: header, description, hint and buttons. fn card_height(g : Game, w : Int, lv : Int) -> Int { let (dl, hl, pad) = dense(lv) let inner = w - 2 * pad let desc = if dl == 0 { 0 } else { wrap(g.description, inner, fs, dl).length() } let hint = if hl == 0 || g.installed || g.install_hint == "" { 0 } else { wrap(g.install_hint, inner - 20, fs_small, hl).length() } pad + 48 + (if desc > 0 { 14 + desc * 22 } else { 4 }) + (if hint > 0 { 10 + hint * 18 } else { 0 }) + 14 + 30 + pad } ///| Place the cards: (game index, x, y, w, h) in grid coordinates (0, 0 at the grid's top-left), /// featured games two columns wide when there are two, rows as tall as their tallest card. pub fn grid_layout(games : Array[Game], inner_w : Int, lv? : Int = 0, min_w? : Int = card_min_w) -> (Array[(Int, Int, Int, Int, Int)], Int, Int) { let gap = grid_gap(lv) let cols = { let n = (inner_w + gap) / (min_w + gap) if n < 1 { 1 } else { n } } let col_w = (inner_w - gap * (cols - 1)) / cols let out : Array[(Int, Int, Int, Int, Int)] = [] let mut col = 0 let mut y = 0 let mut row : Array[Int] = [] let flush = (row : Array[Int], y : Int) => { let mut h = 0 for k in row { if out[k].4 > h { h = out[k].4 } } for k in row { let (i, x, ry, w, _) = out[k] out[k] = (i, x, ry, w, h) } y + h + gap } for i, g in games { let span = if g.featured && cols >= 2 { 2 } else { 1 } if col + span > cols { y = flush(row, y) row = [] col = 0 } let w = col_w * span + gap * (span - 1) out.push((i, col * (col_w + gap), y, w, card_height(g, w, lv))) row.push(out.length() - 1) col = col + span } let total = if row.length() > 0 { flush(row, y) - gap } else { 0 } (out, total, cols) } ///| The gap between cards at density `lv`. fn grid_gap(lv : Int) -> Int { if lv >= 3 { 12 } else { gap } } ///| The roomiest grid that fits `inner_h`: (cells, height, columns, density). The densest when /// none does (it scrolls then). pub fn grid_fit(games : Array[Game], inner_w : Int, inner_h : Int) -> (Array[(Int, Int, Int, Int, Int)], Int, Int, Int) { let mut last = (([] : Array[(Int, Int, Int, Int, Int)]), 0, 1, 4) for st in fit_steps { let (lv, mw) = st let (cells, total, cols) = grid_layout(games, inner_w, lv~, min_w=mw) last = (cells, total, cols, lv) if total <= inner_h { break } } last } ///| Draw the whole library for an output `size`. pub fn Library::render( self : Library, games : Array[Game], modes : Array[Mode], rig : Rig, size : @types.Size, tokens : @theme.Tokens, ) -> Array[@types.DrawOp] { let p = self.pal(tokens) let w = size.w.reinterpret_as_int() let h = size.h.reinterpret_as_int() let bottom = tokens.panel_height.reinterpret_as_int() self.regions.clear() self.games = games let ops : Array[@types.DrawOp] = [] if self.background == "Solid" { ops.push(@ui.fill_rect(@ui.rect(0, 0, w, h), p.bg)) } else { // A light veil under the top bar keeps its text readable on any wallpaper. ops.push(@ui.fill_rect(@ui.rect(0, 0, w, top_h), @ui.with_alpha(p.panel, 120))) } let side_w = if w >= 1100 { 272 } else if w >= 700 { 220 } else { 0 } let main_x = if side_w > 0 { side_w + 16 } else { 8 } let body_h = h - top_h - bottom - 8 // ---- top bar ---- ops.push(@ui.fill_rect(@ui.rect(20, 14, 28, 28), p.accent, radius=7)) draw_glyph(ops, "Monitor", 25, 19, 18, p.accent_fg) ops.push(@ui.text(58, 28, "Gamerig", 16, p.fg, bold=true)) ops.push(@ui.text(main_x + 12, 20, "Library", fs, p.fg, bold=true)) let n = games.length() ops.push(@ui.text(main_x + 12, 38, n.to_string() + (if n == 1 { " app" } else { " apps" }), fs_small, p.muted)) let sw = if w - main_x > 700 { 288 } else { 200 } let sx = w - 20 - sw let sr = @ui.rect(sx, 10, sw, 36) ops.push(@ui.fill_rect(sr, p.panel, radius=8)) ops.push(@ui.stroke_rect(sr, if self.focused { p.accent } else { p.border }, radius=8)) draw_glyph(ops, "Search", sx + 12, 20, 16, p.muted) if self.query == "" { ops.push(@ui.text(sx + 36, 28, if self.focused { "Type to filter apps…" } else { "Filter apps…" }, fs, p.faint)) } else { let shown = ellipsize(self.query, sw - 76, fs) ops.push(@ui.text(sx + 36, 28, shown, fs, p.fg)) let cx = sx + 36 + approx_width(shown, fs) + 1 if self.focused { ops.push(@ui.line(cx, 19, cx, 37, p.accent, width=2)) } let cr = @ui.rect(sx + sw - 30, 16, 24, 24) draw_glyph(ops, "Close", sx + sw - 26, 20, 16, if self.hover == ClearSearch { p.fg } else { p.muted }) self.regions.push((cr, ClearSearch)) } self.regions.push((sr, Search)) // Hints, left of the search box, when there is room. let hx = sx - 16 if hx - (main_x + 140) > 220 { let mut x = hx for part in [("Switch", ["Alt", "Tab"])] { let (label, keys) = part x = x - approx_width(label, fs_small) ops.push(@ui.text(x, 28, label, fs_small, p.muted)) x = x - 6 for k in keys.rev() { let kw = approx_width(k, fs_small) + 10 x = x - kw ops.push(@ui.fill_rect(@ui.rect(x, 18, kw, 20), p.badge_bg, radius=4)) ops.push(@ui.text(x + 5, 28, k, fs_small, p.badge_fg, bold=true)) x = x - 4 } } } // ---- sidebar ---- if side_w > 0 { self.render_sidebar(ops, games, rig, 8, top_h, side_w, body_h, p) } // ---- main ---- let mw = w - main_x - 8 let mr = @ui.rect(main_x, top_h, mw, body_h) ops.push(@ui.fill_rect(mr, p.panel, radius=12)) let shown = filter_games(games, self.category, self.query) ops.push(@ui.text(main_x + 24, top_h + 32, self.category.title(), 16, p.fg, bold=true)) let sub = if rig.error != "" { rig.error } else { shown.length().to_string() + " of " + count_in(games, self.category).to_string() + " apps" + (if self.query != "" { " matching “" + self.query + "”" } else { "" }) } ops.push(@ui.text(main_x + 24, top_h + 58, ellipsize(sub, mw - 160, fs), fs, if rig.error != "" { p.warn } else { p.muted })) let rr = @ui.rect(main_x + mw - 104, top_h + 18, 84, 28) if self.hover == RefreshButton { ops.push(@ui.fill_rect(rr, p.row_sel, radius=8)) } draw_glyph(ops, "Refresh", main_x + mw - 96, top_h + 24, 16, p.muted) ops.push(@ui.text(main_x + mw - 74, top_h + 32, "Refresh", fs_small + 1, p.muted, bold=true)) self.regions.push((rr, RefreshButton)) let gx = main_x + 24 let gy = top_h + 88 let gw = mw - 48 let gh = body_h - 88 - 16 let (cells, total, cols, lv) = grid_fit(shown, gw, gh) let gap = grid_gap(lv) self.content_h = total self.view_h = gh self.clamp_scroll() self.shown = cells.map(cell => (shown[cell.0].id, cell.1 / ((gw - gap * (cols - 1)) / cols + gap), cell.2)) if self.selected != "" && !shown.iter().any(g => g.id == self.selected) { self.selected = "" } ops.push(@types.DrawOp::PushClip(@ui.rect(gx - 4, gy - 4, gw + 8, gh + 8))) if shown.length() == 0 { let msg = if games.length() == 0 { if rig.connected { "The library is empty." } else { "Waiting for the library daemon…" } } else if self.category == Running && self.query == "" { "Nothing running. Launch something!" } else { "No apps match." } ops.push(@ui.text(gx + gw / 2, gy + 60, msg, fs, p.muted, align=CENTER)) } for cell in cells { let (i, x, y, cw, ch) = cell let y = gy + y - self.scroll if y + ch < gy - 8 || y > gy + gh + 8 { continue } self.render_card(ops, shown[i], modes_of(modes, shown[i].id), gx + x, y, cw, ch, p, gy, gh, lv) } ops.push(@types.DrawOp::PopClip) // A scrollbar when the grid is taller than its view. if total > gh && gh > 0 { let th = if gh * gh / total < 32 { 32 } else { gh * gh / total } let ty = gy + (gh - th) * self.scroll / (total - gh) ops.push(@ui.fill_rect(@ui.rect(main_x + mw - 10, ty, 4, th), @ui.with_alpha(p.muted, 120), radius=2)) } ops } ///| fn Library::render_sidebar( self : Library, ops : Array[@types.DrawOp], games : Array[Game], rig : Rig, x : Int, y : Int, w : Int, h : Int, p : Pal, ) -> Unit { ops.push(@ui.fill_rect(@ui.rect(x, y, w, h), p.panel, radius=12)) ops.push(@ui.text(x + 16, y + 18, "Library", fs_small, p.muted, bold=true)) let mut ry = y + 32 for cat in categories { let r = @ui.rect(x + 8, ry, w - 16, 36) let sel = self.category == cat if sel { ops.push(@ui.fill_rect(r, p.row_sel, radius=8)) } else if self.hover == Cat(cat) { ops.push(@ui.fill_rect(r, @ui.with_alpha(p.row_sel, 140), radius=8)) } let glyph = match cat { All => "Home" Games => "Play" Launchers => "Folder" Tools => "Wrench" Running => "Running" } let fg = if sel { p.fg } else { p.muted } draw_glyph(ops, glyph, x + 18, ry + 9, 18, fg) ops.push(@ui.text(x + 46, ry + 18, cat.label(), fs + 1, if sel { p.fg } else { @ui.lerp_color(p.muted, p.fg, 1, 2) }, bold=sel)) ops.push(@ui.text(x + w - 18, ry + 18, count_in(games, cat).to_string(), fs_small, p.muted, align=RIGHT)) self.regions.push((r, Cat(cat))) ry = ry + 38 } ry = ry + 14 ops.push(@ui.text(x + 16, ry, "Running Apps", fs_small, p.muted, bold=true)) ry = ry + 16 let running = games.filter(g => g.running) if running.length() == 0 { ops.push(@ui.text(x + 18, ry + 14, "Nothing running. Launch something!", fs_small, p.muted)) ry = ry + 30 } let status_top = y + h - 108 for g in running { if ry + 32 > status_top - 8 { break } let r = @ui.rect(x + 8, ry, w - 16, 32) if self.hover == RunningRow(g.id) || self.hover == RunningStop(g.id) { ops.push(@ui.fill_rect(r, p.row_sel, radius=8)) } ops.push(@ui.circle(x + 22, ry + 16, 4, p.ok)) ops.push(@ui.text(x + 34, ry + 16, ellipsize(g.name, w - 90, fs), fs, p.fg)) let sr = @ui.rect(x + w - 40, ry + 4, 24, 24) draw_glyph(ops, "Close", x + w - 36, ry + 8, 16, if self.hover == RunningStop(g.id) { p.warn } else { p.muted }) self.regions.push((sr, RunningStop(g.id))) self.regions.push((r, RunningRow(g.id))) ry = ry + 34 } // Rig status, at the bottom. let sy = status_top ops.push(@ui.text(x + 12, sy + 12, "Rig Status", fs_small, p.muted, bold=true)) let rows = [ ("Monitor", "GPU", rig.gpu), ("Monitor", "Display", rig.resolution + (if rig.display != "" && rig.display != "—" { " on " + rig.display } else { "" })), ("Speaker", "Audio", rig.audio), ] for k, row in rows { let (g, label, value) = row let yy = sy + 38 + k * 26 draw_glyph(ops, g, x + 12, yy - 7, 14, p.muted) ops.push(@ui.text(x + 34, yy, label, fs_small, p.muted)) let vx = x + 96 if label == "Audio" { ops.push(@ui.circle(vx + 3, yy, 3, if rig.audio_ok { p.ok } else { p.faint })) ops.push(@ui.text(vx + 14, yy, ellipsize(value, w - 124, fs_small), fs_small, p.fg)) } else { ops.push(@ui.text(vx, yy, ellipsize(value, w - 110, fs_small), fs_small, p.fg)) } } if !rig.connected { ops.push(@ui.circle(x + w - 20, sy + 12, 4, p.warn)) } } ///| fn Library::render_card( self : Library, ops : Array[@types.DrawOp], g : Game, modes : Array[Mode], x : Int, y : Int, w : Int, h : Int, p : Pal, clip_y : Int, clip_h : Int, lv : Int, ) -> Unit { let (dl, hl, pad) = dense(lv) let r = @ui.rect(x, y, w, h) let hov = match self.hover { Card(id) | Button(id, _) | StopButton(id) | FocusButton(id) => id == g.id _ => false } ops.push(@ui.fill_rect(r, if hov { p.card_hover } else { p.card }, radius=14)) if self.selected == g.id { ops.push(@ui.stroke_rect(r, p.accent, width=2, radius=14)) } // Icon tile, name and badges. let (tbg, tfg) = tile_colors(g.icon, p) ops.push(@ui.fill_rect(@ui.rect(x + pad, y + pad, 44, 44), tbg, radius=10)) draw_glyph(ops, g.icon, x + pad + 10, y + pad + 10, 24, tfg, letter=g.name.to_array()[:1].to_owned() |> String::from_array) ops.push(@ui.text(x + pad + 58, y + pad + 12, ellipsize(g.name, w - 2 * pad - 58, 16), 16, p.fg, bold=true)) let bx = x + pad + 56 let bw = badge(ops, bx, y + pad + 28, category_badge(g.category), p.badge_bg, p.badge_fg) let st = g.status() let (sbg, sfg) = match st { Ready | Running => (p.ok_bg, p.ok) NotInstalled => (p.badge_bg, p.badge_fg) } ignore(badge(ops, bx + bw + 6, y + pad + 28, st.label(), sbg, sfg)) // Description and hint. let mut ty = y + pad + 48 + 14 + 11 for line in (if dl == 0 { [] } else { wrap(g.description, w - 2 * pad, fs, dl) }) { ops.push(@ui.text(x + pad, ty, line, fs, p.fg)) ty = ty + 22 } if hl > 0 && !g.installed && g.install_hint != "" { ty = ty + 10 - 2 draw_glyph(ops, "Info", x + pad, ty - 7, 14, p.muted) for line in wrap(g.install_hint, w - 2 * pad - 20, fs_small, hl) { ops.push(@ui.text(x + pad + 20, ty, line, fs_small, p.muted)) ty = ty + 18 } } // Buttons, at the bottom. let by = y + h - pad - 30 let mut bx = x + pad if g.running { bx = bx + self.button(ops, bx, by, "Switch to", "ExternalLink", true, FocusButton(g.id), p) + 8 bx = bx + self.button(ops, bx, by, "Stop", "Stop", false, StopButton(g.id), p) + 8 } else { bx = bx + self.button(ops, bx, by, "Launch", "Play", g.installed, Button(g.id, "default"), p) + 8 for m in modes { if bx + approx_width(m.label, fs_small + 1) + 42 > x + w - 12 { break } bx = bx + self.button(ops, bx, by, m.label, "Monitor", false, Button(g.id, m.id), p) + 8 } } // The card itself (after the buttons, so they win), only where it shows. let top = if y < clip_y { clip_y } else { y } let bot = if y + h > clip_y + clip_h { clip_y + clip_h } else { y + h } if bot > top { self.regions.push((@ui.rect(x, top, w, bot - top), Card(g.id))) } } ///| fn Library::clamp_scroll(self : Library) -> Unit { let max = self.content_h - self.view_h let max = if max < 0 { 0 } else { max } if self.scroll > max { self.scroll = max } if self.scroll < 0 { self.scroll = 0 } } ///| What is at `p` (from the last render; buttons before the cards under them). pub fn Library::hit(self : Library, p : @types.Point) -> Hit { // The grid is clipped: a button scrolled out of view is not there. for reg in self.regions { if @ui.contains(reg.0, p) { return reg.1 } } NoHit } ///| Wheel over the desktop: scroll the grid. True when it moved. pub fn Library::scroll_by(self : Library, dy : Int) -> Bool { let before = self.scroll self.scroll = self.scroll + dy * 4 self.clamp_scroll() self.scroll != before } ///| The game with `id` from the last render. fn Library::game(self : Library, id : String) -> Game? { for g in self.games { if g.id == id { return Some(g) } } None } ///| What a click on `hit` does. fn Library::activate(self : Library, hit : Hit) -> Action { match hit { Cat(cat) => { self.category = cat self.scroll = 0 NoAction } ClearSearch => { self.query = "" self.scroll = 0 NoAction } RefreshButton => Refresh Card(id) => { self.selected = id NoAction } Button(id, mode) => { self.selected = id Launch(id, mode) } StopButton(id) | RunningStop(id) => Stop(id) FocusButton(id) | RunningRow(id) => Focus(id) Search | NoHit => NoAction } } ///| Move the keyboard selection by `dx` cards / `dy` rows. fn Library::step(self : Library, dx : Int, dy : Int) -> Unit { let cards = self.shown if cards.length() == 0 { return } let mut at = -1 for i, cd in cards { if cd.0 == self.selected { at = i } } if at < 0 { self.selected = cards[0].0 return } if dx != 0 { let n = at + dx let n = if n < 0 { 0 } else if n >= cards.length() { cards.length() - 1 } else { n } self.selected = cards[n].0 } else { // The card in the next / previous row nearest in column. let (_, col, y) = cards[at] let mut best = -1 let mut best_d = 0 for i, cd in cards { let ok = if dy > 0 { cd.2 > y } else { cd.2 < y } if ok { let d = (cd.2 - y).abs() * 100 + (cd.1 - col).abs() if best < 0 || d < best_d { best = i best_d = d } } } if best >= 0 { self.selected = cards[best].0 } } self.reveal() } ///| Scroll so the selected card shows. fn Library::reveal(self : Library) -> Unit { for cd in self.shown { if cd.0 == self.selected { if cd.2 < self.scroll { self.scroll = cd.2 } else if cd.2 + 160 > self.scroll + self.view_h { self.scroll = cd.2 + 160 - self.view_h } } } self.clamp_scroll() } ///| One input event; (redraw, what to do). pub fn Library::handle(self : Library, ev : @types.InputEvent) -> (Bool, Action) { match ev { PointerMove(pt) => { let h = self.hit(pt) if h != self.hover { self.hover = h (true, NoAction) } else { (false, NoAction) } } PointerDown(pt) => { self.press = self.hit(pt) let was = self.focused self.focused = true (!was, NoAction) } PointerUp(pt) => { let h = self.hit(pt) let pressed = self.press self.press = NoHit if h == pressed && h != NoHit { (true, self.activate(h)) } else { (false, NoAction) } } PointerLeave => { let changed = self.hover != NoHit self.hover = NoHit (changed, NoAction) } FocusLost => { let was = self.focused self.focused = false (was, NoAction) } KeyDown(k) => self.key(k) } } ///| fn Library::key(self : Library, k : String) -> (Bool, Action) { match k { "ArrowLeft" | "Left" => self.step(-1, 0) "ArrowRight" | "Right" => self.step(1, 0) "ArrowUp" | "Up" => self.step(0, -1) "ArrowDown" | "Down" => self.step(0, 1) "Enter" | "Return" => match self.game(self.selected) { Some(g) if g.running => return (true, Focus(g.id)) Some(g) => return (true, Launch(g.id, "default")) None => { self.step(0, 0) return (true, NoAction) } } "Backspace" => { let a = self.query.to_array() if a.length() == 0 { return (false, NoAction) } self.query = String::from_array(a[:a.length() - 1].to_owned()) self.scroll = 0 } "Escape" => { if self.query == "" && self.selected == "" { return (false, NoAction) } self.query = "" self.selected = "" self.scroll = 0 } _ => if k.length() == 1 && k.to_array()[0] >= ' ' && self.query.length() < 64 { self.query = self.query + k self.scroll = 0 self.selected = "" } else { return (false, NoAction) } } (true, NoAction) } ///| The right-click settings. pub fn Library::config_items(self : Library) -> Array[ConfigEntry] { [ { key: "background", label: "Background", choices: background_choices, value: self.background }, { key: "colors", label: "Colours", choices: colors_choices, value: self.colors }, ] } ///| Set one; true when it changed something on screen. pub fn Library::set_config(self : Library, key : String, value : String) -> Bool { match key { "colors" if colors_choices.contains(value) => { self.colors = value true } "background" if background_choices.contains(value) => { self.background = value true } _ => false } }