diff options
| author | Danilo M. <danix@danix.xyz> | 2026-10-23 17:54:25 +0200 |
|---|---|---|
| committer | Danilo M. <danix@danix.xyz> | 2026-10-23 17:54:25 +0200 |
| commit | 588d78a19cac279b240588f47c26442d5abf3a12 (patch) | |
| tree | 844da2fd64710edf34ba5896387184bcda602c2e /desktop | |
| parent | c3a6fd802117ec6c3c4355d8d2b6016f0cd21778 (diff) | |
| download | quickshell-588d78a19cac279b240588f47c26442d5abf3a12.tar.gz quickshell-588d78a19cac279b240588f47c26442d5abf3a12.zip | |
companion: editable per-app lines in a settings page
Her per-app lines were hardcoded in Lines.js under app:<class>. They now live in Prefs.companionApps once edited and fall back to Lines.js until then, read live by Companion.qml so a save needs no restart. The new Companion phrases page does full CRUD on apps and their phrases, with a draft, SaveBar and Reset to the built-in set, mirroring Ask presets. Companion settings move out of Desktop into their own Companion group.
Diffstat (limited to 'desktop')
| -rw-r--r-- | desktop/Prefs.qml | 19 | ||||
| -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 | ||||
| -rw-r--r-- | desktop/modules/companion/CompanionModule.qml | 2 | ||||
| -rw-r--r-- | desktop/modules/companion/PhrasesModule.qml | 64 | ||||
| -rw-r--r-- | desktop/modules/companion/PhrasesPage.qml | 188 | ||||
| -rw-r--r-- | desktop/shell.qml | 1 |
9 files changed, 410 insertions, 26 deletions
diff --git a/desktop/Prefs.qml b/desktop/Prefs.qml index 80515a4..f7e841b 100644 --- a/desktop/Prefs.qml +++ b/desktop/Prefs.qml @@ -75,6 +75,21 @@ Singleton { root.write(Object.assign({}, root.doc, { companionBlushFile: p })); } + // The companion's app lines as saved: a map of lowercased window class + // to phrases, or null for the defaults in companion/Lines.js. The + // Companion phrases page edits it. + property var companionApps: null + function setCompanionApps(map) { + root.companionApps = map; + root.write(Object.assign({}, root.doc, { companionApps: map })); + } + function resetCompanionApps() { + root.companionApps = null; + const d = Object.assign({}, root.doc); + delete d.companionApps; + root.write(d); + } + // The settings window's sidebar width, dragged by its edge. readonly property int sidebarMin: 160 readonly property int sidebarMax: 420 @@ -137,6 +152,8 @@ Singleton { const sk = parsed.companionSkirtless; root.companionSkirtless = Number.isInteger(sk) && sk >= 0 ? sk : 60; root.companionBlushFile = typeof parsed.companionBlushFile === "string" ? parsed.companionBlushFile : ""; + const ca = parsed.companionApps; + root.companionApps = ca && typeof ca === "object" && !Array.isArray(ca) ? ca : null; root.ready = true; } @@ -152,6 +169,6 @@ Singleton { onFileChanged: reload() onLoaded: root.reparse() // Missing file: the defaults, and nothing written. - onLoadFailed: { root.doc = {}; root.pinnedIp = ""; root.presets = null; root.settingsSidebar = 200; root.assistantModel = ""; root.companion = true; root.companionBlushFile = ""; root.companionSkirtless = 60; root.ready = true; } + onLoadFailed: { root.doc = {}; root.pinnedIp = ""; root.presets = null; root.settingsSidebar = 200; root.assistantModel = ""; root.companion = true; root.companionBlushFile = ""; root.companionSkirtless = 60; root.companionApps = null; root.ready = true; } } } 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"); diff --git a/desktop/modules/companion/CompanionModule.qml b/desktop/modules/companion/CompanionModule.qml index 6ed36ec..8d6a68d 100644 --- a/desktop/modules/companion/CompanionModule.qml +++ b/desktop/modules/companion/CompanionModule.qml @@ -23,7 +23,7 @@ Module { // nf-md-account_heart, present in Inconsolata Nerd Font. icon: "\u{F0899}" settings: true - group: "Desktop" + group: "Companion" keywords: ["companion", "2b", "mascot", "character", "show", "hide", "wallpaper", "blush", "leotard", "timeout"] page: Component { diff --git a/desktop/modules/companion/PhrasesModule.qml b/desktop/modules/companion/PhrasesModule.qml new file mode 100644 index 0000000..7e4895a --- /dev/null +++ b/desktop/modules/companion/PhrasesModule.qml @@ -0,0 +1,64 @@ +// 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. + +import QtQuick +import "../.." +import "../../companion/Lines.js" as Lines + +// The lines the companion says when focus moves to a program she +// recognizes, by its lowercased window class. They live in Prefs and she +// reads them live, so a save needs no restart. The page edits a draft here; +// Save hands it to Prefs, Reset drops the saved set so Lines.js' defaults +// show again. +Module { + id: pm + name: "phrases" + label: "Companion phrases" + // nf-fa-comment, present in Inconsolata Nerd Font. + icon: "\uf075" + settings: true + group: "Companion" + keywords: ["companion", "phrase", "line", "say", "speak", "app", "program", + "window", "class", "firefox", "2b"] + searchItems: pm.shown.map((a, i) => ({ text: a.cls + ": " + a.lines.join(" / "), key: "app:" + i })) + + // null while there are no edits: the page then shows the saved set, or + // Lines.js' defaults when nothing is saved. + property var draft: null + // Reset was pressed: Save drops the saved set rather than saving a copy + // of the defaults, so later default changes still arrive. + property bool resetting: false + readonly property var shown: pm.draft ?? Lines.appList(Prefs.companionApps) + // Every app needs a class and at least one non-empty phrase; Save waits. + readonly property bool valid: pm.shown.every(a => String(a.cls).trim() !== "" + && (a.lines || []).some(l => String(l).trim() !== "")) + dirty: pm.draft !== null + + function edit(list) { pm.draft = list; pm.resetting = false; } + function reset() { pm.draft = Lines.appList(null); pm.resetting = true; } + function save() { + if (pm.resetting) Prefs.resetCompanionApps(); + else Prefs.setCompanionApps(Lines.toMap(pm.draft)); + pm.discard(); + } + function discard() { pm.draft = null; pm.resetting = false; } + + page: Component { + Page { + title: "Companion phrases" + actions: Component { + SaveBar { dirty: pm.dirty; valid: pm.valid; note: "every app needs a class and a phrase" + onSave: pm.save(); onDiscard: pm.discard() } + } + PhrasesPage { width: parent.width; owner: pm } + } + } +} diff --git a/desktop/modules/companion/PhrasesPage.qml b/desktop/modules/companion/PhrasesPage.qml new file mode 100644 index 0000000..67c1e03 --- /dev/null +++ b/desktop/modules/companion/PhrasesPage.qml @@ -0,0 +1,188 @@ +// 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. + +import QtQuick +import "../.." + +// One card per recognized program: its window class and the lines she says +// when focus moves to it. Every change edits the module's draft; the +// header's Save hands the set to Prefs. Reset fills the draft with the +// built-in set from Lines.js. +Column { + id: page + spacing: 10 + + // The module, holding the draft. Not named pm, its id (it would bind to + // itself, AGENTS.md). + required property QtObject owner + readonly property var list: owner.shown + // The app card awaiting a second click on Remove. + property int confirming: -1 + + // Saves a copy of the list with app i replaced by fn(app), or removed + // when fn returns null. + function edit(i, fn) { + const out = []; + for (let j = 0; j < page.list.length; j++) { + const a = page.list[j]; + const c = { cls: a.cls, lines: a.lines.slice() }; + if (j !== i) { out.push(c); continue; } + const r = fn(c); + if (r) out.push(r); + } + page.confirming = -1; + page.owner.edit(out); + } + + function addApp() { page.owner.edit(page.list.concat([{ cls: "", lines: [""] }])); } + function removeApp(i) { page.edit(i, () => null); } + function setCls(i, t) { page.edit(i, a => Object.assign(a, { cls: t })); } + function addLine(i) { page.edit(i, a => Object.assign(a, { lines: a.lines.concat([""]) })); } + function setLine(i, j, t) { + page.edit(i, a => { + const ls = a.lines.slice(); + ls[j] = t; + return Object.assign(a, { lines: ls }); + }); + } + function removeLine(i, j) { page.edit(i, a => Object.assign(a, { lines: a.lines.filter((_, k) => k !== j) })); } + + component Small: Text { + color: Theme.subtext + font { family: Theme.fontFamily; pixelSize: Theme.fontSize - 4 } + } + + // Reports every keystroke into the draft. Not bound to value, which + // would reset the cursor; it follows the value only when the draft moves + // under it (Discard, Reset). Empty is outlined, since Save refuses it. + component Field: Rectangle { + id: field + property string value: "" + property string placeholder: "" + signal edited(string text) + height: 32 + radius: 8 + color: Qt.alpha(Theme.base, 0.6) + border.width: ti.text.trim() === "" ? 1 : 0 + border.color: Theme.red + onValueChanged: if (ti.text !== field.value) ti.text = field.value + TextInput { + activeFocusOnTab: true + id: ti + anchors { fill: parent; leftMargin: 10; rightMargin: 10 } + verticalAlignment: TextInput.AlignVCenter + clip: true + selectByMouse: true + color: Theme.text + font { family: Theme.fontFamily; pixelSize: Theme.fontSize - 3 } + Component.onCompleted: text = field.value + onTextEdited: field.edited(text) + Text { + anchors.verticalCenter: parent.verticalCenter + visible: parent.text === "" + text: field.placeholder + color: Theme.overlay + font: parent.font + } + } + } + + Small { + width: page.width + wrapMode: Text.Wrap + text: "When focus moves to a program she recognizes, she says one of its lines at random, a third " + + "of the time and under her 30 s line cooldown. The class is Hyprland's window class, " + + "lowercased (hyprctl activewindow shows it). Save applies at once." + } + + Repeater { + // A count, not the list: a new list on every keystroke would rebuild + // every card and drop the focus of the field being typed in. + model: page.list.length + + delegate: Rectangle { + id: card + required property int index + readonly property var modelData: page.list[index] ?? { cls: "", lines: [] } + objectName: "app:" + index + width: page.width + height: body.implicitHeight + 20 + radius: 10 + color: Qt.alpha(Theme.surface, 0.45) + + Column { + id: body + anchors { left: parent.left; right: parent.right; top: parent.top; margins: 10 } + spacing: 8 + + Item { + width: parent.width + height: 32 + Field { + anchors { left: parent.left; right: del.left; rightMargin: 8; top: parent.top; bottom: parent.bottom } + value: card.modelData.cls + placeholder: "window class, e.g. firefox" + onEdited: t => page.setCls(card.index, t) + } + Button { + id: del + anchors { right: parent.right; verticalCenter: parent.verticalCenter } + danger: true + text: page.confirming === card.index ? "Confirm" : "Remove" + onClicked: { + if (page.confirming === card.index) page.removeApp(card.index); + else page.confirming = card.index; + } + } + } + + Repeater { + model: card.modelData.lines.length + delegate: Row { + id: phraseRow + required property int index + width: body.width + spacing: 6 + Field { + width: phraseRow.width - removeBtn.width - phraseRow.spacing + value: card.modelData.lines[phraseRow.index] ?? "" + placeholder: "a line she says" + onEdited: t => page.setLine(card.index, phraseRow.index, t) + } + Button { + id: removeBtn + // nf-fa-xmark, present in Inconsolata Nerd Font. + icon: "\uf00d" + danger: true + onClicked: page.removeLine(card.index, phraseRow.index) + } + } + } + + Button { + text: "Add phrase" + onClicked: page.addLine(card.index) + } + } + } + } + + Row { + spacing: 8 + Button { text: "Add app"; onClicked: page.addApp() } + Button { + text: "Reset to defaults" + danger: true + enabled: !page.owner.resetting && (Prefs.companionApps !== null || page.owner.dirty) + onClicked: page.owner.reset() + } + } +} diff --git a/desktop/shell.qml b/desktop/shell.qml index 8aaead3..1331673 100644 --- a/desktop/shell.qml +++ b/desktop/shell.qml @@ -88,6 +88,7 @@ ShellRoot { SunsetModule {}, IdleModule {}, CompanionModule {}, + PhrasesModule {}, NotificationsModule {}, AutostartModule {}, ObsScenesModule {}, |
