This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Notification.qml
115 lines (96 loc) · 2.96 KB
/
Notification.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
import QtQuick 2.0
import Ubuntu.Components 1.1
Rectangle {
id: notification
anchors {
horizontalCenter: parent.horizontalCenter
bottom: parent.bottom
bottomMargin: (mainView.useDeprecatedToolbar && toolbar.opened && !toolbar.locked ? toolbar.height : 0) + units.gu(2) + ((!mainView.anchorToKeyboard && Qt.inputMethod.visible) ? Qt.inputMethod.keyboardRectangle.height : 0)
leftMargin: units.gu(2)
rightMargin: units.gu(2)
}
readonly property real labelPadding: units.gu(4.5)
height: label.height + units.gu(3)
width: label.width + labelPadding
radius: label.height / (2 * label.lineCount) + units.gu(1.5)
color: Qt.rgba(0,0,0,0.7)
opacity: showing ? 1 : 0
Behavior on opacity {
UbuntuNumberAnimation {}
}
property bool showing: false
property alias text: label.text
property MainView mainView
property var queue: []
property color textColor: "white"
Component.onCompleted: mainView = findMainView() //This cannot be done as a property binding because the method will later return the QQuickRootItem.
function show(text) {
queue.push(text)
if (!showing && !timer.running) { //!timer.running for the time between two notifications (when timer.interval === 800)
update()
}
}
function update() {
notification.text = ""
notification.text = queue.shift()
notification.showing = true
}
onShowingChanged: {
if (showing) {
timer.restart()
} else {
if (queue.length > 0) {
timer.interval = 800
timer.restart()
}
}
}
Label {
id: label
anchors.centerIn: parent
fontSize: "medium"
color: textColor
horizontalAlignment: Text.AlignHCenter
wrapMode: Text.WordWrap
property real wantedWidth: 0 //Needed for multi-line notifications
onTextChanged: {
if (text !== "") {
wantedWidth = Math.min(contentWidth, mainView.width - notification.anchors.rightMargin - notification.anchors.leftMargin - notification.labelPadding)
} else {
wantedWidth = 0
}
}
state: (wantedWidth === 0) ? "" : "wanted"
states: [
State {
name: ""
},
State {
name: "wanted"
PropertyChanges {
target: label
width: wantedWidth
}
}
]
}
Timer {
id: timer
interval: 2000
onTriggered: {
if (interval === 2000) {
showing = false
} else {
interval = 2000
update()
}
}
}
function findMainView() {
var up = parent
while (up.parent !== null) {
up = up.parent
}
return up
}
}