Remove message text selection hack

This commit is contained in:
miruka
2020-03-24 11:26:17 -04:00
parent f148837fae
commit 710dba09ec
6 changed files with 174 additions and 366 deletions

View File

@@ -19,120 +19,26 @@ TextEdit {
onLinkActivated: Qt.openUrlExternally(link)
Component.onCompleted: updateSelection()
// If index is a whole number, the label will get two \n before itself
// in container.joinedSelection. If it's a decimal number, if gets one \n.
property real index
property HSelectableLabelContainer container
property bool selectable: true
function updateSelection() {
if (! selectable && label.selectedText) {
label.deselect()
updateContainerSelectedTexts()
return
}
if (! selectable) return
if (! container.reversed &&
container.selectionStart <= container.selectionEnd ||
container.reversed &&
container.selectionStart > container.selectionEnd)
{
var first = container.selectionStart
var firstPos = container.selectionStartPosition
var last = container.selectionEnd
var lastPos = container.selectionEndPosition
} else {
var first = container.selectionEnd
var firstPos = container.selectionEndPosition
var last = container.selectionStart
var lastPos = container.selectionStartPosition
}
if (first === index && last === index) {
select(
label.positionAt(firstPos.x, firstPos.y),
label.positionAt(lastPos.x, lastPos.y),
)
} else if ((! container.reversed && first < index && index < last) ||
(container.reversed && first > index && index > last))
{
label.selectAll()
} else if (first === index) {
label.select(positionAt(firstPos.x, firstPos.y), length)
} else if (last === index) {
label.select(0, positionAt(lastPos.x, lastPos.y))
} else {
label.deselect()
}
updateContainerSelectedTexts()
}
function updateContainerSelectedTexts() {
container.selectedTexts[index] = selectedText
container.selectedTextsChanged()
}
function selectWordAt(position) {
container.clearSelection()
label.cursorPosition = positionAt(position.x, position.y)
label.selectWord()
updateContainerSelectedTexts()
}
function selectAllText() {
container.clearSelection()
label.selectAll()
updateContainerSelectedTexts()
}
Connections {
target: container
onSelectionInfoChanged: updateSelection()
onDeselectAll: deselect()
}
DropArea {
anchors.fill: parent
onPositionChanged: {
if (! container.selecting) {
container.clearSelection()
container.selectionStart = index
container.selectionStartPosition = Qt.point(drag.x, drag.y)
container.selecting = true
} else {
container.selectionEnd = index
container.selectionEndPosition = Qt.point(drag.x, drag.y)
}
}
}
TapHandler {
acceptedButtons: Qt.LeftButton
onTapped: {
tapCount === 2 ? selectWordAt(eventPoint.position) :
tapCount === 3 ? selectAllText() :
container.clearSelection()
}
}
PointHandler {
onActiveChanged:
active ? container.dragStarted() : container.dragStopped()
onPointChanged: container.dragPointChanged(point)
}
// XXX
// TapHandler {
// acceptedButtons: Qt.LeftButton
// onTapped: {
// tapCount === 2 ? selectWordAt(eventPoint.position) :
// tapCount === 3 ? selectAllText() :
// null
// }
// }
MouseArea {
anchors.fill: label

View File

@@ -1,92 +0,0 @@
// SPDX-License-Identifier: LGPL-3.0-or-later
import QtQuick 2.12
import Clipboard 0.1
FocusScope {
signal deselectAll()
signal dragStarted()
signal dragStopped()
signal dragPointChanged(var eventPoint)
property bool reversed: false
property bool selecting: false
property real selectionStart: -1
property real selectionEnd: -1
property point selectionStartPosition: Qt.point(-1, -1)
property point selectionEndPosition: Qt.point(-1, -1)
property var selectedTexts: ({})
readonly property var selectionInfo: [
selectionStart, selectionStartPosition,
selectionEnd, selectionEndPosition,
]
readonly property string joinedSelection: {
const toCopy = []
for (const key of Object.keys(selectedTexts).sort()) {
if (! selectedTexts[key]) continue
// For some dumb reason, Object.keys convert the floats to strings
toCopy.push(Number.isInteger(parseFloat(key)) ? "\n\n" : "\n")
toCopy.push(selectedTexts[key])
}
if (reversed) toCopy.reverse()
return toCopy.join("").trim()
}
onJoinedSelectionChanged:
if (joinedSelection) Clipboard.selection = joinedSelection
onDragStarted: {
draggedItem.Drag.active = true
}
onDragStopped: {
draggedItem.Drag.drop()
draggedItem.Drag.active = false
selecting = false
}
onDragPointChanged: {
const pos = mapFromItem(
mainUI, eventPoint.scenePosition.x, eventPoint.scenePosition.y,
)
draggedItem.x = pos.x
draggedItem.y = pos.y
}
function clearSelection() {
selecting = false
selectionStart = -1
selectionEnd = -1
selectionStartPosition = Qt.point(-1, -1)
selectionEndPosition = Qt.point(-1, -1)
deselectAll()
}
// PointHandler and TapHandler won't activate if the press occurs inside
// a label child, so we need a Point/TapHandler inside them too.
PointHandler {
// We don't use a DragHandler because they have an unchangable minimum
// drag distance before they activate.
id: pointHandler
onActiveChanged: active ? dragStarted() : dragStopped()
onPointChanged: dragPointChanged(point)
}
TapHandler {
acceptedButtons: Qt.LeftButton
onTapped: clearSelection()
}
// This item will trigger the children labels's DropAreas
Item { id: draggedItem }
}