blob: 05a21d18b0b1832e72395a7751757a788a4a9ebd (
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
|
pragma ComponentBehavior: Bound
import QtQuick
import QtQuick.Controls
import qs.Common
import qs.Services
import qs.Widgets
Column {
id: root
property var currentTab: NotepadStorageService.tabs.length > NotepadStorageService.currentTabIndex ? NotepadStorageService.tabs[NotepadStorageService.currentTabIndex] : null
property bool contentLoaded: false
property int draggedIndex: -1
property int dropTargetIndex: -1
property bool suppressShiftAnimation: false
readonly property real tabItemSize: 128 + Theme.spacingXS
signal tabSwitched(int tabIndex)
signal tabClosed(int tabIndex)
signal newTabRequested
function hasUnsavedChangesForTab(tab) {
if (!tab)
return false;
if (tab.id === currentTab?.id) {
return root.parent?.hasUnsavedChanges ? root.parent.hasUnsavedChanges() : false;
}
return false;
}
spacing: Theme.spacingXS
Row {
width: parent.width
height: 36
spacing: Theme.spacingXS
ScrollView {
width: parent.width - newTabButton.width - Theme.spacingXS
height: parent.height
clip: true
ScrollBar.horizontal.visible: false
ScrollBar.vertical.visible: false
Row {
spacing: Theme.spacingXS
Repeater {
model: NotepadStorageService.tabs
delegate: Item {
id: delegateItem
required property int index
required property var modelData
readonly property bool isActive: NotepadStorageService.currentTabIndex === index
readonly property bool isHovered: tabMouseArea.containsMouse && !closeMouseArea.containsMouse
readonly property real tabWidth: 128
property bool longPressing: false
property bool dragging: false
property point dragStartPos: Qt.point(0, 0)
property int targetIndex: -1
property int originalIndex: -1
property real dragAxisOffset: 0
Timer {
id: longPressTimer
interval: 200
repeat: false
onTriggered: {
if (NotepadStorageService.tabs.length > 1) {
delegateItem.longPressing = true
}
}
}
readonly property real shiftOffset: {
if (root.draggedIndex < 0)
return 0
if (index === root.draggedIndex)
return 0
var dragIdx = root.draggedIndex
var dropIdx = root.dropTargetIndex
var myIdx = index
var shiftAmount = root.tabItemSize
if (dropIdx < 0)
return 0
if (dragIdx < dropIdx && myIdx > dragIdx && myIdx <= dropIdx)
return -shiftAmount
if (dragIdx > dropIdx && myIdx >= dropIdx && myIdx < dragIdx)
return shiftAmount
return 0
}
width: tabWidth
height: 32
z: dragging ? 100 : 0
transform: Translate {
x: shiftOffset
Behavior on x {
enabled: !root.suppressShiftAnimation
NumberAnimation {
duration: 150
easing.type: Easing.OutCubic
}
}
}
Item {
id: tabVisual
anchors.fill: parent
z: 1
layer.enabled: dragging
layer.smooth: true
transform: Translate {
x: dragging ? dragAxisOffset : 0
}
Rectangle {
id: tabRect
anchors.fill: parent
radius: Theme.cornerRadius
color: isActive ? Theme.primaryPressed : isHovered ? Theme.primaryHoverLight : Theme.withAlpha(Theme.primaryPressed, 0)
border.width: isActive || dragging ? 0 : 1
border.color: dragging ? Theme.primary : Theme.outlineMedium
clip: true
Row {
id: tabContent
anchors.fill: parent
anchors.leftMargin: Theme.spacingM
anchors.rightMargin: Theme.spacingM
spacing: Theme.spacingXS
StyledText {
id: tabText
width: parent.width - (tabCloseButton.visible ? tabCloseButton.width + Theme.spacingXS : 0)
text: {
var prefix = ""
if (hasUnsavedChangesForTab(modelData)) {
prefix = "● "
}
return prefix + (modelData.title || "Untitled")
}
font.pixelSize: Theme.fontSizeSmall
color: isActive ? Theme.primary : Theme.surfaceText
font.weight: isActive ? Font.Medium : Font.Normal
elide: Text.ElideMiddle
maximumLineCount: 1
wrapMode: Text.NoWrap
anchors.verticalCenter: parent.verticalCenter
}
Rectangle {
id: tabCloseButton
width: 20
height: 20
radius: Theme.cornerRadius
color: closeMouseArea.containsMouse ? Theme.surfaceTextHover : Theme.withAlpha(Theme.surfaceTextHover, 0)
visible: NotepadStorageService.tabs.length > 1
anchors.verticalCenter: parent.verticalCenter
DankIcon {
name: "close"
size: 14
color: Theme.surfaceTextMedium
anchors.centerIn: parent
}
MouseArea {
id: closeMouseArea
anchors.fill: parent
hoverEnabled: true
cursorShape: Qt.PointingHandCursor
z: 100
onClicked: root.tabClosed(index)
}
}
}
Behavior on color {
ColorAnimation {
duration: Theme.shortDuration
easing.type: Theme.standardEasing
}
}
}
}
MouseArea {
id: tabMouseArea
anchors.fill: parent
hoverEnabled: true
preventStealing: dragging || longPressing
cursorShape: dragging || longPressing ? Qt.ClosedHandCursor : Qt.PointingHandCursor
acceptedButtons: Qt.LeftButton
onPressed: mouse => {
if (mouse.button === Qt.LeftButton && NotepadStorageService.tabs.length > 1) {
delegateItem.dragStartPos = Qt.point(mouse.x, mouse.y)
longPressTimer.start()
}
}
onReleased: mouse => {
longPressTimer.stop()
var wasDragging = delegateItem.dragging
var didReorder = wasDragging && delegateItem.targetIndex >= 0 && delegateItem.targetIndex !== delegateItem.originalIndex
if (didReorder) {
root.suppressShiftAnimation = true
NotepadStorageService.reorderTab(delegateItem.originalIndex, delegateItem.targetIndex)
}
delegateItem.longPressing = false
delegateItem.dragging = false
delegateItem.dragAxisOffset = 0
delegateItem.targetIndex = -1
delegateItem.originalIndex = -1
root.draggedIndex = -1
root.dropTargetIndex = -1
if (didReorder) {
Qt.callLater(() => {
root.suppressShiftAnimation = false
})
}
if (wasDragging || mouse.button !== Qt.LeftButton)
return
root.tabSwitched(index)
}
onPositionChanged: mouse => {
if (delegateItem.longPressing && !delegateItem.dragging) {
var distance = Math.sqrt(Math.pow(mouse.x - delegateItem.dragStartPos.x, 2) + Math.pow(mouse.y - delegateItem.dragStartPos.y, 2))
if (distance > 5) {
delegateItem.dragging = true
delegateItem.targetIndex = index
delegateItem.originalIndex = index
root.draggedIndex = index
root.dropTargetIndex = index
}
}
if (!delegateItem.dragging)
return
var axisOffset = mouse.x - delegateItem.dragStartPos.x
delegateItem.dragAxisOffset = axisOffset
var itemSize = root.tabItemSize
var rawSlot = axisOffset / itemSize
var slotOffset = rawSlot >= 0
? Math.floor(rawSlot + 0.4)
: Math.ceil(rawSlot - 0.4)
var tabCount = NotepadStorageService.tabs.length
var newTargetIndex = Math.max(0, Math.min(tabCount - 1, delegateItem.originalIndex + slotOffset))
if (newTargetIndex !== delegateItem.targetIndex) {
delegateItem.targetIndex = newTargetIndex
root.dropTargetIndex = newTargetIndex
}
}
}
}
}
}
}
DankActionButton {
id: newTabButton
width: 32
height: 32
iconName: "add"
iconSize: Theme.iconSize - 4
iconColor: Theme.surfaceText
onClicked: root.newTabRequested()
}
}
}
|