quickshell-toki-night/services/Hypr.qml

86 lines
2.8 KiB
QML

pragma Singleton
import Quickshell
import Quickshell.Hyprland
import Quickshell.Io
import QtQuick
Singleton {
id: root
readonly property var toplevels: Hyprland.toplevels
readonly property var workspaces: Hyprland.workspaces
readonly property var monitors: Hyprland.monitors
property HyprlandToplevel activeToplevel: null
readonly property HyprlandWorkspace focusedWorkspace: Hyprland.focusedWorkspace
readonly property HyprlandMonitor focusedMonitor: Hyprland.focusedMonitor
property string kbLayout: "?"
readonly property int arbitraryRaceConditionDelay: 50
function dispatch(request: string): void {
Hyprland.dispatch(request);
}
function monitorFor(screen: ShellScreen): HyprlandMonitor {
return Hyprland.monitorFor(screen);
}
Connections {
target: Hyprland
function onRawEvent(event: HyprlandEvent): void {
const n = event.name;
if (n.endsWith("v2"))
return;
if (n === "activelayout") {
root.kbLayout = event.parse(2)[1].slice(0, 2).toLowerCase();
} else if (["workspace", "moveworkspace", "activespecial", "focusedmon"].includes(n)) {
Hyprland.refreshWorkspaces();
Hyprland.refreshMonitors();
} else if (["openwindow", "closewindow", "movewindow"].includes(n)) {
Hyprland.refreshToplevels();
Hyprland.refreshWorkspaces();
} else if (n.includes("mon")) {
Hyprland.refreshMonitors();
} else if (n.includes("workspace")) {
Hyprland.refreshWorkspaces();
} else if (n.includes("window") || n.includes("group") || ["pin", "fullscreen", "changefloatingmode", "minimize"].includes(n)) {
Hyprland.refreshToplevels();
}
}
function onActiveToplevelChanged() {
toplevelTimer.start();
}
}
onFocusedWorkspaceChanged: toplevelTimer.start()
// Delay update to account for Hyprland's processing delay
// (Prevent false null reports)
Timer {
id: toplevelTimer
interval: root.arbitraryRaceConditionDelay
repeat: false
onTriggered: {
const toplevel = Hyprland.activeToplevel;
// Invalidate active toplevel if in different workspace
if (toplevel && toplevel?.workspace === focusedWorkspace) {
root.activeToplevel = toplevel;
} else {
root.activeToplevel = null;
}
}
}
Process {
running: true
command: ["hyprctl", "-j", "devices"]
stdout: StdioCollector {
onStreamFinished: root.kbLayout = JSON.parse(text).keyboards.find(k => k.main).active_keymap.slice(0, 2).toLowerCase()
}
}
}