From 9f4f3c9475330addb4874d36cb518cbd997f4365 Mon Sep 17 00:00:00 2001 From: Slava Monich Date: Sun, 5 Dec 2021 03:25:24 +0200 Subject: [PATCH] Hide "Extra options..." context menu item if it's not needed If all context actions fit into the context menu limit (5 items in portrait, 4 items in landscape) it makes no sense to show this "Extra options..." thing. --- qml/components/MessageListViewItem.qml | 63 +++++++++++++++++++++++--- qml/pages/ChatPage.qml | 26 ++++++----- 2 files changed, 70 insertions(+), 19 deletions(-) diff --git a/qml/components/MessageListViewItem.qml b/qml/components/MessageListViewItem.qml index 9012d80..768db9d 100644 --- a/qml/components/MessageListViewItem.qml +++ b/qml/components/MessageListViewItem.qml @@ -42,14 +42,45 @@ ListItem { return existingMessage.id === messageId }); readonly property bool isOwnMessage: page.myUserId === myMessage.sender.user_id + readonly property bool canDeleteMessage: myMessage.can_be_deleted_for_all_users || (myMessage.can_be_deleted_only_for_self && myMessage.chat_id === page.myUserId) property bool hasContentComponent property bool additionalOptionsOpened + readonly property var additionalItemsModel: (extraContentLoader.item && ("extraContextMenuItems" in extraContentLoader.item)) ? + extraContentLoader.item.extraContextMenuItems : 0 + readonly property int numberOfExtraOptionsOtherThanDeleteMessage: + (showCopyMessageToClipboardMenuItem ? 0 : 1) + + (showForwardMessageMenuItem ? 0 : 1) + + (page.canPinMessages() ? 1 : 0) + + (additionalItemsModel ? additionalItemsModel.length : 0) + readonly property bool deleteMessageIsOnlyExtraOption: canDeleteMessage && !numberOfExtraOptionsOtherThanDeleteMessage + + readonly property int maxContextMenuItemCount: page.isPortrait ? 5 : 4 + readonly property int baseContextMenuItemCount: (canReplyToMessage ? 1 : 0) + + (myMessage.can_be_edited ? 1 : 0) + 2 /* "Select Message" and "More Options..." */ + readonly property bool showCopyMessageToClipboardMenuItem: (baseContextMenuItemCount + 1) <= maxContextMenuItemCount + readonly property bool showForwardMessageMenuItem: (baseContextMenuItemCount + 2) <= maxContextMenuItemCount + // And don't count "More Options..." for "Delete Message" if "Delete Message" is the only extra option + readonly property bool haveSpaceForDeleteMessageMenuItem: (baseContextMenuItemCount + 3 - (deleteMessageIsOnlyExtraOption ? 1 : 0)) <= maxContextMenuItemCount + highlighted: (down || isSelected || additionalOptionsOpened) && !menuOpen openMenuOnPressAndHold: !messageListItem.precalculatedValues.pageIsSelecting signal replyToMessage() signal editMessage() + signal forwardMessage() + + function deleteMessage() { + var chatId = page.chatInformation.id + var messageId = myMessage.id + Remorse.itemAction(messageListItem, qsTr("Message deleted"), function() { + tdLibWrapper.deleteMessages(chatId, [ messageId ]); + }) + } + + function copyMessageToClipboard() { + Clipboard.text = Functions.getMessageText(myMessage, true, userInformation.id, true) + } onClicked: { if(messageListItem.precalculatedValues.pageIsSelecting) { @@ -105,27 +136,45 @@ ListItem { sourceComponent: Component { ContextMenu { MenuItem { - visible: messageListItem.canReplyToMessage - onClicked: messageListItem.replyToMessage() + visible: canReplyToMessage + onClicked: replyToMessage() text: qsTr("Reply to Message") } MenuItem { visible: myMessage.can_be_edited - onClicked: messageListItem.editMessage() + onClicked: editMessage() text: qsTr("Edit Message") } MenuItem { - onClicked: { - page.toggleMessageSelection(myMessage); - } + onClicked: page.toggleMessageSelection(myMessage) text: qsTr("Select Message") } MenuItem { + visible: showCopyMessageToClipboardMenuItem + onClicked: copyMessageToClipboard() + text: qsTr("Copy Message to Clipboard") + } + MenuItem { + visible: showForwardMessageMenuItem + onClicked: forwardMessage() + text: qsTr("Forward Message") + } + MenuItem { + visible: canDeleteMessage && haveSpaceForDeleteMessageMenuItem + onClicked: deleteMessage() + text: qsTr("Delete Message") + } + MenuItem { + visible: (numberOfExtraOptionsOtherThanDeleteMessage > 0) || + (deleteMessageIsOnlyExtraOption && !haveSpaceForDeleteMessageMenuItem) onClicked: { messageOptionsDrawer.myMessage = myMessage; messageOptionsDrawer.userInformation = userInformation; messageOptionsDrawer.sourceItem = messageListItem - messageOptionsDrawer.additionalItemsModel = (extraContentLoader.item && ("extraContextMenuItems" in extraContentLoader.item)) ? extraContentLoader.item.extraContextMenuItems : 0; + messageOptionsDrawer.additionalItemsModel = additionalItemsModel + messageOptionsDrawer.showCopyMessageToClipboardMenuItem = !showCopyMessageToClipboardMenuItem + messageOptionsDrawer.showForwardMessageMenuItem = !showForwardMessageMenuItem + messageOptionsDrawer.showDeleteMessageMenuItem = canDeleteMessage && !haveSpaceForDeleteMessageMenuItem messageListItem.additionalOptionsOpened = true; messageOptionsDrawer.open = true; } diff --git a/qml/pages/ChatPage.qml b/qml/pages/ChatPage.qml index 3967df5..a41d512 100644 --- a/qml/pages/ChatPage.qml +++ b/qml/pages/ChatPage.qml @@ -704,19 +704,21 @@ Page { property var userInformation: ({}) property var additionalItemsModel: 0 property var sourceItem + property bool showCopyMessageToClipboardMenuItem + property bool showForwardMessageMenuItem + property bool showDeleteMessageMenuItem property list messageOptionsModel: [ NamedAction { - visible: true + visible: messageOptionsDrawer.showCopyMessageToClipboardMenuItem name: qsTr("Copy Message to Clipboard") - action: function () { Clipboard.text = Functions.getMessageText(messageOptionsDrawer.myMessage, true, messageOptionsDrawer.userInformation.id, true); } + action: messageOptionsDrawer.myMessage.copyMessageToClipboard }, NamedAction { - visible: messageOptionsDrawer.myMessage.can_be_forwarded + visible: messageOptionsDrawer.showForwardMessageMenuItem && messageOptionsDrawer.myMessage.can_be_forwarded name: qsTr("Forward Message") action: function () { - var messagesToForward = [ messageOptionsDrawer.myMessage ]; - startForwardingMessages(messagesToForward); + startForwardingMessages([messageOptionsDrawer.myMessage]) } }, NamedAction { @@ -732,13 +734,9 @@ Page { } }, NamedAction { - visible: messageOptionsDrawer.myMessage.can_be_deleted_for_all_users || (messageOptionsDrawer.myMessage.can_be_deleted_only_for_self && messageOptionsDrawer.myMessage.chat_id === chatPage.myUserId) + visible: messageOptionsDrawer.showDeleteMessageMenuItem name: qsTr("Delete Message") - action: function () { - var chatId = chatPage.chatInformation.id; - var messageId = messageOptionsDrawer.myMessage.id; - Remorse.itemAction(messageOptionsDrawer.sourceItem, qsTr("Message deleted"), function() { tdLibWrapper.deleteMessages(chatId, [ messageId ]); }); - } + action: messageOptionsDrawer.sourceItem.deleteMessage } ] @@ -749,7 +747,8 @@ Page { jointModel.push(additionalItemsModel[j]); } for (var i = 0; i < messageOptionsModel.length; i++) { - jointModel.push(messageOptionsModel[i]); + var item = messageOptionsModel[i] + if (item.visible) jointModel.push(item) } drawerListView.model = jointModel; focus = true // Take the focus away from the text field @@ -1310,6 +1309,9 @@ Page { newMessageTextField.text = Functions.getMessageText(myMessage, false, chatPage.myUserId, true) newMessageTextField.focus = true } + onForwardMessage: { + startForwardingMessages([myMessage]) + } } } Component {