summaryrefslogtreecommitdiff
path: root/raveos-hyprland-theme/theme-data/DankMaterialShell/quickshell/Services/ExtWorkspaceService.qml
blob: 48a8f9a11a628a6a652292051d80d6c8f013cf5a (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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
pragma Singleton
pragma ComponentBehavior: Bound

import QtQuick
import Quickshell

Singleton {
    id: root

    property bool extWorkspaceAvailable: false
    property var groups: []
    property var _cachedWorkspaces: ({})

    signal stateChanged

    Connections {
        target: DMSService
        function onCapabilitiesReceived() {
            checkCapabilities();
        }
        function onConnectionStateChanged() {
            if (DMSService.isConnected) {
                checkCapabilities();
            } else {
                extWorkspaceAvailable = false;
            }
        }
        function onExtWorkspaceStateUpdate(data) {
            if (extWorkspaceAvailable) {
                handleStateUpdate(data);
            }
        }
    }

    Component.onCompleted: {
        if (DMSService.dmsAvailable) {
            checkCapabilities();
        }
    }

    function checkCapabilities() {
        if (!DMSService.capabilities || !Array.isArray(DMSService.capabilities)) {
            extWorkspaceAvailable = false;
            return;
        }

        const hasExtWorkspace = DMSService.capabilities.includes("extworkspace");
        if (hasExtWorkspace && !extWorkspaceAvailable) {
            if (typeof CompositorService !== "undefined") {
                const useExtWorkspace = DMSService.forceExtWorkspace || (!CompositorService.isNiri && !CompositorService.isHyprland && !CompositorService.isDwl && !CompositorService.isSway && !CompositorService.isScroll && !CompositorService.isMiracle);
                if (!useExtWorkspace) {
                    console.info("ExtWorkspaceService: ext-workspace available but compositor has native support");
                    extWorkspaceAvailable = false;
                    return;
                }
            }
            extWorkspaceAvailable = true;
            console.info("ExtWorkspaceService: ext-workspace capability detected");
            DMSService.addSubscription("extworkspace");
            requestState();
        } else if (!hasExtWorkspace) {
            extWorkspaceAvailable = false;
        }
    }

    function requestState() {
        if (!DMSService.isConnected || !extWorkspaceAvailable) {
            return;
        }

        DMSService.sendRequest("extworkspace.getState", null, response => {
            if (response.result) {
                handleStateUpdate(response.result);
            }
        });
    }

    function handleStateUpdate(state) {
        groups = state.groups || [];
        if (groups.length === 0) {
            console.warn("ExtWorkspaceService: Received empty workspace groups from backend");
        } else {
            console.log("ExtWorkspaceService: Updated with", groups.length, "workspace groups");
        }
        stateChanged();
    }

    function activateWorkspace(workspaceID, groupID = "") {
        if (!DMSService.isConnected || !extWorkspaceAvailable) {
            return;
        }

        DMSService.sendRequest("extworkspace.activateWorkspace", {
            "workspaceID": workspaceID,
            "groupID": groupID
        }, response => {
            if (response.error) {
                console.warn("ExtWorkspaceService: activateWorkspace error:", response.error);
            }
        });
    }

    function deactivateWorkspace(workspaceID, groupID = "") {
        if (!DMSService.isConnected || !extWorkspaceAvailable) {
            return;
        }

        DMSService.sendRequest("extworkspace.deactivateWorkspace", {
            "workspaceID": workspaceID,
            "groupID": groupID
        }, response => {
            if (response.error) {
                console.warn("ExtWorkspaceService: deactivateWorkspace error:", response.error);
            }
        });
    }

    function removeWorkspace(workspaceID, groupID = "") {
        if (!DMSService.isConnected || !extWorkspaceAvailable) {
            return;
        }

        DMSService.sendRequest("extworkspace.removeWorkspace", {
            "workspaceID": workspaceID,
            "groupID": groupID
        }, response => {
            if (response.error) {
                console.warn("ExtWorkspaceService: removeWorkspace error:", response.error);
            }
        });
    }

    function createWorkspace(groupID, name) {
        if (!DMSService.isConnected || !extWorkspaceAvailable) {
            return;
        }

        DMSService.sendRequest("extworkspace.createWorkspace", {
            "groupID": groupID,
            "name": name
        }, response => {
            if (response.error) {
                console.warn("ExtWorkspaceService: createWorkspace error:", response.error);
            }
        });
    }

    function getGroupForOutput(outputName) {
        for (const group of groups) {
            if (group.outputs && group.outputs.includes(outputName)) {
                return group;
            }
        }
        return null;
    }

    function getWorkspacesForOutput(outputName) {
        const group = getGroupForOutput(outputName);
        return group ? (group.workspaces || []) : [];
    }

    function getActiveWorkspaces() {
        const active = [];
        for (const group of groups) {
            if (!group.workspaces)
                continue;
            for (const ws of group.workspaces) {
                if (ws.active) {
                    active.push({
                        workspace: ws,
                        group: group,
                        outputs: group.outputs || []
                    });
                }
            }
        }
        return active;
    }

    function getActiveWorkspaceForOutput(outputName) {
        const group = getGroupForOutput(outputName);
        if (!group || !group.workspaces)
            return null;

        for (const ws of group.workspaces) {
            if (ws.active) {
                return ws;
            }
        }
        return null;
    }

    function getVisibleWorkspaces(outputName) {
        const workspaces = getWorkspacesForOutput(outputName);
        let visible = workspaces.filter(ws => !ws.hidden);

        const hasValidCoordinates = visible.some(ws => ws.coordinates && ws.coordinates.length > 0);
        if (hasValidCoordinates) {
            visible = visible.sort((a, b) => {
                const coordsA = a.coordinates || [0, 0];
                const coordsB = b.coordinates || [0, 0];
                if (coordsA[0] !== coordsB[0])
                    return coordsA[0] - coordsB[0];
                return coordsA[1] - coordsB[1];
            });
        }

        const cacheKey = outputName;
        if (!_cachedWorkspaces[cacheKey]) {
            _cachedWorkspaces[cacheKey] = {
                workspaces: [],
                lastNames: []
            };
        }

        const cache = _cachedWorkspaces[cacheKey];
        const currentNames = visible.map(ws => ws.name || ws.id);
        const namesChanged = JSON.stringify(cache.lastNames) !== JSON.stringify(currentNames);

        if (namesChanged || cache.workspaces.length !== visible.length) {
            cache.workspaces = visible.map(ws => ({
                        id: ws.id,
                        name: ws.name,
                        coordinates: ws.coordinates,
                        state: ws.state,
                        active: ws.active,
                        urgent: ws.urgent,
                        hidden: ws.hidden
                    }));
            cache.lastNames = currentNames;
            return cache.workspaces;
        }

        for (let i = 0; i < visible.length; i++) {
            const src = visible[i];
            const dst = cache.workspaces[i];
            dst.id = src.id;
            dst.name = src.name;
            dst.coordinates = src.coordinates;
            dst.state = src.state;
            dst.active = src.active;
            dst.urgent = src.urgent;
            dst.hidden = src.hidden;
        }

        return cache.workspaces;
    }

    function getUrgentWorkspaces() {
        const urgent = [];
        for (const group of groups) {
            if (!group.workspaces)
                continue;
            for (const ws of group.workspaces) {
                if (ws.urgent) {
                    urgent.push({
                        workspace: ws,
                        group: group,
                        outputs: group.outputs || []
                    });
                }
            }
        }
        return urgent;
    }

    function switchToWorkspace(outputName, workspaceName) {
        const workspaces = getWorkspacesForOutput(outputName);
        for (const ws of workspaces) {
            if (ws.name === workspaceName || ws.id === workspaceName) {
                activateWorkspace(ws.name || ws.id);
                return;
            }
        }
        console.warn("ExtWorkspaceService: workspace not found:", workspaceName);
    }
}