Files
glassMxrph/quickshell/lock/LockState.qml
T
mxandClaude Opus 5.5 99e64a7713 Add glassMxrph shell sources
Initial commit of the Quickshell desktop suite: bar, dock, launcher,
settings, Control Center, dashboard, OSD, lock screen, desktop widgets,
plugin host with the vendored Noctalia/Omarchy compatibility layer,
helpers, packaging, tests and phase documentation.

Includes the optimization work so far: usage widgets and dashboard
gauges no longer animate continuously, desktop widgets pause while
covered, system controls stop flickering on background polls, dock app
matching uses an indexed catalog, and workspace ghosts are created only
for occupied slots.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-07 22:36:17 +02:00

234 lines
7.7 KiB
QML

pragma Singleton
import QtQuick
import Quickshell
import Quickshell.Io
import Quickshell.Services.Pam
import "../services"
import "../core"
// Lock screen state and authentication. Every attempt starts a fresh PAM
// conversation (the bundled pam/password.conf, or a system service when
// lock.pamService is set) and answers its password prompt.
Singleton {
id: root
readonly property var options: ConfigService.ready ? ConfigService.data.lock : ({
enabled: true,
backdrop: "wallpaper",
glass: true,
media: true,
powerButtons: true,
pamService: ""
})
property bool locked: false
// A preview runs the same UI in ordinary windows: nothing is locked and a
// correct password simply closes it. Used by Settings and for testing.
property bool previewing: false
property bool unlocking: false
property string primary: ""
property string status: ""
property bool busy: false
property int failures: 0
property string purpose: ""
property var pending: null
property string testResult: ""
property var shots: ({})
// Live lock surfaces, for diagnostics and tests.
property var surfaces: []
property string realName: ""
// Active keyboard layout, read from Hyprland when the lock screen shows.
property string layout: ""
readonly property bool shown: locked || previewing
readonly property string runtimeDir: (Quickshell.env("XDG_RUNTIME_DIR") || "/tmp") + "/glassMxrph"
// Glass parameters follow the desktop's HyprGlass values for a matching look.
readonly property var glass: {
const v = ConfigService.ready ? ConfigService.data.hyprglass.values : {};
const pick = (key, fallback) => Number.isFinite(v[key]) ? v[key] : fallback;
return {
refraction: pick("refraction_strength", 3) * 6,
dispersion: Math.min(0.6, pick("chromatic_aberration", 0.45) * 0.35),
fresnel: pick("fresnel_strength", 0.5),
specular: pick("specular_strength", 0.8),
edge: Math.max(10, pick("edge_thickness", 0.065) * 300),
frost: Math.min(1, pick("blur_strength", 0.45) / 1.2 + 0.25),
brightness: pick("brightness", 0.95),
saturation: pick("saturation", 1) * (1 + pick("vibrancy", 0.15))
};
}
function lock() {
if (locked || grab.running)
return;
previewing = false;
ShellState.settingsOpen = false;
ShellState.controlCenterOpen = false;
ShellState.launcherOpen = false;
primary = ScreenService.active ? ScreenService.active.name : "";
status = "";
if (options.backdrop === "screenshot") {
grab.names = Quickshell.screens.map(s => s.name).filter(n => /^[\w.-]+$/.test(n));
grab.running = true;
grabTimeout.restart();
} else
locked = true;
}
function preview() {
if (locked)
return;
ShellState.settingsOpen = false;
primary = ScreenService.active ? ScreenService.active.name : "";
status = "";
previewing = true;
}
function cancelPreview() {
if (previewing && !busy) {
previewing = false;
status = "";
}
}
function submit(password, why) {
if (busy || !password)
return false;
purpose = why || (previewing ? "preview" : "unlock");
pending = password;
busy = true;
status = "";
if (!pam.start()) {
busy = false;
pending = null;
status = "Authentication is unavailable";
if (purpose === "test")
testResult = "error";
return false;
}
return true;
}
function test(password) {
testResult = "";
return submit(password, "test");
}
function finishUnlock() {
unlocking = true;
unlockTimer.restart();
}
Connections {
target: ShellState
function onLockRequested() {
root.lock();
}
}
PamContext {
id: pam
config: root.options.pamService || "password.conf"
configDirectory: root.options.pamService ? "/etc/pam.d" : Qt.resolvedUrl("pam")
onPamMessage: {
if (responseRequired && root.pending !== null) {
respond(root.pending);
root.pending = null;
}
}
onCompleted: result => {
root.busy = false;
root.pending = null;
const ok = result === PamResult.Success;
if (root.purpose === "test") {
root.testResult = ok ? "accepted" : "rejected";
return;
}
if (ok)
root.finishUnlock();
else {
root.failures++;
root.status = result === PamResult.Error ? "Authentication error" : "Incorrect password";
}
}
}
Timer {
id: unlockTimer
interval: 420
onTriggered: {
root.locked = false;
root.previewing = false;
root.unlocking = false;
root.status = "";
if (Object.keys(root.shots).length) {
Quickshell.execDetached(["rm", "-f"].concat(Object.values(root.shots)));
root.shots = {};
}
}
}
// Screenshot backdrops: one grim capture per output, then lock (or lock anyway after a timeout).
Process {
id: grab
property var names: []
command: ["sh", "-c", "mkdir -p \"$1\" && shift && dir=\"$1\" && shift && for name; do grim -l 0 -o \"$name\" \"$dir/lock-$name.png\" & done; wait", "sh", root.runtimeDir, root.runtimeDir].concat(names)
// Quickshell 0.3.1 omits QProcess::ExitStatus from its type metadata.
// qmllint disable signal-handler-parameters
onExited: {
grabTimeout.stop();
const shots = {};
for (const name of names)
shots[name] = root.runtimeDir + "/lock-" + name + ".png";
root.shots = shots;
root.locked = true;
}
// qmllint enable signal-handler-parameters
}
Timer {
id: grabTimeout
interval: 1500
onTriggered: {
grab.running = false;
root.locked = true;
}
}
onShownChanged: if (shown)
layoutProbe.running = true
Process {
id: layoutProbe
command: ["hyprctl", "devices", "-j"]
stdout: StdioCollector {
onStreamFinished: {
try {
const keyboards = JSON.parse(text).keyboards || [];
const main = keyboards.find(k => k.main) || keyboards[0];
root.layout = main && main.active_keymap ? main.active_keymap.slice(0, 2).toUpperCase() : "";
} catch (e) {
root.layout = "";
}
}
}
}
Process {
running: true
command: ["sh", "-c", "getent passwd \"$USER\" | cut -d: -f5 | cut -d, -f1"]
stdout: StdioCollector {
onStreamFinished: root.realName = text.trim()
}
}
IpcHandler {
target: "lock"
function lock(): void {
root.lock();
}
function preview(): void {
root.preview();
}
function closePreview(): void {
root.cancelPreview();
}
function status(): string {
return JSON.stringify({
locked: root.locked,
previewing: root.previewing,
busy: root.busy,
status: root.status,
primary: root.primary,
testResult: root.testResult
});
}
}
}