216 lines
6.9 KiB
QML
216 lines
6.9 KiB
QML
import QtQuick
|
|
import Quickshell
|
|
import Quickshell.Wayland
|
|
import Quickshell.Hyprland
|
|
import "types"
|
|
|
|
// Snapshot of monitors / workspaces / windows for the overview.
|
|
// Call refresh() when the overlay is shown; browsing a monitor only rebuilds.
|
|
Scope {
|
|
id: root
|
|
|
|
required property string browsedMonitor
|
|
|
|
property list<Monitor> monitors
|
|
property list<MonitorStats> monitorStats
|
|
property list<OverviewRow> rows
|
|
property QtObject snapshotRoot
|
|
readonly property bool rebuildPending: delayedRebuild.running
|
|
|
|
signal rebuilt()
|
|
|
|
Component { id: snapRootComp; QtObject {} }
|
|
Component { id: monitorComp; Monitor {} }
|
|
Component { id: monitorStatsComp; MonitorStats {} }
|
|
Component { id: workspaceInfoComp; WorkspaceInfo {} }
|
|
Component { id: windowInfoComp; WindowInfo {} }
|
|
Component { id: overviewRowComp; OverviewRow {} }
|
|
|
|
Timer {
|
|
id: delayedRebuild
|
|
interval: 80
|
|
repeat: false
|
|
onTriggered: root.rebuild()
|
|
}
|
|
|
|
Connections {
|
|
target: Hyprland.monitors
|
|
function onValuesChanged() { delayedRebuild.restart(); }
|
|
}
|
|
Connections {
|
|
target: Hyprland.workspaces
|
|
function onValuesChanged() { delayedRebuild.restart(); }
|
|
}
|
|
Connections {
|
|
target: Hyprland.toplevels
|
|
function onValuesChanged() { delayedRebuild.restart(); }
|
|
}
|
|
|
|
function refresh() {
|
|
Hyprland.refreshMonitors();
|
|
Hyprland.refreshWorkspaces();
|
|
Hyprland.refreshToplevels();
|
|
delayedRebuild.restart();
|
|
root.rebuild();
|
|
}
|
|
|
|
function monitorNameOf(tl, ws) {
|
|
if (tl?.monitor?.name)
|
|
return tl.monitor.name;
|
|
if (ws?.monitor?.name)
|
|
return ws.monitor.name;
|
|
const ipcMon = tl?.lastIpcObject?.monitor;
|
|
if (typeof ipcMon === "string" && ipcMon.length)
|
|
return ipcMon;
|
|
if (typeof ipcMon === "number") {
|
|
const m = (Hyprland.monitors?.values ?? []).find(mon => mon.id === ipcMon);
|
|
if (m)
|
|
return m.name;
|
|
}
|
|
return "";
|
|
}
|
|
|
|
function resolveWayland(tl) {
|
|
if (tl?.wayland)
|
|
return tl.wayland;
|
|
const title = tl?.title ?? "";
|
|
const appId = tl?.lastIpcObject?.class ?? "";
|
|
const list = ToplevelManager?.toplevels?.values ?? [];
|
|
let match = list.find(t => t.title === title && (!appId || t.appId === appId));
|
|
if (match)
|
|
return match;
|
|
return list.find(t => title && t.title === title) ?? null;
|
|
}
|
|
|
|
function snapshotWindow(tl) {
|
|
const ipc = tl.lastIpcObject;
|
|
return windowInfoComp.createObject(root.snapshotRoot, {
|
|
address: tl.address ?? ipc?.address ?? "",
|
|
title: tl.title ?? ipc?.title ?? "",
|
|
appClass: ipc?.class ?? "",
|
|
size: (ipc && ipc.size) ? [ipc.size[0], ipc.size[1]] : [0, 0],
|
|
at: (ipc && ipc.at) ? [ipc.at[0], ipc.at[1]] : [0, 0],
|
|
wayland: root.resolveWayland(tl),
|
|
activated: !!tl.activated,
|
|
monitorName: "",
|
|
});
|
|
}
|
|
|
|
function collectToplevels() {
|
|
const seen = {};
|
|
const out = [];
|
|
function add(tl) {
|
|
if (!tl)
|
|
return;
|
|
const key = tl.address ?? tl.lastIpcObject?.address ?? "";
|
|
if (key) {
|
|
if (seen[key])
|
|
return;
|
|
seen[key] = true;
|
|
}
|
|
out.push(tl);
|
|
}
|
|
for (const tl of (Hyprland.toplevels?.values ?? []))
|
|
add(tl);
|
|
for (const ws of (Hyprland.workspaces?.values ?? [])) {
|
|
for (const tl of (ws.toplevels?.values ?? []))
|
|
add(tl);
|
|
}
|
|
return out;
|
|
}
|
|
|
|
function collectMonitor() {
|
|
return (Hyprland.monitors?.values ?? []).slice()
|
|
.sort((a, b) => a.id - b.id)
|
|
.map(m => monitorComp.createObject(root.snapshotRoot, {
|
|
monitorId: m.id,
|
|
name: m.name,
|
|
x: m.x,
|
|
y: m.y,
|
|
width: m.width,
|
|
height: m.height,
|
|
scale: m.scale || 1,
|
|
}));
|
|
}
|
|
|
|
function rebuild() {
|
|
const oldRoot = root.snapshotRoot;
|
|
root.snapshotRoot = snapRootComp.createObject(root);
|
|
|
|
const monitorSnaps = collectMonitor();
|
|
|
|
const byId = {};
|
|
for (const ws of (Hyprland.workspaces?.values ?? [])) {
|
|
byId[ws.id] = {
|
|
id: ws.id,
|
|
active: !!ws.active,
|
|
monitorName: ws.monitor?.name ?? "",
|
|
windows: [],
|
|
};
|
|
}
|
|
|
|
for (const tl of root.collectToplevels()) {
|
|
if (tl.mapped === false)
|
|
continue;
|
|
const ws = tl.workspace;
|
|
const ipcWs = tl.lastIpcObject?.workspace;
|
|
const wsId = ws?.id ?? (typeof ipcWs === "object" ? ipcWs?.id : ipcWs);
|
|
if (!(wsId in byId)) {
|
|
byId[wsId] = {
|
|
id: wsId,
|
|
active: !!(ws && ws.active),
|
|
monitorName: "",
|
|
windows: [],
|
|
};
|
|
}
|
|
const monName = root.monitorNameOf(tl, ws) || byId[wsId].monitorName;
|
|
if (!byId[wsId].monitorName)
|
|
byId[wsId].monitorName = monName;
|
|
const win = root.snapshotWindow(tl);
|
|
win.monitorName = monName;
|
|
byId[wsId].windows.push(win);
|
|
}
|
|
|
|
for (const w of Object.values(byId))
|
|
w.windows.sort((a, b) => (a.at?.[0] ?? 0) - (b.at?.[0] ?? 0));
|
|
|
|
const stats = monitorSnaps.map(mon => {
|
|
const wss = Object.values(byId).filter(w => w.monitorName === mon.name);
|
|
const windows = Object.values(byId).reduce(
|
|
(n, w) => n + w.windows.filter(win => win.monitorName === mon.name).length,
|
|
0
|
|
);
|
|
return monitorStatsComp.createObject(root.snapshotRoot, {
|
|
workspaces: wss.length,
|
|
windows: windows,
|
|
});
|
|
});
|
|
|
|
const browsed = root.browsedMonitor;
|
|
const rows = Object.values(byId)
|
|
.filter(w => {
|
|
if (!browsed)
|
|
return false;
|
|
return w.monitorName === browsed
|
|
|| w.windows.some(win => win.monitorName === browsed);
|
|
})
|
|
.sort((a, b) => a.id - b.id)
|
|
.map(w => overviewRowComp.createObject(root.snapshotRoot, {
|
|
workspace: workspaceInfoComp.createObject(root.snapshotRoot, {
|
|
workspaceId: w.id,
|
|
active: w.active,
|
|
}),
|
|
toplevels: w.monitorName === browsed
|
|
? w.windows
|
|
: w.windows.filter(win => win.monitorName === browsed),
|
|
}));
|
|
|
|
root.monitors = monitorSnaps;
|
|
root.monitorStats = stats;
|
|
root.rows = rows;
|
|
if (oldRoot)
|
|
oldRoot.destroy();
|
|
root.rebuilt();
|
|
}
|
|
}
|