File: Tabs.qml

package info (click to toggle)
angelfish 25.04.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,740 kB
  • sloc: cpp: 2,703; xml: 611; javascript: 273; makefile: 16; sh: 11; sql: 7
file content (401 lines) | stat: -rw-r--r-- 13,084 bytes parent folder | download
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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
// SPDX-FileCopyrightText: 2021 Felipe Kinoshita <kinofhek@gmail.com>
// SPDX-FileCopyrightText: 2023 Michael Lang <criticaltemp@protonmail.com>
// SPDX-License-Identifier: LGPL-2.0-or-later

import QtQuick 2.15
import QtQuick.Controls 2.15 as QQC2
import QtQuick.Layouts 1.15
import org.kde.kirigami 2.19 as Kirigami

import org.kde.angelfish 1.0

RowLayout {
    id: tabsComponent
    spacing: 0

    Shortcut {
        sequences: ["Ctrl+Tab", "Ctrl+PgDown"]
        onActivated: {
            if (listview.currentIndex != listview.count -1) {
                tabs.currentIndex++;
            } else {
                tabs.currentIndex = 0;
            }
        }
    }
    Shortcut {
        sequences: ["Ctrl+Shift+Tab", "Ctrl+PgUp"]
        onActivated: {
            if (listview.currentIndex === 0) {
                tabs.currentIndex = listview.count - 1;
            } else {
                tabs.currentIndex--;
            }
        }
    }

    Shortcut {
        sequence: "Ctrl+W"
        onActivated: {
            tabs.tabsModel.closeTab(listview.currentIndex);
        }
    }

    Shortcut {
        sequence: "Ctrl+1"
        onActivated: {
            if (listview.currentIndex !== 0) {
                tabs.currentIndex = 0;
            }
        }
    }
    Shortcut {
        sequence: "Ctrl+2"
        onActivated: {
            if (listview.currentIndex !== 1) {
                tabs.currentIndex = 1;
            }
        }
    }
    Shortcut {
        sequence: "Ctrl+3"
        onActivated: {
            if (listview.currentIndex !== 2) {
                tabs.currentIndex = 2;
            }
        }
    }
    Shortcut {
        sequence: "Ctrl+4"
        onActivated: {
            if (listview.currentIndex !== 3) {
                tabs.currentIndex = 3;
            }
        }
    }
    Shortcut {
        sequence: "Ctrl+5"
        onActivated: {
            if (listview.currentIndex !== 4) {
                tabs.currentIndex = 4;
            }
        }
    }
    Shortcut {
        sequence: "Ctrl+6"
        onActivated: {
            if (listview.currentIndex !== 5) {
                tabs.currentIndex = 5;
            }
        }
    }
    Shortcut {
        sequence: "Ctrl+7"
        onActivated: {
            if (listview.currentIndex !== 6) {
                tabs.currentIndex = 6;
            }
        }
    }
    Shortcut {
        sequence: "Ctrl+8"
        onActivated: {
            if (listview.currentIndex !== 7) {
                tabs.currentIndex = 7;
            }
        }
    }
    Shortcut {
        sequence: "Ctrl+9"
        onActivated: {
            if (listview.currentIndex !== listview.count - 1) {
                tabs.currentIndex = listview.count - 1;
            }
        }
    }

    ListView {
        id: listview
        visible: Settings.showTabBar || listview.count > 1
        Layout.fillWidth: true
        Layout.preferredHeight: footerItem.height
        model: tabs.model
        orientation: ListView.Horizontal
        currentIndex: tabs.currentIndex
        interactive: false
        boundsBehavior: Flickable.StopAtBounds
        headerPositioning: ListView.OverlayHeader
        header: Rectangle {
            z: 3
            height: parent.height
            width: listview.tabScroll ? height : 0
            color: Kirigami.Theme.backgroundColor

            QQC2.ToolButton {
                visible: listview.tabScroll
                enabled: !listview.atXBeginning
                icon.name: "arrow-left"
                onClicked: listview.flick(1000, 0)
                onDoubleClicked: listview.flick(5000, 0)
            }
        }

        WheelHandler {
            acceptedDevices: PointerDevice.Mouse | PointerDevice.TouchPad
            onWheel: listview.flick(event.angleDelta.y * 10, 0)
        }

        property int baseWidth: Kirigami.Units.gridUnit * 14
        property real tabWidth: baseWidth * Math.min(Math.max(listview.width / (baseWidth * (listview.count + 1)), 0.4), 1)
        property bool tabScroll: listview.tabWidth * listview.count > listview.width

        delegate: QQC2.ItemDelegate {
            id: control

            leftInset: 0
            rightInset: 0
            topInset: 0
            bottomInset: 0
            padding: 0

            hoverEnabled: true
            highlighted: ListView.isCurrentItem

            width: listview.tabWidth
            height: tabsComponent.height

            onClicked: {
                tabs.currentIndex = model.index;
            }

            background: Rectangle {
                implicitHeight: Kirigami.Units.gridUnit * 3 + Kirigami.Units.smallSpacing * 2
                color: control.highlighted ? Qt.lighter(Kirigami.Theme.backgroundColor, 1.1)
                    : (control.hovered ? Qt.darker(Kirigami.Theme.backgroundColor, 1.05)
                    : Kirigami.Theme.backgroundColor)
                Behavior on color { ColorAnimation { duration: Kirigami.Units.shortDuration } }

                QQC2.ToolSeparator {
                    anchors.top: parent.top
                    anchors.bottom: parent.bottom
                    anchors.right: parent.right
                    orientation: Qt.Vertical
                }

                QQC2.ToolSeparator {
                    visible: index === 0
                    anchors.top: parent.top
                    anchors.bottom: parent.bottom
                    anchors.left: parent.left
                    orientation: Qt.Vertical
                }

                MouseArea {
                    anchors.fill: parent

                    acceptedButtons: Qt.AllButtons

                    onClicked: (mouse) => {
                        if (mouse.button === Qt.MiddleButton) {
                            tabs.tabsModel.closeTab(model.index);
                        } else if (mouse.button === Qt.RightButton) {
                            tabMenu.index = model.index
                            if (tabMenu.visible) {
                                tabMenu.close()
                            } else {
                                tabMenu.popup(control)
                            }
                        }
                    }
                }
            }

            contentItem: RowLayout {
                id: layout
                spacing: Kirigami.Units.smallSpacing

                Kirigami.Icon {
                    id: tabIcon
                    Layout.alignment: Qt.AlignLeft
                    Layout.preferredWidth: Kirigami.Units.iconSizes.small
                    Layout.preferredHeight: width
                    source: tabs.itemAt(model.index).icon
                }

                QQC2.Label {
                    id: titleLabel
                    Layout.fillWidth: true
                    Layout.alignment: Qt.AlignLeft
                    Layout.leftMargin: Kirigami.Units.smallSpacing
                    Layout.rightMargin: Kirigami.Units.smallSpacing
                    text: tabs.itemAt(model.index).readerMode ?
                        i18nc("@label", "Reader mode: %1", tabs.itemAt(model.index).readerTitle)
                        : tabs.itemAt(model.index).title
                    elide: Text.ElideRight
                    horizontalAlignment: Text.AlignLeft
                }

                QQC2.AbstractButton {
                    id: closeButton
                    
                    hoverEnabled: true
                    visible: control.highlighted || control.width > Kirigami.Units.gridUnit * 8
                    Layout.alignment: Qt.AlignRight
                    Layout.preferredWidth: Kirigami.Units.iconSizes.smallMedium
                    Layout.fillHeight: true
                    onClicked: tabs.tabsModel.closeTab(model.index)

                    background: Item {}

                    contentItem: Kirigami.Icon {
                        source: 'tab-close-symbolic'
                        isMask: closeButton.hovered
                        color: Kirigami.Theme.textColor
                        anchors.centerIn: parent
                        implicitWidth: Kirigami.Units.iconSizes.smallMedium
                        implicitHeight: Kirigami.Units.iconSizes.smallMedium
                    }

                    QQC2.ToolTip.visible: closeButton.hovered
                    QQC2.ToolTip.delay: Qt.styleHints.mousePressAndHoldInterval
                    QQC2.ToolTip.text: i18nc("@info:tooltip", "Close tab")
                }

                QQC2.ToolTip.visible: control.hovered
                QQC2.ToolTip.delay: Qt.styleHints.mousePressAndHoldInterval
                QQC2.ToolTip.text: titleLabel.text
            }
        }
        footerPositioning: listview.tabScroll ? ListView.OverlayFooter : ListView.InlineFooter
        footer: Rectangle {
            z: 3
            height: row.height
            width: row.width
            color: Kirigami.Theme.backgroundColor

            Row {
                id: row
                QQC2.ToolButton {
                    visible: listview.tabScroll
                    enabled: !listview.atXEnd
                    icon.name: "arrow-right"
                    onClicked: listview.flick(-1000, 0)
                    onDoubleClicked: listview.flick(-5000, 0)
                }
                QQC2.ToolButton {
                    icon.name: "list-add"
                    onClicked: tabs.tabsModel.newTab(Settings.newTabUrl)

                    QQC2.ToolTip.visible: hoverHandler.hovered
                    QQC2.ToolTip.delay: Qt.styleHints.mousePressAndHoldInterval
                    QQC2.ToolTip.text: i18nc("@info:tooltip", "Open a new tab")

                    HoverHandler {
                        id: hoverHandler
                        acceptedDevices: PointerDevice.Mouse | PointerDevice.Stylus
                    }
                }

            }
        }
        add: Transition {
            NumberAnimation { property: "opacity"; from: 0; to: 1; duration: Kirigami.Units.longDuration; easing.type: Easing.InQuad }
        }
        remove: Transition {
            NumberAnimation { property: "opacity"; to: 0; duration: Kirigami.Units.longDuration; easing.type: Easing.OutQuad }
        }
    }

    Rectangle {
        Layout.alignment: Qt.AlignRight
        z: 3
        height: listview.footerItem.height
        width: height
        color: Kirigami.Theme.backgroundColor

        QQC2.ToolButton {
            id: menuButton
            icon.name: "arrow-down"
            down: menu.visible
            onPressed: menu.visible ? menu.close() : menu.popup(tabsComponent.width, tabsComponent.y + menuButton.height)

            QQC2.ToolTip.visible: hoverHandler.hovered
            QQC2.ToolTip.delay: Qt.styleHints.mousePressAndHoldInterval
            QQC2.ToolTip.text: i18nc("@info:tooltip", "List all tabs")

            HoverHandler {
                id: hoverHandler
                acceptedDevices: PointerDevice.Mouse | PointerDevice.Stylus
            }
        }
    }

    QQC2.Menu {
        id: menu
        Instantiator {
            model: tabs.model
            onObjectAdded: (index, object) => menu.insertItem(index, object)
            onObjectRemoved: (index, object) => menu.removeItem(object)
            delegate: QQC2.MenuItem {
                icon.name: tabs.itemAt(model.index).icon
                text: tabs.itemAt(model.index).title
                onTriggered: {
                    tabs.currentIndex = model.index;
                    menu.close();
                }
            }
        }
    }

    QQC2.Menu {
        id: tabMenu
        property int index

        Kirigami.Action {
            text: i18nc("@action:inmenu", "New Tab")
            icon.name: "list-add"
            shortcut: "Ctrl+T"
            onTriggered: tabs.tabsModel.newTab(Settings.newTabUrl)
        }

        QQC2.MenuSeparator {}

        Kirigami.Action {
            text: i18nc("@action:inmenu", "Reload Tab")
            icon.name: "view-refresh"
            shortcut: "Ctrl+R"
            onTriggered: {
                currentWebView.reload();
            }
        }
        Kirigami.Action {
            text: i18nc("@action:inmenu", "Duplicate Tab")
            icon.name: "tab-duplicate"
            onTriggered: tabs.tabsModel.newTab(currentWebView.url)
        }

        QQC2.MenuSeparator {}

        Kirigami.Action {
            text: i18nc("@action:inmenu", "Bookmark Tab")
            icon.name: urlObserver.bookmarked ? "rating" : "rating-unrated"
            shortcut: "Ctrl+D"
            onTriggered: {
                const request = {
                    url: currentWebView.url,
                    title: currentWebView.title,
                    icon: currentWebView.icon
                }
                BrowserManager.addBookmark(request)
            }
        }

        QQC2.MenuSeparator {}

        Kirigami.Action {
            text: i18nc("@action:inmenu", "Close Tab")
            icon.name: "tab-close"
            onTriggered: tabs.tabsModel.closeTab(tabMenu.index)
        }
    }
}