From c333f2af17817995ce3ccd5dcd355ae9fcb1fb2a Mon Sep 17 00:00:00 2001 From: Ashvin Nihalani Date: Sun, 5 Jul 2020 00:49:31 +0000 Subject: [PATCH] [SMS App] Select Message Text Switched from QT Label to Text Edit as QA Label is not allowed to select text https://stackoverflow.com/questions/49784099/qml-how-to-make-text-or-label-selectable https://bugreports.qt.io/browse/QTBUG-14077 BUG: 418630 - Cannot select text in messaging app --- .gitignore | 1 + smsapp/qml/ChatMessage.qml | 52 +++++++++++++++++++++++++++++--------- 2 files changed, 41 insertions(+), 12 deletions(-) diff --git a/.gitignore b/.gitignore index 1760f77d5..2b1ec6a87 100644 --- a/.gitignore +++ b/.gitignore @@ -16,6 +16,7 @@ KDEConnect.includes *.qmlc *.rej .vscode +.github/workflows/* # Eclipse settings .cproject diff --git a/smsapp/qml/ChatMessage.qml b/smsapp/qml/ChatMessage.qml index abc0bdd36..8a29eba0a 100644 --- a/smsapp/qml/ChatMessage.qml +++ b/smsapp/qml/ChatMessage.qml @@ -31,6 +31,7 @@ Item { property string messageBody property bool sentByMe + property string selectedText property date dateTime property string name @@ -71,6 +72,31 @@ Item { } radius: 6 + MouseArea { + anchors.fill: parent + acceptedButtons: Qt.LeftButton | Qt.RightButton + onClicked: mouse => { + if (mouse.button === Qt.RightButton) { + var selectStart = messageLabel.selectionStart; + var selectEnd = messageLabel.selectionEnd; + selectedText = messageLabel.selectedText; + contextMenu.x = mouse.x; + contextMenu.y = mouse.y; + messageLabel.persistentSelection = true; + contextMenu.open(); + } + } + onPressAndHold: { + var selectStart = messageLabel.selectionStart; + var selectEnd = messageLabel.selectionEnd; + selectedText = messageLabel.selectedText; + contextMenu.x = mouse.x; + contextMenu.y = mouse.y; + messageLabel.persistentSelection = true; + contextMenu.open(); + } + } + Column { id: messageColumn width: parent.width @@ -78,14 +104,17 @@ Item { property int contentWidth: Math.max(messageLabel.implicitWidth, dateLabel.implicitWidth) - Label { + TextEdit { id: messageLabel + selectByMouse: true + readOnly: true leftPadding: Kirigami.Units.largeSpacing rightPadding: Kirigami.Units.largeSpacing topPadding: Kirigami.Units.largeSpacing width: parent.width horizontalAlignment: root.sentByMe ? Text.AlignRight : Text.AlignLeft wrapMode: Text.Wrap + color: Kirigami.Theme.textColor text: root.messageBody } @@ -101,25 +130,24 @@ Item { } } - MouseArea { - anchors.fill: parent - acceptedButtons: Qt.LeftButton | Qt.RightButton - onClicked: mouse => { - if (mouse.button === Qt.RightButton) { - contextMenu.popup() - } - } - onPressAndHold: contextMenu.popup() - } + Menu { id: contextMenu - + exit: Transition {PropertyAction { target: messageLabel; property: "persistentSelection"; value: false }} MenuItem { text: i18nd("kdeconnect-sms", "Copy Message") enabled: messageLabel.visible onTriggered: root.messageCopyRequested(root.messageBody) } + MenuItem { + text: i18nd("kdeconnect-sms", "Copy Selection") + visible: selectedText != "" + onTriggered: { + root.messageCopyRequested(selectedText) + } + } + } } }