import QtQuick import Quickshell import Quickshell.Services.SystemTray import "tray.js" as Tray Item { id: root property string selectedItemId: "" property string expandedSubmenu: "" property bool closeRequested: false property string closeReason: "" property var closePropagation: ({ "requested": false, "source": "", "reason": "" }) property var lastAction: ({}) readonly property ObjectModel itemModel: SystemTray.items readonly property var nativeItems: root.itemModel.values // Rows of shaped tray items (tray.js shapeItem), one per native item. readonly property var items: itemsArray() readonly property int attentionCount: Tray.attentionCount(items) readonly property int itemCount: nativeItems.length function itemKey(item, index) { return Tray.itemKey(item, index); } function shapeItem(item, index) { return Tray.shapeItem(item, index); } function itemsArray() { const result = []; for (let i = 0; i < nativeItems.length; i++) result.push(shapeItem(nativeItems[i], i)); return result; } function nativeItemById(id) { const needle = String(id); for (let i = 0; i < nativeItems.length; i++) if (itemKey(nativeItems[i], i) === needle) { return nativeItems[i]; } return null; } function menuFor(id) { const item = nativeItemById(id); return item && item.hasMenu ? item.menu : null; } function selectedItem() { if (items.length === 0) return null; if (selectedItemId.length === 0) return items[0]; for (let i = 0; i < items.length; i++) if (items[i].id === selectedItemId) { return items[i]; } return items[0]; } function menuRows() { const item = selectedItem(); if (!item || !item.hasMenu) return []; return [ { "id": `${item.id}:native-menu`, "kind": "submenu", "label": `${item.title} menu`, "enabled": true, "expanded": expandedSubmenu === `${item.id}:native-menu`, "native": true, "children": [] } ]; } function rowById(id) { const rows = menuRows(); const needle = String(id); for (let i = 0; i < rows.length; i++) if (rows[i].id === needle) { return rows[i]; } return ({ "id": needle, "kind": "missing", "label": "", "enabled": false, "native": false }); } function stateObject() { const rows = menuRows(); return { "nativeItemCount": items.length, "attentionCount": attentionCount, "selectedItemId": selectedItemId, "items": items, "expandedSubmenu": expandedSubmenu, "siblingsCollapsed": rows.filter(row => { return row.kind === "submenu" && row.expanded; }).length <= 1, "rows": rows, "closeRequested": closeRequested, "closeReason": closeReason, "closePropagation": closePropagation, "lastAction": lastAction }; } function stateJson() { return JSON.stringify(stateObject()); } function expandSubmenu(id) { const row = rowById(id); if (row.kind !== "submenu" || !row.enabled) { lastAction = { "kind": "expand-submenu", "id": String(id), "accepted": false }; return stateJson(); } expandedSubmenu = row.id; closeRequested = false; closeReason = ""; closePropagation = { "requested": false, "source": row.id, "reason": "submenu-expanded" }; lastAction = { "kind": "expand-submenu", "id": row.id, "accepted": true, "native": true }; return stateJson(); } function displayMenu(id, parentWindow, x, y) { const item = nativeItemById(id); if (!item || !item.hasMenu || !parentWindow) return stateJson(); item.display(parentWindow, Math.round(x), Math.round(y)); lastAction = { "kind": "display-menu", "id": String(id), "accepted": true, "native": true }; return stateJson(); } function primaryActivate(id, parentWindow, x, y) { const item = nativeItemById(id); if (item && item.onlyMenu) return displayMenu(id, parentWindow, x, y); return activateRow(id); } function secondaryActivate(id) { const item = nativeItemById(id); if (item) { item.secondaryActivate(); lastAction = { "kind": "secondary-activate", "id": String(id), "accepted": true, "native": true }; } return stateJson(); } function activateRow(id) { const item = nativeItemById(id); if (item) { item.activate(); closeRequested = true; closeReason = "item-activated"; closePropagation = { "requested": true, "source": String(id), "reason": closeReason }; lastAction = { "kind": "activate-item", "id": String(id), "accepted": true, "native": true }; return stateJson(); } const row = rowById(id); if (row.kind === "submenu") return expandSubmenu(row.id); closeRequested = false; closePropagation = { "requested": false, "source": row.id, "reason": row.kind }; lastAction = { "kind": "activate-row", "id": row.id, "rowKind": row.kind, "accepted": false, "native": false }; return stateJson(); } }