Files
pay/guis/mobile/PopupOverlay.qml
T

151 lines
5.1 KiB
QML
Raw Permalink Normal View History

2022-12-19 21:01:07 +01:00
/*
* This file is part of the Flowee project
2025-01-06 14:39:40 +01:00
* Copyright (C) 2022-2025 Tom Zander <tom@flowee.org>
2022-12-19 21:01:07 +01:00
*
* 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, either version 3 of the License, or
* (at your option) any later version.
*
* 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
2025-06-18 17:48:29 +02:00
import QtQuick.Controls.Basic as QQC2
2022-12-19 21:01:07 +01:00
import "../Flowee" as Flowee
FocusScope {
id: root
anchors.fill: parent
enabled: thePopup.visible
2026-03-14 21:14:42 +01:00
property bool isOpen: false
2023-01-05 22:19:16 +01:00
/**
* @param sourceComponent is a Component we set on the loader.
2023-02-19 18:42:33 +01:00
* @param target is the visual item we position next to (vertically).
2024-07-03 23:06:15 +02:00
* @param overlayComponent is a component that we place on top of the
* \a 'target' item, but inside of the popup to avoid dimming.
2023-01-05 22:19:16 +01:00
* @returns the item instance of the sourceComponent template
2024-10-20 21:21:05 +02:00
*
* Note, make sure that the sourceComponent sets an implicitHeight,
* which is used in the popup.
2023-01-05 22:19:16 +01:00
*/
2024-07-03 23:06:15 +02:00
function open(sourceComponent, target, overlayComponent) {
2022-12-19 21:01:07 +01:00
thePopup.palette = mainWindow.palette
2023-02-19 18:42:33 +01:00
thePopup.width = root.width - 18
2023-02-14 15:30:59 +01:00
thePopup.x = (width - thePopup.width) / 2
2024-10-22 00:03:58 +02:00
if (target === null) {
2026-03-14 21:14:42 +01:00
thePopup.sourceRect = Qt.rect(0, 0, 0, 0)
2024-10-22 00:03:58 +02:00
} else {
2026-03-14 21:14:42 +01:00
thePopup.sourceRect = root.mapFromItem(target, 0, 0, target.width, target.height)
2024-10-22 00:03:58 +02:00
}
2026-03-14 21:14:42 +01:00
overlayLoader.sourceComponent = overlayComponent
loader.sourceComponent = sourceComponent // last, as it starts the loading
return loader.item
2022-12-19 21:01:07 +01:00
}
function close() {
2026-03-14 21:14:42 +01:00
thePopup.visible = false
2022-12-19 21:01:07 +01:00
}
2025-03-06 16:54:49 +01:00
Connections {
target: Pay
function onAppProtectionChanged() {
// when the app is locked, make sure we remove all popups
2026-03-14 21:14:42 +01:00
root.close()
2025-03-06 16:54:49 +01:00
}
}
2025-06-19 14:47:19 +02:00
Flowee.Popup {
2022-12-19 21:01:07 +01:00
id: thePopup
width: parent.width
height: 100
modal: true
closePolicy: QQC2.Popup.CloseOnEscape + QQC2.Popup.CloseOnReleaseOutside
property rect sourceRect: Qt.rect(0, 0, 0, 0)
onVisibleChanged: {
if (!visible) { // closing
2026-03-14 21:14:42 +01:00
loader.sourceComponent = undefined
overlayLoader.sourceComponent = undefined
2022-12-19 21:01:07 +01:00
}
2026-03-14 21:14:42 +01:00
root.isOpen = visible // ensure listeners of that property get notified after we acted on visibility changes.
2022-12-19 21:01:07 +01:00
}
2025-03-04 11:40:46 +01:00
background: Item { }
2025-02-14 18:44:09 +01:00
2025-03-04 11:40:46 +01:00
Loader {
id: overlayLoader
2025-06-19 14:47:19 +02:00
width: parent.width - 2
x: 1
2025-03-04 11:40:46 +01:00
}
2025-02-14 18:44:09 +01:00
Rectangle {
color: palette.base
2025-02-14 18:44:09 +01:00
border.color: palette.midlight
border.width: 1
radius: 5
anchors.fill: loader
2025-03-04 11:40:46 +01:00
// the popup imposes a border on us, we take it back
2025-06-19 14:47:19 +02:00
anchors.margins: -10
2025-02-14 18:44:09 +01:00
}
2022-12-19 21:01:07 +01:00
Loader {
id: loader
2025-06-19 14:47:19 +02:00
width: parent.width - 20
x: 10
2022-12-19 21:01:07 +01:00
onLoaded: {
2026-03-14 21:14:42 +01:00
thePopup.visible = true
root.forceActiveFocus()
2024-07-03 23:06:15 +02:00
}
onHeightChanged: {
if (item == null)
2026-03-14 21:14:42 +01:00
return
2024-07-03 23:06:15 +02:00
var h = loader.item.implicitHeight
2025-06-19 14:47:19 +02:00
thePopup.height = h
2026-03-14 21:14:42 +01:00
var rect = thePopup.sourceRect
2024-07-03 23:06:15 +02:00
if (overlayLoader.item) { // the overlay is supposed to be at the same position as the sourceRect
var h2 = overlayLoader.height
2026-03-14 21:14:42 +01:00
thePopup.height += h2
2024-07-03 23:06:15 +02:00
if (root.height - rect.bottom >= h) { // fits below
2026-03-14 21:14:42 +01:00
thePopup.y = rect.bottom - h2
2025-06-19 14:47:19 +02:00
overlayLoader.y = 0
2026-03-14 21:14:42 +01:00
loader.y = h2
2024-07-03 23:06:15 +02:00
}
else if (h < rect.y) { // fits above
2025-06-19 14:47:19 +02:00
thePopup.y = rect.y - h
overlayLoader.y = h
2026-03-14 21:14:42 +01:00
loader.y = 0
2024-07-03 23:06:15 +02:00
}
else {
2026-03-14 21:14:42 +01:00
thePopup.y = root.height - h // make it bottom aligned, even if it overlaps
2024-07-03 23:06:15 +02:00
overlayLoader.y = 0 // item above
2026-03-14 21:14:42 +01:00
loader.y = h2
2024-07-03 23:06:15 +02:00
}
2026-03-14 21:14:42 +01:00
return
2024-07-03 23:06:15 +02:00
}
2026-03-14 21:14:42 +01:00
loader.y = 0
2024-07-03 23:06:15 +02:00
2022-12-19 21:01:07 +01:00
if (root.height - rect.bottom >= h) // fits below
2026-03-14 21:14:42 +01:00
thePopup.y = rect.bottom
2022-12-19 21:01:07 +01:00
else if (h < rect.y) // fits above
2026-03-14 21:14:42 +01:00
thePopup.y = rect.y - h
2022-12-19 21:01:07 +01:00
else
2026-03-14 21:14:42 +01:00
thePopup.y = root.height - h // make it bottom aligned, even if it overlaps
2025-02-04 12:32:22 +01:00
}
2022-12-19 21:01:07 +01:00
}
}
2023-12-07 12:55:39 +01:00
Keys.onPressed: (event)=> {
2022-12-19 21:01:07 +01:00
if (event.key === Qt.Key_Back || event.key === Qt.Key_Escape) {
2026-03-14 21:14:42 +01:00
event.accepted = true
root.close()
2022-12-19 21:01:07 +01:00
}
}
}