File: Notification.qml

package info (click to toggle)
lomiri 0.5.0-7
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 24,088 kB
  • sloc: cpp: 39,498; python: 2,559; javascript: 1,426; ansic: 1,012; sh: 289; xml: 252; makefile: 69
file content (640 lines) | stat: -rw-r--r-- 24,824 bytes parent folder | download | duplicates (3)
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
/*
 * Copyright (C) 2013-2016 Canonical Ltd.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 3.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import QtQuick 2.15
import QtGraphicalEffects 1.12
import Powerd 0.1
import Lomiri.Components 1.3
import Lomiri.Components.ListItems 1.3 as ListItem
import Lomiri.Notifications 1.0
import QMenuModel 1.0
import Utils 0.1
import "../Components"

StyledItem {
    id: notification

    property alias iconSource: icon.fileSource
    property alias secondaryIconSource: secondaryIcon.source
    property alias summary: summaryLabel.text
    property alias body: bodyLabel.text
    property alias value: valueIndicator.value
    property var actions
    property var notificationId
    property var type
    property var hints
    property var notification
    property color color: theme.palette.normal.background
    property bool fullscreen: notification.notification && typeof notification.notification.fullscreen != "undefined" ?
                                  notification.notification.fullscreen : false // fullscreen prop only exists in the mock
    property int maxHeight
    property int margins: units.gu(1)
    property bool privacyMode: false
    property bool hideContent: notification.privacyMode &&
                               notification.notification.urgency !== Notification.Critical &&
                               (notification.type === Notification.Ephemeral || notification.type === Notification.Interactive)


    readonly property real defaultOpacity: 1.0
    property bool hasMouse
    property url background: ""

    objectName: "background"
    implicitHeight: type !== Notification.PlaceHolder ? (fullscreen ? maxHeight : outterColumn.height + shapedBack.anchors.topMargin + margins * 2) : 0

    // FIXME: non-zero initially because of LP: #1354406 workaround, we want this to start at 0 upon creation eventually
    opacity: defaultOpacity - Math.abs(x / notification.width)

    readonly property bool expanded: type === Notification.SnapDecision &&                   // expand only snap decisions, if...
                                     (fullscreen ||                                          // - it's a fullscreen one
                                      ListView.view.currentIndex === index ||                // - it's the one the user clicked on
                                      (ListView.view.currentIndex === -1 && index == 0)      // - the first one after the user closed the previous one
                                      )

    NotificationAudio {
        id: sound
        objectName: "sound"
        source: hints["suppress-sound"] !== "true" && hints["sound-file"] !== undefined ? hints["sound-file"] : ""
    }

    Component.onCompleted: {
        if (type === Notification.PlaceHolder) {
            return;
        }

        // Turn on screen as needed (Powerd.Notification means the screen
        // stays on for a shorter amount of time)
        if (type === Notification.SnapDecision) {
            Powerd.setStatus(Powerd.On, Powerd.SnapDecision);
        } else if (type !== Notification.Confirmation) {
            Powerd.setStatus(Powerd.On, Powerd.Notification);
        }

        // FIXME: using onCompleted because of LP: #1354406 workaround, has to be onOpacityChanged really
        if (opacity == defaultOpacity && hints["suppress-sound"] !== "true" && sound.source !== "") {
            sound.play();
        }
    }

    Component.onDestruction: {
        if (type === Notification.PlaceHolder) {
            return;
        }

        if (type === Notification.SnapDecision) {
            Powerd.setStatus(Powerd.Off, Powerd.SnapDecision);
        } else if (type !== Notification.Confirmation) {
            Powerd.setStatus(Powerd.Off, Powerd.Notification);
        }
    }

    function closeNotification() {
        if (index === ListView.view.currentIndex) { // reset to get the 1st snap decision expanded
            ListView.view.currentIndex = -1;
        }

        // perform the "reject" action
        notification.notification.invokeAction(notification.actions.data(1, ActionModel.RoleActionId));

        notification.notification.close();
    }

    Behavior on x {
        LomiriNumberAnimation { easing.type: Easing.OutBounce }
    }

    onHintsChanged: {
        if (type === Notification.Confirmation && opacity == defaultOpacity && hints["suppress-sound"] !== "true" && sound.source !== "") {
            sound.play();
        }
    }

    onFullscreenChanged: {
        if (fullscreen) {
            notification.notification.urgency = Notification.Critical;
        }
        if (index == 0) {
            ListView.view.topmostIsFullscreen = fullscreen;
        }
    }

    Behavior on implicitHeight {
        enabled: !fullscreen
        LomiriNumberAnimation {
            duration: LomiriAnimation.SnapDuration
        }
    }

    visible: type !== Notification.PlaceHolder

    BorderImage {
        anchors {
            fill: contents
            margins: shapedBack.visible ? -units.gu(1) : -units.gu(1.5)
        }
        source: "../graphics/dropshadow2gu.sci"
        opacity: notification.opacity * 0.5
        enabled: !fullscreen
    }

    LomiriShape {
        id: shapedBack
        objectName: "shapedBack"

        visible: !fullscreen
        anchors {
            fill: parent
            leftMargin: notification.margins
            rightMargin: notification.margins
            topMargin: index == 0 ? notification.margins : 0
        }
        backgroundColor: parent.color
        radius: "small"
        aspect: LomiriShape.Flat
    }

    Rectangle {
        id: nonShapedBack

        visible: fullscreen
        anchors.fill: parent
        color: parent.color
    }

    onXChanged: {
        if (Math.abs(notification.x) > 0.75 * notification.width) {
            closeNotification();
        }
    }

    Item {
        id: contents
        anchors.fill: fullscreen ? nonShapedBack : shapedBack

        LomiriMenuModelPaths {
            id: paths

            source: hints["x-lomiri-private-menu-model"]

            busNameHint: "busName"
            actionsHint: "actions"
            menuObjectPathHint: "menuPath"
        }

        AyatanaMenuModel {
            id: lomiriMenuModel

            property string lastNameOwner: ""

            busName: paths.busName
            actions: paths.actions
            menuObjectPath: paths.menuObjectPath
            onNameOwnerChanged: {
                if (lastNameOwner !== "" && nameOwner === "" && notification.notification !== undefined) {
                    notification.notification.close()
                }
                lastNameOwner = nameOwner
            }
        }

        MouseArea {
            id: interactiveArea

            anchors.fill: parent
            objectName: "interactiveArea"

            drag.target: !fullscreen ? notification : undefined
            drag.axis: Drag.XAxis
            drag.minimumX: -notification.width
            drag.maximumX: notification.width
            hoverEnabled: true

            onClicked: {
                if (notification.type === Notification.Interactive) {
                    notification.notification.invokeAction(actionRepeater.itemAt(0).actionId)
                } else {
                    notification.ListView.view.currentIndex = index;
                }
            }
            onReleased: {
                if (Math.abs(notification.x) < notification.width / 2) {
                    notification.x = 0
                } else {
                    notification.x = notification.width
                }
            }
        }

        NotificationButton {
            objectName: "closeButton"
            width: units.gu(2)
            height: width
            radius: width / 2
            visible: hasMouse && (containsMouse || interactiveArea.containsMouse)
            iconName: "close"
            outline: false
            hoverEnabled: true
            color: theme.palette.normal.negative
            anchors.horizontalCenter: parent.left
            anchors.horizontalCenterOffset: notification.parent.state === "narrow" ? notification.margins / 2 : 0
            anchors.verticalCenter: parent.top
            anchors.verticalCenterOffset: notification.parent.state === "narrow" ? notification.margins / 2 : 0

            onClicked: closeNotification();
        }

        Column {
            id: outterColumn
            objectName: "outterColumn"

            anchors {
                left: parent.left
                right: parent.right
                top: parent.top
                margins: !fullscreen ? notification.margins : 0
            }

            spacing: notification.margins

            Row {
                id: topRow

                spacing: notification.margins
                anchors {
                    left: parent.left
                    right: parent.right
                }

                Item {
                    id: iconWrapper
                    width: units.gu(6)
                    height: width
                    visible: iconSource !== "" && type !== Notification.Confirmation
                    ShapedIcon {
                        id: icon

                        objectName: "icon"
                        anchors.fill: parent
                        shaped: notification.hints["x-lomiri-non-shaped-icon"] !== "true"
                        visible: iconSource !== "" && !blurEffect.visible
                    }

                    FastBlur {
                        id: blurEffect
                        objectName: "blurEffect"
                        visible: notification.hideContent
                        anchors.fill: icon
                        source: icon
                        transparentBorder: true
                        radius: 64
                    }
                }

                Label {
                    objectName: "privacySummaryLabel"
                    width: secondaryIcon.visible ? parent.width - x - units.gu(3) : parent.width - x
                    height: units.gu(6)
                    anchors.verticalCenter: iconWrapper.verticalCenter
                    verticalAlignment: Text.AlignVCenter
                    visible: notification.hideContent
                    fontSize: "medium"
                    font.weight: Font.Light
                    color: theme.palette.normal.backgroundSecondaryText
                    elide: Text.ElideRight
                    textFormat: Text.PlainText
                    text: i18n.tr("New message")
                }

                Column {
                    id: labelColumn
                    width: secondaryIcon.visible ? parent.width - x - units.gu(3) : parent.width - x
                    anchors.verticalCenter: (icon.visible && !bodyLabel.visible) ? iconWrapper.verticalCenter : undefined
                    spacing: units.gu(.4)
                    visible: type !== Notification.Confirmation && !notification.hideContent

                    Label {
                        id: summaryLabel

                        objectName: "summaryLabel"
                        anchors {
                            left: parent.left
                            right: parent.right
                        }
                        fontSize: "medium"
                        font.weight: Font.Light
                        color: theme.palette.normal.backgroundSecondaryText
                        elide: Text.ElideRight
                        textFormat: Text.PlainText
                    }

                    Label {
                        id: bodyLabel

                        objectName: "bodyLabel"
                        anchors {
                            left: parent.left
                            right: parent.right
                        }
                        visible: body != ""
                        fontSize: "small"
                        font.weight: Font.Light
                        color: theme.palette.normal.backgroundTertiaryText
                        wrapMode: Text.Wrap
                        maximumLineCount: {
                            if (type === Notification.SnapDecision) {
                                return 12;
                            } else if (notification.hints["x-lomiri-truncation"] === false) {
                                return 20;
                            } else {
                                return 2;
                            }
                        }
                        elide: Text.ElideRight
                        textFormat: Text.PlainText
                        lineHeight: 1.1
                    }
                }

                Image {
                    id: secondaryIcon

                    objectName: "secondaryIcon"
                    width: units.gu(2)
                    height: width
                    visible: status === Image.Ready
                    fillMode: Image.PreserveAspectCrop
                }
            }

            ListItem.ThinDivider {
                visible: type === Notification.SnapDecision && notification.expanded
            }

            Icon {
                name: "toolkit_chevron-down_3gu"
                visible: type === Notification.SnapDecision && !notification.expanded
                width: units.gu(2)
                height: width
                anchors.horizontalCenter: parent.horizontalCenter
                color: theme.palette.normal.base
            }

            ShapedIcon {
                id: centeredIcon
                objectName: "centeredIcon"
                width: units.gu(4)
                height: width
                shaped: notification.hints["x-lomiri-non-shaped-icon"] !== "true"
                fileSource: icon.fileSource
                visible: fileSource !== "" && type === Notification.Confirmation
                anchors.horizontalCenter: parent.horizontalCenter
            }

            Label {
                id: valueLabel
                objectName: "valueLabel"
                text: body
                anchors.horizontalCenter: parent.horizontalCenter
                visible: type === Notification.Confirmation && body !== ""
                fontSize: "medium"
                font.weight: Font.Light
                color: theme.palette.normal.backgroundSecondaryText
                wrapMode: Text.WordWrap
                maximumLineCount: 1
                elide: Text.ElideRight
                textFormat: Text.PlainText
            }

            ProgressBar {
                id: valueIndicator
                objectName: "valueIndicator"
                visible: type === Notification.Confirmation
                minimumValue: 0
                maximumValue: 100
                showProgressPercentage: false
                anchors {
                    left: parent.left
                    right: parent.right
                }
                height: units.gu(1)
            }

            Column {
                id: dialogColumn
                objectName: "dialogListView"
                spacing: notification.margins

                visible: count > 0 && (notification.expanded || notification.fullscreen)

                anchors {
                    left: parent.left
                    right: parent.right
                    top: fullscreen ? parent.top : undefined
                    bottom: fullscreen ? parent.bottom : undefined
                }

                Repeater {
                    model: lomiriMenuModel

                    NotificationMenuItemFactory {
                        id: menuItemFactory

                        anchors {
                            left: dialogColumn.left
                            right: dialogColumn.right
                        }

                        menuModel: lomiriMenuModel
                        menuData: model
                        menuIndex: index
                        maxHeight: notification.maxHeight
                        background: notification.background

                        onLoaded: {
                            notification.fullscreen = Qt.binding(function() { return fullscreen; });
                        }
                        onAccepted: {
                            notification.notification.invokeAction(actionRepeater.itemAt(0).actionId)
                        }
                    }
                }
            }

            Column {
                id: oneOverTwoCase

                anchors {
                    left: parent.left
                    right: parent.right
                }

                spacing: notification.margins

                visible: notification.type === Notification.SnapDecision && oneOverTwoRepeaterTop.count === 3 && notification.expanded

                Repeater {
                    id: oneOverTwoRepeaterTop

                    model: notification.actions
                    delegate: Loader {
                        id: oneOverTwoLoaderTop

                        property string actionId: id
                        property string actionLabel: label

                        Component {
                            id: oneOverTwoButtonTop

                            NotificationButton {
                                objectName: "notify_oot_button" + index
                                width: oneOverTwoCase.width
                                text: oneOverTwoLoaderTop.actionLabel
                                outline: notification.hints["x-lomiri-private-affirmative-tint"] !== "true"
                                color: notification.hints["x-lomiri-private-affirmative-tint"] === "true" ? theme.palette.normal.positive
                                                                                                             : theme.name == "Lomiri.Components.Themes.SuruDark" ? "#888"
                                                                                                                                                                 : "#666"
                                onClicked: notification.notification.invokeAction(oneOverTwoLoaderTop.actionId)
                            }
                        }
                        sourceComponent: index == 0 ? oneOverTwoButtonTop : undefined
                    }
                }

                Row {
                    spacing: notification.margins

                    Repeater {
                        id: oneOverTwoRepeaterBottom

                        model: notification.actions
                        delegate: Loader {
                            id: oneOverTwoLoaderBottom

                            property string actionId: id
                            property string actionLabel: label

                            Component {
                                id: oneOverTwoButtonBottom

                                NotificationButton {
                                    objectName: "notify_oot_button" + index
                                    width: oneOverTwoCase.width / 2 - spacing / 2
                                    text: oneOverTwoLoaderBottom.actionLabel
                                    outline: notification.hints["x-lomiri-private-rejection-tint"] !== "true"
                                    color: index == 1 && notification.hints["x-lomiri-private-rejection-tint"] === "true" ? theme.palette.normal.negative
                                                                                                                             : theme.name == "Lomiri.Components.Themes.SuruDark" ? "#888"
                                                                                                                                                                                 : "#666"
                                    onClicked: notification.notification.invokeAction(oneOverTwoLoaderBottom.actionId)
                                }
                            }
                            sourceComponent: (index == 1 || index == 2) ? oneOverTwoButtonBottom : undefined
                        }
                    }
                }
            }

            Row {
                id: buttonRow

                objectName: "buttonRow"
                anchors {
                    left: parent.left
                    right: parent.right
                }
                visible: notification.type === Notification.SnapDecision && actionRepeater.count > 0 && !oneOverTwoCase.visible && notification.expanded
                spacing: notification.margins
                layoutDirection: Qt.RightToLeft

                Loader {
                    id: notifySwipeButtonLoader
                    active: notification.hints["x-lomiri-snap-decisions-swipe"] === "true"

                    sourceComponent: SwipeToAct  {
                        objectName: "notify_swipe_button"
                        width: buttonRow.width
                        leftIconName: "call-end"
                        rightIconName: "call-start"
                        clickToAct: notification.hasMouse
                        onRightTriggered: {
                            notification.notification.invokeAction(notification.actions.data(0, ActionModel.RoleActionId))
                        }

                        onLeftTriggered: {
                            notification.notification.invokeAction(notification.actions.data(1, ActionModel.RoleActionId))
                        }
                    }
                }

                Repeater {
                    id: actionRepeater
                    model: notification.actions
                    delegate: Loader {
                        id: loader

                        property string actionId: id
                        property string actionLabel: label
                        active: !notifySwipeButtonLoader.active

                        Component {
                            id: actionButton

                            NotificationButton {
                                objectName: "notify_button" + index
                                width: buttonRow.width / 2 - spacing / 2
                                text: loader.actionLabel
                                outline: (index == 0 && notification.hints["x-lomiri-private-affirmative-tint"] !== "true") ||
                                         (index == 1 && notification.hints["x-lomiri-private-rejection-tint"] !== "true")
                                color: {
                                    var result = "#666";
                                    if (theme.name == "Lomiri.Components.Themes.SuruDark") {
                                        result = "#888"
                                    }
                                    if (index == 0 && notification.hints["x-lomiri-private-affirmative-tint"] === "true") {
                                        result = theme.palette.normal.positive;
                                    }
                                    if (index == 1 && notification.hints["x-lomiri-private-rejection-tint"] === "true") {
                                        result = theme.palette.normal.negative;
                                    }
                                    return result;
                                }
                                onClicked: notification.notification.invokeAction(loader.actionId)
                            }
                        }
                        sourceComponent: (index == 0 || index == 1) ? actionButton : undefined
                    }
                }
            }

            OptionToggle {
                id: optionToggle
                objectName: "notify_button2"
                width: parent.width
                anchors {
                    left: parent.left
                    right: parent.right
                }

                visible: notification.type === Notification.SnapDecision && actionRepeater.count > 3 && !oneOverTwoCase.visible && notification.expanded
                model: notification.actions
                expanded: false
                startIndex: 2
                onTriggered: {
                    notification.notification.invokeAction(id)
                }
            }
        }
    }
}