-
Notifications
You must be signed in to change notification settings - Fork 3
/
TileGrid.qml
672 lines (598 loc) · 16.8 KB
/
TileGrid.qml
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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
import QtQuick 2.0
import QtQuick.Controls 1.1
import QtQuick.Controls.Styles 1.1
import QtQuick.Layouts 1.1
import QtQuick.Window 2.1
import org.kde.plasma.core 2.0 as PlasmaCore
import org.kde.plasma.components 2.0 as PlasmaComponents
import org.kde.draganddrop 2.0 as DragAndDrop
import org.kde.plasma.private.kicker 0.1 as Kicker
import org.kde.kquickcontrolsaddons 2.0
// MouseArea {
DragAndDrop.DropArea {
id: tileGrid
// hoverEnabled: true
property bool isDragging: cellRepeater.dropping
property int cellSize: 60 * units.devicePixelRatio
property real cellMargin: 3 * units.devicePixelRatio
property real cellPushedMargin: 6 * units.devicePixelRatio
property int cellBoxSize: cellMargin + cellSize + cellMargin
property int hoverOutlineSize: 2 * units.devicePixelRatio
property int minColumns: Math.floor(width / cellBoxSize)
property int minRows: Math.floor(height / cellBoxSize)
property int maxColumn: 0
property int maxRow: 0
property int maxWidth: 0
property int maxHeight: 0
property int columns: Math.max(minColumns, maxColumn)
property int rows: Math.max(minRows, maxRow)
property var addedItem: null
readonly property bool adding: addedItem
property int draggedIndex: -1
readonly property var draggedItem: draggedIndex >= 0 ? tileModel[draggedIndex] : null
property bool editing: isDragging && draggedItem || adding
property int dropHoverX: -1
property int dropHoverY: -1
property int dropOffsetX: 0
property int dropOffsetY: 0
readonly property int dropWidth: draggedItem ? draggedItem.w : addedItem ? addedItem.w : 0
readonly property int dropHeight: draggedItem ? draggedItem.h : addedItem ? addedItem.h : 0
property bool canDrop: false
function resetDragHover() {
dropHoverX = -1
dropHoverY = -1
scrollUpArea.containsDrag = false
scrollDownArea.containsDrag = false
addedItem = null
}
function resetDrag() {
resetDragHover()
cellRepeater.dropping = false
draggedIndex = -1
}
function startDrag(index) {
draggedIndex = index
dropHoverX = draggedItem.x
dropHoverY = draggedItem.y
cellRepeater.dropping = true
}
function tileWithin(tile, x1, y1, x2, y2) {
var tileX2 = tile.x + tile.w - 1
var tileY2 = tile.y + tile.h - 1
return (x1 <= tileX2
&& tile.x <= x2
&& y1 <= tileY2
&& tile.y <= y2
)
}
function getGroupAreaRect(groupTile) {
var x1 = groupTile.x
var x2 = groupTile.x + groupTile.w - 1
var y1 = groupTile.y + groupTile.h
var y2 = 2000000 // maxint
// Scan for other groups below this group
// and adjust y2 to above that group.
for (var i = 0; i < tileModel.length; i++) {
var tile = tileModel[i]
if (tile.tileType == "group"
&& tileWithin(tile, x1, y1, x2, y2)
) {
y2 = tile.y - 1
// console.log('group found at y =', tile.y, 'y2 set to', y2)
}
}
return {
x1: x1,
y1: y1,
x2: x2,
y2: y2,
}
}
function moveGroup(groupTile, deltaX, deltaY) {
var area = getGroupAreaRect(groupTile)
// Move tiles below group label
for (var i = 0; i < tileModel.length; i++) {
var tile = tileModel[i]
if (tileWithin(tile, area.x1, area.y1, area.x2, area.y2)) {
tile.x += deltaX
tile.y += deltaY
}
}
// We call this in moveTile so no need to duplicate work.
// tileGrid.tileModelChanged()
}
function moveTile(tile, cellX, cellY) {
if (tile.tileType == "group") {
moveGroup(tile, cellX - tile.x, cellY - tile.y)
}
tile.x = cellX
tile.y = cellY
tileGrid.tileModelChanged()
}
onDrop: {
// console.log('onDrop', JSON.stringify(draggedItem))
if (draggedItem) {
tileGrid.moveTile(draggedItem, dropHoverX, dropHoverY)
tileGrid.resetDrag()
// event.accept(Qt.MoveAction)
} else if (addedItem) {
addedItem.x = dropHoverX
addedItem.y = dropHoverY
tileGrid.tileModel.push(addedItem)
tileGrid.tileModelChanged()
tileGrid.resetDrag()
}
}
function parseDropUrl(url) {
var workingDir = Qt.resolvedUrl('.')
var endsWithDesktop = url.indexOf('.desktop') === url.length - '.desktop'.length
var isRelativeDesktopUrl = endsWithDesktop && (
url.indexOf(workingDir) === 0
// || url.indexOf('file:///usr/share/applications/') === 0
// || url.indexOf('/.local/share/applications/') >= 0
|| url.indexOf('/share/applications/') >= 0 // 99% certain this desktop file should be accessed relatively.
)
logger.debug('parseDropUrl', workingDir, endsWithDesktop, isRelativeDesktopUrl)
logger.debug('onUrlDropped', 'url', url)
if (isRelativeDesktopUrl) {
// Remove the path because .favoriteId is just the file name.
// However passing the favoriteId in mimeData.url will prefix the current QML path because it's a QUrl.
var tokens = url.toString().split('/')
var favoriteId = tokens[tokens.length-1]
logger.debug('isRelativeDesktopUrl', tokens, favoriteId)
return favoriteId
} else {
return url
}
}
function dragTick(event) {
var dragX = event.x + scrollView.flickableItem.contentX - dropOffsetX
var dragY = event.y + scrollView.flickableItem.contentY - dropOffsetY
var modelX = Math.floor(dragX / cellBoxSize)
var modelY = Math.floor(dragY / cellBoxSize)
// console.log('onDragMove', event.x, event.y, modelX, modelY)
scrollUpArea.checkContains(event)
scrollDownArea.checkContains(event)
if (draggedItem) {
} else if (addedItem) {
} else if (event && event.mimeData && event.mimeData.url) {
var url = event.mimeData.url.toString()
// console.log('new addedItem', event.mimeData.url, url)
url = parseDropUrl(url)
addedItem = newTile(url)
dropHoverX = modelX
dropHoverY = modelY
} else {
return
}
dropHoverX = Math.max(0, Math.min(modelX, columns - dropWidth))
dropHoverY = Math.max(0, modelY)
canDrop = !hits(dropHoverX, dropHoverY, dropWidth, dropHeight)
}
onDragEnter: dragTick(event)
onDragMove: dragTick(event)
onDragLeave: {
// console.log('onExited')
resetDragHover()
}
property var hitBox: [] // hitBox[y][x]
function updateSize() {
var c = 0;
var r = 0;
var w = 1;
var h = 1;
for (var i = 0; i < tileModel.length; i++) {
var tile = tileModel[i]
c = Math.max(c, tile.x + tile.w)
r = Math.max(r, tile.y + tile.h)
w = Math.max(w, tile.w)
h = Math.max(h, tile.h)
}
// Add extra rows when dragging so we can drop scrolled down
if (draggedItem) {
// c += draggedItem.w
r += draggedItem.h
}
// Rebuild hitBox
var hbColumns = Math.max(minColumns, c)
var hbRows = Math.max(minRows, r)
var hb = new Array(hbRows)
for (var i = 0; i < hbRows; i++) {
hb[i] = new Array(hbColumns)
}
for (var i = 0; i < tileModel.length; i++) {
var tile = tileModel[i]
if (i == draggedIndex) {
continue;
}
for (var j = tile.y; j < tile.y + tile.h; j++) {
for (var k = tile.x; k < tile.x + tile.w; k++) {
hb[j][k] = true
}
}
}
// Update Properties
hitBox = hb
maxColumn = c
maxRow = r
maxWidth = w
maxHeight = h
}
function update() {
var urlList = []
for (var i = 0; i < tileModel.length; i++) {
var tile = tileModel[i]
if (tile.url) {
urlList.push(tile.url)
}
}
appsModel.tileGridModel.favorites = urlList
updateSize()
}
onDraggedItemChanged: update()
onTileModelChanged: update()
property var tileModel: []
function hits(x, y, w, h) {
// console.log('hits', [columns,rows], [x,y,w,h], hitBox)
for (var j = y; j < y + h; j++) {
if (j < 0 || j >= hitBox.length) {
continue; // Should we return true when out of bounds?
}
for (var k = x; k < x + w; k++) {
if (k < 0 || k >= hitBox[j].length) {
continue; // Should we return true when out of bounds?
}
if (hitBox[j][k]) {
return true
}
}
}
return false
}
function getTileAt(cellX, cellY) {
for (var i = 0; i < tileModel.length; i++) {
var tile = tileModel[i]
if (tileWithin(tile, cellX, cellY, cellX, cellY)) {
return tile
}
}
return null
}
function getTilesInArea(area) {
var tileList = []
// Move tiles below group label
for (var i = 0; i < tileModel.length; i++) {
var tile = tileModel[i]
if (tileWithin(tile, area.x1, area.y1, area.x2, area.y2)) {
tileList.push(tile)
}
}
// Sort results by y, then by x
// tileList.sort(function(a, b) {
// if (a.y == b.y) {
// return b.x - a.x
// } else {
// return b.y - a.y
// }
// })
return tileList
}
function getTileLabel(tile) {
if (tile.url) {
var app = appsModel.tileGridModel.getApp(tile.url)
var labelText = tile.label || app.display || app.url || ""
return labelText
} else {
return ""
}
}
function sortGroupTiles(groupTile) {
var area = getGroupAreaRect(groupTile)
var tileList = getTilesInArea(area)
var cursorX = groupTile.x
var cursorY = groupTile.y + groupTile.h
var rowH = 0
tileList.sort(function(a, b) {
var aLabel = getTileLabel(a)
var bLabel = getTileLabel(b)
return aLabel.localeCompare(bLabel)
})
for (var i = 0; i < tileList.length; i++) {
var tile = tileList[i]
var tileX2 = cursorX + tile.w - 1
// If there's not enough room on this row
if (tileX2 > area.x2) {
// Move to the next row
cursorX = groupTile.x
cursorY += rowH
}
tile.x = cursorX
tile.y = cursorY
rowH = Math.max(rowH, tile.h)
cursorX += tile.w
}
// We call this in moveTile so no need to duplicate work.
tileGrid.tileModelChanged()
}
ScrollView {
id: scrollView
anchors.fill: parent
readonly property int scrollTop: flickableItem ? flickableItem.contentY : 0
readonly property int scrollHeight: flickableItem ? flickableItem.contentHeight : 0
readonly property int scrollTopAtBottom: viewport ? scrollHeight - viewport.height : 0
readonly property bool scrollAtTop: scrollTop == 0
readonly property bool scrollAtBottom: scrollTop >= scrollTopAtBottom
function scrollBy(deltaY) {
if (flickableItem) {
// console.log('scrollHeight', scrollTopAtBottom, scrollHeight, viewport.height)
flickableItem.contentY = Math.max(0, Math.min(scrollTop + deltaY, scrollTopAtBottom))
}
}
__wheelAreaScrollSpeed: cellBoxSize
style: ScrollViewStyle {
transientScrollBars: true
}
Item {
id: scrollItem
width: columns * cellBoxSize
height: rows * cellBoxSize
// Rectangle {
// anchors.fill: parent
// color: "#88336699"
// }
Repeater {
id: cellRepeater
property int cellCount: columns * rows
property bool dropping: false
onCellCountChanged: {
if (!dropping) {
model = cellCount
}
}
model: 0
Item {
id: cellItem
property int modelX: modelData % columns
property int modelY: Math.floor(modelData / columns)
x: modelX * cellBoxSize
y: modelY * cellBoxSize
width: cellBoxSize
height: cellBoxSize
readonly property bool hasDrag: tileGrid.editing && dropHoverX >= 0 && dropHoverY >= 0
readonly property bool tileHovered: (hasDrag
&& dropHoverX <= modelX && modelX < dropHoverX + dropWidth
&& dropHoverY <= modelY && modelY < dropHoverY + dropHeight
)
readonly property bool isDraggingGroup: hasDrag && draggedItem && draggedItem.tileType == "group"
readonly property bool groupAreaHovered: {
if (isDraggingGroup) {
var groupArea = getGroupAreaRect(draggedItem)
return groupArea.x1 <= modelX && modelX <= groupArea.x2
&& groupArea.y1 <= modelY && modelY <= groupArea.y2
} else {
return false
}
}
Rectangle {
anchors.fill: parent
anchors.margins: cellMargin
color: {
if (cellItem.tileHovered) {
if (canDrop) {
return "#88336699"
} else {
return "#88880000"
}
} else if (cellItem.groupAreaHovered) {
return "#8848395d" // purple
} else {
return "transparent"
}
}
border.width: 1
border.color: tileGrid.editing ? "#44000000" : "transparent"
}
MouseArea {
anchors.fill: parent
acceptedButtons: Qt.LeftButton | Qt.RightButton
onClicked: {
if (mouse.button == Qt.RightButton) {
cellContextMenu.cellX = cellItem.modelX
cellContextMenu.cellY = cellItem.modelY
var pos = mapToItem(scrollItem, mouse.x, mouse.y) // cellContextMenu is a child of scrollItem
cellContextMenu.open(pos.x, pos.y)
} else if (plasmoid.configuration.popupDismissOnLeftClick && mouse.button == Qt.LeftButton) {
plasmoid.expanded = false
popup.visible = false
}
}
}
}
}
PlasmaComponents.ContextMenu {
id: cellContextMenu
property int cellX: -1
property int cellY: -1
PlasmaComponents.MenuItem {
icon: "group-new"
text: i18n("New Group")
visible: !plasmoid.configuration.tilesLocked
onClicked: {
var tile = tileGrid.addGroup(cellContextMenu.cellX, cellContextMenu.cellY)
tileGrid.editTile(tile)
}
}
PlasmaComponents.MenuItem {
icon: plasmoid.configuration.tilesLocked ? "object-unlocked" : "object-locked"
text: plasmoid.configuration.tilesLocked ? i18n("Unlock Tiles") : i18n("Lock Tiles")
onClicked: {
plasmoid.configuration.tilesLocked = !plasmoid.configuration.tilesLocked
}
}
}
Repeater {
id: tileModelRepeater
model: tileModel
// onCountChanged: console.log('onCountChanged', count)
TileItem {
id: tileItem
}
}
}
}
/* Scroll on hover with drag */
property int scrollAreaTickDelta: cellBoxSize
property int scrollAreaTickInterval: 200
property int scrollAreaSize: Math.min(cellBoxSize * 1.5, scrollView.height / 5) // 20vh or 90pt
Item {
id: scrollUpArea
anchors.left: parent.left
anchors.right: parent.right
anchors.top: parent.top
height: scrollAreaSize
property bool active: !scrollView.scrollAtTop
property bool containsDrag: false
property bool ticking: active && containsDrag
function checkContains(event) {
containsDrag = scrollUpArea.contains(Qt.point(event.x, event.y))
}
Timer {
id: scrollUpTicker
interval: scrollAreaTickInterval
repeat: true
running: parent.ticking
onTriggered: {
scrollView.scrollBy(-scrollAreaTickDelta)
}
}
Rectangle {
anchors.fill: parent
opacity: parent.ticking ? 1 : 0
gradient: Gradient {
GradientStop { position: 0.0; color: theme.highlightColor }
GradientStop { position: 0.3; color: "transparent" }
}
}
}
Item {
id: scrollDownArea
anchors.left: parent.left
anchors.right: parent.right
anchors.bottom: parent.bottom
height: scrollAreaSize
property bool active: !scrollView.scrollAtBottom
property bool containsDrag: false
property bool ticking: active && containsDrag
function checkContains(event) {
var mouseY = event.y - (parent.height - height)
containsDrag = scrollDownArea.contains(Qt.point(event.x, mouseY))
}
Timer {
id: scrollDownTicker
interval: scrollAreaTickInterval
repeat: true
running: parent.ticking
onTriggered: {
scrollView.scrollBy(scrollAreaTickDelta)
}
}
Rectangle {
anchors.fill: parent
opacity: parent.ticking ? 1 : 0
gradient: Gradient {
GradientStop { position: 0.7; color: "transparent" }
GradientStop { position: 1.0; color: theme.highlightColor }
}
}
}
function newTile(url) {
return {
"x": 0,
"y": 0,
"w": 2,
"h": 2,
"url": url,
}
}
function removeIndex(i) {
tileModel.splice(i, 1) // remove 1 item at index
tileModelChanged()
}
function removeApp(url) {
var removedCount = 0
for (var i = tileModel.length - 1; i >= 0; i--) {
var tile = tileModel[i]
if (tile.url == url) {
removedCount += 1
tileModel.splice(i, 1) // remove 1 item at index
}
}
if (removedCount > 0) {
tileModelChanged()
}
}
function addTile(tile) {
tileModel.push(tile)
tileModelChanged()
}
function findOpenPos(w, h) {
for (var y = 0; y < rows; y++) {
for (var x = 0; x < columns - (w-1); x++) {
if (hits(x, y, w, h))
continue
// Room open for
return {
x: x,
y: y,
}
}
}
// Current grid has no room.
// Add to new row.
return {
x: 0,
y: rows
}
}
function parseTileXY(tile, x, y) {
if (typeof x !== "undefined" && typeof y !== "undefined") {
tile.x = x
tile.y = y
} else {
var openPos = findOpenPos(tile.w, tile.h)
tile.x = openPos.x
tile.y = openPos.y
}
}
function addApp(url, x, y) {
var tile = newTile(url)
parseTileXY(tile, x, y)
tileModel.push(tile)
tileModelChanged()
return tile
}
function hasAppTile(url) {
for (var i = 0; i < tileModel.length; i++) {
var tile = tileModel[i]
if (tile.url == url) {
return true
}
}
return false
}
function limit(minValue, value, maxValue) {
return Math.max(minValue, Math.min(value, maxValue))
}
function addGroup(x, y) {
var tile = newTile("")
parseTileXY(tile, x, y)
tile.tileType = "group"
tile.label = i18nc("default group label", "Group")
tile.w = limit(2, columns-x, 6) // 6 unless we have less columns.
tile.h = 1
tileModel.push(tile)
tileModelChanged()
return tile
}
signal editTile(var tile)
}