summaryrefslogtreecommitdiff
path: root/raveos-theme/hyprland/theme-data/DankMaterialShell/quickshell/Widgets/DankTabBar.qml
blob: d3b72f1227029d471e70bc060daf5e97bdce73a3 (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
import QtQuick
import qs.Common
import qs.Widgets

FocusScope {
    id: tabBar

    property alias model: tabRepeater.model
    property int currentIndex: 0
    property int spacing: Theme.spacingL
    property int tabHeight: 56
    property bool showIcons: true
    property bool equalWidthTabs: true
    property bool enableArrowNavigation: true
    property Item nextFocusTarget: null
    property Item previousFocusTarget: null

    signal tabClicked(int index)
    signal actionTriggered(int index)

    focus: false
    activeFocusOnTab: true
    height: tabHeight

    KeyNavigation.tab: nextFocusTarget
    KeyNavigation.down: nextFocusTarget
    KeyNavigation.backtab: previousFocusTarget
    KeyNavigation.up: previousFocusTarget

    Keys.onPressed: event => {
        if (!tabBar.activeFocus || tabRepeater.count === 0)
            return;
        function findSelectableIndex(startIndex, step) {
            let idx = startIndex;
            for (let i = 0; i < tabRepeater.count; i++) {
                idx = (idx + step + tabRepeater.count) % tabRepeater.count;
                const item = tabRepeater.itemAt(idx);
                if (item && !item.isAction)
                    return idx;
            }
            return -1;
        }

        const goToIndex = nextIndex => {
            if (nextIndex >= 0 && nextIndex !== tabBar.currentIndex) {
                tabBar.currentIndex = nextIndex;
                tabBar.tabClicked(nextIndex);
            }
        };

        const resolveTarget = item => {
            if (!item)
                return null;

            if (item.focusTarget)
                return resolveTarget(item.focusTarget);

            return item;
        };

        const focusItem = item => {
            const target = resolveTarget(item);
            if (!target)
                return false;

            if (target.requestFocus) {
                Qt.callLater(() => target.requestFocus());
                return true;
            }

            if (target.forceActiveFocus) {
                Qt.callLater(() => target.forceActiveFocus());
                return true;
            }

            return false;
        };

        if (event.key === Qt.Key_Right && tabBar.enableArrowNavigation) {
            const baseIndex = (tabBar.currentIndex >= 0 && tabBar.currentIndex < tabRepeater.count) ? tabBar.currentIndex : -1;
            const nextIndex = findSelectableIndex(baseIndex, 1);
            if (nextIndex >= 0) {
                goToIndex(nextIndex);
                event.accepted = true;
            }
        } else if (event.key === Qt.Key_Left && tabBar.enableArrowNavigation) {
            const baseIndex = (tabBar.currentIndex >= 0 && tabBar.currentIndex < tabRepeater.count) ? tabBar.currentIndex : 0;
            const nextIndex = findSelectableIndex(baseIndex, -1);
            if (nextIndex >= 0) {
                goToIndex(nextIndex);
                event.accepted = true;
            }
        } else if (event.key === Qt.Key_Tab && (event.modifiers & Qt.ShiftModifier)) {
            if (focusItem(tabBar.previousFocusTarget)) {
                event.accepted = true;
            }
        } else if (event.key === Qt.Key_Tab || event.key === Qt.Key_Down) {
            if (focusItem(tabBar.nextFocusTarget)) {
                event.accepted = true;
            }
        } else if (event.key === Qt.Key_Up) {
            if (focusItem(tabBar.previousFocusTarget)) {
                event.accepted = true;
            }
        }
    }

    Row {
        id: tabRow
        anchors.fill: parent
        spacing: tabBar.spacing

        Repeater {
            id: tabRepeater

            Item {
                id: tabItem
                property bool isAction: modelData && modelData.isAction === true
                property bool isActive: !isAction && tabBar.currentIndex === index
                property bool hasIcon: tabBar.showIcons && modelData && modelData.icon && modelData.icon.length > 0
                property bool hasText: modelData && modelData.text && modelData.text.length > 0

                width: tabBar.equalWidthTabs ? (tabBar.width - tabBar.spacing * Math.max(0, tabRepeater.count - 1)) / Math.max(1, tabRepeater.count) : Math.max(contentCol.implicitWidth + Theme.spacingXL, 64)
                height: tabBar.tabHeight

                Column {
                    id: contentCol
                    anchors.centerIn: parent
                    spacing: Theme.spacingXS

                    DankIcon {
                        name: modelData.icon || ""
                        anchors.horizontalCenter: parent.horizontalCenter
                        size: Theme.iconSize
                        color: tabItem.isActive ? Theme.primary : Theme.surfaceText
                        visible: hasIcon
                    }

                    StyledText {
                        text: modelData.text || ""
                        anchors.horizontalCenter: parent.horizontalCenter
                        font.pixelSize: Theme.fontSizeMedium
                        color: tabItem.isActive ? Theme.primary : Theme.surfaceText
                        font.weight: Font.Medium
                        visible: hasText
                    }
                }

                Rectangle {
                    id: stateLayer
                    anchors.fill: parent
                    color: Theme.surfaceTint
                    opacity: tabArea.pressed ? 0.12 : (tabArea.containsMouse ? 0.08 : 0)
                    visible: opacity > 0
                    radius: Theme.cornerRadius
                    Behavior on opacity {
                        NumberAnimation {
                            duration: Theme.shortDuration
                            easing.type: Theme.standardEasing
                        }
                    }
                }

                DankRipple {
                    id: tabRipple
                    cornerRadius: Theme.cornerRadius
                }

                MouseArea {
                    id: tabArea
                    anchors.fill: parent
                    hoverEnabled: true
                    cursorShape: Qt.PointingHandCursor
                    onPressed: mouse => tabRipple.trigger(mouse.x, mouse.y)
                    onClicked: {
                        if (tabItem.isAction) {
                            tabBar.actionTriggered(index);
                        } else {
                            tabBar.tabClicked(index);
                        }
                    }
                }
            }
        }
    }

    Rectangle {
        id: indicator
        y: parent.height + 7
        height: 3
        width: 60
        topLeftRadius: Theme.cornerRadius
        topRightRadius: Theme.cornerRadius
        bottomLeftRadius: 0
        bottomRightRadius: 0
        color: Theme.primary
        visible: false

        property bool animationEnabled: false
        property bool initialSetupComplete: false

        Behavior on x {
            enabled: indicator.animationEnabled
            NumberAnimation {
                duration: Theme.mediumDuration
                easing.type: Theme.standardEasing
            }
        }

        Behavior on width {
            enabled: indicator.animationEnabled
            NumberAnimation {
                duration: Theme.mediumDuration
                easing.type: Theme.standardEasing
            }
        }
    }

    Rectangle {
        width: parent.width
        height: 1
        y: parent.height + 10
        color: Theme.outlineStrong
    }

    function updateIndicator() {
        if (tabRepeater.count === 0 || currentIndex < 0 || currentIndex >= tabRepeater.count) {
            return;
        }

        const item = tabRepeater.itemAt(currentIndex);
        if (!item || item.isAction) {
            return;
        }

        const tabPos = item.mapToItem(tabBar, 0, 0);
        const tabCenterX = tabPos.x + item.width / 2;
        const indicatorWidth = 60;

        if (tabPos.x < 10 && currentIndex > 0) {
            Qt.callLater(updateIndicator);
            return;
        }

        if (!indicator.initialSetupComplete) {
            indicator.animationEnabled = false;
            indicator.width = indicatorWidth;
            indicator.x = tabCenterX - indicatorWidth / 2;
            indicator.visible = true;
            indicator.initialSetupComplete = true;
            indicator.animationEnabled = true;
        } else {
            indicator.width = indicatorWidth;
            indicator.x = tabCenterX - indicatorWidth / 2;
            indicator.visible = true;
        }
    }

    onCurrentIndexChanged: {
        Qt.callLater(updateIndicator);
    }
    onWidthChanged: Qt.callLater(updateIndicator)
}