Implement selection across multiple MessageDelegates

BUG: 457669
This commit is contained in:
Tobias Fella
2022-11-26 18:10:10 +01:00
parent 99ad4130d9
commit 405c2c3d2d
3 changed files with 96 additions and 3 deletions

View File

@@ -549,8 +549,66 @@ Kirigami.ScrollablePage {
}
}
}
}
function indexAtRelative(x, y) {
return indexAt(x + contentX, y + contentY)
}
MouseArea {
id: selectionArea
property int selectionStartIndex
property int selectionEndIndex
property int selectionStartPos
property int selectionEndPos
property int upperIndex: selectionStartIndex > selectionEndIndex ? selectionStartIndex : selectionEndIndex
property int upperPos: selectionStartIndex > selectionEndIndex ? selectionStartPos : (selectionStartIndex == selectionEndIndex ? (selectionStartPos > selectionEndPos ? selectionEndPos : selectionStartPos) : selectionEndPos)
property int lowerIndex: selectionStartIndex > selectionEndIndex ? selectionEndIndex : selectionStartIndex
property int lowerPos: selectionStartIndex > selectionEndIndex ? selectionEndPos : (selectionStartIndex == selectionEndIndex ? (selectionStartPos > selectionEndPos ? selectionStartPos : selectionEndPos) : selectionStartPos)
signal selectionChanged
anchors.fill: parent
function indexAndPos(x, y) {
const index = messageListView.indexAtRelative(x, y);
if (index == -1) {
return;
}
const item = messageListView.itemAtIndex(index);
const relItemY = item.y - messageListView.contentY;
const pos = item.positionAt(x, y - relItemY);
return [index, pos]
}
onPressed: {
[selectionEndIndex, selectionEndPos] = indexAndPos(mouse.x, mouse.y);
[selectionStartIndex, selectionStartPos] = indexAndPos(mouse.x, mouse.y);
selectionChanged();
}
onPositionChanged: {
if (!pressed) {
return
}
[selectionEndIndex, selectionEndPos] = indexAndPos(mouse.x, mouse.y);
selectionChanged();
}
}
Kirigami.Action {
onTriggered: {
var text = ""
for (let i = selectionArea.upperIndex; i >= selectionArea.lowerIndex; i--) {
text += messageListView.itemAtIndex(i).selectedText
if (i > selectionArea.lowerIndex) {
text += " "
}
}
Clipboard.saveText(text)
}
shortcut: "Ctrl+C"
}
}
footer: ChatBox {
id: chatBox