pragma Singleton import QtQuick import Quickshell import Quickshell.Io import Quickshell.Services.SystemTray import "../core" Singleton { id: root readonly property var items: SystemTray.items.values readonly property var options: ConfigService.ready ? ConfigService.data.tray : ({ enabled: false, hoverMenus: true, hoverDelay: 400, iconSize: 20, spacing: 8, background: "pill" }) property var currentItem: null property var anchorItem: null property bool opened: false property bool closeOnLeave: false property var windows: [] // Bar windows, for test drivers and hover hand-off. property var barWindows: [] property var hoveredWindows: [] property var icons: [] // Only click-opened menus grab keyboard focus; hover menus never steal it. readonly property bool grabbing: opened && !closeOnLeave readonly property bool pointerInside: (anchorItem && anchorItem.pointerInside) || hoveredWindows.length > 0 onPointerInsideChanged: updateDismissal() onItemsChanged: if (currentItem && !items.includes(currentItem)) close() onCurrentItemChanged: if (!currentItem) close() onOptionsChanged: if (!options.enabled) close() function registerIcon(icon) { icons = icons.concat([icon]); } function unregisterIcon(icon) { if (anchorItem === icon) close(); icons = icons.filter(i => i !== icon); } function registerBar(window) { barWindows = barWindows.concat([window]); } function unregisterBar(window) { barWindows = barWindows.filter(w => w !== window); } function registerWindow(window) { windows = windows.concat([window]); } function unregisterWindow(window) { windows = windows.filter(w => w !== window); setHovered(window, false); } function setHovered(window, inside) { hoveredWindows = hoveredWindows.filter(w => w !== window); if (inside) hoveredWindows = hoveredWindows.concat([window]); } function updateDismissal() { if (opened && closeOnLeave && !pointerInside) leaveTimer.restart(); else leaveTimer.stop(); } function open(item, icon, hover) { if (!options.enabled || !item || !item.hasMenu || !icon) return false; ShellState.controlCenterOpen = false; ShellState.notchOpen = false; ShellState.launcherOpen = false; ShellState.settingsOpen = false; currentItem = item; anchorItem = icon; closeOnLeave = hover; opened = true; updateDismissal(); return true; } function close() { opened = false; leaveTimer.stop(); } function openById(id, monitor) { const icon = icons.find(i => i.trayItem && i.trayItem.id === id && (!monitor || i.barScreen.name === monitor)); return icon ? open(icon.trayItem, icon, false) : false; } Connections { target: ShellState function onControlCenterOpenChanged() { if (ShellState.controlCenterOpen) root.close(); } function onSettingsOpenChanged() { if (ShellState.settingsOpen) root.close(); } function onLauncherOpenChanged() { if (ShellState.launcherOpen) root.close(); } } Timer { id: leaveTimer interval: 300 onTriggered: if (!root.pointerInside) root.close() } IpcHandler { target: "tray" function open(id: string, monitor: string): bool { return root.openById(id, monitor); } function close(): void { root.close(); } function status(): string { return JSON.stringify({ opened: root.opened, grabbing: root.grabbing, currentId: root.currentItem ? root.currentItem.id : "", items: root.items.map(i => ({ id: i.id, title: i.title, hasMenu: i.hasMenu, onlyMenu: i.onlyMenu })), menus: root.windows.filter(w => w.visible).map(w => w.snapshot()) }); } } }