Gamerig Library: gamerig's game library as a Chandra desktop module
CI / test (push) Successful in 2m25s

- New desktop module gamerig-library (components/gamerig-library, logic in lib/gamelib): the
  layout of gamerig's web library (desktop/ui) in draw ops. Top bar with the Gamerig mark, app
  count and search; sidebar with categories and counts, running apps (switch to, stop) and rig
  status (GPU, display, audio); card grid with featured games two columns wide, icon tiles,
  category and status badges, description, install hint, Launch / Big Picture / Switch to / Stop.
  Typing filters, arrows + Enter launch, the wheel scrolls. Colours: follow desktop, dark, light.
- New game-library capability: tables 26-28 (status, games, launch modes), request 45, in the
  wasmer/wasmtime stub, the MoonBit wasmhost and the Rust host-wasm.
- compositor/shell/gamelib.mbt polls GET $GAMERIG_DESKTOP_API/api/state (default
  http://127.0.0.1:8091) every 2 s with curl on a socket and POSTs launch/stop/focus; a file://
  base reads a state file (tests).
- --desktop NAME picks what fills the desktop at start. scripts/gamerig-kiosk.sh now runs a
  hybrid kiosk with the library as the desktop (CHANDRA_KIOSK_MODE=strict for the old one).
- Tests: lib/gamelib, compositor/host/gamelib_test.mbt, a host-wasm test; docs/gamerig-library.md
  with a screenshot.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DPL5hgvjzw8hHirxRsXSsE
This commit is contained in:
Claude committed 2026-11-26 02:57:07 +00:00
1 parent fa932f9ac0
commit f556df2f4b
57 files changed
+3551 -29

No files matched your search

+171
View File
@@ -0,0 +1,171 @@
///| A /api/state answer like gamerig's server.py gives.
let sample : String =
#|{"system": {"gpu": "NVIDIA GeForce GTX 1650", "resolution": "1920x1080", "display": ":0",
#| "audio": "PulseAudio (Selkies)", "audio_ok": true, "hostname": "rig"},
#| "apps": [
#| {"id": "steam", "name": "Steam", "category": "games", "featured": true, "icon": "Play",
#| "description": "Your Steam library, store and friends.", "installed": true, "running": false,
#| "modes": [{"id": "default", "label": "Launch"}, {"id": "bigpicture", "label": "Big Picture"}]},
#| {"id": "lutris", "name": "Lutris", "category": "launchers", "icon": "Folder",
#| "description": "Open gaming platform.", "installed": false, "install_hint": "apt-get install lutris"},
#| {"id": "terminal", "name": "Terminal", "category": "tools", "icon": "Terminal",
#| "description": "Interactive shell inside the rig.", "installed": true, "running": true, "pid": 42},
#| {"name": "no id"}
#| ]}
///|
fn sample_state() -> @gamelib.State {
match @gamelib.parse_state(sample) {
Ok(s) => s
Err(e) => abort(e)
}
}
///|
fn tokens() -> @theme.Tokens {
let c = @ui.color(230, 230, 230, 255)
{
name: "dark",
palette: {
desktop_top: c,
desktop_bottom: c,
surface: c,
surface_hover: c,
surface_popup: c,
foreground: c,
foreground_muted: c,
accent: c,
accent_foreground: c,
border: c,
},
font_size: 14,
radius: 8,
spacing: 8,
panel_height: 40,
}
}
///|
fn texts(ops : Array[@types.DrawOp]) -> Array[String] {
let out : Array[String] = []
for op in ops {
if op is Text(t) {
out.push(t.text)
}
}
out
}
///|
test "parse_state reads the rig, the games and their extra modes" {
let s = sample_state()
assert_eq(s.rig.gpu, "NVIDIA GeForce GTX 1650")
assert_true(s.rig.connected)
assert_true(s.rig.audio_ok)
assert_eq(s.games.map(g => g.id), ["steam", "lutris", "terminal"])
assert_eq(s.modes, [{ game: "steam", id: "bigpicture", label: "Big Picture" }])
assert_eq(s.games[1].install_hint, "apt-get install lutris")
assert_eq(s.games[2].status(), Running)
assert_true(@gamelib.parse_state("nope") is Err(_))
assert_eq(@gamelib.error_message("{\"error\": \"unknown app\"}"), "unknown app")
}
///|
test "filtering: categories, running, search words, featured first" {
let g = sample_state().games
assert_eq(@gamelib.count_in(g, All), 3)
assert_eq(@gamelib.count_in(g, Games), 1)
assert_eq(@gamelib.count_in(g, Running), 1)
assert_eq(@gamelib.filter_games(g, All, "shell rig").map(x => x.id), ["terminal"])
assert_eq(@gamelib.filter_games(g, All, "").map(x => x.id), ["steam", "lutris", "terminal"])
assert_eq(@gamelib.filter_games(g, Tools, "steam").length(), 0)
}
///|
test "wrap and ellipsize keep to the width" {
let lines = @gamelib.wrap("one two three four five six seven", 60, 10, 2)
assert_eq(lines.length(), 2)
for l in lines {
assert_true(@gamelib.approx_width(l, 10) <= 60)
}
assert_true(lines[1].has_suffix("…"))
assert_eq(@gamelib.ellipsize("short", 100, 10), "short")
}
///|
test "grid: featured cards span two columns, rows wrap" {
let g = sample_state().games
let (cells, total, cols) = @gamelib.grid_layout(g, 900)
assert_eq(cols, 3)
// steam (featured) two columns, lutris next to it, terminal on the next row.
assert_eq(cells[0].3 > cells[1].3, true)
assert_eq(cells[1].2, 0)
assert_true(cells[2].2 > 0)
assert_true(total >= cells[2].2 + cells[2].4)
// One column: nothing spans.
let (one, _, c1) = @gamelib.grid_layout(g, 300)
assert_eq(c1, 1)
assert_eq(one[0].3, one[1].3)
}
///|
test "render shows the sidebar, the cards and the rig; clicks launch, stop and filter" {
let lib = @gamelib.Library::new()
let s = sample_state()
let size : @types.Size = { w: 1600, h: 1000 }
let ops = lib.render(s.games, s.modes, s.rig, size, tokens())
let t = texts(ops)
for want in ["Gamerig", "Library", "Games", "Running Apps", "Rig Status", "Steam", "Big Picture", "Switch to", "Stop", "Not installed", "All apps"] {
assert_true(t.contains(want), msg="missing " + want)
}
assert_true(t.iter().any(x => x.has_prefix("NVIDIA GeForce")))
let click = (h : @gamelib.Hit) => {
for reg in lib.regions {
if reg.1 == h {
let p : @types.Point = { x: reg.0.x + 2, y: reg.0.y + 2 }
ignore(lib.handle(PointerDown(p)))
return lib.handle(PointerUp(p)).1
}
}
abort("no region")
}
assert_eq(click(Button("steam", "bigpicture")), Launch("steam", "bigpicture"))
assert_eq(click(Button("steam", "default")), Launch("steam", "default"))
assert_eq(click(StopButton("terminal")), Stop("terminal"))
assert_eq(click(RunningRow("terminal")), Focus("terminal"))
assert_eq(click(RefreshButton), Refresh)
assert_eq(click(Cat(Tools)), NoAction)
assert_eq(lib.category, Tools)
let t = texts(lib.render(s.games, s.modes, s.rig, size, tokens()))
assert_false(t.contains("Steam"))
assert_true(t.contains("Terminal"))
}
///|
test "typing filters, Enter launches the selection, Escape clears" {
let lib = @gamelib.Library::new()
let s = sample_state()
let size : @types.Size = { w: 1280, h: 720 }
ignore(lib.render(s.games, s.modes, s.rig, size, tokens()))
for k in ["l", "u", "t"] {
ignore(lib.handle(KeyDown(k)))
}
assert_eq(lib.query, "lut")
ignore(lib.render(s.games, s.modes, s.rig, size, tokens()))
ignore(lib.handle(KeyDown("Right")))
assert_eq(lib.selected, "lutris")
assert_eq(lib.handle(KeyDown("Enter")).1, Launch("lutris", "default"))
ignore(lib.handle(KeyDown("Backspace")))
assert_eq(lib.query, "lu")
ignore(lib.handle(KeyDown("Escape")))
assert_eq(lib.query, "")
}
///|
test "disconnected: says it is waiting, and the error" {
let lib = @gamelib.Library::new()
let rig = { ..@gamelib.Rig::unknown(), error: "Library daemon not reachable" }
let t = texts(lib.render([], [], rig, { w: 1280, h: 720 }, tokens()))
assert_true(t.contains("Waiting for the library daemon…"))
assert_true(t.contains("Library daemon not reachable"))
}
+319
View File
@@ -0,0 +1,319 @@
///| The Gamerig Library desktop module's data (components/gamerig-library): what gamerig's library
/// daemon reports at `GET /api/state` (gamerig docker/desktop/server.py), and how the library
/// sorts it into categories and searches it. The same types serve the shell, which polls the daemon
/// and parses its JSON here (`parse_state`), and the module, which gets them back through the
/// `game-library` capability.
///| A sidebar filter. `Running` is not a category of the catalog but a view of what runs now.
#derive(Eq, Debug)
pub(all) enum Category {
All
Games
Launchers
Tools
Running
}
///|
pub let categories : Array[Category] = [All, Games, Launchers, Tools, Running]
///|
pub fn Category::key(self : Category) -> String {
match self {
All => "all"
Games => "games"
Launchers => "launchers"
Tools => "tools"
Running => "running"
}
}
///|
pub fn Category::label(self : Category) -> String {
match self {
All => "All"
Games => "Games"
Launchers => "Launchers"
Tools => "Tools"
Running => "Running"
}
}
///| The heading over the grid ("All apps", "Games" ...).
pub fn Category::title(self : Category) -> String {
match self {
All => "All apps"
Running => "Running now"
c => c.label()
}
}
///|
pub fn Category::parse(key : String) -> Category {
match key.trim().to_owned().to_lower() {
"games" | "game" => Games
"launchers" | "launcher" => Launchers
"tools" | "tool" => Tools
"running" => Running
_ => All
}
}
///| The singular label of a game's own category, for its badge.
pub fn category_badge(key : String) -> String {
match Category::parse(key) {
Games => "Game"
Launchers => "Launcher"
Tools => "Tool"
_ => "App"
}
}
///| One game or app of the library.
#derive(Eq, Debug)
pub(all) struct Game {
id : String
name : String
category : String
description : String
icon : String
install_hint : String
featured : Bool
installed : Bool
running : Bool
}
///| A way to start `game` other than plain Launch (Steam's Big Picture).
#derive(Eq, Debug)
pub(all) struct Mode {
game : String
id : String
label : String
}
///| The rig, as the library daemon describes it.
#derive(Eq, Debug)
pub(all) struct Rig {
connected : Bool
error : String
gpu : String
resolution : String
display : String
audio : String
audio_ok : Bool
hostname : String
}
///| Before the daemon first answered.
pub fn Rig::unknown() -> Rig {
{
connected: false,
error: "",
gpu: "Detecting GPU…",
resolution: "—",
display: "—",
audio: "Detecting audio…",
audio_ok: false,
hostname: "",
}
}
///| What a card says about its game.
#derive(Eq, Debug)
pub(all) enum Status {
Running
Ready
NotInstalled
}
///|
pub fn Game::status(self : Game) -> Status {
if self.running {
Running
} else if self.installed {
Ready
} else {
NotInstalled
}
}
///|
pub fn Status::label(self : Status) -> String {
match self {
Running => "Running"
Ready => "Ready"
NotInstalled => "Not installed"
}
}
///| Everything `GET /api/state` says.
#derive(Eq, Debug)
pub(all) struct State {
rig : Rig
games : Array[Game]
modes : Array[Mode]
}
// ---------------------------------------------------------------------------
// JSON (gamerig desktop/ui/library parse_state, in Chandra's types)
///|
fn str_field(obj : Map[String, Json], key : String, fallback : String) -> String {
match obj.get(key) {
Some(String(s)) => s
Some(Number(n, ..)) => n.to_int().to_string()
_ => fallback
}
}
///|
fn bool_field(obj : Map[String, Json], key : String) -> Bool {
match obj.get(key) {
Some(True) => true
Some(String("true")) => true
Some(Number(n, ..)) => n != 0.0
_ => false
}
}
///| One game from its JSON, and its extra launch modes. Without an id there is nothing to act on.
fn parse_game(j : Json, modes : Array[Mode]) -> Game? {
guard j is Object(m) else { return None }
let id = str_field(m, "id", "")
if id == "" {
return None
}
if m.get("modes") is Some(Array(items)) {
for item in items {
if item is Object(md) {
let mid = str_field(md, "id", "")
if mid != "" && mid != "default" {
modes.push({ game: id, id: mid, label: str_field(md, "label", mid) })
}
}
}
}
Some({
id,
name: str_field(m, "name", id),
category: str_field(m, "category", "tools").to_lower(),
description: str_field(m, "description", ""),
icon: str_field(m, "icon", ""),
install_hint: str_field(m, "install_hint", ""),
featured: bool_field(m, "featured"),
installed: bool_field(m, "installed"),
running: bool_field(m, "running"),
})
}
///|
fn parse_rig(v : Json?) -> Rig {
let base = Rig::unknown()
guard v is Some(Object(m)) else { return { ..base, connected: true } }
{
connected: true,
error: "",
gpu: str_field(m, "gpu", base.gpu),
resolution: str_field(m, "resolution", base.resolution),
display: str_field(m, "display", base.display),
audio: str_field(m, "audio", base.audio),
audio_ok: bool_field(m, "audio_ok"),
hostname: str_field(m, "hostname", ""),
}
}
///| Parse the body of `GET /api/state`.
pub fn parse_state(body : String) -> Result[State, String] {
let j = @json.parse(body) catch {
e => return Err("Invalid answer from the library daemon: \{e}")
}
guard j is Object(m) else {
return Err("The library daemon answered with something other than an object")
}
let games : Array[Game] = []
let modes : Array[Mode] = []
if m.get("apps") is Some(Array(items)) {
for item in items {
if parse_game(item, modes) is Some(g) {
games.push(g)
}
}
}
Ok({ rig: parse_rig(m.get("system")), games, modes })
}
///| The daemon's `{"error": "..."}`, or the body itself when it is not one.
pub fn error_message(body : String) -> String {
let j = @json.parse(body) catch { _ => return body.trim().to_owned() }
match j {
Object(m) =>
match m.get("error") {
Some(String(s)) => s
_ => body
}
_ => body
}
}
// ---------------------------------------------------------------------------
// Filtering
///|
fn in_category(g : Game, cat : Category) -> Bool {
match cat {
All => true
Running => g.running
c => g.category == c.key()
}
}
///|
fn matches_query(g : Game, query : String) -> Bool {
let q = query.trim().to_owned().to_lower()
if q == "" {
return true
}
let hay = "\{g.name} \{g.id} \{g.category} \{g.description}".to_lower()
for word in q.split(" ") {
let w = word.to_owned()
if w != "" && hay.find(w) is None {
return false
}
}
true
}
///| The cards to show: in the category, matching every word of the search, featured games first
/// and otherwise in catalog order.
pub fn filter_games(games : Array[Game], category : Category, query : String) -> Array[Game] {
let picked = games.filter(g => in_category(g, category) && matches_query(g, query))
let featured = picked.filter(g => g.featured)
let rest = picked.filter(g => !g.featured)
[..featured, ..rest]
}
///|
pub fn count_in(games : Array[Game], category : Category) -> Int {
games.filter(g => in_category(g, category)).length()
}
///| `game`'s extra launch modes.
pub fn modes_of(modes : Array[Mode], game : String) -> Array[Mode] {
modes.filter(m => m.game == game)
}
///| A non-negative decimal number (an HTTP status), 0 when `s` isn't one.
pub fn parse_int(s : String) -> Int {
let t = s.trim()
if t.length() == 0 || t.length() > 9 {
return 0
}
let mut v = 0
for ch in t {
if ch < '0' || ch > '9' {
return 0
}
v = v * 10 + (ch.to_int() - '0'.to_int())
}
v
}
+6
View File
@@ -0,0 +1,6 @@
import {
"moonbitlang/core/json",
"chandra/desktop/interface/chandra/desktop/types" @types,
"chandra/desktop/interface/chandra/desktop/theme" @theme,
"chandra/desktop/lib/ui" @ui,
}
+997
View File
@@ -0,0 +1,997 @@
///| 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"]
///| 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
/// 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",
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 {
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..<pts.length() {
let (ax, ay) = pts[i]
let (bx, by) = pts[(i + 1) % pts.length()]
l(ax, ay, bx, by)
}
}
"Square" =>
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
}
///| Height of a card `w` wide: header, description, hint and buttons.
fn card_height(g : Game, w : Int) -> Int {
let inner = w - 40
let desc = wrap(g.description, inner, fs, 3).length()
let hint = if g.installed || g.install_hint == "" { 0 } else { wrap(g.install_hint, inner - 20, fs_small, 3).length() }
20 + 48 + 14 + desc * 22 + (if hint > 0 { 10 + hint * 18 } else { 0 }) + 20 + 30 + 20
}
///| 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) -> (Array[(Int, Int, Int, Int, Int)], Int, Int) {
let cols = {
let n = (inner_w + gap) / (card_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)))
row.push(out.length() - 1)
col = col + span
}
let total = if row.length() > 0 { flush(row, y) - gap } else { 0 }
(out, total, cols)
}
///| 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] = []
ops.push(@ui.fill_rect(@ui.rect(0, 0, w, h), p.bg))
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) = grid_layout(shown, gw)
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)
}
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,
) -> Unit {
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 + 20, y + 20, 44, 44), tbg, radius=10))
draw_glyph(ops, g.icon, x + 30, y + 30, 24, tfg, letter=g.name.to_array()[:1].to_owned() |> String::from_array)
ops.push(@ui.text(x + 78, y + 32, ellipsize(g.name, w - 98, 16), 16, p.fg, bold=true))
let bx = x + 76
let bw = badge(ops, bx, y + 48, 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 + 48, st.label(), sbg, sfg))
// Description and hint.
let mut ty = y + 20 + 48 + 14 + 11
for line in wrap(g.description, w - 40, fs, 3) {
ops.push(@ui.text(x + 20, ty, line, fs, p.fg))
ty = ty + 22
}
if !g.installed && g.install_hint != "" {
ty = ty + 10 - 2
draw_glyph(ops, "Info", x + 20, ty - 7, 14, p.muted)
for line in wrap(g.install_hint, w - 60, fs_small, 3) {
ops.push(@ui.text(x + 40, ty, line, fs_small, p.muted))
ty = ty + 18
}
}
// Buttons, at the bottom.
let by = y + h - 20 - 30
let mut bx = x + 20
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: "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
}
_ => false
}
}