diff options
Diffstat (limited to 'desktop/companion')
| -rw-r--r-- | desktop/companion/Companion.qml | 2 | ||||
| -rw-r--r-- | desktop/companion/Lines.js | 85 | ||||
| -rw-r--r-- | desktop/companion/README.md | 12 | ||||
| -rw-r--r-- | desktop/companion/test_lines.js | 63 |
4 files changed, 138 insertions, 24 deletions
diff --git a/desktop/companion/Companion.qml b/desktop/companion/Companion.qml index 3102c59..fe2d751 100644 --- a/desktop/companion/Companion.qml +++ b/desktop/companion/Companion.qml @@ -258,7 +258,7 @@ PanelWindow { function say(kind, force) { const t = Date.now(); if (root.hidden || (!force && !Mood.canSpeak(root.lastLineAt, t))) return; - const line = Lines.pick(kind, Math.random()); + const line = Lines.pick(kind, Math.random(), Prefs.companionApps); if (line === "") return; root.lastLineAt = t; bubble.show(line, 3000); diff --git a/desktop/companion/Lines.js b/desktop/companion/Lines.js index d6450df..8ba5173 100644 --- a/desktop/companion/Lines.js +++ b/desktop/companion/Lines.js @@ -28,23 +28,27 @@ var lines = { dodge: ["...Fine.", "I'll move."], destruct: ["Self-destruct sequence initiated."], aftermath: ["...That was unpleasant."], +}; - // The focused window's class, lowercased, as "app:<class>". A class - // with no entry says nothing. - "app:firefox": ["Researching again?", "Don't open too many tabs.", "The Bunker's network is faster."], - "app:kitty": ["A terminal. Serious work.", "Careful with that rm.", "I'll watch your commands."], - "app:discord": ["Talking to the Resistance?", "Don't tell them where we are."], - "app:signal": ["Encrypted. Good."], - "app:steam": ["Training simulation?", "Don't play all night."], - "app:gimp": ["Making art?"], - "app:com.obsproject.studio": ["Are we recording?", "I'll stay out of frame."], - "app:md.obsidian.obsidian": ["Writing a report?", "Taking notes. Good habit."], - "app:sublime_text": ["Writing code?", "Mind the typos."], - "app:qtmaildir": ["Any orders from Command?", "Mail. Anything urgent?"], - "app:virt-manager": ["More machines?", "Androids running androids."], - "app:audacious": ["Music. I like this."], - "app:llamachat": ["Talking to Pod?"], - "app:qtpass": ["I'm not looking."], +// The lines for a program she recognizes, by lowercased window class: one is +// said at random when focus moves to it. The settings window's Companion +// phrases page edits them into Prefs.companionApps, which then replaces this +// whole set; a class with no entry says nothing. +var apps = { + firefox: ["Researching again?", "Don't open too many tabs.", "The Bunker's network is faster."], + kitty: ["A terminal. Serious work.", "Careful with that rm.", "I'll watch your commands."], + discord: ["Talking to the Resistance?", "Don't tell them where we are."], + signal: ["Encrypted. Good."], + steam: ["Training simulation?", "Don't play all night."], + gimp: ["Making art?"], + "com.obsproject.studio": ["Are we recording?", "I'll stay out of frame."], + "md.obsidian.obsidian": ["Writing a report?", "Taking notes. Good habit."], + sublime_text: ["Writing code?", "Mind the typos."], + qtmaildir: ["Any orders from Command?", "Mail. Anything urgent?"], + "virt-manager": ["More machines?", "Androids running androids."], + audacious: ["Music. I like this."], + llamachat: ["Talking to Pod?"], + qtpass: ["I'm not looking."], }; // The game's opening, one sentence per bubble. @@ -56,8 +60,53 @@ var monologue = [ "I often think about the god who blessed us with this cryptic puzzle...and wonder if we'll ever get the chance to kill him.", ]; -// A line for kind; roll is Math.random(). "" for a kind with no lines. -function pick(kind, roll) { +// The app set in effect: saved (Prefs.companionApps) when it is a plain +// object, these otherwise. A malformed saved value must not leave her mute. +function appMap(saved) { + return saved && typeof saved === "object" && !Array.isArray(saved) ? saved : apps; +} + +// The app set as an ordered list of {cls, lines}, for the settings page. +function appList(saved) { + const m = appMap(saved); + const keys = Object.keys(m); + const out = []; + for (let i = 0; i < keys.length; i++) + out.push({ cls: keys[i], lines: (m[keys[i]] || []).slice() }); + return out; +} + +// A line for an app class, empty strings skipped; "" when the class has none. +function pickApp(cls, saved, roll) { + const all = appMap(saved)[cls] || []; + const l = []; + for (let i = 0; i < all.length; i++) + if (typeof all[i] === "string" && all[i].trim() !== "") l.push(all[i]); + return l.length ? l[Math.min(l.length - 1, Math.floor(roll * l.length))] : ""; +} + +// A line for a reaction kind, or, with an "app:" prefix, for that class. +function pick(kind, roll, saved) { + if (kind.slice(0, 4) === "app:") return pickApp(kind.slice(4), saved, roll); const l = lines[kind] || []; return l.length ? l[Math.min(l.length - 1, Math.floor(roll * l.length))] : ""; } + +// The page's list back to a map for Prefs: empty classes and phrases dropped, +// the class lowercased (Hyprland reports it that way), a repeated class +// keeping its last entry. +function toMap(list) { + const m = {}; + for (let i = 0; i < list.length; i++) { + const cls = String(list[i].cls || "").trim().toLowerCase(); + if (cls === "") continue; + const src = list[i].lines || []; + const ls = []; + for (let j = 0; j < src.length; j++) { + const s = String(src[j] || "").trim(); + if (s !== "") ls.push(s); + } + if (ls.length) m[cls] = ls; + } + return m; +} diff --git a/desktop/companion/README.md b/desktop/companion/README.md index 4bb87f2..4fd54c4 100644 --- a/desktop/companion/README.md +++ b/desktop/companion/README.md @@ -11,7 +11,7 @@ The design, with what was measured on the way, is | `Companion.qml` | the window, inputs, timers, speech, IPC (`companion toggle/show/hide`) | | `Figure.qml` | draws a frame: crossfade, bob, drop out and back, explosion | | `Mood.js` | which state wins, from a snapshot; all the timings are its `T` | -| `Lines.js` | her lines and the opening monologue | +| `Lines.js` | her reaction and per-app lines, and the opening monologue | | `Admired.qml` | the wallpaper list she blushes at, and what wallp has on each monitor | | `ListFile.js` | that list as plain text: parse and toggle | | `prep-frames` | generated images to display frames | @@ -21,7 +21,7 @@ The design, with what was measured on the way, is it wins over the directory's own file (see AGENTS.md). Whether she is shown is `companion` in `Prefs.qml`, so it survives a -restart: SUPER+\ and the switch on Settings > Desktop > Companion +restart: SUPER+\ and the switch on Settings > Companion (`desktop/modules/companion/`) both set it. ## Art @@ -56,9 +56,10 @@ frame falls back to its outfit's `base`. - The program in front of her is Hyprland's `activewindow` event, `class,title`. It also fires on every title change, so she reacts only when the class changes, a third of the time, under the 30 s line - cooldown. Lines are `app:<class>` in `Lines.js`, class lowercased; a - class with none says nothing. -- A listed wallpaper (Settings > Desktop > Companion for the file, the + cooldown. Lines are by lowercased class in `Lines.js`; a class with + none says nothing. Settings > Companion > Companion phrases edits them + into `Prefs.companionApps`, which then replaces the defaults. +- A listed wallpaper (Settings > Companion for the file, the heart in the wallpaper picker for entries) on an empty workspace: she turns to it with `watch` and blushes with `blush3`, at most every 3 minutes. Checked on `workspacev2` and `closewindow`, and whenever she @@ -83,3 +84,4 @@ frame falls back to its outfit's `base`. ## Check node desktop/companion/test_mood.js + node desktop/companion/test_lines.js diff --git a/desktop/companion/test_lines.js b/desktop/companion/test_lines.js new file mode 100644 index 0000000..f70a9ff --- /dev/null +++ b/desktop/companion/test_lines.js @@ -0,0 +1,63 @@ +// Copyright (C) 2026 Danilo M. <danix@danix.xyz> +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License version 2 as +// published by the Free Software Foundation. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// Run: node desktop/companion/test_lines.js +// Lines.js is a QML library, so its .pragma line is stripped before node +// evaluates it. + +const fs = require("fs"); +const assert = require("assert"); + +const src = fs.readFileSync(__dirname + "/Lines.js", "utf8") + .replace(/^\.pragma library$/m, ""); +const L = new Function(src + + "\nreturn { lines, apps, monologue, appMap, appList, pickApp, pick, toMap };")(); + +// The defaults are what she says until a set is saved. +assert.strictEqual(L.pickApp("firefox", null, 0), L.apps.firefox[0]); +assert.strictEqual(L.pickApp("firefox", null, 0.99), L.apps.firefox[L.apps.firefox.length - 1]); +assert.strictEqual(L.pickApp("nope", null, 0), ""); +assert.strictEqual(L.appMap(null), L.apps); +// A hand edit with a typo reads as the defaults, not as silence. +assert.strictEqual(L.appMap("typo"), L.apps); +assert.strictEqual(L.appMap(["not", "a", "map"]), L.apps); + +// A saved set replaces the defaults, class by class. +assert.strictEqual(L.pickApp("firefox", { firefox: ["hi"] }, 0), "hi"); +assert.strictEqual(L.pickApp("kitty", { firefox: ["hi"] }, 0), ""); +assert.strictEqual(L.pick("app:firefox", 0, { firefox: ["custom"] }), "custom"); +assert.strictEqual(L.pick("app:firefox", 0), L.apps.firefox[0]); + +// Empty phrases are skipped when picking; a class with only empty ones says +// nothing. +assert.strictEqual(L.pickApp("firefox", { firefox: ["", " ", "only"] }, 0), "only"); +assert.strictEqual(L.pickApp("firefox", { firefox: ["", " "] }, 0), ""); + +// Reactions still come from lines. +assert.strictEqual(L.pick("shown", 0), L.lines.shown[0]); +assert.strictEqual(L.pick("nope", 0), ""); + +// The saved set as an ordered list, for the settings page. +const list = L.appList(null); +assert.strictEqual(list.length, Object.keys(L.apps).length); +assert.deepStrictEqual(list[0], { cls: "firefox", lines: L.apps.firefox.slice() }); +assert.deepStrictEqual(L.appList({ kitty: ["a"] }), [{ cls: "kitty", lines: ["a"] }]); + +// Back to a map: trim, lowercase the class, drop empty classes and empty +// phrases, and a repeated class keeps its last entry. +assert.deepStrictEqual(L.toMap([ + { cls: " Firefox ", lines: [" one ", "", " ", "two"] }, + { cls: "", lines: ["dropped"] }, + { cls: "kitty", lines: ["", " "] }, + { cls: "firefox", lines: ["last"] }, +]), { firefox: ["last"] }); + +console.log("ok"); |
