summaryrefslogtreecommitdiff
path: root/bar/Workspace.qml
blob: f794718a3e0ce9bedf4d0be2f14ca425d3ee13c9 (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
import QtQuick
import QtQuick.Layouts
import Quickshell
import Quickshell.Hyprland
import Quickshell.Wayland
import qs.config
import qs.components

Item {
    id: root

    implicitWidth: layout.implicitWidth

    property var ws: Hyprland.workspaces
    property int activeIndex: -1
    property bool activeOccupied: false
    property int previousActiveIndex: -1

    property real activeRectX: {
        if (activeIndex < 0)
            return 0;
        return layout.x + activeIndex * Bar.workspaceIconSize + activeIndex * Bar.workspaceSpacing;
    }

    function getWorkspaceStats(index) {
        const w = ws.values.find(i => i.id === index + 1);
        return {
            isOccupied: w?.toplevels?.values?.length,
            isActive: w?.active,
            isUrgent: w?.urgent
        };
    }

    Rectangle {
        id: activeRect
        x: activeRectX
        anchors.verticalCenter: layout.verticalCenter
        implicitWidth: Bar.workspaceActiveIconSize
        implicitHeight: Bar.workspaceIconSize - Bar.workspaceHorizontalSpacing
        radius: Bar.workspaceRounding
        color: root.activeOccupied ? WallustColors.color4 : WallustColors.foreground
        opacity: root.activeOccupied ? Bar.workspaceActiveOpacity : Bar.workspaceEmptyOpacity / 3
        Behavior on x {
            ISpringAnimation {}
        }
        Behavior on color {
            ColorAnimation {
                duration: Bar.workspaceAnimationDuration
                easing.type: Easing.InOutQuad
            }
        }
        Behavior on opacity {
            NumberAnimation {
                duration: Bar.workspaceAnimationDuration
                easing.type: Easing.InOutQuad
            }
        }
    }

    RowLayout {
        id: layout
        anchors.verticalCenter: parent.verticalCenter
        implicitWidth: repeater.width
        spacing: Bar.workspaceSpacing

        Repeater {
            id: repeater
            model: Bar.workspaces

            Rectangle {
                property var st: getWorkspaceStats(index)
                property bool active: st?.isActive || false

                implicitWidth: active ? Bar.workspaceActiveIconSize : Bar.workspaceIconSize
                implicitHeight: Bar.workspaceIconSize - Bar.workspaceHorizontalSpacing
                radius: Bar.workspaceRounding
                color: st.isOccupied ? WallustColors.color4 : WallustColors.foreground
                opacity: active && st.isOccupied ? Bar.workspaceActiveOpacity : st.isOccupied ? Bar.workspaceOpacity : Bar.workspaceEmptyOpacity

                Icon {
                    anchors.centerIn: parent
                    text: !st.isUrgent ? Icons.ws[index + 1] : Icons.ws['urgent']
                    font.pixelSize: !active ? Bar.workspaceIconSize / 2 : Bar.workspaceActiveIconSize / 2.5
                    opacity: !st.isOccupied ? 0 : 1
                    color: !st.isActive ? WallustColors.color15 : WallustColors.foreground
                    Behavior on font.pixelSize {
                        ISpringAnimation {}
                    }
                    Behavior on color {
                        ColorAnimation {
                            duration: Bar.workspaceAnimationDuration
                            easing.type: Easing.InOutQuad
                        }
                    }
                }

                MouseArea {
                    anchors.fill: parent

                    acceptedButtons: Qt.LeftButton | Qt.RightButton | Qt.MiddleButton
                    hoverEnabled: true

                    onPressed: {
                        if (!parent.active)
                            Hyprland.dispatch(`workspace ${index + 1}`);
                    }
                    function moveActive() {
                        if (!active) {
                            previousActiveIndex = root.activeIndex;
                            activeRect.x = parent.x;
                            activeRect.implicitWidth = Bar.workspaceIconSize;
                        }
                    }
                    onEntered: {
                        moveActive();
                    }
                    // https://github.com/quickshell-mirror/quickshell/issues/118
                    // onPositionChanged: {
                    //     moveActive();
                    // }
                    onExited: {
                        if (!active && previousActiveIndex >= 0) {
                            // just in case
                            activeRect.x = layout.x + previousActiveIndex * Bar.workspaceIconSize + previousActiveIndex * Bar.workspaceSpacing;
                            activeRect.implicitWidth = Bar.workspaceActiveIconSize;
                            previousActiveIndex = -1;
                        }
                    }
                    onWheel: event => {
                        // no idea wats wrong w/ prev so we just unify it to use +/- 1
                        if (event.angleDelta.y < 0) {
                            if (activeIndex + 1 < Bar.workspaces)
                                Hyprland.dispatch(`workspace +1`);
                            else
                                Hyprland.dispatch(`workspace 1`);
                        } else if (event.angleDelta.y > 0) {
                            if (activeIndex + 1 > 1) {
                                Hyprland.dispatch(`workspace -1`);
                            } else if (Bar.workspaces > 1) {
                                Hyprland.dispatch(`workspace ${Bar.workspaces}`);
                            }
                        }
                    }
                }

                Behavior on opacity {
                    NumberAnimation {
                        duration: Bar.workspaceAnimationDuration
                        easing.type: Easing.InOutQuad
                    }
                }
                Behavior on implicitWidth {
                    NumberAnimation {
                        duration: Bar.workspaceAnimationDuration
                        easing.type: Easing.InOutQuad
                    }
                }
                Behavior on color {
                    ColorAnimation {
                        duration: Bar.workspaceAnimationDuration
                        easing.type: Easing.InOutQuad
                    }
                }

                onActiveChanged: if (active) {
                    root.activeIndex = index;
                    root.activeOccupied = st.isOccupied;
                    activeRect.implicitWidth = Bar.workspaceActiveIconSize;
                    activeRect.x = activeRectX;
                    previousActiveIndex = index;
                }
            }
        }
    }
    MouseArea {
        height: Bar.height
        width: parent.width

        // sync w the inner MouseArea
        onWheel: event => {
            // no idea wats wrong w/ prev so we just unify it to use +/- 1
            if (event.angleDelta.y < 0) {
                if (activeIndex + 1 < Bar.workspaces)
                    Hyprland.dispatch(`workspace +1`);
                else
                    Hyprland.dispatch(`workspace 1`);
            } else if (event.angleDelta.y > 0) {
                if (activeIndex + 1 > 1) {
                    Hyprland.dispatch(`workspace -1`);
                } else if (Bar.workspaces > 1) {
                    Hyprland.dispatch(`workspace ${Bar.workspaces}`);
                }
            }
        }
    }
}