aboutsummaryrefslogtreecommitdiffstats
path: root/sysui/home/WidgetGrid.qml
blob: 00757f010fb57ae6812da51d291b61bb7d1aaaf2 (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
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
/****************************************************************************
**
** Copyright (C) 2019 Luxoft Sweden AB
** Copyright (C) 2018 Pelagicore AG
** Contact: https://www.qt.io/licensing/
**
** This file is part of the Neptune 3 UI.
**
** $QT_BEGIN_LICENSE:GPL-QTAS$
** Commercial License Usage
** Licensees holding valid commercial Qt Automotive Suite licenses may use
** this file in accordance with the commercial license agreement provided
** with the Software or, alternatively, in accordance with the terms
** contained in a written agreement between you and The Qt Company.  For
** licensing terms and conditions see https://www.qt.io/terms-conditions.
** For further information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 or (at your option) any later version
** approved by the KDE Free Qt Foundation. The licenses are as published by
** the Free Software Foundation and appearing in the file LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
** SPDX-License-Identifier: GPL-3.0
**
****************************************************************************/

import QtQuick 2.6
import QtQuick.Controls 2.1

import shared.controls 1.0
import shared.utils 1.0
import shared.animations 1.0

import shared.NeptuneWidgetGrid 1.0

import shared.Sizes 1.0

Item {
    id: root

    readonly property int rowHeight: height / widgetsList.numRows
    readonly property int maxWidgetHeight: rowHeight * widgetsList.maxWidgetHeightRows

    readonly property int widgetCount: widgetsList.count
    readonly property int maxWidgetCount: widgetsList.numRows

    readonly property real resizerHandleHeight: Sizes.dp(32)

    property real exposedRectTopMargin
    property real exposedRectBottomMargin

    property Item activeApplicationParent
    property bool moveBottomWidgetToDrawer: false
    property Item widgetDrawer

    property var applicationModel

    property string widgetStateWhenMaximized: ""
    property int clickedIndexWhenMaximized: -1

    QtObject {
        id: d
        property bool widgetIsBeingDragged: false
    }

    WidgetListModel {
        id: widgetsList
        applicationModel: root.applicationModel
    }

    Item {
        id: widgetColumn
        anchors.fill: parent

        readonly property real swapThreshold: Sizes.dp(32)
        function onWidgetMoved(draggedDelegate) {
            var draggedWidget = draggedDelegate.widget;
            var deltaY = draggedWidget.y - draggedDelegate.y

            if (deltaY > 0) {
                // if the bottom edge of the widget being dragged comes close enough to the
                // bottom edge of some other widget we swap them.

                var draggedBottom = draggedWidget.y + draggedWidget.height

                var i;
                for (i = 0; i < repeater.count; i++) {
                    if (draggedDelegate.modelIndex === i)
                        continue;

                    var otherDelegate = repeater.itemAt(i);
                    var widgetBottom = otherDelegate.yNormal + otherDelegate.heightNormal;

                    if (Math.abs(draggedBottom - widgetBottom) <= swapThreshold) {
                        // swap!
                        widgetsList.move(draggedDelegate.modelIndex, i, 1);
                        return;
                    }
                }
            } else {
                // if the top edge of the widget being dragged comes close enough to the
                // top edge of some other widget we swap them.

                var i;
                for (i = 0; i < repeater.count; i++) {
                    if (draggedDelegate.modelIndex === i)
                        continue;

                    var widgetDelegateY = repeater.itemAt(i).yNormal;

                    if (Math.abs(draggedWidget.y - widgetDelegateY) < swapThreshold) {
                        // swap!
                        widgetsList.move(draggedDelegate.modelIndex, i, 1);
                        return;
                    }
                }
            }

        }

        property bool resizingWidgets: false
        property real startResizeDragY
        property int widgetIndexAboveHandle
        function onResizeHandlePressed(pos) {
            startResizeDragY = pos.y;

            var i = 0;
            var accumulatedHeight = 0;
            while (true) {
                var appInfo = widgetsList.application(i);
                accumulatedHeight += appInfo.heightRows * rowHeight;
                if (accumulatedHeight >= pos.y) {
                    widgetIndexAboveHandle = i;
                    break;
                } else {
                    i++;
                }
            }

            // init resize geometry
            for (i = 0; i < repeater.count; i++) {
                var delegate = repeater.itemAt(i);
                delegate.yWhenResizing = delegate.yNormal;
                delegate.heightWhenResizing = delegate.heightNormal;
            }

            resizingWidgets = true;
        }
        function onResizeHandleDragged(pos) {
            var delta = withinBoundsY(pos.y) - startResizeDragY;
            delta = computeUsableDragDelta(delta);

            // above handle. positive delta enlarges widgets
            var i = widgetIndexAboveHandle;
            var remainingDelta = delta;
            while (remainingDelta !== 0 && i >= 0) {
                var delegate = repeater.itemAt(i);
                var appInfo = delegate.appInfo;
                var minHeight = appInfo.minHeightRows * rowHeight;
                var targetHeight = (appInfo.heightRows * rowHeight) + remainingDelta;

                if (remainingDelta > 0) {
                    if (targetHeight <= root.maxWidgetHeight) {
                        delegate.heightWhenResizing = targetHeight;
                        remainingDelta = 0;
                    } else {
                        delegate.heightWhenResizing = root.maxWidgetHeight;
                        remainingDelta -= root.maxWidgetHeight - (appInfo.heightRows * rowHeight);
                        i--;
                    }
                } else {
                    if (targetHeight >= minHeight) {
                        delegate.heightWhenResizing = targetHeight;
                        remainingDelta = 0;
                    } else {
                        delegate.heightWhenResizing = minHeight;
                        remainingDelta -= -((appInfo.heightRows * rowHeight) - minHeight);
                        i--;
                    }
                }
            }

            // below handle. positive delta shrinks widgets
            i = widgetIndexAboveHandle + 1;
            remainingDelta = delta;
            while (remainingDelta !== 0 && i < repeater.count) {
                var delegate = repeater.itemAt(i);
                var appInfo = delegate.appInfo;
                var minHeight = appInfo.minHeightRows * rowHeight;
                var targetHeight = (appInfo.heightRows * rowHeight) - remainingDelta;

                if (remainingDelta < 0) {
                    if (targetHeight <= root.maxWidgetHeight) {
                        delegate.heightWhenResizing = targetHeight;
                        remainingDelta = 0;
                    } else {
                        delegate.heightWhenResizing = root.maxWidgetHeight;
                        remainingDelta -= -(root.maxWidgetHeight - (appInfo.heightRows * rowHeight));
                        i++;
                    }
                } else {
                    if (targetHeight >= minHeight) {
                        delegate.heightWhenResizing = targetHeight;
                        remainingDelta = 0;
                    } else {
                        delegate.heightWhenResizing = minHeight;
                        remainingDelta -= (appInfo.heightRows * rowHeight) - minHeight;
                        i++;
                    }
                }
            }

            // update y values according to the new heights
            var accumulatedY = 0;
            for (i = 0; i < repeater.count; i++) {
                var delegate = repeater.itemAt(i);
                delegate.yWhenResizing = accumulatedY;
                accumulatedY += delegate.heightWhenResizing
            }
        }
        function onResizeHandleReleased(pos) {
            // apply sizes
            var i;
            for (i = 0; i < repeater.count; i++) {
                var delegate = repeater.itemAt(i);
                delegate.appInfo.heightRows = Math.round(delegate.heightWhenResizing / root.rowHeight);
            }

            resizingWidgets = false;
        }
        function computeUsableDragDelta(delta) {
            // above handle. positive delta enlarges widgets
            var i = widgetIndexAboveHandle;
            var remainingDelta = delta;
            var usableDeltaAbove = 0
            while (remainingDelta !== 0 && i >= 0) {
                var appInfo = widgetsList.application(i);
                var minHeight = appInfo.minHeightRows * rowHeight;
                var targetHeight = (appInfo.heightRows * rowHeight) + remainingDelta;

                if (remainingDelta > 0) {
                    if (targetHeight <= root.maxWidgetHeight) {
                        usableDeltaAbove += remainingDelta;
                        remainingDelta = 0;
                    } else {
                        var thisWidgetDelta = root.maxWidgetHeight - (appInfo.heightRows * rowHeight);
                        usableDeltaAbove += thisWidgetDelta;
                        remainingDelta -= thisWidgetDelta;
                        i--;
                    }
                } else {
                    if (targetHeight >= minHeight) {
                        usableDeltaAbove += remainingDelta;
                        remainingDelta = 0;
                    } else {
                        var thisWidgetDelta = -((appInfo.heightRows * rowHeight) - minHeight);
                        usableDeltaAbove += thisWidgetDelta;
                        remainingDelta -= thisWidgetDelta;
                        i--;
                    }
                }
            }

            // below handle. positive delta shrinks widgets
            i = widgetIndexAboveHandle + 1;
            remainingDelta = usableDeltaAbove;
            var usableDelta = 0;
            while (remainingDelta !== 0 && i < widgetsList.count) {
                var appInfo = widgetsList.application(i);
                var minHeight = appInfo.minHeightRows * rowHeight;
                var targetHeight = (appInfo.heightRows * rowHeight) - remainingDelta;

                if (remainingDelta < 0) {
                    if (targetHeight <= root.maxWidgetHeight) {
                        usableDelta += remainingDelta;
                        remainingDelta = 0;
                    } else {
                        var thisWidgetDelta = -(root.maxWidgetHeight - (appInfo.heightRows * rowHeight))
                        remainingDelta += thisWidgetDelta;
                        usableDelta += thisWidgetDelta;
                        i++;
                    }
                } else {
                    if (targetHeight >= minHeight) {
                        usableDelta += remainingDelta;
                        remainingDelta = 0;
                    } else {
                        var thisWidgetDelta = (appInfo.heightRows * rowHeight) - minHeight;
                        usableDelta += thisWidgetDelta;
                        remainingDelta -= thisWidgetDelta;
                        i++;
                    }
                }
            }
            return usableDelta;
        }
        function withinBoundsY(someY) {
            return Math.max(0, Math.min(someY, height));
        }

        Repeater {
            id: repeater
            model: widgetsList

            delegate: Column {
                id: repeaterDelegate
                objectName: "homeWidget_" + (appInfo ? appInfo.id: "none")

                // So that the touch area of the resize handle in this item covers the widget
                // in the next delegate
                z: repeater.model.count - model.index

                Component.onCompleted: {
                    initialized = true;
                }

                readonly property real yNormal: model.rowIndex * root.rowHeight
                property real yWhenResizing

                width: root.width

                readonly property real heightNormal: appInfo? appInfo.heightRows * root.rowHeight : 0
                property real heightWhenResizing

                // Only enable geometry behaviors once WidgetGrid gets its size,
                // otherwise widgets will animate from size (0,0) to their final sizes on startup.
                property bool geometryBehaviorsEnabled: root.width > 0 && !widgetsList.populating
                Behavior on y { enabled: geometryBehaviorsEnabled; DefaultSmoothedAnimation {} }
                Behavior on height { enabled: geometryBehaviorsEnabled; DefaultSmoothedAnimation {} }

                property alias appInfo: appWidget.appInfo
                readonly property int modelIndex: model.index

                readonly property bool isAtBottom: model.index === (repeater.count - 1)

                readonly property Item widget: appWidget

                property real gridCenteredScale: 1
                property real gridCenterX
                property real gridCenterY
                transform: Scale { origin.x: gridCenterX; origin.y: gridCenterY; xScale: gridCenteredScale; yScale: gridCenteredScale}

                property bool initialized: false
                state: {
                    if (initialized) {
                        if (root.applicationModel.activeAppInfo && !appInfo.active && !isAtBottom) {
                            return "hidden";
                        } else if (!appInfo.asWidget) {
                            return "closing";
                        } else {
                            return widgetColumn.resizingWidgets ? "resizing" : "normal"
                        }
                    } else {
                        return "";
                    }
                }
                states: [
                    State {
                        name: "normal"
                        PropertyChanges {
                            target: repeaterDelegate; y: yNormal; height: heightNormal
                        }
                    },
                    State {
                        name: "resizing"
                        PropertyChanges { target: repeaterDelegate; y: yWhenResizing; height: heightWhenResizing }
                    },
                    State {
                        name: "closing"
                        PropertyChanges {
                            target: repeaterDelegate
                            y: yNormal; height: heightNormal
                            scale: 0.75; opacity: 0.0
                        }
                    },
                    State {
                        name: "hidden"
                        PropertyChanges {
                            target: repeaterDelegate; y: yNormal; height: heightNormal
                            gridCenteredScale: 0.9; visible: false; opacity: 0
                        }
                    }
                ]
                transitions: [
                    Transition {
                        to: "closing"
                        SequentialAnimation {
                            DefaultNumberAnimation { properties: "opacity,scale" }
                            ScriptAction { script: { widgetsList.remove(model.index); }}
                        }
                    },
                    Transition {
                        enabled: !widgetsList.populating
                        from: ""; to: "normal"
                        PropertyAction { property: "height" }
                        DefaultNumberAnimation { property: "y"; from: widgetColumn.height }
                    },
                    Transition {
                        to: "hidden"
                        SequentialAnimation {
                            PropertyAction { property: "visible"; value: true }
                            ScriptAction { script: {
                                var pos = root.mapToItem(repeaterDelegate, root.width / 2, root.height / 2);
                                repeaterDelegate.gridCenterX = pos.x;
                                repeaterDelegate.gridCenterY = pos.y;
                             }}
                            DefaultNumberAnimation { properties: "gridCenteredScale,opacity" }
                        }
                    },
                    Transition {
                        from: "hidden"; to: "normal"
                        SequentialAnimation {
                            PropertyAction { property: "visible"; value: true }
                            ScriptAction { script: {
                                var pos = root.mapToItem(repeaterDelegate, root.width / 2, root.height / 2);
                                repeaterDelegate.gridCenterX = pos.x;
                                repeaterDelegate.gridCenterY = pos.y;
                             }}
                            DefaultNumberAnimation { properties: "gridCenteredScale,opacity" }
                        }
                    }
                ]

                Item {
                    id: appWidgetSlot
                    objectName: "appWidgetSlot_" + (appInfo ? appInfo.id: "none")

                    width: repeaterDelegate.width
                    height: repeaterDelegate.height - resizeHandle.height


                    ApplicationWidget {
                        id: appWidget
                        objectName: "applicationWidget_" + (appInfo ? appInfo.id : "none")

                        appInfo: model.appInfo
                        exposedRectTopMargin: active ? root.exposedRectTopMargin : 0.0
                        exposedRectBottomMargin: active ? root.exposedRectBottomMargin : 0.0

                        // in root coords
                        property real dragStartPosY
                        property real dragStartTouchPosY
                        property real dragTouchPosY

                        onDraggedOntoPos: {
                            dragTouchPosY = appWidget.mapToItem(root, pos.x, pos.y).y;
                            widgetColumn.onWidgetMoved(repeaterDelegate);
                        }
                        onDragStarted: {
                            dragStartPosY = appWidget.mapToItem(root, 0, 0).y
                            dragTouchPosY = dragStartTouchPosY = appWidget.mapToItem(root, pos.x, pos.y).y
                            beingDragged = true;
                        }
                        onDragEnded: {
                            beingDragged = false;
                        }

                        onBeingDraggedChanged: d.widgetIsBeingDragged = beingDragged

                        onCloseClicked: {
                            appInfo.asWidget = false;
                        }

                        widgetState: {
                            if (!appInfo || !appInfo.asWidget) {
                                return "";
                            } else {
                                switch (Math.round(height / (root.rowHeight - root.resizerHandleHeight))) {
                                case 0:
                                case 1:
                                    return "Widget1Row";
                                case 2:
                                    return "Widget2Rows";
                                default:
                                    return "Widget3Rows";
                                }
                            }
                        }

                        widgetHeight: {
                            if (widgetState === "Widget1Row") {
                                return root.rowHeight - root.resizerHandleHeight;
                            } else if (widgetState === "Widget2Rows") {
                                return (root.rowHeight * 2) - root.resizerHandleHeight;
                            } else {
                                return (root.rowHeight * 3) - root.resizerHandleHeight;
                            }
                        }

                        state: {
                            if (beingDragged) {
                                return "dragging"
                            } else if (active && root.activeApplicationParent) {
                                return "active"
                            } else if (repeaterDelegate.isAtBottom && root.moveBottomWidgetToDrawer) {
                                return "drawer"
                            } else {
                                return "home"
                            }
                        }

                        states: [
                            State {
                                name: "home"
                                ParentChange {
                                    target: appWidget; parent: appWidgetSlot
                                    x: 0; y: 0; scale: 1
                                    width: appWidgetSlot.width; height: appWidgetSlot.height
                                }
                                PropertyChanges { target: appWidget; buttonsVisible: repeater.count > 1 }
                            },
                            State {
                                name: "active"
                                ParentChange {
                                    target: appWidget; parent: root.activeApplicationParent
                                    x: 0; y: 0; scale: 1
                                    width: root.activeApplicationParent.width; height: root.activeApplicationParent.height
                                }
                                PropertyChanges { target: appWidget; buttonsVisible: false; clipWindow: false }
                            },
                            State {
                                name: "drawer"
                                ParentChange {
                                    target: appWidget; parent: root.widgetDrawer
                                    x: 0; y: 0; scale: 1
                                    width: root.widgetDrawer.width; height: root.widgetDrawer.height
                                }
                                PropertyChanges { target: appWidget; buttonsVisible: false }
                            },
                            State {
                                name: "dragging"
                                ParentChange {
                                    target: appWidget; parent: root
                                    x: 0; y: appWidget.dragStartPosY + (appWidget.dragTouchPosY - appWidget.dragStartTouchPosY)
                                    scale: 1
                                    width: appWidgetSlot.width; height: appWidgetSlot.height
                                }
                            }
                        ]

                        transitions: [
                            Transition {
                                to: "home,drawer"
                                SequentialAnimation {
                                    id: anim
                                    property int oldDelegateZ
                                    PropertyAction { target: anim; property: "oldDelegateZ"; value: repeaterDelegate.z }
                                    PropertyAction { target: repeaterDelegate; property: "z"; value: 100 }
                                    ParentAnimation {
                                        DefaultNumberAnimation { properties: "x,y,width,height" }
                                    }
                                    PropertyAction { target: repeaterDelegate; property: "z"; value: anim.oldDelegateZ }
                                }
                            },
                            Transition {
                                to: "active"
                                SequentialAnimation {
                                    id: activeAnim
                                    property int oldDelegateZ
                                    PropertyAction { target: appWidget; property: "clipWindow"; value: true }
                                    PropertyAction { target: activeAnim; property: "oldDelegateZ"; value: repeaterDelegate.z }
                                    PropertyAction { target: repeaterDelegate; property: "z"; value: 100 }
                                    ParentAnimation {
                                        DefaultNumberAnimation { properties: "x,y,width,height" }
                                    }
                                    PropertyAction { target: repeaterDelegate; property: "z"; value: activeAnim.oldDelegateZ }
                                }
                            }
                        ]
                    }
                }

                ResizeHandle {
                    id: resizeHandle
                    objectName: "resizeHandle" + model.index

                    visible: repeaterDelegate.isAtBottom || opacity === 0 ? false : true
                    opacity: d.widgetIsBeingDragged || root.applicationModel.activeAppInfo ? 0 : 1
                    Behavior on opacity { DefaultNumberAnimation{} }

                    width: parent.width
                    height: root.resizerHandleHeight

                    MouseArea {
                        anchors.fill: parent
                        objectName: "menuWidgetMenuDrag"

                        // don't let it cover the area near widget corners as they can have buttons
                        // like widget-drag and widget-close
                        anchors.leftMargin: Sizes.dp(90)
                        anchors.rightMargin: Sizes.dp(90)

                        // touch area spills out beyond resizeHandle's geometry so that it's easier to hit
                        anchors.topMargin: -parent.height
                        anchors.bottomMargin: -parent.height

                        // distance between the center of the mouse area and the point that was pressed
                        property real pressedYDelta

                        // Always tell widgetColumn that the vertical center of the handle got pressed as it
                        // sits neatly between the widget above and the widget below whereas its touch
                        // area can spill over both neighboring widgets. Makes it possible for widgetColumn
                        // to figure out which handle is being dragged without having to know about the
                        // size of their touch areas.
                        onPressed: {
                            pressedYDelta = mouseY - (height / 2);
                            widgetColumn.onResizeHandlePressed(mapToItem(root, mouseX, height / 2))
                        }
                        onReleased: widgetColumn.onResizeHandleReleased(mapToItem(root, mouseX, mouseY - pressedYDelta))
                        onPositionChanged: widgetColumn.onResizeHandleDragged(mapToItem(root, mouseX, mouseY - pressedYDelta))
                    }
                }
            }
        }
    }
}