aboutsummaryrefslogtreecommitdiffstats
path: root/desktop/modules/monitors/MonitorsPage.qml
blob: 06fc182d29bf09efa98ec1ed054d7b424d0b9731 (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
// 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 QtQuick.Controls as C
import "../.."
import "../appearance" as A
import "Mon.js" as M

// The keep-or-revert bar while an Apply is live, the layout drawn to scale
// (drag a monitor to move it; it snaps to the other's edges), and one card
// per monitor: mode, scale, rotation, VRR, position.
Column {
    id: page
    required property QtObject mod
    spacing: 14

    // Leaving the page with an Apply unanswered is a Revert.
    Component.onDestruction: if (page.mod.pending) page.mod.revert()

    // The stock ComboBox paints from the palette, light by default; these
    // are the desktop's colours, as in qtmaildir's TagsKeysPage.
    component Combo: C.ComboBox {
        font { family: Theme.fontFamily; pixelSize: Theme.fontSize - 3 }
        palette {
            base: Theme.surface; window: Theme.base; button: Theme.surface
            text: Theme.text; buttonText: Theme.text; windowText: Theme.text
            highlight: Theme.accent; highlightedText: Theme.base
            mid: Qt.alpha(Theme.text, 0.2); dark: Qt.alpha(Theme.text, 0.2); light: Theme.surface
        }
    }

    component Label: Text {
        width: 90
        anchors.verticalCenter: parent.verticalCenter
        font { family: Theme.fontFamily; pixelSize: Theme.fontSize - 3 }
        color: Theme.subtext
    }

    Rectangle {
        visible: page.mod.pending
        width: parent.width
        height: 44
        radius: 10
        color: Qt.alpha(Theme.yellow, 0.18)
        border.width: 1
        border.color: Qt.alpha(Theme.yellow, 0.5)
        Text {
            anchors { left: parent.left; leftMargin: 14; verticalCenter: parent.verticalCenter }
            text: "Keep these settings? Reverting in " + page.mod.countdown + "s"
            font { family: Theme.fontFamily; pixelSize: Theme.fontSize - 2 }
            color: Theme.text
        }
        Row {
            anchors { right: parent.right; rightMargin: 10; verticalCenter: parent.verticalCenter }
            spacing: 8
            Button { text: "Revert"; onClicked: page.mod.revert() }
            Button { text: "Keep"; onClicked: page.mod.keep() }
        }
    }

    // ---- layout ----
    Rectangle {
        id: canvas
        width: parent.width
        height: 260
        radius: 10
        color: Theme.surface

        // Logical rectangles, and the fit of their bounds into the canvas.
        readonly property var rects: page.mod.draft.map(d => {
            const p = M.xy(d), s = M.size(d.mode, d.scale, d.transform);
            return { x: p.x, y: p.y, w: s.w, h: s.h };
        })
        readonly property real minX: Math.min.apply(null, rects.map(r => r.x).concat([0]))
        readonly property real minY: Math.min.apply(null, rects.map(r => r.y).concat([0]))
        readonly property real maxX: Math.max.apply(null, rects.map(r => r.x + r.w).concat([1]))
        readonly property real maxY: Math.max.apply(null, rects.map(r => r.y + r.h).concat([1]))
        readonly property real k: Math.min((width - 40) / (maxX - minX), (height - 40) / (maxY - minY))
        readonly property real ox: (width - (maxX - minX) * k) / 2
        readonly property real oy: (height - (maxY - minY) * k) / 2

        Repeater {
            model: page.mod.draft.length
            Rectangle {
                id: mon
                required property int index
                readonly property var r: canvas.rects[index] ?? { x: 0, y: 0, w: 1, h: 1 }
                readonly property var d: page.mod.draft[index]
                // The drag offset in canvas pixels, zero when not dragging.
                property real dx: 0
                property real dy: 0
                x: canvas.ox + (r.x - canvas.minX) * canvas.k + dx
                y: canvas.oy + (r.y - canvas.minY) * canvas.k + dy
                width: r.w * canvas.k
                height: r.h * canvas.k
                radius: 6
                color: Qt.alpha(Theme.accent, drag.pressed ? 0.35 : 0.18)
                border.width: 1
                border.color: Qt.alpha(Theme.accent, 0.7)

                Column {
                    anchors.centerIn: parent
                    Text {
                        anchors.horizontalCenter: parent.horizontalCenter
                        text: mon.d ? mon.d.name : ""
                        font { family: Theme.fontFamily; pixelSize: Theme.fontSize - 1; bold: true }
                        color: Theme.text
                    }
                    Text {
                        anchors.horizontalCenter: parent.horizontalCenter
                        text: mon.d ? mon.d.model : ""
                        font { family: Theme.fontFamily; pixelSize: Theme.fontSize - 5 }
                        color: Theme.subtext
                    }
                }

                MouseArea {
                    id: drag
                    anchors.fill: parent
                    enabled: !page.mod.pending
                    // The page's ScrollView would take a vertical drag.
                    preventStealing: true
                    cursorShape: pressed ? Qt.ClosedHandCursor : Qt.OpenHandCursor
                    property point start
                    onPressed: m => start = mapToItem(canvas, m.x, m.y)
                    onPositionChanged: m => {
                        const p = mapToItem(canvas, m.x, m.y);
                        mon.dx = p.x - start.x;
                        mon.dy = p.y - start.y;
                    }
                    onCanceled: { mon.dx = 0; mon.dy = 0; }
                    onReleased: {
                        // A click is not a move: it would snap, and make an auto position exact.
                        if (Math.hypot(mon.dx, mon.dy) < 4) { mon.dx = 0; mon.dy = 0; return; }
                        const moved = { x: mon.r.x + mon.dx / canvas.k, y: mon.r.y + mon.dy / canvas.k, w: mon.r.w, h: mon.r.h };
                        const others = canvas.rects.filter((o, i) => i !== mon.index);
                        const s = M.snap(moved, others, 64);
                        mon.dx = 0;
                        mon.dy = 0;
                        if (s.x !== mon.r.x || s.y !== mon.r.y) page.mod.set(mon.index, "position", s.x + "x" + s.y);
                    }
                }
            }
        }
    }

    // ---- cards ----
    // Keyed on orig, a new array on every read, so Discard and a reload
    // rebuild the cards and their controls rebind to the draft: Toggle and
    // ComboBox set their own checked and currentIndex on a click.
    Repeater {
        model: page.mod.orig
        Rectangle {
            id: card
            required property int index
            readonly property var d: page.mod.draft[index]
            readonly property bool auto: !!d && M.AUTO.includes(d.position)
            objectName: d ? "mon:" + d.name : ""
            width: page.width
            height: rows.implicitHeight + 28
            radius: 10
            color: Theme.surface
            visible: !!d

            Column {
                id: rows
                anchors { fill: parent; margins: 14 }
                // Keep writes the draft, so it may not change from what was tried.
                enabled: !page.mod.pending
                spacing: 8

                Text {
                    text: card.d ? card.d.name + "   " + card.d.make + " " + card.d.model : ""
                    font { family: Theme.fontFamily; pixelSize: Theme.fontSize - 1; bold: true }
                    color: Theme.text
                }

                Row {
                    spacing: 8
                    Label { text: "Mode" }
                    Combo {
                        width: 220
                        // From orig, which set() leaves alone: a new model on every edit
                        // could reset the shown index.
                        model: page.mod.orig[card.index] ? page.mod.orig[card.index].modes.map(m => m.replace("@", " @ ") + " Hz") : []
                        currentIndex: card.d ? card.d.modes.indexOf(card.d.mode) : -1
                        onActivated: i => page.mod.set(card.index, "mode", card.d.modes[i])
                    }
                }

                Row {
                    spacing: 8
                    Label { text: "Scale" }
                    // Read on every change: a click on Apply does not end editing.
                    A.Field {
                        value: card.d ? card.d.scale : ""
                        onValueChanged: if (card.d && value !== card.d.scale) page.mod.set(card.index, "scale", value)
                    }
                }

                Row {
                    spacing: 8
                    Label { text: "Rotation" }
                    Repeater {
                        model: [0, 90, 180, 270]
                        A.Tab {
                            required property int modelData
                            required property int index
                            text: modelData + "°"
                            selected: !!card.d && card.d.transform % 4 === index
                            onClicked: page.mod.set(card.index, "transform", (card.d.transform >= 4 ? 4 : 0) + index)
                        }
                    }
                    A.Toggle {
                        anchors.verticalCenter: parent.verticalCenter
                        label: "Flipped"
                        checked: !!card.d && card.d.transform >= 4
                        onToggled: page.mod.set(card.index, "transform", card.d.transform % 4 + (checked ? 4 : 0))
                    }
                }

                Row {
                    spacing: 8
                    Label { text: "VRR" }
                    A.Toggle {
                        anchors.verticalCenter: parent.verticalCenter
                        label: "Adaptive sync"
                        checked: !!card.d && card.d.vrr
                        onToggled: page.mod.set(card.index, "vrr", checked)
                    }
                }

                Row {
                    spacing: 8
                    Label { text: "Position" }
                    Combo {
                        width: 220
                        model: ["exact"].concat(M.AUTO)
                        currentIndex: card.auto ? M.AUTO.indexOf(card.d.position) + 1 : 0
                        // Rebound after a pick, so a later drag to an exact
                        // position shows here.
                        onActivated: i => {
                            page.mod.set(card.index, "position", i === 0 ? card.d.x + "x" + card.d.y : M.AUTO[i - 1]);
                            currentIndex = Qt.binding(() => card.auto ? M.AUTO.indexOf(card.d.position) + 1 : 0);
                        }
                    }
                    Text {
                        anchors.verticalCenter: parent.verticalCenter
                        visible: !card.auto
                        text: card.d ? card.d.position.replace("x", ", ") + "   drag in the layout to move" : ""
                        font { family: Theme.fontFamily; pixelSize: Theme.fontSize - 4 }
                        color: Theme.subtext
                    }
                }
            }
        }
    }

    Text {
        width: parent.width
        wrapMode: Text.Wrap
        text: "Apply tries the settings live. Keep writes sections/monitors.lua; Revert, leaving the page, "
            + "or 15 seconds with no answer restores it. Only connected monitors are written."
        font { family: Theme.fontFamily; pixelSize: Theme.fontSize - 4 }
        color: Theme.subtext
    }
}