| @@ -3,8 +3,11 @@ import QtQuick.Layouts |
| 3 | 3 | | import Quickshell |
| 4 | 4 | | import Quickshell.Io |
| 5 | 5 | | |
| 6 | | − | // Settings app > Display: monitors (live, with a keep/revert countdown), kanshi profiles, brightness, night light. |
| 7 | | − | // Monitor changes are runtime only: kanshi re-applies its profile on hotplug, persistent layouts live in its config. |
| 6 | + | // Settings app > Display: layout + per-monitor mode/scale/position, brightness, night light. |
| 7 | + | // Monitors are Hyprland rules (hypr/source/monitors.lua, global `monitors`). A change is applied live with |
| 8 | + | // `hyprctl eval 'monitors.apply()'` behind a keep/revert countdown, and only saved on keep: |
| 9 | + | // ~/.config/aurora/monitors.json {"layout": "extend", "outputs": {"eDP-1": {mode, position, scale}}} (state) |
| 10 | + | // ~/.config/aurora/monitors.lua the same as a Lua table, dofile'd by monitors.lua |
| 8 | 11 | | Flickable { |
| 9 | 12 | | id: flick |
| 10 | 13 | | clip: true |
| @@ -12,55 +15,81 @@ Flickable { |
| 12 | 15 | | contentHeight: col.implicitHeight |
| 13 | 16 | | boundsBehavior: Flickable.StopAtBounds |
| 14 | 17 | | |
| 15 | | − | readonly property string kanshiFile: Quickshell.env("HOME") + "/.config/kanshi/config" |
| 18 | + | readonly property string dir: Quickshell.env("HOME") + "/.config/aurora" |
| 16 | 19 | | property var monitors: [] // hyprctl monitors all -j |
| 17 | | − | property var revertSpec: null // spec to restore if the change isn't kept |
| 20 | + | property var saved: ({ layout: "extend", outputs: {} }) |
| 21 | + | property string layout: "extend" // live value, differs from saved.layout during the countdown |
| 22 | + | property string revertCode: "" // lua that undoes the pending change |
| 23 | + | property var pendingSave: null // what keep() stores: { layout } or { output, spec } |
| 18 | 24 | | property int countdown: 0 |
| 19 | 25 | | property string message: "" |
| 20 | 26 | | |
| 21 | 27 | | function refresh() { monProc.running = true } |
| 22 | 28 | | Component.onCompleted: refresh() |
| 23 | 29 | | |
| 24 | | − | // monitor -> its current spec |
| 25 | | − | function specOf(m) { |
| 26 | | − | if (m.disabled) return { output: m.name, disabled: true } |
| 27 | | − | return { output: m.name, mode: m.width + "x" + m.height + "@" + m.refreshRate.toFixed(2), |
| 28 | | − | position: m.x + "x" + m.y, scale: m.scale } |
| 29 | | − | } |
| 30 | 30 | | // only characters a monitor name / mode / position can contain, so nothing can break out of the Lua string |
| 31 | 31 | | function q(s) { return '"' + String(s).replace(/[^A-Za-z0-9@x._\-]/g, "") + '"' } |
| 32 | | − | function lua(spec) { |
| 33 | | − | if (spec.disabled) return "hl.monitor({ output = " + q(spec.output) + ", disabled = true })" |
| 34 | | − | return "hl.monitor({ output = " + q(spec.output) + ", mode = " + q(spec.mode) + ", position = " |
| 35 | | − | + q(spec.position) + ", scale = " + Number(spec.scale) + " })" |
| 32 | + | function luaSpec(s) { |
| 33 | + | return "{ mode = " + q(s.mode) + ", position = " + q(s.position) + ", scale = " + (Number(s.scale) || 1) + " }" |
| 34 | + | } |
| 35 | + | function setOutputCode(name, s) { return "monitors.outputs[" + q(name) + "] = " + luaSpec(s) + "; monitors.apply()" } |
| 36 | + | function setLayoutCode(l) { return "monitors.layout = " + q(l) + "; monitors.apply()" } |
| 37 | + | |
| 38 | + | function specOf(m) { |
| 39 | + | return { mode: m.width + "x" + m.height + "@" + m.refreshRate.toFixed(2), position: m.x + "x" + m.y, scale: m.scale } |
| 36 | 40 | | } |
| 37 | 41 | | function apply(m, change) { |
| 38 | | − | var spec |
| 39 | | − | if (change.disabled) spec = { output: m.name, disabled: true } |
| 40 | | − | else if (m.disabled) spec = { output: m.name, mode: "preferred", position: "auto", scale: 1 } |
| 41 | | − | else { |
| 42 | | − | spec = specOf(m) |
| 43 | | − | for (var k in change) spec[k] = change[k] |
| 44 | | − | } |
| 45 | | − | revertSpec = specOf(m) |
| 46 | | − | run(lua(spec)) |
| 42 | + | var spec = specOf(m) |
| 43 | + | for (var k in change) spec[k] = change[k] |
| 44 | + | begin(setOutputCode(m.name, spec), setOutputCode(m.name, specOf(m)), { output: m.name, spec: spec }) |
| 45 | + | } |
| 46 | + | function setLayout(l) { |
| 47 | + | if (l === layout) return |
| 48 | + | var old = layout |
| 49 | + | layout = l |
| 50 | + | begin(setLayoutCode(l), setLayoutCode(old), { layout: l }) |
| 51 | + | } |
| 52 | + | function begin(code, undo, save) { |
| 53 | + | if (pendingSave) keep() // a second change while counting down: the first one evidently works |
| 54 | + | revertCode = undo |
| 55 | + | pendingSave = save |
| 56 | + | run(code) |
| 47 | 57 | | countdown = 15 |
| 48 | 58 | | tick.restart() |
| 49 | 59 | | } |
| 50 | | − | function keep() { revertSpec = null; countdown = 0; tick.stop() } |
| 60 | + | function keep() { |
| 61 | + | if (pendingSave) { |
| 62 | + | var o = { layout: saved.layout, outputs: Object.assign({}, saved.outputs) } |
| 63 | + | if (pendingSave.layout) o.layout = pendingSave.layout |
| 64 | + | else o.outputs[pendingSave.output] = pendingSave.spec |
| 65 | + | saved = o |
| 66 | + | save() |
| 67 | + | } |
| 68 | + | pendingSave = null |
| 69 | + | revertCode = "" |
| 70 | + | countdown = 0 |
| 71 | + | tick.stop() |
| 72 | + | } |
| 51 | 73 | | function revert() { |
| 52 | | − | if (revertSpec) run(lua(revertSpec)) |
| 53 | | − | keep() |
| 74 | + | if (revertCode !== "") run(revertCode) |
| 75 | + | if (pendingSave && pendingSave.layout) layout = saved.layout |
| 76 | + | pendingSave = null |
| 77 | + | revertCode = "" |
| 78 | + | countdown = 0 |
| 79 | + | tick.stop() |
| 80 | + | } |
| 81 | + | function save() { |
| 82 | + | jsonFile.setText(JSON.stringify(saved, null, 2) + "\n") |
| 83 | + | var lines = [] |
| 84 | + | for (var n in saved.outputs) lines.push(" [" + q(n) + "] = " + luaSpec(saved.outputs[n]) + ",") |
| 85 | + | luaFile.setText("-- generated by aurora's settings app (SetDisplay.qml); edit there, not here\n" |
| 86 | + | + "return {\n layout = " + q(saved.layout) + ",\n outputs = {\n" + lines.join("\n") |
| 87 | + | + (lines.length ? "\n" : "") + " },\n}\n") |
| 54 | 88 | | } |
| 55 | 89 | | function run(code) { |
| 56 | 90 | | evalProc.command = ["hyprctl", "eval", code] |
| 57 | 91 | | evalProc.running = true |
| 58 | 92 | | } |
| 59 | | − | function enabledCount() { |
| 60 | | − | var n = 0 |
| 61 | | − | for (var i = 0; i < monitors.length; i++) if (!monitors[i].disabled) n++ |
| 62 | | − | return n |
| 63 | | − | } |
| 64 | 93 | | |
| 65 | 94 | | Timer { |
| 66 | 95 | | id: tick |
| @@ -85,20 +114,28 @@ Flickable { |
| 85 | 114 | | } |
| 86 | 115 | | } |
| 87 | 116 | | } |
| 88 | | − | Timer { id: refreshLater; interval: 600; onTriggered: flick.refresh() } |
| 117 | + | Timer { id: refreshLater; interval: 800; onTriggered: flick.refresh() } |
| 89 | 118 | | |
| 90 | | − | // kanshi profiles: `profile NAME {` lines |
| 91 | | − | property var profiles: [] |
| 92 | 119 | | FileView { |
| 93 | | − | path: flick.kanshiFile |
| 94 | | − | printErrors: false |
| 120 | + | id: jsonFile |
| 121 | + | path: flick.dir + "/monitors.json" |
| 122 | + | blockLoading: true |
| 123 | + | printErrors: false // no file until the first kept change |
| 95 | 124 | | onLoaded: { |
| 96 | | − | var out = [], re = /^\s*profile\s+(\S+)\s*\{/gm, mm |
| 97 | | − | var t = text() |
| 98 | | − | while ((mm = re.exec(t)) !== null) out.push(mm[1]) |
| 99 | | − | flick.profiles = out |
| 125 | + | try { |
| 126 | + | var j = JSON.parse(text()) |
| 127 | + | flick.saved = { layout: j.layout || "extend", outputs: j.outputs || {} } |
| 128 | + | } catch (e) {} |
| 129 | + | flick.layout = flick.saved.layout |
| 100 | 130 | | } |
| 101 | 131 | | } |
| 132 | + | FileView { // write-only |
| 133 | + | id: luaFile |
| 134 | + | path: flick.dir + "/monitors.lua" |
| 135 | + | preload: false |
| 136 | + | printErrors: false |
| 137 | + | onSaveFailed: err => flick.message = "could not write monitors.lua (error " + err + ")" |
| 138 | + | } |
| 102 | 139 | | |
| 103 | 140 | | ColumnLayout { |
| 104 | 141 | | id: col |
| @@ -137,6 +174,23 @@ Flickable { |
| 137 | 174 | | font.pixelSize: Theme.fontSize - 1 |
| 138 | 175 | | } |
| 139 | 176 | | |
| 177 | + | SectionLabel { text: "layout" } |
| 178 | + | Choice { |
| 179 | + | label: "screens" |
| 180 | + | options: [{ value: "extend", label: "extend" }, { value: "laptop", label: "laptop only" }, |
| 181 | + | { value: "external", label: "external only" }] |
| 182 | + | value: flick.layout |
| 183 | + | onPicked: x => flick.setLayout(x) |
| 184 | + | } |
| 185 | + | Text { |
| 186 | + | Layout.fillWidth: true |
| 187 | + | text: "applies whenever a monitor is plugged in. external only turns the laptop panel back on when the last external screen is unplugged." |
| 188 | + | color: Theme.dim |
| 189 | + | wrapMode: Text.WordWrap |
| 190 | + | font.family: Theme.font |
| 191 | + | font.pixelSize: Theme.fontSize - 1 |
| 192 | + | } |
| 193 | + | |
| 140 | 194 | | Repeater { |
| 141 | 195 | | model: flick.monitors |
| 142 | 196 | | delegate: ColumnLayout { |
| @@ -158,15 +212,6 @@ Flickable { |
| 158 | 212 | | font.family: Theme.font |
| 159 | 213 | | font.pixelSize: Theme.fontSize - 1 |
| 160 | 214 | | } |
| 161 | | − | Switch { |
| 162 | | − | label: "enabled" |
| 163 | | − | hint: !mon.m.disabled && flick.enabledCount() <= 1 ? "the only active monitor can't be turned off" : "" |
| 164 | | − | checked: !mon.m.disabled |
| 165 | | − | onToggled: on => { |
| 166 | | − | if (!on && flick.enabledCount() <= 1) return |
| 167 | | − | flick.apply(mon.m, { disabled: !on }) |
| 168 | | − | } |
| 169 | | − | } |
| 170 | 215 | | Text { |
| 171 | 216 | | visible: !mon.m.disabled |
| 172 | 217 | | text: "mode" |
| @@ -212,39 +257,6 @@ Flickable { |
| 212 | 257 | | } |
| 213 | 258 | | } |
| 214 | 259 | | |
| 215 | | − | SectionLabel { text: "profiles (kanshi)" } |
| 216 | | − | Text { |
| 217 | | − | Layout.fillWidth: true |
| 218 | | − | text: "changes above are temporary: kanshi re-applies its profile when monitors are plugged in." |
| 219 | | − | color: Theme.dim |
| 220 | | − | wrapMode: Text.WordWrap |
| 221 | | − | font.family: Theme.font |
| 222 | | − | font.pixelSize: Theme.fontSize - 1 |
| 223 | | − | } |
| 224 | | − | Flow { |
| 225 | | − | Layout.fillWidth: true |
| 226 | | − | spacing: 4 |
| 227 | | − | Repeater { |
| 228 | | − | model: flick.profiles |
| 229 | | − | delegate: IconBtn { |
| 230 | | − | required property string modelData |
| 231 | | − | implicitHeight: 26 |
| 232 | | − | icon: Theme.iMonitor |
| 233 | | − | label: modelData |
| 234 | | − | onClicked: { |
| 235 | | − | Quickshell.execDetached(["kanshictl", "switch", modelData]) |
| 236 | | − | refreshLater.restart() |
| 237 | | − | } |
| 238 | | − | } |
| 239 | | − | } |
| 240 | | − | IconBtn { |
| 241 | | − | implicitHeight: 26 |
| 242 | | − | icon: Theme.iConsole |
| 243 | | − | label: "edit config" |
| 244 | | − | onClicked: Quickshell.execDetached(["sh", "-c", 'exec foot -e "${EDITOR:-nvim}" "$1"', "sh", flick.kanshiFile]) |
| 245 | | − | } |
| 246 | | − | } |
| 247 | | − | |
| 248 | 260 | | SectionLabel { text: "brightness & colour" } |
| 249 | 261 | | Knob { |
| 250 | 262 | | visible: Bright.available |