Files
pay/guis/mobile/PopupOverlay.qml
T

138 lines
4.9 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
import QtQuick.Controls as QQC2
import "../Flowee" as Flowee
FocusScope {
id: root
anchors.fill: parent
enabled: thePopup.visible
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) {
thePopup.sourceRect = Qt.rect(0, 0, 0, 0);
} else {
thePopup.sourceRect = root.mapFromItem(target, 0, 0, target.width, target.height);
}
2024-07-03 23:06:15 +02:00
overlayLoader.sourceComponent = overlayComponent;
2022-12-19 21:01:07 +01:00
loader.sourceComponent = sourceComponent; // last, as it starts the loading
return loader.item;
}
function close() {
thePopup.visible = false;
}
QQC2.Popup {
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
loader.sourceComponent = undefined;
2025-01-06 14:39:40 +01:00
overlayLoader.sourceComponent = undefined;
2022-12-19 21:01:07 +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
}
background: Rectangle {
2023-02-21 16:40:46 +01:00
color: palette.light
border.color: palette.midlight
2022-12-19 21:01:07 +01:00
border.width: 1
radius: 5
}
Loader {
id: loader
2024-07-03 23:06:15 +02:00
width: parent.width
2022-12-19 21:01:07 +01:00
onLoaded: {
2024-07-03 23:06:15 +02:00
thePopup.visible = true;
root.forceActiveFocus();
}
onHeightChanged: {
if (item == null)
return;
var h = loader.item.implicitHeight
thePopup.height = h + 20; // 20 is the top and bottom inset of the popup
2022-12-19 21:01:07 +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
thePopup.height += h2 + 10;
if (root.height - rect.bottom >= h) { // fits below
thePopup.y = rect.bottom - h2 - 22;
overlayLoader.y = 0
loader.y = h2 + 10;
}
else if (h < rect.y) { // fits above
thePopup.y = rect.y - h;
overlayLoader.y = h + 10;
loader.y = 0;
}
else {
thePopup.y = root.height - h; // make it bottom aligned, even if it overlaps
overlayLoader.y = 0 // item above
loader.y = h2 + 10;
}
return;
}
2024-10-22 00:03:58 +02: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
thePopup.y = rect.bottom;
else if (h < rect.y) // fits above
thePopup.y = rect.y - h;
else
thePopup.y = root.height - h; // make it bottom aligned, even if it overlaps
2024-07-03 23:06:15 +02:00
2022-12-19 21:01:07 +01:00
}
}
2024-07-03 23:06:15 +02:00
Loader {
// use offsets to counter the popup insets
width: parent.width + 40
x: -20
id: overlayLoader
}
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) {
event.accepted = true;
root.close();
}
}
}