Repository navigation
Expand file tree
/
Copy pathPluginList.qml
More file actions
382 lines (336 loc) · 11.8 KB
/
Copy pathPluginList.qml
File metadata and controls
382 lines (336 loc) · 11.8 KB
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
import QtQuick
import QtQuick.Controls as QQC
import qs.Commons
import qs.Ui
FocusScope {
id: root
property var plugins: []
property string selectedId: ""
property string emptyText: "No plugins match this search."
property bool activateOnSingleClick: false
property color foreground: Color.foreground
property color accent: Color.accent
property real scrollNubRightOffset: 0
property bool viewportRestorePending: false
property var viewportAnchor: null
signal selected(string pluginId)
signal activated(var plugin)
signal detailsRequested(var plugin)
signal searchRequested()
activeFocusOnTab: true
readonly property real rowHeight: Style.space(76)
function idFor(plugin) {
return plugin && plugin.id !== undefined ? String(plugin.id) : ""
}
function selectedPlugin() {
if (!Array.isArray(plugins) || list.currentIndex < 0 || list.currentIndex >= plugins.length)
return null
return plugins[list.currentIndex]
}
function indexForId(pluginId) {
var id = String(pluginId || "")
if (!Array.isArray(plugins)) return -1
for (var i = 0; i < plugins.length; i++)
if (idFor(plugins[i]) === id) return i
return -1
}
function captureViewportAnchor() {
var index = indexForId(selectedId)
if (index < 0) return null
var item = index === list.currentIndex
? list.currentItem : list.itemAtIndex(index)
if (!item) {
return {
pluginId: String(selectedId),
hasViewportOffset: false,
viewportOffset: 0
}
}
var offset = Number(item.y) - Number(list.contentY)
var itemHeight = Number(item.height)
return {
pluginId: String(selectedId),
hasViewportOffset: isFinite(offset) && isFinite(itemHeight)
&& offset < list.height && offset + itemHeight > 0,
viewportOffset: offset
}
}
function beginModelReset() {
if (!viewportRestorePending) viewportAnchor = captureViewportAnchor()
viewportRestorePending = true
}
function restoreViewportAnchor(anchor) {
var activeIndex = indexForId(selectedId)
if (activeIndex < 0) {
list.currentIndex = -1
return
}
if (list.currentIndex !== activeIndex) list.currentIndex = activeIndex
list.forceLayout()
var anchorId = anchor && anchor.pluginId !== undefined
? String(anchor.pluginId) : ""
var anchorIndex = indexForId(anchorId)
if (!anchor || anchor.hasViewportOffset !== true || anchorIndex < 0) {
list.positionViewAtIndex(activeIndex, ListView.Contain)
return
}
list.positionViewAtIndex(anchorIndex, ListView.Beginning)
list.forceLayout()
var anchorItem = list.itemAtIndex(anchorIndex)
if (!anchorItem) {
list.positionViewAtIndex(activeIndex, ListView.Contain)
return
}
var minimumContentY = Number(list.originY)
var maximumContentY = minimumContentY
+ Math.max(0, Number(list.contentHeight) - Number(list.height))
var desiredContentY = Number(anchorItem.y) - Number(anchor.viewportOffset)
list.contentY = Math.max(minimumContentY,
Math.min(maximumContentY, desiredContentY))
var activeItem = list.itemAtIndex(activeIndex)
if (!activeItem
|| activeItem.y + activeItem.height <= list.contentY
|| activeItem.y >= list.contentY + list.height)
list.positionViewAtIndex(activeIndex, ListView.Contain)
}
function endModelReset() {
if (!viewportRestorePending) {
syncCurrentIndex()
return
}
var anchor = viewportAnchor
restoreViewportAnchor(anchor)
viewportAnchor = null
viewportRestorePending = false
}
function syncCurrentIndex() {
var next = indexForId(selectedId)
if (next < 0 && Array.isArray(plugins) && plugins.length > 0) next = 0
if (list.currentIndex !== next) list.currentIndex = next
if (next >= 0) list.positionViewAtIndex(next, ListView.Contain)
}
function move(delta) {
if (!Array.isArray(plugins) || plugins.length === 0) return
var next = Math.max(0, Math.min(plugins.length - 1, list.currentIndex + delta))
list.currentIndex = next
list.positionViewAtIndex(next, ListView.Contain)
}
function movePage(delta) {
var rows = Math.max(1, Math.floor(list.height / (rowHeight + list.spacing)))
move(delta * rows)
}
function focusCurrentItem() {
syncCurrentIndex()
list.forceActiveFocus()
}
function focusFirstItem() {
if (Array.isArray(plugins) && plugins.length > 0) {
list.currentIndex = 0
list.positionViewAtBeginning()
var plugin = selectedPlugin()
var id = idFor(plugin)
if (id !== selectedId) root.selected(id)
}
list.forceActiveFocus()
}
onPluginsChanged: {
if (!viewportRestorePending) Qt.callLater(syncCurrentIndex)
}
onSelectedIdChanged: {
if (indexForId(selectedId) !== list.currentIndex) {
if (!viewportRestorePending) Qt.callLater(syncCurrentIndex)
}
}
Keys.onPressed: function(event) {
if (event.key === Qt.Key_Down || event.key === Qt.Key_J) {
move(1); event.accepted = true
} else if (event.key === Qt.Key_Up || event.key === Qt.Key_K) {
if (list.currentIndex <= 0) {
if (!event.isAutoRepeat) root.searchRequested()
}
else move(-1)
event.accepted = true
} else if (event.key === Qt.Key_Right || event.key === Qt.Key_L) {
var detailsPlugin = selectedPlugin()
if (detailsPlugin) root.detailsRequested(detailsPlugin)
event.accepted = detailsPlugin !== null
} else if (event.key === Qt.Key_PageDown) {
movePage(1); event.accepted = true
} else if (event.key === Qt.Key_PageUp) {
movePage(-1); event.accepted = true
} else if (event.key === Qt.Key_Home || event.text === "g") {
if (plugins.length > 0) list.currentIndex = 0
list.positionViewAtBeginning()
event.accepted = true
} else if (event.key === Qt.Key_End || event.text === "G") {
if (plugins.length > 0) list.currentIndex = plugins.length - 1
list.positionViewAtEnd()
event.accepted = true
} else if (event.key === Qt.Key_Return || event.key === Qt.Key_Enter || event.key === Qt.Key_Space) {
var plugin = selectedPlugin()
if (plugin) root.activated(plugin)
event.accepted = true
}
}
ListView {
id: list
anchors.fill: parent
focus: true
clip: true
model: Array.isArray(root.plugins) ? root.plugins : []
spacing: Style.space(5)
boundsBehavior: Flickable.StopAtBounds
reuseItems: true
cacheBuffer: Math.max(0, height)
currentIndex: -1
onCurrentIndexChanged: {
var plugin = root.selectedPlugin()
if (!plugin) return
var id = root.idFor(plugin)
if (id !== root.selectedId) root.selected(id)
}
delegate: CursorSurface {
id: row
required property int index
required property var modelData
readonly property bool rowSelected: index === list.currentIndex
readonly property bool installed: !!(modelData && modelData.installed === true)
readonly property bool disabled: installed
&& typeof modelData.enabled === "boolean" && modelData.enabled === false
readonly property bool updateAvailable: installed
&& modelData.versionUpdateAvailable === true
readonly property string pluginId: root.idFor(modelData)
readonly property string title: String(modelData.name || modelData.id || "Unnamed plugin")
readonly property string description: String(modelData.description || "No description provided.")
width: list.width
height: root.rowHeight
current: rowSelected
hasCursor: rowSelected && root.activeFocus
foreground: root.foreground
accent: root.accent
Column {
anchors.left: parent.left
anchors.right: parent.right
anchors.verticalCenter: parent.verticalCenter
anchors.leftMargin: Style.space(12)
anchors.rightMargin: Style.space(12)
spacing: Style.space(3)
Row {
width: parent.width
spacing: Style.space(7)
Row {
id: stateIndicators
spacing: Style.space(4)
Text {
id: installedIndicator
visible: row.installed
text: row.updateAvailable ? "\uf021" : ""
textFormat: Text.PlainText
y: Math.round((parent.height - height) / 2)
color: root.accent
font.family: Style.font.family
font.pixelSize: Style.font.body
Accessible.ignored: true
}
Text {
id: disabledIndicator
visible: row.disabled
text: ""
textFormat: Text.PlainText
y: Math.round((parent.height - height) / 2)
color: root.accent
font.family: Style.font.family
font.pixelSize: Style.font.body
Accessible.ignored: true
}
}
Text {
width: Math.max(0, parent.width
- (stateIndicators.width > 0
? stateIndicators.width + parent.spacing : 0))
text: row.title
textFormat: Text.PlainText
color: root.foreground
font.family: Style.font.family
font.pixelSize: Style.font.title
font.bold: row.rowSelected
elide: Text.ElideRight
}
}
Text {
width: parent.width
text: row.description
textFormat: Text.PlainText
color: Util.alpha(root.foreground, 0.72)
font.family: Style.font.family
font.pixelSize: Style.font.bodySmall
wrapMode: Text.WordWrap
maximumLineCount: 2
elide: Text.ElideRight
}
}
MouseArea {
anchors.fill: parent
hoverEnabled: true
cursorShape: Qt.PointingHandCursor
onClicked: function(mouse) {
list.currentIndex = row.index
list.forceActiveFocus()
if (root.activateOnSingleClick || (mouse.button === Qt.LeftButton && mouse.modifiers & Qt.ControlModifier))
root.activated(row.modelData)
}
onDoubleClicked: root.activated(row.modelData)
}
Accessible.name: row.title
+ (row.installed ? ", installed" : "")
+ (row.disabled ? ", disabled" : "")
+ (row.updateAvailable ? ", update available" : "")
Accessible.description: row.description
Accessible.role: Accessible.ListItem
}
Text {
visible: list.count === 0
anchors.centerIn: parent
width: Math.max(0, parent.width - Style.space(32))
text: root.emptyText
color: Util.alpha(root.foreground, 0.68)
font.family: Style.font.family
font.pixelSize: Style.font.body
horizontalAlignment: Text.AlignHCenter
wrapMode: Text.WordWrap
}
}
QQC.ScrollBar {
id: pluginListScrollNub
anchors.top: root.top
anchors.right: root.right
anchors.bottom: root.bottom
anchors.rightMargin: -root.scrollNubRightOffset
orientation: Qt.Vertical
position: list.visibleArea.yPosition
size: list.visibleArea.heightRatio
policy: list.contentHeight > list.height + 0.5
? QQC.ScrollBar.AlwaysOn : QQC.ScrollBar.AlwaysOff
z: 10
interactive: true
width: Style.space(10)
padding: 0
minimumSize: Math.min(1, Style.space(72) / Math.max(1, list.height))
onPositionChanged: {
if (!pressed) return
var maximumPosition = Math.max(0, 1 - size)
var nextPosition = Math.max(0, Math.min(maximumPosition, position))
list.contentY = list.originY + nextPosition * list.contentHeight
}
contentItem: Rectangle {
implicitWidth: Style.space(10)
implicitHeight: Style.space(72)
radius: 0
color: Util.alpha(root.accent, 0.92)
}
background: Item {}
Accessible.name: "Plugin list scroll position"
}
Accessible.name: "Plugin list"
Accessible.role: Accessible.List
}