2020-07-14 11:40:58 +00:00
|
|
|
/*
|
|
|
|
Copyright (C) 2011 Jocelyn Turcotte <turcotte.j@gmail.com>
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU Library General Public
|
|
|
|
License as published by the Free Software Foundation; either
|
|
|
|
version 2 of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
Library General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Library General Public License
|
|
|
|
along with this program; see the file COPYING.LIB. If not, write to
|
|
|
|
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
|
|
|
Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import QtQuick 2.13
|
|
|
|
import QtQuick.Controls 2.13
|
|
|
|
import QtGraphicalEffects 1.13
|
2021-07-09 21:00:27 +00:00
|
|
|
import QtQml.Models 2.13
|
2021-09-28 15:04:06 +00:00
|
|
|
|
2022-07-14 11:03:36 +00:00
|
|
|
import StatusQ.Core 0.1
|
2021-10-20 22:47:23 +00:00
|
|
|
import StatusQ.Components 0.1
|
|
|
|
|
2022-07-14 11:03:36 +00:00
|
|
|
import utils 1.0
|
2021-10-27 21:27:49 +00:00
|
|
|
import shared 1.0
|
2022-07-14 11:03:36 +00:00
|
|
|
import shared.controls 1.0
|
2021-10-27 21:27:49 +00:00
|
|
|
import shared.panels 1.0
|
2022-03-16 21:20:03 +00:00
|
|
|
import shared.controls.chat 1.0
|
2020-07-14 11:40:58 +00:00
|
|
|
|
|
|
|
Rectangle {
|
|
|
|
id: container
|
|
|
|
|
2022-02-08 12:08:02 +00:00
|
|
|
property var model
|
2020-07-14 11:40:58 +00:00
|
|
|
property Item delegate
|
|
|
|
property alias suggestionsModel: filterItem.model
|
|
|
|
property alias filter: filterItem.filter
|
2021-07-09 21:00:27 +00:00
|
|
|
property alias formattedPlainTextFilter: filterItem.formattedFilter
|
2021-08-25 12:21:48 +00:00
|
|
|
property alias suggestionFilter: filterItem
|
2020-07-14 11:40:58 +00:00
|
|
|
property alias property: filterItem.property
|
2020-07-20 16:48:23 +00:00
|
|
|
property int cursorPosition
|
|
|
|
signal itemSelected(var item, int lastAtPosition, int lastCursorPosition)
|
2020-10-20 17:38:48 +00:00
|
|
|
property alias listView: listView
|
2022-02-21 19:51:41 +00:00
|
|
|
property var inputField
|
2020-11-18 17:14:29 +00:00
|
|
|
property bool shouldHide: false
|
|
|
|
|
2021-07-09 21:00:27 +00:00
|
|
|
onFormattedPlainTextFilterChanged: {
|
|
|
|
// We need to callLater because the sort needs to happen before setting the index
|
|
|
|
Qt.callLater(function () {
|
|
|
|
listView.currentIndex = 0
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-11-18 17:14:29 +00:00
|
|
|
onCursorPositionChanged: {
|
|
|
|
if (shouldHide) {
|
|
|
|
shouldHide = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function hide() {
|
|
|
|
shouldHide = true
|
2021-09-21 14:27:23 +00:00
|
|
|
formattedPlainTextFilter = "";
|
2020-11-18 17:14:29 +00:00
|
|
|
}
|
2020-07-14 11:40:58 +00:00
|
|
|
|
2020-10-20 17:38:48 +00:00
|
|
|
function selectCurrentItem() {
|
2021-07-09 21:00:27 +00:00
|
|
|
container.itemSelected(mentionsListDelegate.items.get(listView.currentIndex).model, filterItem.lastAtPosition, filterItem.cursorPosition)
|
2020-10-20 17:38:48 +00:00
|
|
|
}
|
2020-07-14 11:40:58 +00:00
|
|
|
|
2021-01-21 15:52:05 +00:00
|
|
|
onVisibleChanged: {
|
|
|
|
if (visible && listView.currentIndex === -1) {
|
|
|
|
// If the previous selection was made using the mouse, the currentIndex was changed to -1
|
|
|
|
// We change it back to 0 so that it can be used to select using the keyboard
|
|
|
|
listView.currentIndex = 0
|
|
|
|
}
|
2022-02-21 19:51:41 +00:00
|
|
|
if (visible) {
|
|
|
|
listView.forceActiveFocus();
|
|
|
|
}
|
2021-01-21 15:52:05 +00:00
|
|
|
}
|
|
|
|
|
2020-07-14 11:40:58 +00:00
|
|
|
z: parent.z + 100
|
2020-11-18 17:14:29 +00:00
|
|
|
visible: !shouldHide && filter.length > 0 && suggestionsModel.count > 0
|
2021-02-08 16:29:14 +00:00
|
|
|
height: Math.min(400, listView.contentHeight + Style.current.padding)
|
2020-10-20 17:38:48 +00:00
|
|
|
|
2020-07-14 11:40:58 +00:00
|
|
|
opacity: visible ? 1.0 : 0
|
|
|
|
Behavior on opacity {
|
|
|
|
NumberAnimation { }
|
|
|
|
}
|
|
|
|
|
2020-11-18 17:14:29 +00:00
|
|
|
color: Style.current.background
|
2020-10-20 17:38:48 +00:00
|
|
|
radius: Style.current.radius
|
|
|
|
border.width: 0
|
2020-07-14 11:40:58 +00:00
|
|
|
layer.enabled: true
|
|
|
|
layer.effect: DropShadow{
|
|
|
|
width: container.width
|
|
|
|
height: container.height
|
|
|
|
x: container.x
|
|
|
|
y: container.y + 10
|
|
|
|
visible: container.visible
|
|
|
|
source: container
|
|
|
|
horizontalOffset: 0
|
|
|
|
verticalOffset: 2
|
|
|
|
radius: 10
|
|
|
|
samples: 15
|
|
|
|
color: "#22000000"
|
|
|
|
}
|
|
|
|
|
2021-10-01 15:58:36 +00:00
|
|
|
SuggestionFilterPanel {
|
2020-07-14 11:40:58 +00:00
|
|
|
id: filterItem
|
|
|
|
sourceModel: container.model
|
2020-07-20 16:48:23 +00:00
|
|
|
cursorPosition: container.cursorPosition
|
2020-07-14 11:40:58 +00:00
|
|
|
}
|
|
|
|
|
2022-07-14 11:03:36 +00:00
|
|
|
StatusListView {
|
2020-10-20 17:38:48 +00:00
|
|
|
id: listView
|
2022-08-10 06:57:34 +00:00
|
|
|
objectName: "suggestionBoxList"
|
2021-07-07 20:23:05 +00:00
|
|
|
model: mentionsListDelegate
|
2020-10-20 17:38:48 +00:00
|
|
|
keyNavigationEnabled: true
|
|
|
|
anchors.fill: parent
|
|
|
|
anchors.topMargin: Style.current.halfPadding
|
|
|
|
anchors.leftMargin: Style.current.halfPadding
|
|
|
|
anchors.rightMargin: Style.current.halfPadding
|
|
|
|
anchors.bottomMargin: Style.current.halfPadding
|
2022-02-21 19:51:41 +00:00
|
|
|
Keys.priority: Keys.AfterItem
|
|
|
|
Keys.forwardTo: container.inputField
|
|
|
|
Keys.onPressed: {
|
|
|
|
if (event.key === Qt.Key_Enter || event.key === Qt.Key_Return) {
|
|
|
|
container.itemSelected(mentionsListDelegate.items.get(listView.currentIndex).model, filterItem.lastAtPosition, filterItem.cursorPosition)
|
|
|
|
} else if (event.key === Qt.Key_Escape) {
|
|
|
|
container.hide();
|
|
|
|
} else if (event.key !== Qt.Key_Up && event.key !== Qt.Key_Down) {
|
|
|
|
event.accepted = false;
|
|
|
|
}
|
|
|
|
}
|
2020-07-14 11:40:58 +00:00
|
|
|
|
2021-07-07 20:23:05 +00:00
|
|
|
DelegateModelGeneralized {
|
|
|
|
id: mentionsListDelegate
|
2021-07-09 21:00:27 +00:00
|
|
|
|
2021-07-07 20:23:05 +00:00
|
|
|
lessThan: [
|
|
|
|
function(left, right) {
|
2021-07-09 19:43:10 +00:00
|
|
|
// Priorities:
|
|
|
|
// 1. Match at the start
|
|
|
|
// 2. Match in the start of one of the three words
|
|
|
|
// 3. Alphabetical order (also in case of multiple matches at the start of the name)
|
2021-07-07 20:23:05 +00:00
|
|
|
|
2021-07-09 19:43:10 +00:00
|
|
|
const leftProp = left[container.property.find(p => !!left[p])].toLowerCase()
|
|
|
|
const rightProp = right[container.property.find(p => !!right[p])].toLowerCase()
|
2020-10-20 17:38:48 +00:00
|
|
|
|
2021-07-09 21:00:27 +00:00
|
|
|
if (!formattedPlainTextFilter) {
|
|
|
|
return leftProp < rightProp
|
|
|
|
}
|
|
|
|
|
2021-07-09 19:43:10 +00:00
|
|
|
// check the start of the string
|
|
|
|
const leftMatches = leftProp.startsWith(formattedPlainTextFilter)
|
|
|
|
|
|
|
|
const rightMatches = rightProp.startsWith(formattedPlainTextFilter)
|
|
|
|
|
|
|
|
if (leftMatches === true && rightMatches === true) {
|
|
|
|
return leftProp < rightProp
|
|
|
|
}
|
|
|
|
|
|
|
|
if (leftMatches || rightMatches) {
|
|
|
|
return leftMatches && !rightMatches
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for the start of the 3 word names
|
|
|
|
let leftMatchesIndex = leftProp.indexOf(" " + formattedPlainTextFilter)
|
|
|
|
let rightMatchesIndex = rightProp.indexOf(" " + formattedPlainTextFilter)
|
|
|
|
if (leftMatchesIndex === rightMatchesIndex) {
|
|
|
|
return leftProp < rightProp
|
|
|
|
}
|
|
|
|
|
|
|
|
// Change index so that -1 is not the smallest
|
|
|
|
if (leftMatchesIndex === -1) {
|
|
|
|
leftMatchesIndex = 999
|
|
|
|
}
|
|
|
|
if (rightMatchesIndex === -1) {
|
|
|
|
rightMatchesIndex = 999
|
|
|
|
}
|
|
|
|
|
|
|
|
return leftMatchesIndex < rightMatchesIndex
|
2020-10-20 17:38:48 +00:00
|
|
|
}
|
2021-07-07 20:23:05 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
model: container.suggestionsModel
|
|
|
|
|
|
|
|
delegate: Rectangle {
|
2021-07-09 21:00:27 +00:00
|
|
|
id: itemDelegate
|
2022-08-10 06:57:34 +00:00
|
|
|
objectName: model.name
|
2021-07-09 21:00:27 +00:00
|
|
|
color: ListView.isCurrentItem ? Style.current.backgroundHover : Style.current.transparent
|
2021-07-07 20:23:05 +00:00
|
|
|
border.width: 0
|
2022-12-01 10:24:25 +00:00
|
|
|
width: ListView.view.width
|
2021-07-07 20:23:05 +00:00
|
|
|
height: 42
|
|
|
|
radius: Style.current.radius
|
|
|
|
|
2022-03-16 21:20:03 +00:00
|
|
|
UserImage {
|
2021-07-07 20:23:05 +00:00
|
|
|
id: accountImage
|
|
|
|
anchors.verticalCenter: parent.verticalCenter
|
|
|
|
anchors.left: parent.left
|
|
|
|
anchors.leftMargin: Style.current.smallPadding
|
2022-03-16 21:20:03 +00:00
|
|
|
imageWidth: 32
|
|
|
|
imageHeight: 32
|
|
|
|
|
|
|
|
name: model.name
|
|
|
|
pubkey: model.publicKey
|
2022-03-30 15:30:28 +00:00
|
|
|
image: model.icon
|
2022-03-16 21:20:03 +00:00
|
|
|
interactive: false
|
2021-07-07 20:23:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
StyledText {
|
|
|
|
text: model[container.property.find(p => !!model[p])]
|
|
|
|
color: Style.current.textColor
|
|
|
|
anchors.verticalCenter: parent.verticalCenter
|
|
|
|
anchors.left: accountImage.right
|
|
|
|
anchors.leftMargin: Style.current.smallPadding
|
|
|
|
font.pixelSize: 15
|
|
|
|
}
|
|
|
|
|
|
|
|
MouseArea {
|
|
|
|
cursorShape: Qt.PointingHandCursor
|
|
|
|
anchors.fill: parent
|
|
|
|
hoverEnabled: true
|
|
|
|
onEntered: {
|
2021-07-09 21:00:27 +00:00
|
|
|
listView.currentIndex = itemDelegate.DelegateModel.itemsIndex
|
2021-07-07 20:23:05 +00:00
|
|
|
}
|
|
|
|
onClicked: {
|
|
|
|
container.itemSelected(model, filterItem.lastAtPosition, filterItem.cursorPosition)
|
|
|
|
}
|
2020-07-14 11:40:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|