aboutsummaryrefslogtreecommitdiffstats
path: root/desktop/island/StatusPill.qml
blob: dd97855c26467e4d1c81611974fc127bde3f88b9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
// Copyright (C) 2026 Danilo M. <danix@danix.xyz>
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 2 as
// published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

import Quickshell
import Quickshell.Widgets
import Quickshell.Services.SystemTray
import QtQuick
import ".."
import "perf"
import "Logic.js" as Logic

// Performance icon, controls icon, volume icon, tray, layout flag. The
// performance, controls and volume icons open their panels in this capsule;
// the tray is a plain tray; the flag switches layout on scroll and ignores
// clicks.
Capsule {
    id: pill

    required property QtObject sound
    // The island window, which tray menus are positioned against.
    required property QtObject window
    property string panel: ""

    signal openSound()
    signal openControls()
    signal openPerf()
    signal openDrawer()
    // A drawer page, from a control tile's right-click.
    signal openPage(string page)
    // Emitted just before a tray menu opens; the menu cannot report its close.
    signal menuShown()

    expanded: panel === "sound" || panel === "controls" || panel === "perf"
    restWidth: restRow.implicitWidth + 24
    panelWidth: panel === "perf" ? 600 : 320
    panelHeight: (panel === "controls" ? controlsPanel.implicitHeight
                : panel === "perf" ? perfPanel.implicitHeight
                : soundPanel.implicitHeight) + 28

    // The detailed sampling (per-core, network, VRAM) runs only while the
    // panel shows it.
    Binding { target: Perf; property: "detailed"; value: pill.panel === "perf" }

    // Edit mode never outlives its panel.
    onPanelChanged: controlsPanel.editing = false

    Row {
        id: restRow
        anchors.centerIn: parent
        spacing: 12
        opacity: pill.expanded ? 0 : 1
        visible: opacity > 0
        Behavior on opacity { NumberAnimation { duration: 120 } }

        // The worst severity as a dot on the corner: IconImage cannot be
        // recoloured without an effect layer. Never the accent (AGENTS.md).
        Item {
            anchors.verticalCenter: parent.verticalCenter
            width: 18
            height: 18

            IconImage {
                anchors.fill: parent
                source: Quickshell.iconPath("utilities-system-monitor", true)
            }

            Rectangle {
                anchors { right: parent.right; top: parent.top; rightMargin: -2; topMargin: -2 }
                width: 8
                height: 8
                radius: 4
                visible: Perf.severity !== "ok"
                color: Perf.severity === "crit" ? Theme.red : Theme.yellow
                border.width: 1
                border.color: Theme.base
            }

            MouseArea {
                anchors.fill: parent
                cursorShape: Qt.PointingHandCursor
                onClicked: pill.openPerf()
            }
        }

        IconImage {
            anchors.verticalCenter: parent.verticalCenter
            implicitSize: 18
            source: Quickshell.iconPath("preferences-system", true)

            MouseArea {
                anchors.fill: parent
                cursorShape: Qt.PointingHandCursor
                onClicked: pill.openControls()
            }
        }

        IconImage {
            anchors.verticalCenter: parent.verticalCenter
            implicitSize: 18
            source: Quickshell.iconPath(Logic.volumeIcon(pill.sound.volume, pill.sound.muted), true)

            MouseArea {
                anchors.fill: parent
                cursorShape: Qt.PointingHandCursor
                onClicked: pill.openSound()
            }
        }

        Repeater {
            model: SystemTray.items.values

            delegate: IconImage {
                id: trayIcon
                required property SystemTrayItem modelData
                anchors.verticalCenter: parent.verticalCenter
                implicitSize: 16
                source: modelData.icon

                MouseArea {
                    anchors.fill: parent
                    acceptedButtons: Qt.LeftButton | Qt.RightButton
                    cursorShape: Qt.PointingHandCursor
                    onClicked: mouse => {
                        const item = trayIcon.modelData;
                        const wantsMenu = mouse.button === Qt.RightButton || item.onlyMenu;
                        if (!wantsMenu) { item.activate(); return; }
                        if (!item.hasMenu) return;
                        const p = trayIcon.mapToItem(null, 0, trayIcon.height + 6);
                        pill.menuShown();
                        item.display(pill.window, p.x, p.y);
                    }
                }
            }
        }

        Image {
            anchors.verticalCenter: parent.verticalCenter
            width: 18
            height: 18
            sourceSize: Qt.size(18, 18)
            source: Qt.resolvedUrl("assets/" + Keyboard.flag)

            // One switch per wheel notch (120). A touchpad sends many small
            // deltas, and switching on each made the count erratic.
            // A MouseArea, not a WheelHandler: the handler never fired here,
            // while MouseArea.onWheel is what SoundPage already relies on.
            MouseArea {
                anchors.fill: parent
                property real acc: 0
                onWheel: wheel => {
                    acc += wheel.angleDelta.y;
                    if (Math.abs(acc) >= 120) {
                        Keyboard.cycle(acc > 0 ? "prev" : "next");
                        acc = 0;
                    }
                    wheel.accepted = true;
                }
            }
        }
    }

    SoundPanel {
        id: soundPanel
        x: 14
        y: 14
        width: pill.panelWidth - 28
        sound: pill.sound
        opacity: pill.panel === "sound" ? 1 : 0
        visible: opacity > 0
        Behavior on opacity { NumberAnimation { duration: 160 } }
        onOpenDrawer: pill.openDrawer()
    }

    ControlsPanel {
        id: controlsPanel
        x: 14
        y: 14
        width: pill.panelWidth - 28
        sound: pill.sound
        opacity: pill.panel === "controls" ? 1 : 0
        visible: opacity > 0
        Behavior on opacity { NumberAnimation { duration: 160 } }
        onOpenDrawer: page => pill.openPage(page)
    }

    PerfPanel {
        id: perfPanel
        x: 14
        y: 14
        width: 600 - 28
        opacity: pill.panel === "perf" ? 1 : 0
        visible: opacity > 0
        Behavior on opacity { NumberAnimation { duration: 160 } }
    }
}