summaryrefslogtreecommitdiff
path: root/raveos-theme/hyprland/theme-data/DankMaterialShell/quickshell/Modules/DankBar/CenterSection.qml
blob: df86d4d3a0265868ed5e81936bd8622e8f157c62 (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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
import QtQuick
import qs.Common

Item {
    id: root

    property var widgetsModel: null
    property var components: null
    property bool noBackground: false
    required property var axis
    property string section: "center"
    property var parentScreen: null
    property real widgetThickness: 30
    property real barThickness: 48
    property real barSpacing: 4
    property var barConfig: null
    property var blurBarWindow: null
    property bool overrideAxisLayout: false
    property bool forceVerticalLayout: false

    readonly property bool isVertical: overrideAxisLayout ? forceVerticalLayout : (axis?.isVertical ?? false)
    readonly property real widgetSpacing: {
        const baseSpacing = noBackground ? 2 : Theme.spacingXS;
        const outlineThickness = (barConfig?.widgetOutlineEnabled ?? false) ? (barConfig?.widgetOutlineThickness ?? 1) : 0;
        return baseSpacing + (outlineThickness * 2);
    }

    property var centerWidgets: []
    property int totalWidgets: 0
    property real totalSize: 0

    function updateLayout() {
        if (SettingsData.centeringMode === "geometric") {
            applyGeometricLayout();
        } else {
            applyIndexLayout();
        }
    }

    function applyGeometricLayout() {
        if ((isVertical ? height : width) <= 0 || !visible)
            return;

        centerWidgets = [];
        totalWidgets = 0;
        totalSize = 0;

        for (var i = 0; i < centerRepeater.count; i++) {
            const loader = centerRepeater.itemAt(i);
            if (loader && loader.active && loader.item) {
                centerWidgets.push(loader.item);
                totalWidgets++;
                totalSize += isVertical ? loader.item.height : loader.item.width;
            }
        }

        if (totalWidgets === 0)
            return;

        if (totalWidgets > 1)
            totalSize += widgetSpacing * (totalWidgets - 1);

        positionWidgetsGeometric();
    }

    function positionWidgetsGeometric() {
        const parentLength = isVertical ? height : width;
        const parentCenter = parentLength / 2;
        let currentPos = parentCenter - (totalSize / 2);

        centerWidgets.forEach(widget => {
            if (isVertical) {
                widget.anchors.verticalCenter = undefined;
                widget.y = currentPos;
            } else {
                widget.anchors.horizontalCenter = undefined;
                widget.x = currentPos;
            }
            const widgetSize = isVertical ? widget.height : widget.width;
            currentPos += widgetSize + widgetSpacing;
        });
    }

    function applyIndexLayout() {
        if ((isVertical ? height : width) <= 0 || !visible)
            return;

        centerWidgets = [];
        totalWidgets = 0;
        totalSize = 0;

        let configuredMiddleWidget = null;
        let configuredLeftWidget = null;
        let configuredRightWidget = null;

        const configuredWidgets = centerRepeater.count;
        const isOddConfigured = configuredWidgets % 2 === 1;
        const configuredMiddlePos = Math.floor(configuredWidgets / 2);
        const configuredLeftPos = isOddConfigured ? -1 : ((configuredWidgets / 2) - 1);
        const configuredRightPos = isOddConfigured ? -1 : (configuredWidgets / 2);

        for (var i = 0; i < centerRepeater.count; i++) {
            const wrapper = centerRepeater.itemAt(i);
            if (!wrapper)
                continue;

            if (isOddConfigured && i === configuredMiddlePos && wrapper.active && wrapper.item)
                configuredMiddleWidget = wrapper.item;
            if (!isOddConfigured && i === configuredLeftPos && wrapper.active && wrapper.item)
                configuredLeftWidget = wrapper.item;
            if (!isOddConfigured && i === configuredRightPos && wrapper.active && wrapper.item)
                configuredRightWidget = wrapper.item;

            if (wrapper.active && wrapper.item) {
                centerWidgets.push(wrapper.item);
                totalWidgets++;
                totalSize += isVertical ? wrapper.item.height : wrapper.item.width;
            }
        }

        if (totalWidgets === 0)
            return;

        if (totalWidgets > 1)
            totalSize += widgetSpacing * (totalWidgets - 1);

        positionWidgetsByIndex(configuredWidgets, configuredMiddleWidget, configuredLeftWidget, configuredRightWidget);
    }

    function positionWidgetsByIndex(configuredWidgets, configuredMiddleWidget, configuredLeftWidget, configuredRightWidget) {
        const parentCenter = (isVertical ? height : width) / 2;
        const isOddConfigured = configuredWidgets % 2 === 1;

        centerWidgets.forEach(widget => {
            if (isVertical)
                widget.anchors.verticalCenter = undefined;
            else
                widget.anchors.horizontalCenter = undefined;
        });

        if (isOddConfigured && configuredMiddleWidget) {
            const middleWidget = configuredMiddleWidget;
            const middleIndex = centerWidgets.indexOf(middleWidget);
            const middleSize = isVertical ? middleWidget.height : middleWidget.width;

            if (isVertical)
                middleWidget.y = parentCenter - (middleSize / 2);
            else
                middleWidget.x = parentCenter - (middleSize / 2);

            let currentPos = isVertical ? middleWidget.y : middleWidget.x;
            for (var i = middleIndex - 1; i >= 0; i--) {
                const size = isVertical ? centerWidgets[i].height : centerWidgets[i].width;
                currentPos -= (widgetSpacing + size);
                if (isVertical)
                    centerWidgets[i].y = currentPos;
                else
                    centerWidgets[i].x = currentPos;
            }

            currentPos = (isVertical ? middleWidget.y : middleWidget.x) + middleSize;
            for (var i = middleIndex + 1; i < totalWidgets; i++) {
                currentPos += widgetSpacing;
                if (isVertical)
                    centerWidgets[i].y = currentPos;
                else
                    centerWidgets[i].x = currentPos;
                currentPos += isVertical ? centerWidgets[i].height : centerWidgets[i].width;
            }
            return;
        }

        if (totalWidgets === 1) {
            const widget = centerWidgets[0];
            const size = isVertical ? widget.height : widget.width;
            if (isVertical)
                widget.y = parentCenter - (size / 2);
            else
                widget.x = parentCenter - (size / 2);
            return;
        }

        if (!configuredLeftWidget || !configuredRightWidget) {
            if (totalWidgets % 2 === 1) {
                const middleIndex = Math.floor(totalWidgets / 2);
                const middleWidget = centerWidgets[middleIndex];

                if (!middleWidget)
                    return;

                const middleSize = isVertical ? middleWidget.height : middleWidget.width;

                if (isVertical)
                    middleWidget.y = parentCenter - (middleSize / 2);
                else
                    middleWidget.x = parentCenter - (middleSize / 2);

                let currentPos = isVertical ? middleWidget.y : middleWidget.x;
                for (var i = middleIndex - 1; i >= 0; i--) {
                    const size = isVertical ? centerWidgets[i].height : centerWidgets[i].width;
                    currentPos -= (widgetSpacing + size);
                    if (isVertical)
                        centerWidgets[i].y = currentPos;
                    else
                        centerWidgets[i].x = currentPos;
                }

                currentPos = (isVertical ? middleWidget.y : middleWidget.x) + middleSize;
                for (var i = middleIndex + 1; i < totalWidgets; i++) {
                    currentPos += widgetSpacing;
                    if (isVertical)
                        centerWidgets[i].y = currentPos;
                    else
                        centerWidgets[i].x = currentPos;
                    currentPos += isVertical ? centerWidgets[i].height : centerWidgets[i].width;
                }
            } else {
                const leftIndex = (totalWidgets / 2) - 1;
                const rightIndex = totalWidgets / 2;
                const fallbackLeft = centerWidgets[leftIndex];
                const fallbackRight = centerWidgets[rightIndex];

                if (!fallbackLeft || !fallbackRight)
                    return;

                const halfSpacing = widgetSpacing / 2;
                const leftSize = isVertical ? fallbackLeft.height : fallbackLeft.width;

                if (isVertical) {
                    fallbackLeft.y = parentCenter - halfSpacing - leftSize;
                    fallbackRight.y = parentCenter + halfSpacing;
                } else {
                    fallbackLeft.x = parentCenter - halfSpacing - leftSize;
                    fallbackRight.x = parentCenter + halfSpacing;
                }

                let currentPos = isVertical ? fallbackLeft.y : fallbackLeft.x;
                for (var i = leftIndex - 1; i >= 0; i--) {
                    const size = isVertical ? centerWidgets[i].height : centerWidgets[i].width;
                    currentPos -= (widgetSpacing + size);
                    if (isVertical)
                        centerWidgets[i].y = currentPos;
                    else
                        centerWidgets[i].x = currentPos;
                }

                currentPos = (isVertical ? fallbackRight.y + fallbackRight.height : fallbackRight.x + fallbackRight.width);
                for (var i = rightIndex + 1; i < totalWidgets; i++) {
                    currentPos += widgetSpacing;
                    if (isVertical)
                        centerWidgets[i].y = currentPos;
                    else
                        centerWidgets[i].x = currentPos;
                    currentPos += isVertical ? centerWidgets[i].height : centerWidgets[i].width;
                }
            }
            return;
        }

        const leftWidget = configuredLeftWidget;
        const rightWidget = configuredRightWidget;
        const leftIndex = centerWidgets.indexOf(leftWidget);
        const rightIndex = centerWidgets.indexOf(rightWidget);
        const halfSpacing = widgetSpacing / 2;
        const leftSize = isVertical ? leftWidget.height : leftWidget.width;

        if (isVertical) {
            leftWidget.y = parentCenter - halfSpacing - leftSize;
            rightWidget.y = parentCenter + halfSpacing;
        } else {
            leftWidget.x = parentCenter - halfSpacing - leftSize;
            rightWidget.x = parentCenter + halfSpacing;
        }

        let currentPos = isVertical ? leftWidget.y : leftWidget.x;
        for (var i = leftIndex - 1; i >= 0; i--) {
            const size = isVertical ? centerWidgets[i].height : centerWidgets[i].width;
            currentPos -= (widgetSpacing + size);
            if (isVertical)
                centerWidgets[i].y = currentPos;
            else
                centerWidgets[i].x = currentPos;
        }

        currentPos = (isVertical ? rightWidget.y + rightWidget.height : rightWidget.x + rightWidget.width);
        for (var i = rightIndex + 1; i < totalWidgets; i++) {
            currentPos += widgetSpacing;
            if (isVertical)
                centerWidgets[i].y = currentPos;
            else
                centerWidgets[i].x = currentPos;
            currentPos += isVertical ? centerWidgets[i].height : centerWidgets[i].width;
        }
    }

    height: parent.height
    width: parent.width
    anchors.centerIn: parent

    implicitWidth: isVertical ? widgetThickness : totalSize
    implicitHeight: isVertical ? totalSize : widgetThickness

    Timer {
        id: layoutTimer
        interval: 0
        repeat: false
        onTriggered: root.updateLayout()
    }

    Component.onCompleted: layoutTimer.restart()

    onWidthChanged: {
        if (width > 0)
            layoutTimer.restart();
    }

    onHeightChanged: {
        if (height > 0)
            layoutTimer.restart();
    }

    onVisibleChanged: {
        if (visible && (isVertical ? height : width) > 0)
            layoutTimer.restart();
    }

    Repeater {
        id: centerRepeater
        model: root.widgetsModel

        onCountChanged: layoutTimer.restart()

        Item {
            property var itemData: modelData
            readonly property real itemSpacing: root.widgetSpacing

            width: root.isVertical ? root.width : (widgetLoader.item ? widgetLoader.item.width : 0)
            height: widgetLoader.item ? widgetLoader.item.height : 0

            readonly property bool active: widgetLoader.active
            readonly property var item: widgetLoader.item

            WidgetHost {
                id: widgetLoader

                anchors.verticalCenter: !root.isVertical ? parent.verticalCenter : undefined
                anchors.horizontalCenter: root.isVertical ? parent.horizontalCenter : undefined

                widgetId: itemData.widgetId
                widgetData: itemData
                spacerSize: itemData.size || 20
                components: root.components
                isInColumn: root.isVertical
                axis: root.axis
                section: "center"
                parentScreen: root.parentScreen
                widgetThickness: root.widgetThickness
                barThickness: root.barThickness
                barSpacing: root.barSpacing
                barConfig: root.barConfig
                blurBarWindow: root.blurBarWindow
                isFirst: index === 0
                isLast: index === centerRepeater.count - 1
                sectionSpacing: parent.itemSpacing
                isLeftBarEdge: false
                isRightBarEdge: false
                isTopBarEdge: false
                isBottomBarEdge: false

                onContentItemReady: contentItem => {
                    contentItem.widthChanged.connect(() => layoutTimer.restart());
                    contentItem.heightChanged.connect(() => layoutTimer.restart());
                    layoutTimer.restart();
                }

                onActiveChanged: layoutTimer.restart()
            }
        }
    }

    Connections {
        target: SettingsData
        function onCenteringModeChanged() {
            layoutTimer.restart();
        }
    }
}