matrixion/qml/ui/Snackbar.qml

94 lines
2.1 KiB
QML
Raw Normal View History

2022-01-30 21:14:33 +03:00
// SPDX-FileCopyrightText: 2022 Nheko Contributors
// SPDX-License-Identifier: GPL-3.0-or-later
import QtQuick 2.15
import QtQuick.Controls 2.15
import im.nheko
2022-01-30 21:14:33 +03:00
Popup {
id: snackbar
property string currentMessage: ""
2022-04-16 03:13:01 +03:00
property var messages: []
2022-01-30 21:14:33 +03:00
function showNotification(msg) {
messages.push(msg);
currentMessage = messages[0];
if (!visible) {
open();
dismissTimer.start();
}
}
opacity: 0
padding: Nheko.paddingLarge
2022-04-16 03:13:01 +03:00
parent: Overlay.overlay
x: (parent.width - width) / 2
y: -100
2022-01-30 21:14:33 +03:00
background: Rectangle {
2022-04-11 05:18:16 +03:00
color: timelineRoot.palette.dark
2022-01-30 21:14:33 +03:00
opacity: 0.8
2022-04-16 03:13:01 +03:00
radius: Nheko.paddingLarge
}
contentItem: Label {
color: timelineRoot.palette.light
font.bold: true
text: snackbar.currentMessage
width: Math.max(Overlay.overlay ? Overlay.overlay.width / 2 : 0, 400)
2022-01-30 21:14:33 +03:00
}
enter: Transition {
NumberAnimation {
duration: 200
easing.type: Easing.OutCubic
2022-04-16 03:13:01 +03:00
from: 0.0
property: "opacity"
target: snackbar
to: 1.0
2022-01-30 21:14:33 +03:00
}
NumberAnimation {
duration: 1000
easing.type: Easing.OutCubic
2022-04-16 03:13:01 +03:00
from: -100
properties: "y"
target: snackbar
to: 100
2022-01-30 21:14:33 +03:00
}
}
exit: Transition {
NumberAnimation {
duration: 300
easing.type: Easing.InCubic
2022-04-16 03:13:01 +03:00
from: 1.0
property: "opacity"
target: snackbar
to: 0.0
2022-01-30 21:14:33 +03:00
}
NumberAnimation {
duration: 300
easing.type: Easing.InCubic
2022-04-16 03:13:01 +03:00
from: 100
properties: "y"
target: snackbar
to: -100
}
}
onAboutToHide: {
messages.shift();
}
onClosed: {
if (messages.length > 0) {
currentMessage = messages[0];
open();
dismissTimer.restart();
2022-01-30 21:14:33 +03:00
}
}
2022-04-16 03:13:01 +03:00
Timer {
id: dismissTimer
interval: 10000
2022-01-30 21:14:33 +03:00
2022-04-16 03:13:01 +03:00
onTriggered: snackbar.close()
}
}