diff --git a/src/qml/Base/HOkCancelPopup.qml b/src/qml/Base/HOkCancelPopup.qml index 32b43f8b..720ff81a 100644 --- a/src/qml/Base/HOkCancelPopup.qml +++ b/src/qml/Base/HOkCancelPopup.qml @@ -2,10 +2,16 @@ import QtQuick 2.12 import QtQuick.Layouts 1.12 HPopup { + id: popup onAboutToShow: okClicked = false + signal ok() + signal cancel() + + property alias label: label + property alias text: label.text property bool okClicked: false @@ -15,8 +21,8 @@ HPopup { { name: "cancel", text: qsTr("Cancel"), iconName: "cancel" }, ] box.buttonCallbacks: ({ - ok: button => { okClicked = true; popup.close() }, - cancel: button => { okClicked = false; popup.close() }, + ok: button => { okClicked = true; popup.ok(); popup.close() }, + cancel: button => { okClicked = false; popup.cancel(); popup.close() }, }) diff --git a/src/qml/Chat/ClearMessagesPopup.qml b/src/qml/Chat/ClearMessagesPopup.qml new file mode 100644 index 00000000..b1918b47 --- /dev/null +++ b/src/qml/Chat/ClearMessagesPopup.qml @@ -0,0 +1,16 @@ +import QtQuick 2.12 +import "../Base" + +HOkCancelPopup { + text: qsTr( + "Clear this room's messages?\n" + + "The messages will only be removed on your side. " + + "They will be available again after you restart the application." + ) + + onOk: py.callClientCoro(userId, "clear_events", [roomId]) + + + property string userId: "" + property string roomId: "" +} diff --git a/src/qml/Chat/Timeline/EventDelegate.qml b/src/qml/Chat/Timeline/EventDelegate.qml index ce144648..7ba9d850 100644 --- a/src/qml/Chat/Timeline/EventDelegate.qml +++ b/src/qml/Chat/Timeline/EventDelegate.qml @@ -127,8 +127,10 @@ Column { HMenuItem { icon.name: "clear-messages" text: qsTr("Clear messages") - onTriggered: py.callClientCoro( - chatPage.userId, "clear_events", [chatPage.roomId], + onTriggered: Utils.makePopup( + "Chat/ClearMessagesPopup.qml", + chatPage, + {userId: chatPage.userId, roomId: chatPage.roomId}, ) } diff --git a/src/qml/Shortcuts.qml b/src/qml/Shortcuts.qml index df506d4f..97ec2946 100644 --- a/src/qml/Shortcuts.qml +++ b/src/qml/Shortcuts.qml @@ -116,10 +116,13 @@ HShortcutHandler { HShortcut { enabled: window.uiState.page == "Chat/Chat.qml" sequences: settings.keys.clearRoomMessages - onPressed: py.callClientCoro( - window.uiState.pageProperties.userId, - "clear_events", - [window.uiState.pageProperties.roomId], + onPressed: Utils.makePopup( + "Chat/ClearMessagesPopup.qml", + mainUI, + { + userId: window.uiState.pageProperties.userId, + roomId: window.uiState.pageProperties.roomId, + } ) } } diff --git a/src/qml/utils.js b/src/qml/utils.js index e97bc285..d9acf3f3 100644 --- a/src/qml/utils.js +++ b/src/qml/utils.js @@ -1,3 +1,45 @@ +function makeObject(url, parent=null, properties={}, callback=null) { + let comp = Qt.createComponent(url, Component.Asynchronous) + let ready = false + + comp.statusChanged.connect(status => { + if ([Component.Null, Component.Error].includes(status)) { + console.error("Failed creating component: ", comp.errorString()) + + } else if (status == Component.Ready) { + let incu = comp.incubateObject(parent, properties, Qt.Asynchronous) + + if (incu.status == Component.Ready) { + if (callback) callback(incu.object) + return + } + + incu.onStatusChanged = (istatus) => { + if (incu.status == Component.Error) { + console.error("Failed incubating object: ", + incu.errorString()) + + } else if (istatus == Component.Ready && callback && ! ready) { + if (callback) callback(incu.object) + ready = true + } + } + } + }) + + if (comp.status == Component.Ready) comp.statusChanged(comp.status) +} + + +function makePopup(url, parent=null, properties={}, callback=null) { + makeObject(url, parent, properties, (popup) => { + popup.open() + popup.closed.connect(() => { popup.destroy() }) + if (callback) callback(popup) + }) +} + + function isEmptyObject(obj) { return Object.entries(obj).length === 0 && obj.constructor === Object }