Files
lucid/Glass.qml
T
Sn3akyy1 ec6bbbe6ad Merge #46, #47: Windows and Input pages for Hyprland
# Conflicts:
#	Prefs.qml
#	README.md
#	lucidprefs/NavGlyph.qml
#	lucidprefs/Settings.qml
2026-09-29 17:58:08 +02:00

430 lines
15 KiB
QML

import QtQuick
import Quickshell
import Quickshell.Io
pragma Singleton
// the glass slider and what it reaches: kitty's own opacity file, and the
// per-app window rules in lucid-glass.lua for modules/glass.lua. also keeps
// hyprland's window borders on the palette, for modules/decorations.lua
Singleton {
id: root
readonly property string marker: "# generated by lucid settings"
readonly property string kittyDir: Quickshell.env("HOME") + "/.config/kitty"
readonly property string confPath: root.kittyDir + "/lucid-glass.conf"
readonly property string dataPath: Quickshell.env("HOME") + "/.config/hypr/lucid-glass.lua"
readonly property string modulePath: Quickshell.env("HOME") + "/.config/hypr/modules/glass.lua"
// one slider, three surfaces, each let through at its own weight: shell
// chrome furthest, kitty all the way (its text stays opaque either way),
// app windows least, because a window's opacity takes its text with it
readonly property real surfaceWeight: 0.85
readonly property real kittyWeight: 1
readonly property real appWeight: 0.25
readonly property real kittyOpacity: root.opacityOf("kitty")
readonly property real appFollow: root.round2(1 - Theme.blurAmount * root.appWeight)
function round2(v) {
return Math.round(Math.max(0, Math.min(1, v)) * 100) / 100;
}
// ---- apps -------------------------------------------------------------
// ids: desktop entries, native before flatpak. class: what hyprland sees,
// compared case-blind. terminals are left out: their own config owns the
// background, and a window rule would take the text with it
readonly property var catalog: [
{ "id": "kitty", "name": "kitty", "ids": ["kitty"], "class": ["kitty"], "selfManaged": true },
{ "id": "vscodium", "name": "VSCodium", "ids": ["codium", "vscodium"], "class": ["codium", "vscodium"] },
{ "id": "vscode", "name": "Visual Studio Code", "ids": ["code", "visual-studio-code", "code-oss"], "class": ["code", "code-oss", "code-url-handler"] },
{ "id": "zed", "name": "Zed", "ids": ["dev.zed.Zed", "zed"], "class": ["dev.zed.zed", "zed"] },
{ "id": "obsidian", "name": "Obsidian", "ids": ["obsidian", "md.obsidian.Obsidian"], "class": ["obsidian"] },
{ "id": "zen", "name": "Zen", "ids": ["zen", "zen-browser", "app.zen_browser.zen"], "class": ["zen", "zen-alpha", "zen-browser"] },
{ "id": "firefox", "name": "Firefox", "ids": ["firefox", "org.mozilla.firefox"], "class": ["firefox"] },
{ "id": "chromium", "name": "Chromium", "ids": ["chromium", "org.chromium.Chromium"], "class": ["chromium"] },
{ "id": "nautilus", "name": "Files", "ids": ["org.gnome.Nautilus"], "class": ["org.gnome.nautilus", "nautilus"] },
{ "id": "thunar", "name": "Thunar", "ids": ["thunar"], "class": ["thunar"] },
{ "id": "dolphin", "name": "Dolphin", "ids": ["org.kde.dolphin"], "class": ["org.kde.dolphin", "dolphin"] },
{ "id": "discord", "name": "Discord", "ids": ["discord", "com.discordapp.Discord"], "class": ["discord"] },
{ "id": "vesktop", "name": "Vesktop", "ids": ["vesktop", "dev.vencord.Vesktop"], "class": ["vesktop"] },
{ "id": "telegram", "name": "Telegram", "ids": ["org.telegram.desktop", "telegramdesktop"], "class": ["org.telegram.desktop", "telegramdesktop"] },
{ "id": "spotify", "name": "Spotify", "ids": ["spotify", "com.spotify.Client", "spotify-launcher"], "class": ["spotify"] },
{ "id": "thunderbird", "name": "Thunderbird", "ids": ["org.mozilla.Thunderbird", "thunderbird"], "class": ["thunderbird", "org.mozilla.thunderbird"] },
{ "id": "calculator", "name": "Calculator", "ids": ["org.gnome.Calculator", "gnome-calculator"], "class": ["org.gnome.calculator", "gnome-calculator"] },
{ "id": "texteditor", "name": "Text Editor", "ids": ["org.gnome.TextEditor"], "class": ["org.gnome.texteditor"] },
{ "id": "loupe", "name": "Image Viewer", "ids": ["org.gnome.Loupe"], "class": ["org.gnome.loupe"] }
]
function isSelfManaged(key) {
const app = root.appById(key);
return app !== null && app.selfManaged === true;
}
function appById(key) {
return root.catalog.find((a) => {
return a.id === key;
}) || null;
}
function entryOf(app) {
for (const id of app.ids) {
const e = DesktopEntries.byId(id);
if (e)
return e;
}
return null;
}
// desktop entries arrive over a few seconds, so this settles late
readonly property var installed: {
void DesktopEntries.applications.values.length;
return root.catalog.filter((a) => {
return a.selfManaged !== true && root.entryOf(a) !== null;
}).map((a) => {
return a.id;
});
}
// "@" marks a window class typed in from what is open, not a catalog app
function isCustom(key) {
return String(key).charAt(0) === "@";
}
function classesOf(key) {
const app = root.appById(key);
return app ? app.class : [String(key).slice(1)];
}
function nameOf(key) {
const app = root.appById(key);
return app ? app.name : String(key).slice(1);
}
function iconOf(key) {
const app = root.appById(key);
if (!app)
return Quickshell.iconPath(String(key).slice(1), true);
const e = root.entryOf(app);
return e && e.icon ? Quickshell.iconPath(e.icon, true) : "";
}
readonly property var chosen: Prefs.splitList(Prefs.glassApps)
// the ones on the page right now: catalog apps that are installed, plus
// every class added by hand, which we cannot check for
readonly property var shown: {
void root.installed.length;
const out = root.present ? ["kitty"] : [];
return out.concat(root.chosen.filter((k) => {
return root.isCustom(k) || root.installed.indexOf(k) !== -1;
}));
}
readonly property var values: {
const out = {};
for (const e of Prefs.splitList(Prefs.glassValues)) {
const i = e.indexOf("=");
const v = i > 0 ? parseFloat(e.slice(i + 1)) : NaN;
if (!isNaN(v))
out[e.slice(0, i)] = root.round2(v);
}
return out;
}
function isChosen(key) {
return root.chosen.indexOf(key) !== -1;
}
function setChosen(key, on) {
const list = root.chosen.filter((k) => {
return k !== key;
});
if (on)
list.push(key);
else
root.setOpacity(key, -1);
Prefs.set("glassApps", list.join(","));
}
function hasOwnValue(key) {
return root.values[key] !== undefined;
}
// what the slider alone would give this app, at its surface's own weight
function followFor(key) {
return root.isSelfManaged(key) ? root.round2(1 - Theme.blurAmount * root.kittyWeight) : root.appFollow;
}
function opacityOf(key) {
return root.hasOwnValue(key) ? root.values[key] : root.followFor(key);
}
// -1 drops the app's own value and puts it back to following the slider
function setOpacity(key, v) {
const out = [];
for (const k in root.values) {
if (k !== key)
out.push(k + "=" + root.values[k]);
}
if (v >= 0)
out.push(key + "=" + root.round2(v));
out.sort();
Prefs.set("glassValues", out.join(","));
}
// ---- the rules file ---------------------------------------------------
function luaStr(s) {
return "\"" + String(s).replace(/\\/g, "\\\\").replace(/"/g, "\\\"") + "\"";
}
readonly property string rulesLua: {
let out = "-- written by Lucid Settings > Glass, and rewritten on every change there\n";
out += "return {\n apps = {\n";
for (const key of root.chosen) {
if (root.isSelfManaged(key))
continue;
const classes = root.classesOf(key).filter((c) => {
return String(c).trim() !== "";
});
if (classes.length === 0)
continue;
out += " { name = " + root.luaStr(root.nameOf(key)) + ", class = { " + classes.map(root.luaStr).join(", ") + " }, opacity = " + root.opacityOf(key).toFixed(2) + " },\n";
}
out += " },\n}\n";
return out;
}
function writeRules() {
if (!Prefs.loaded || !dataFile.ready || root.rulesLua === dataFile.current)
return ;
dataFile.current = root.rulesLua;
dataFile.setText(root.rulesLua);
}
onRulesLuaChanged: rulesDebounce.restart()
Timer {
id: rulesDebounce
interval: 400
repeat: false
onTriggered: root.writeRules()
}
Connections {
function onLoadedChanged() {
rulesDebounce.restart();
}
target: Prefs
}
// decorations.lua reads the palette cache itself at config load, so this
// only has to tell it the cache has changed since. once at startup too, in
// case it changed while the shell was down
function syncBorders() {
// a border set in Settings > Windows goes back on top, so the two never race
Quickshell.execDetached(["hyprctl", "eval", "if LucidBorders then LucidBorders.apply() end if LucidSettings then LucidSettings.apply(true) end"]);
}
Component.onCompleted: root.syncBorders()
Connections {
function onRecoloured() {
root.syncBorders();
}
target: Theme
}
FileView {
id: dataFile
property bool ready: false
property string current: ""
path: root.dataPath
blockLoading: true
printErrors: false
onLoaded: {
dataFile.current = dataFile.text();
dataFile.ready = true;
rulesDebounce.restart();
}
onLoadFailed: {
dataFile.current = "";
dataFile.ready = true;
rulesDebounce.restart();
}
// the rules live in hyprland, so it re-reads once the file is on disk
onSaved: Quickshell.execDetached(["hyprctl", "eval", "if LucidGlass then LucidGlass.apply() end"])
}
// missing after --no-hypr, and the page says so
FileView {
path: root.modulePath
printErrors: false
watchChanges: true
onFileChanged: reload()
onLoaded: {
root.moduleInstalled = true;
root.moduleProbed = true;
}
onLoadFailed: {
root.moduleInstalled = false;
root.moduleProbed = true;
}
}
property bool moduleInstalled: false
property bool moduleProbed: false
// ---- windows open right now -------------------------------------------
// the page offers these, so anything not in the catalog can still be set
property var openClasses: []
function scan() {
scanProc.running = false;
scanProc.running = true;
}
Process {
id: scanProc
command: ["hyprctl", "clients", "-j"]
stdout: StdioCollector {
onStreamFinished: {
let seen = [];
try {
for (const w of JSON.parse(this.text)) {
const c = String(w.class || "").trim();
if (c !== "" && seen.indexOf(c) === -1)
seen.push(c);
}
} catch (e) {
seen = [];
}
seen.sort((a, b) => {
return a.toLowerCase() < b.toLowerCase() ? -1 : 1;
});
root.openClasses = seen;
}
}
}
// ---- kitty ------------------------------------------------------------
property bool probed: false
// kitty is installed and has a config dir to write into
property bool present: false
// kitty.conf pulls the file in; without the include nothing we write is read
property bool linked: false
property bool ready: false
property string onDisk: ""
property string applied: ""
readonly property string kittyConf: root.marker + "\n" + "dynamic_background_opacity yes\n" + "background_opacity " + root.kittyOpacity.toFixed(2) + "\n"
function apply() {
if (!root.probed || !root.ready || !root.present)
return ;
var want = root.kittyConf;
if (want === root.applied)
return ;
root.applied = want;
// already on disk is what kitty read at startup; nothing to reload
if (want === root.onDisk)
return ;
confFile.setText(want);
reloadProc.running = false;
reloadProc.running = true;
}
// dragging the slider lands a step at a time; write once it settles
onKittyOpacityChanged: writeDebounce.restart()
Timer {
id: writeDebounce
interval: 250
repeat: false
onTriggered: root.apply()
}
Process {
id: probe
running: true
command: ["sh", "-c", "command -v kitty >/dev/null && [ -d \"" + root.kittyDir + "\" ] && echo yes || echo no;" + "grep -q 'lucid-glass.conf' \"" + root.kittyDir + "/kitty.conf\" 2>/dev/null && echo yes || echo no"]
stdout: StdioCollector {
onStreamFinished: {
var l = this.text.split("\n");
root.present = (l[0] || "").trim() === "yes";
root.linked = (l[1] || "").trim() === "yes";
root.probed = true;
if (root.present && !root.linked) {
linkProc.running = false;
linkProc.running = true;
}
root.apply();
}
}
}
// an install from before this existed has no include, so add it once, with
// the copy beside it standing in as the "already done" mark: taking the
// line back out by hand has to stick
Process {
id: linkProc
command: ["sh", "-c", "f=\"" + root.kittyDir + "/kitty.conf\"; [ -f \"$f\" ] || { echo no; exit 0; };" + "grep -q 'lucid-glass.conf' \"$f\" && { echo yes; exit 0; };" + "[ -f \"$f.pre-lucid-glass\" ] && { echo no; exit 0; };" + "cp \"$f\" \"$f.pre-lucid-glass\";" + "printf '\\n# added by Lucid \\342\\200\\224 opacity follows the Glass slider\\ninclude ./lucid-glass.conf\\n' >> \"$f\"; echo yes"]
stdout: StdioCollector {
onStreamFinished: root.linked = this.text.trim() === "yes"
}
}
// background_opacity only follows a reload when dynamic_background_opacity
// was already set when that window started, so the first kitty after an
// install has to be reopened once before the slider moves anything
Process {
id: reloadProc
command: ["sh", "-c", "pkill -USR1 -x kitty 2>/dev/null; exit 0"]
}
FileView {
id: confFile
path: root.confPath
blockLoading: true
onLoaded: {
root.onDisk = confFile.text();
root.ready = true;
root.apply();
}
onLoadFailed: {
root.onDisk = "";
root.ready = true;
root.apply();
}
// a dropped write would otherwise stay "applied" and never be retried
onSaveFailed: root.applied = ""
}
}