aboutsummaryrefslogtreecommitdiffstats
path: root/desktop/modules/companion/PhrasesModule.qml
blob: 7e4895a03c63406a960ab78a93a5a7f16edb048c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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 }
        }
    }
}