summaryrefslogtreecommitdiffstats
path: root/desktop/modules/hyprland/BindsPage.qml
blob: eca71bea443eb00ada95b44fe757fb6ad1725e17 (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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
// 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 "Hypr.js" as H

// One row per live bind: the combo as keycaps, what it does (its
// description, then the action as the Lua source writes it), and where it is
// set. Clicking a row opens its combo for editing when the source writes it
// as a plain string; one built at run time (a loop) says where to change it.
// Source shows the files that hold binds, Clashes the combos bound twice.
Column {
    id: page

    required property QtObject mod
    property string tab: "list"
    property string query: ""
    // The mod.rows index being edited, -1 for none.
    property int editing: -1
    property string draft: ""
    property string error: ""
    property bool busy: false

    spacing: 10

    readonly property var shown: {
        const q = page.query.trim().toLowerCase();
        const out = [];
        page.mod.rows.forEach((r, i) => {
            const hay = r.keys.join(" + ") + " " + r.desc + " " + (r.src ? r.src.action + " " + r.src.file : "");
            if (q === "" || hay.toLowerCase().includes(q)) out.push({ r: r, i: i });
        });
        return out;
    }
    readonly property var clashes: H.clashes(page.mod.rows)
        .reduce((a, g) => a.concat(g.map(r => ({ r: r, i: page.mod.rows.indexOf(r) }))), [])

    // Other binds already on the combo being typed, in the same submap.
    readonly property var draftClash: {
        const c = H.parseCombo(page.draft);
        const me = page.mod.rows[page.editing];
        if (!c || !me) return [];
        return page.mod.rows.filter(r => r !== me && r.bind.submap === me.bind.submap
                                         && r.bind.modmask === c.mask && r.bind.key.toLowerCase() === c.key.toLowerCase());
    }

    // The files holding binds, each line numbered. Reads mod.rows so it
    // recomputes when a file is reloaded.
    readonly property var sourceFiles: {
        const used = [];
        for (const r of page.mod.rows) if (r.src && !used.includes(r.src.file)) used.push(r.src.file);
        return page.mod.files.filter(f => used.includes(f)).map(f => ({
            file: f,
            text: (page.mod.texts[f] ?? "").replace(/\n$/, "").split("\n").map((l, i) => String(i + 1).padStart(4) + "  " + l).join("\n")
        }));
    }

    function toggle(i) {
        page.error = "";
        if (page.editing === i) { page.editing = -1; return; }
        page.editing = i;
        const r = page.mod.rows[i];
        page.draft = r.src && r.src.key !== null ? r.src.key : "";
    }

    function save() {
        if (page.busy || page.editing < 0) return;
        page.busy = true;
        page.error = "";
        page.mod.rebind(page.mod.rows[page.editing], page.draft, err => {
            page.busy = false;
            page.error = err;
            if (err === "") page.editing = -1;
        });
    }

    component Small: Text {
        color: Theme.subtext
        font { family: Theme.fontFamily; pixelSize: Theme.fontSize - 4 }
        elide: Text.ElideRight
    }

    component Keycap: Rectangle {
        property alias text: capLabel.text
        width: capLabel.implicitWidth + 10
        height: 22
        radius: 4
        color: Theme.surfaceAlt
        Text {
            id: capLabel
            anchors.centerIn: parent
            color: Theme.text
            font { family: Theme.iconFamily; pixelSize: Theme.fontSize - 4; bold: true }
        }
    }

    component BindRow: Column {
        id: br
        required property var row
        required property int idx
        readonly property bool open: page.editing === br.idx
        readonly property bool editable: br.row.src !== null && br.row.src.key !== null
        objectName: "bind:" + br.idx
        onOpenChanged: if (open && editable) comboField.focusIn()
        width: page.width
        spacing: 6

        Item {
            width: parent.width
            height: 46

            Rectangle {
                anchors.fill: parent
                radius: 6
                color: rowArea.containsMouse || br.open ? Qt.alpha(Theme.accent, 0.12) : "transparent"
            }

            Row {
                x: 8
                width: parent.width * 0.3 - 16
                anchors.verticalCenter: parent.verticalCenter
                spacing: 4
                clip: true
                Repeater {
                    model: br.row.keys
                    Row {
                        required property string modelData
                        required property int index
                        spacing: 4
                        Text {
                            visible: index > 0
                            anchors.verticalCenter: parent.verticalCenter
                            text: "+"
                            color: Theme.subtext
                            font { family: Theme.fontFamily; pixelSize: Theme.fontSize - 4 }
                        }
                        Keycap { text: modelData }
                    }
                }
            }

            Column {
                x: parent.width * 0.3
                width: parent.width * 0.5 - 12
                anchors.verticalCenter: parent.verticalCenter
                Text {
                    width: parent.width
                    text: br.row.desc !== "" ? br.row.desc : "No description"
                    color: br.row.desc !== "" ? Theme.text : Theme.subtext
                    elide: Text.ElideRight
                    font { family: Theme.fontFamily; pixelSize: Theme.fontSize - 3 }
                }
                Text {
                    width: parent.width
                    visible: br.row.src !== null
                    text: br.row.src ? br.row.src.action : ""
                    color: Theme.blue
                    elide: Text.ElideRight
                    font { family: Theme.iconFamily; pixelSize: Theme.fontSize - 4 }
                }
            }

            Small {
                x: parent.width * 0.8
                width: parent.width * 0.2 - 8
                anchors.verticalCenter: parent.verticalCenter
                text: br.row.src ? br.row.src.file.replace(/^sections\//, "") + ":" + br.row.src.line : "outside the config"
            }

            MouseArea {
                id: rowArea
                anchors.fill: parent
                hoverEnabled: true
                cursorShape: Qt.PointingHandCursor
                onClicked: page.toggle(br.idx)
            }
        }

        Column {
            visible: br.open
            x: 8
            width: parent.width - 16
            spacing: 6
            bottomPadding: 8

            Small {
                width: parent.width
                visible: !br.editable
                wrapMode: Text.Wrap
                text: br.row.src
                    ? "Built at run time, " + br.row.src.keyExpr + " in " + br.row.src.file + " line " + br.row.src.line + ". Change it there."
                    : "No hl.bind call in the config files made this one: a plugin or hyprctl did."
            }

            Item {
                visible: br.editable
                width: parent.width
                height: 32
                Field {
                    id: comboField
                    anchors { left: parent.left; right: saveBtn.left; rightMargin: 8 }
                    mono: true
                    text: page.draft
                    placeholder: "SUPER + SHIFT + q"
                    onEdited: t => page.draft = t
                    onAccepted: page.save()
                }
                Button {
                    id: saveBtn
                    anchors { right: cancelBtn.left; rightMargin: 6; verticalCenter: parent.verticalCenter }
                    text: page.busy ? "Checking…" : "Save"
                    enabled: !page.busy && page.draft.trim() !== "" && page.draft.trim() !== br.row.src?.key
                    onClicked: page.save()
                }
                Button {
                    id: cancelBtn
                    anchors { right: parent.right; verticalCenter: parent.verticalCenter }
                    text: "Cancel"
                    onClicked: page.toggle(br.idx)
                }
            }

            Small {
                width: parent.width
                visible: br.editable && page.draftClash.length > 0
                wrapMode: Text.Wrap
                color: Theme.yellow
                text: "Already bound: " + page.draftClash.map(r => r.desc || r.src?.action || "?").join(", ") + ". Saving keeps both."
            }
            Small {
                width: parent.width
                visible: br.editable && page.error !== ""
                wrapMode: Text.Wrap
                color: Theme.red
                text: page.error
            }
            Small {
                width: parent.width
                visible: br.editable
                wrapMode: Text.Wrap
                text: "Hyprland checks the combo first. Saving rewrites line " + (br.row.src?.line ?? "") + " of "
                    + (br.row.src?.file ?? "") + ", and Hyprland reloads."
            }
        }
    }

    Small {
        width: parent.width
        text: page.mod.rows.length + " bindings, live from Hyprland."
    }

    Item {
        width: parent.width
        height: 32
        Row {
            id: tabs
            spacing: 6
            anchors.verticalCenter: parent.verticalCenter
            Chip { text: "List"; selected: page.tab === "list"; onClicked: page.tab = "list" }
            Chip { text: "Source"; selected: page.tab === "source"; onClicked: page.tab = "source" }
            Chip {
                text: "Clashes · " + H.clashes(page.mod.rows).length
                selected: page.tab === "clashes"
                onClicked: page.tab = "clashes"
            }
        }
        Field {
            visible: page.tab === "list"
            anchors { left: tabs.right; leftMargin: 12; right: parent.right }
            text: page.query
            placeholder: "Search keys, actions and files"
            onEdited: t => page.query = t
        }
    }

    Item {
        visible: page.tab !== "source"
        width: parent.width
        height: 20
        Small { x: 8; text: "Keys" }
        Small { x: parent.width * 0.3; text: "Does" }
        Small { x: parent.width * 0.8; text: "Set at" }
    }

    Repeater {
        model: page.tab === "list" ? page.shown : page.tab === "clashes" ? page.clashes : []
        BindRow {
            required property var modelData
            row: modelData.r
            idx: modelData.i
        }
    }

    Small {
        width: parent.width
        visible: page.tab === "clashes" && page.clashes.length === 0
        text: "No combo is bound twice."
    }

    Repeater {
        model: page.tab === "source" ? page.sourceFiles : []
        Column {
            required property var modelData
            width: page.width
            spacing: 4
            Text {
                text: modelData.file
                color: Theme.text
                font { family: Theme.fontFamily; pixelSize: Theme.fontSize - 2; bold: true }
            }
            TextEdit {
                width: parent.width
                readOnly: true
                selectByMouse: true
                textFormat: TextEdit.PlainText
                wrapMode: TextEdit.NoWrap
                clip: true
                text: modelData.text
                color: Theme.text
                selectionColor: Qt.alpha(Theme.accent, 0.4)
                font { family: Theme.iconFamily; pixelSize: Theme.fontSize - 4 }
            }
        }
    }
}