Files
pay/guis/mobile/PopupOverlay.qml
T

91 lines
3.1 KiB
QML
Raw Permalink Normal View History

2022-12-19 21:01:07 +01:00
/*
* This file is part of the Flowee project
2023-01-05 22:19:16 +01:00
* Copyright (C) 2022-2023 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).
2023-01-05 22:19:16 +01:00
* @returns the item instance of the sourceComponent template
*/
2022-12-19 21:01:07 +01:00
function open(sourceComponent, target) {
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
2022-12-19 21:01:07 +01:00
thePopup.sourceRect = root.mapFromItem(target, 0, 0, target.width, target.height);
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;
}
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
anchors.fill: parent
onLoaded: {
thePopup.height = item.implictHeight
var h = item.implicitHeight
var rect = thePopup.sourceRect;
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
thePopup.visible = true;
root.forceActiveFocus();
}
}
}
Keys.onReleased: (event)=> {
if (event.key === Qt.Key_Back || event.key === Qt.Key_Escape) {
event.accepted = true;
root.close();
}
}
}