From 73f18f4fe9e7a5ad6472349e457391e36c372682 Mon Sep 17 00:00:00 2001 From: Tobias Fella Date: Sat, 3 Apr 2021 02:16:46 +0200 Subject: [PATCH] Refactor the context menu loading --- .../Component/Timeline/FileDelegate.qml | 2 +- .../Component/Timeline/ImageDelegate.qml | 60 -------------- .../Menu/Timeline/FileDelegateContextMenu.qml | 54 +++++++++++-- .../Timeline/MessageDelegateContextMenu.qml | 21 +++-- imports/NeoChat/Page/RoomPage.qml | 80 ++++++------------- 5 files changed, 84 insertions(+), 133 deletions(-) diff --git a/imports/NeoChat/Component/Timeline/FileDelegate.qml b/imports/NeoChat/Component/Timeline/FileDelegate.qml index 5c3a3fb50..c2d09651c 100644 --- a/imports/NeoChat/Component/Timeline/FileDelegate.qml +++ b/imports/NeoChat/Component/Timeline/FileDelegate.qml @@ -43,7 +43,7 @@ RowLayout { Kirigami.Heading { Layout.fillWidth: true level: 4 - text: display + text: model.display wrapMode: Label.Wrap } diff --git a/imports/NeoChat/Component/Timeline/ImageDelegate.qml b/imports/NeoChat/Component/Timeline/ImageDelegate.qml index 9109e4d1d..eed65595d 100644 --- a/imports/NeoChat/Component/Timeline/ImageDelegate.qml +++ b/imports/NeoChat/Component/Timeline/ImageDelegate.qml @@ -63,66 +63,6 @@ Image { } } - MouseArea { - id: messageMouseArea - - enabled: !img.readonly - - anchors.fill: parent - - acceptedButtons: Qt.LeftButton | Qt.RightButton - - onClicked: { - if(mouse.button === Qt.LeftButton) { - fullScreenImage.createObject(parent, {"filename": eventId, "localPath": currentRoom.urlToDownload(eventId)}).showFullScreen() - } else { - openContextMenu() - } - } - - function openContextMenu() { - var contextMenu = imageDelegateContextMenu.createObject(root, {'room': currentRoom, 'author': author, 'message': message, 'eventId': eventId}); - contextMenu.viewSource.connect(function() { - messageSourceSheet.createObject(ApplicationWindow.overlay, {"sourceText": toolTip}).open() - }) - contextMenu.downloadAndOpen.connect(downloadAndOpen) - contextMenu.saveFileAs.connect(saveFileAs) - contextMenu.reply.connect(function() { - roomPanelInput.replyModel = Object.assign({}, model) - roomPanelInput.isReply = true - roomPanelInput.focus() - }) - contextMenu.remove.connect(function() { - currentRoom.redactEvent(eventId) - }) - contextMenu.popup() - } - - Component { - id: messageSourceSheet - - MessageSourceSheet {} - } - - Component { - id: openFolderDialog - - OpenFolderDialog {} - } - - Component { - id: imageDelegateContextMenu - - FileDelegateContextMenu {} - } - - Component { - id: fullScreenImage - - FullScreenImage {} - } - } - function saveFileAs() { var dialog = fileDialog.createObject(ApplicationWindow.overlay) dialog.open() diff --git a/imports/NeoChat/Menu/Timeline/FileDelegateContextMenu.qml b/imports/NeoChat/Menu/Timeline/FileDelegateContextMenu.qml index c16d98c3b..2306868d4 100644 --- a/imports/NeoChat/Menu/Timeline/FileDelegateContextMenu.qml +++ b/imports/NeoChat/Menu/Timeline/FileDelegateContextMenu.qml @@ -9,39 +9,79 @@ import org.kde.kirigami 2.14 as Kirigami import NeoChat.Dialog 1.0 import NeoChat.Menu 1.0 +import org.kde.neochat 1.0 + +import Qt.labs.platform 1.1 + MessageDelegateContextMenu { id: root - signal downloadAndOpen() - signal saveFileAs() + required property var file + required property var progressInfo property list actions: [ Kirigami.Action { text: i18n("Open Externally") icon.name: "document-open" - onTriggered: downloadAndOpen() + onTriggered: { + if (file.downloaded) { + if (!Qt.openUrlExternally(progressInfo.localPath)) { + Qt.openUrlExternally(progressInfo.localDir); + } + } else { + file.onDownloadedChanged.connect(function() { + if (!Qt.openUrlExternally(progressInfo.localPath)) { + Qt.openUrlExternally(progressInfo.localDir); + } + }); + currentRoom.downloadFile(eventId, StandardPaths.writableLocation(StandardPaths.CacheLocation) + "/" + eventId.replace(":", "_").replace("/", "_").replace("+", "_") + currentRoom.fileNameToDownload(eventId)) + } + } }, Kirigami.Action { text: i18n("Save As") icon.name: "document-save" - onTriggered: saveFileAs() + onTriggered: { + var dialog = saveAsDialog.createObject(ApplicationWindow.overlay) + dialog.open() + dialog.currentFile = dialog.folder + "/" + currentRoom.fileNameToDownload(eventId) + } }, Kirigami.Action { text: i18n("Reply") icon.name: "mail-replied-symbolic" - onTriggered: reply(author, message) + onTriggered: { + ChatBoxHelper.replyToMessage(eventId, message, author); + } }, Kirigami.Action { visible: author.id === currentRoom.localUser.id || currentRoom.canSendState("redact") text: i18n("Remove") icon.name: "edit-delete-remove" icon.color: "red" - onTriggered: remove() + onTriggered: { + currentRoom.redactEvent(eventId); + } }, Kirigami.Action { text: i18n("View Source") icon.name: "code-context" - onTriggered: viewSource() + onTriggered: { + messageSourceSheet.createObject(root, {'sourceText': root.source}).open(); + } } ] + Component { + id: saveAsDialog + FileDialog { + fileMode: FileDialog.SaveFile + folder: StandardPaths.writableLocation(StandardPaths.DownloadLocation) + onAccepted: { + if (!currentFile) { + return; + } + currentRoom.downloadFile(eventId, currentFile) + } + } + } } diff --git a/imports/NeoChat/Menu/Timeline/MessageDelegateContextMenu.qml b/imports/NeoChat/Menu/Timeline/MessageDelegateContextMenu.qml index b262a9b5d..f0e233d11 100644 --- a/imports/NeoChat/Menu/Timeline/MessageDelegateContextMenu.qml +++ b/imports/NeoChat/Menu/Timeline/MessageDelegateContextMenu.qml @@ -18,33 +18,38 @@ Loader { required property var author required property string message required property string eventId - - signal viewSource() - signal reply(var author, string message) - signal remove() + required property string source property list actions: [ Kirigami.Action { text: i18n("Reply") icon.name: "mail-replied-symbolic" - onTriggered: reply(author, message) + onTriggered: { + ChatBoxHelper.replyToMessage(eventId, message, author); + } }, Kirigami.Action { visible: author.id === currentRoom.localUser.id || currentRoom.canSendState("redact") text: i18n("Remove") icon.name: "edit-delete-remove" icon.color: "red" - onTriggered: remove() + onTriggered: { + currentRoom.redactEvent(eventId); + } }, Kirigami.Action { text: i18n("Copy") icon.name: "edit-copy" - onTriggered: Clipboard.saveText(message) + onTriggered: { + Clipboard.saveText(message) + } }, Kirigami.Action { text: i18n("View Source") icon.name: "code-context" - onTriggered: viewSource() + onTriggered: { + messageSourceSheet.createObject(page, {'sourceText': loadRoot.source}).open(); + } } ] diff --git a/imports/NeoChat/Page/RoomPage.qml b/imports/NeoChat/Page/RoomPage.qml index dbec80066..9deee828c 100644 --- a/imports/NeoChat/Page/RoomPage.qml +++ b/imports/NeoChat/Page/RoomPage.qml @@ -438,6 +438,17 @@ Kirigami.ScrollablePage { Layout.preferredHeight: info.h / info.w * width Layout.maximumHeight: Kirigami.Units.gridUnit * 15 Layout.maximumWidth: Kirigami.Units.gridUnit * 30 + TapHandler { + acceptedButtons: Qt.RightButton + onTapped: openFileContext(author, model.display, eventId, toolTip, progressInfo, parent) + } + TapHandler { + acceptedButtons: Qt.LeftButton + onLongPressed: openFileContext(author, model.display, eventId, toolTip, progressInfo, parent) + onTapped: { + fullScreenImage.createObject(parent, {"filename": eventId, "localPath": currentRoom.urlToDownload(eventId)}).showFullScreen() + } + } } } } @@ -630,17 +641,17 @@ Kirigami.ScrollablePage { MessageSourceSheet {} } - Component { - id: openFolderDialog - - OpenFolderDialog {} - } - Component { id: fileDelegateContextMenu FileDelegateContextMenu {} } + + Component { + id: fullScreenImage + + FullScreenImage {} + } } footer: ChatBox { @@ -739,71 +750,26 @@ Kirigami.ScrollablePage { } /// Open message context dialog for file and videos - function openFileContext(author, message, eventId, toolTip, progressInfo, file) { + function openFileContext(author, message, eventId, source, progressInfo, file) { const contextMenu = fileDelegateContextMenu.createObject(page, { 'author': author, 'message': message, 'eventId': eventId, + 'source': source, + 'file': file, + 'progressInfo': progressInfo, }); - contextMenu.downloadAndOpen.connect(function() { - if (file.downloaded) { - if (!Qt.openUrlExternally(progressInfo.localPath)) { - Qt.openUrlExternally(progressInfo.localDir); - } - } else { - file.onDownloadedChanged.connect(function() { - if (!Qt.openUrlExternally(progressInfo.localPath)) { - Qt.openUrlExternally(progressInfo.localDir); - } - }); - currentRoom.downloadFile(eventId, Platform.StandardPaths.writableLocation(Platform.StandardPaths.CacheLocation) + "/" + eventId.replace(":", "_").replace("/", "_").replace("+", "_") + currentRoom.fileNameToDownload(eventId)) - } - }); - contextMenu.saveFileAs.connect(function() { - const folderDialog = openFolderDialog.createObject(ApplicationWindow.overlay) - - folderDialog.chosen.connect(function(path) { - if (!path) { - return; - } - - currentRoom.downloadFile(eventId, path + "/" + currentRoom.fileNameToDownload(eventId)) - }) - - folderDialog.open() - }) - contextMenu.viewSource.connect(function() { - messageSourceSheet.createObject(page, { - 'sourceText': toolTip, - }).open(); - }); - contextMenu.reply.connect(function(replyUser, replyContent) { - ChatBoxHelper.replyToMessage(eventId, replyContent, replyUser); - }) - contextMenu.remove.connect(function() { - currentRoom.redactEvent(eventId); - }) contextMenu.open(); } /// Open context menu for normal message - function openMessageContext(author, message, eventId, toolTip) { + function openMessageContext(author, message, eventId, source) { const contextMenu = messageDelegateContextMenu.createObject(page, { 'author': author, 'message': message, 'eventId': eventId, + 'source': source, }); - contextMenu.viewSource.connect(function() { - messageSourceSheet.createObject(page, { - 'sourceText': toolTip, - }).open(); - }); - contextMenu.reply.connect(function(replyUser, replyContent) { - ChatBoxHelper.replyToMessage(eventId, replyContent, replyUser); - }) - contextMenu.remove.connect(function() { - currentRoom.redactEvent(eventId); - }) contextMenu.open(); } }