From 98b99e49f230f41909a552b9e986b5da94f72c4c Mon Sep 17 00:00:00 2001 From: Black Hat Date: Tue, 23 Oct 2018 10:44:54 +0800 Subject: [PATCH] Somewhat better autocompletion. --- imports/Spectral/Menu/MessageContextMenu.qml | 1 - imports/Spectral/Panel/RoomPanelInput.qml | 32 ++++++++++++-------- 2 files changed, 20 insertions(+), 13 deletions(-) diff --git a/imports/Spectral/Menu/MessageContextMenu.qml b/imports/Spectral/Menu/MessageContextMenu.qml index 0d44299..973e428 100644 --- a/imports/Spectral/Menu/MessageContextMenu.qml +++ b/imports/Spectral/Menu/MessageContextMenu.qml @@ -37,7 +37,6 @@ Menu { text: "Reply" onTriggered: { - roomPanelInput.clear() roomPanelInput.isReply = true roomPanelInput.replyUserID = model.author.id roomPanelInput.replyEventID = model.eventId diff --git a/imports/Spectral/Panel/RoomPanelInput.qml b/imports/Spectral/Panel/RoomPanelInput.qml index a0b588d..f4dac28 100644 --- a/imports/Spectral/Panel/RoomPanelInput.qml +++ b/imports/Spectral/Panel/RoomPanelInput.qml @@ -18,6 +18,10 @@ Rectangle { property string replyEventID property string replyContent + property bool isAutoCompleting + property var autoCompleteModel + property int autoCompleteBeginPosition + property int autoCompleteEndPosition color: MSettings.darkTheme ? "#303030" : "#fafafa" @@ -37,9 +41,13 @@ Rectangle { id: userAutoComplete + visible: isAutoCompleting + contentItem: ListView { id: userAutoCompleteListView + model: autoCompleteModel + clip: true orientation: ListView.Horizontal @@ -176,10 +184,6 @@ Rectangle { Keys.onReturnPressed: { if (event.modifiers & Qt.ShiftModifier) { insert(cursorPosition, "\n") - } else if (userAutoComplete.visible) { - remove(text.lastIndexOf(" ") + 1, text.length) - insert(cursorPosition, userAutoCompleteListView.currentItem.displayName + " ") - userAutoComplete.visible = false } else { postMessage(text) text = "" @@ -187,17 +191,21 @@ Rectangle { } Keys.onTabPressed: { - if (userAutoComplete.visible) { + if (isAutoCompleting) { if (userAutoCompleteListView.currentIndex + 1 == userAutoCompleteListView.count) userAutoCompleteListView.currentIndex = 0 else userAutoCompleteListView.currentIndex++ } else { - var lastWord = text.substring(0, cursorPosition).split(" ").pop() - if (!lastWord) return - var model = currentRoom.getUsers(lastWord) - if (model.length === 0) return - userAutoCompleteListView.model = model - userAutoComplete.visible = true + autoCompleteBeginPosition = text.substring(0, cursorPosition).lastIndexOf(" ") + 1 + var autoCompletePrefix = text.substring(0, cursorPosition).split(" ").pop() + if (!autoCompletePrefix) return + autoCompleteModel = currentRoom.getUsers(autoCompletePrefix) + if (autoCompleteModel.length === 0) return + isAutoCompleting = true + autoCompleteEndPosition = cursorPosition } + remove(autoCompleteBeginPosition, autoCompleteEndPosition) + autoCompleteEndPosition = autoCompleteBeginPosition + userAutoCompleteListView.currentItem.displayName.length + insert(cursorPosition, userAutoCompleteListView.currentItem.displayName) } onTextChanged: { @@ -205,7 +213,7 @@ Rectangle { repeatTimer.start() currentRoom.cachedInput = text - if (userAutoComplete.visible) userAutoComplete.visible = false + if (cursorPosition < autoCompleteBeginPosition || cursorPosition > autoCompleteEndPosition) isAutoCompleting = false } function postMessage(text) {