Somewhat better autocompletion.

This commit is contained in:
Black Hat 2018-10-23 10:44:54 +08:00
parent d9e778fe0c
commit 98b99e49f2
2 changed files with 20 additions and 13 deletions

View File

@ -37,7 +37,6 @@ Menu {
text: "Reply"
onTriggered: {
roomPanelInput.clear()
roomPanelInput.isReply = true
roomPanelInput.replyUserID = model.author.id
roomPanelInput.replyEventID = model.eventId

View File

@ -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) {