summaryrefslogtreecommitdiff
path: root/bar/Workspace.qml
blob: 51c4ba4cd149ccef39cb7a01d5152693c37d03fd (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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
import QtQuick
import QtQuick.Layouts
import Quickshell.Hyprland
import Qt5Compat.GraphicalEffects
import qs.config
import qs.components

Item {
	id: root

	implicitWidth: layout.implicitWidth

	property var ws: Hyprland.workspaces
	property var toplevels: Hyprland.toplevels.values // update activeOccupied when toplevels changes as getWorkspaceStat's return is non reactive

	property int activeIndex: -1
	property bool activeOccupied: false
	property bool toChild: false

	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
		};
	}

	onToplevelsChanged: {
		Qt.callLater(() => {
			activeOccupied = getWorkspaceStats(activeIndex).isOccupied || false;
		});
	}

	DropShadow {
		anchors.fill: activeRect
		source: activeRect
		radius: 8
		cached: true
		color: root.activeOccupied ? Colors.accent : Colors.foreground1
		opacity: root.activeOccupied ? Bar.wsActiveOpacity : Bar.wsEmptyOpacity / 3

		Behavior on color {
			ColorAnimation {
				duration: Bar.wsAnimationDuration
				easing.type: Easing.InOutQuad
			}
		}
		Behavior on opacity {
			NumberAnimation {
				duration: Bar.wsAnimationDuration
				easing.type: Easing.InOutQuad
			}
		}
	}
	IRect {
		id: activeRect
		x: layout.x + root.activeIndex * Bar.wsIconSize + root.activeIndex * Bar.wsSpacing
		anchors.verticalCenter: layout.verticalCenter
		implicitWidth: Bar.wsActiveIconSize
		implicitHeight: Bar.wsIconSize - Bar.wsHorizontalSpacing
		radius: Bar.wsRounding
		color: root.activeOccupied ? Colors.accent : Colors.foreground1
		opacity: root.activeOccupied ? Bar.wsActiveOpacity : Bar.wsEmptyOpacity / 3
		Behavior on x {
			ISpringAnimation {}
		}
		Behavior on color {
			ColorAnimation {
				duration: Bar.wsAnimationDuration
				easing.type: Easing.InOutQuad
			}
		}
		Behavior on opacity {
			NumberAnimation {
				duration: Bar.wsAnimationDuration
				easing.type: Easing.InOutQuad
			}
		}
		Behavior on width {
			NumberAnimation {
				duration: Bar.wsAnimationDuration
				easing.type: Easing.InOutQuad
			}
		}
	}

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

		Repeater {
			id: repeater
			model: Bar.wss

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

				implicitWidth: active ? Bar.wsActiveIconSize : Bar.wsIconSize
				implicitHeight: Bar.wsIconSize - Bar.wsHorizontalSpacing
				radius: Bar.wsRounding
				color: st.isOccupied ? Colors.accent : Colors.foreground1
				opacity: active && st.isOccupied ? Bar.wsActiveOpacity : st.isOccupied ? Bar.wsOpacity : Bar.wsEmptyOpacity

				Icon {
					anchors.centerIn: parent
					text: !st.isUrgent ? Icons.ws[index + 1] : Icons.ws['urgent']
					font.pixelSize: !active ? Bar.wsIconSize / 2 : Bar.wsActiveIconSize / 2.5
					opacity: !st.isOccupied ? 0 : 1
					color: !st.isUrgent ? !st.isActive ? Colors.foreground2 : Colors.foreground1 : Colors.red
					Behavior on font.pixelSize {
						ISpringAnimation {}
					}
					Behavior on color {
						ColorAnimation {
							duration: Bar.wsAnimationDuration
							easing.type: Easing.InOutQuad
						}
					}
				}

				MouseArea {
					anchors {
						fill: parent
						topMargin: Bar.wsExtraMouseArea * -1
						bottomMargin: Bar.wsExtraMouseArea * -1
					}

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

					onPressed: {
						if (!parent.active)
							Hyprland.dispatch(`hl.dsp.focus({ workspace = '${index + 1}' })`);
					}
					function moveActive() {
						activeRect.x = parent.x;
						root.activeOccupied = st.isOccupied || false;
						activeRect.implicitWidth = active ? Bar.wsActiveIconSize : Bar.wsIconSize;
					}
					onEntered: {
						root.toChild = true;
						moveActive();
					}
					// https://github.com/quickshell-mirror/quickshell/issues/118
					// onEntered: {
					//     moveActive();
					// }
					onExited: {
						root.toChild = false;
						// outer MouseArea will handle this
						// if (!active && previousActiveIndex >= 0) {
						//     // just in case
						//     activeRect.x = layout.x + previousActiveIndex * Bar.wsIconSize + previousActiveIndex * Bar.wsSpacing;
						//     activeRect.implicitWidth = Bar.wsActiveIconSize;
						//     // root.activeOccupied = parent.st.isOccupied || false;
						//     root.activeOccupied = getWorkspaceStats(previousActiveIndex).isOccupied || false;
						//     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.wss)
								Hyprland.dispatch(`hl.dsp.focus({ workspace = '+1' })`);
							else
								Hyprland.dispatch(`hl.dsp.focus({ workspace = '1' })`);
						} else if (event.angleDelta.y > 0) {
							if (activeIndex + 1 > 1) {
								Hyprland.dispatch(`hl.dsp.focus({ workspace = '-1' })`);
							} else if (Bar.wss > 1) {
								Hyprland.dispatch(`hl.dsp.focus({ workspace = '${Bar.wss}' })`);
							}
						}
					}
				}

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

				onActiveChanged: if (active) {
					root.activeIndex = index;
					root.activeOccupied = st.isOccupied;
					activeRect.implicitWidth = Bar.wsActiveIconSize;
				}
			}
		}
	}
	MouseArea {
		height: Bar.height
		width: parent.width
		hoverEnabled: true
		z: -1000 // to be below the child's MouseArea
		acceptedButtons: Qt.MiddleButton

		// sync w the inner MouseArea
		// https://github.com/quickshell-mirror/quickshell/issues/118 // but onEnter won't update the mouseX
		onEntered: {
			const abovedItemIndex = Math.round((mouseX - layout.x) / (Bar.wsIconSize + Bar.wsSpacing)) - 1; // TODO: workspaceActiveIconSize might be before
			activeRect.x = mouseX - activeRect.width / 2;
			root.activeOccupied = root.getWorkspaceStats(abovedItemIndex).isOccupied || false;
			activeRect.implicitWidth = root.activeIndex === abovedItemIndex ? Bar.wsActiveIconSize : Bar.wsIconSize;
		}
		onExited: {
			if (!root.toChild && root.activeIndex >= 0) {
				// just in case
				activeRect.x = layout.x + root.activeIndex * Bar.wsIconSize + root.activeIndex * Bar.wsSpacing;
				activeRect.implicitWidth = Bar.wsActiveIconSize;
				root.activeOccupied = root.getWorkspaceStats(root.activeIndex).isOccupied || false;
			}
		}
		onWheel: event => {
			// no idea wats wrong w/ prev so we just unify it to use +/- 1
			if (event.angleDelta.y < 0) {
				if (root.activeIndex + 1 < Bar.wss)
					Hyprland.dispatch(`hl.dsp.focus({ workspace = '+1' })`);
				else
					Hyprland.dispatch(`hl.dsp.focus({ workspace = '1' })`);
			} else if (event.angleDelta.y > 0) {
				if (root.activeIndex + 1 > 1)
					Hyprland.dispatch(`hl.dsp.focus({ workspace = '1' })`);
				else if (Bar.wss > 1)
					Hyprland.dispatch(`hl.dsp.focus({ workspace = '${Bar.wss}' })`);
			}
		}
	}
}