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.
This commit is contained in:
Slava Monich 2021-12-05 03:25:24 +02:00
parent 0400d12e52
commit 9f4f3c9475
2 changed files with 70 additions and 19 deletions

View file

@ -42,14 +42,45 @@ ListItem {
return existingMessage.id === messageId return existingMessage.id === messageId
}); });
readonly property bool isOwnMessage: page.myUserId === myMessage.sender.user_id 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 hasContentComponent
property bool additionalOptionsOpened 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 highlighted: (down || isSelected || additionalOptionsOpened) && !menuOpen
openMenuOnPressAndHold: !messageListItem.precalculatedValues.pageIsSelecting openMenuOnPressAndHold: !messageListItem.precalculatedValues.pageIsSelecting
signal replyToMessage() signal replyToMessage()
signal editMessage() 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: { onClicked: {
if(messageListItem.precalculatedValues.pageIsSelecting) { if(messageListItem.precalculatedValues.pageIsSelecting) {
@ -105,27 +136,45 @@ ListItem {
sourceComponent: Component { sourceComponent: Component {
ContextMenu { ContextMenu {
MenuItem { MenuItem {
visible: messageListItem.canReplyToMessage visible: canReplyToMessage
onClicked: messageListItem.replyToMessage() onClicked: replyToMessage()
text: qsTr("Reply to Message") text: qsTr("Reply to Message")
} }
MenuItem { MenuItem {
visible: myMessage.can_be_edited visible: myMessage.can_be_edited
onClicked: messageListItem.editMessage() onClicked: editMessage()
text: qsTr("Edit Message") text: qsTr("Edit Message")
} }
MenuItem { MenuItem {
onClicked: { onClicked: page.toggleMessageSelection(myMessage)
page.toggleMessageSelection(myMessage);
}
text: qsTr("Select Message") text: qsTr("Select Message")
} }
MenuItem { 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: { onClicked: {
messageOptionsDrawer.myMessage = myMessage; messageOptionsDrawer.myMessage = myMessage;
messageOptionsDrawer.userInformation = userInformation; messageOptionsDrawer.userInformation = userInformation;
messageOptionsDrawer.sourceItem = messageListItem 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; messageListItem.additionalOptionsOpened = true;
messageOptionsDrawer.open = true; messageOptionsDrawer.open = true;
} }

View file

@ -704,19 +704,21 @@ Page {
property var userInformation: ({}) property var userInformation: ({})
property var additionalItemsModel: 0 property var additionalItemsModel: 0
property var sourceItem property var sourceItem
property bool showCopyMessageToClipboardMenuItem
property bool showForwardMessageMenuItem
property bool showDeleteMessageMenuItem
property list<NamedAction> messageOptionsModel: [ property list<NamedAction> messageOptionsModel: [
NamedAction { NamedAction {
visible: true visible: messageOptionsDrawer.showCopyMessageToClipboardMenuItem
name: qsTr("Copy Message to Clipboard") name: qsTr("Copy Message to Clipboard")
action: function () { Clipboard.text = Functions.getMessageText(messageOptionsDrawer.myMessage, true, messageOptionsDrawer.userInformation.id, true); } action: messageOptionsDrawer.myMessage.copyMessageToClipboard
}, },
NamedAction { NamedAction {
visible: messageOptionsDrawer.myMessage.can_be_forwarded visible: messageOptionsDrawer.showForwardMessageMenuItem && messageOptionsDrawer.myMessage.can_be_forwarded
name: qsTr("Forward Message") name: qsTr("Forward Message")
action: function () { action: function () {
var messagesToForward = [ messageOptionsDrawer.myMessage ]; startForwardingMessages([messageOptionsDrawer.myMessage])
startForwardingMessages(messagesToForward);
} }
}, },
NamedAction { NamedAction {
@ -732,13 +734,9 @@ Page {
} }
}, },
NamedAction { 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") name: qsTr("Delete Message")
action: function () { action: messageOptionsDrawer.sourceItem.deleteMessage
var chatId = chatPage.chatInformation.id;
var messageId = messageOptionsDrawer.myMessage.id;
Remorse.itemAction(messageOptionsDrawer.sourceItem, qsTr("Message deleted"), function() { tdLibWrapper.deleteMessages(chatId, [ messageId ]); });
}
} }
] ]
@ -749,7 +747,8 @@ Page {
jointModel.push(additionalItemsModel[j]); jointModel.push(additionalItemsModel[j]);
} }
for (var i = 0; i < messageOptionsModel.length; i++) { 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; drawerListView.model = jointModel;
focus = true // Take the focus away from the text field 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.text = Functions.getMessageText(myMessage, false, chatPage.myUserId, true)
newMessageTextField.focus = true newMessageTextField.focus = true
} }
onForwardMessage: {
startForwardingMessages([myMessage])
}
} }
} }
Component { Component {