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
|
|
|
|
|
|
|
import utils 1.0
|
2021-10-27 21:27:49 +00:00
|
|
|
import shared.controls 1.0
|
2021-10-20 22:47:23 +00:00
|
|
|
|
|
|
|
import StatusQ.Components 0.1
|
|
|
|
|
2021-10-27 21:27:49 +00:00
|
|
|
import shared 1.0
|
|
|
|
import shared.panels 1.0
|
2020-07-14 11:40:58 +00:00
|
|
|
|
|
|
|
Rectangle {
|
|
|
|
id: container
|
|
|
|
|
2022-01-11 23:16:17 +00:00
|
|
|
property QtObject 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
|
2020-11-18 17:14:29 +00:00
|
|
|
property bool shouldHide: false
|
|
|
|
|
2021-07-09 21:00:27 +00:00
|
|
|
Timer {
|
|
|
|
id: timer
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-10-20 17:38:48 +00:00
|
|
|
ListView {
|
|
|
|
id: listView
|
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
|
2020-07-14 11:40:58 +00:00
|
|
|
clip: true
|
|
|
|
|
|
|
|
property int selectedIndex
|
|
|
|
property var selectedItem: selectedIndex == -1 ? null : model[selectedIndex]
|
|
|
|
signal suggestionClicked(var item)
|
|
|
|
|
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
|
|
|
|
color: ListView.isCurrentItem ? Style.current.backgroundHover : Style.current.transparent
|
2021-07-07 20:23:05 +00:00
|
|
|
border.width: 0
|
|
|
|
width: parent.width
|
|
|
|
height: 42
|
|
|
|
radius: Style.current.radius
|
|
|
|
|
2021-10-20 22:47:23 +00:00
|
|
|
StatusSmartIdenticon {
|
2021-07-07 20:23:05 +00:00
|
|
|
id: accountImage
|
|
|
|
anchors.verticalCenter: parent.verticalCenter
|
|
|
|
anchors.left: parent.left
|
|
|
|
anchors.leftMargin: Style.current.smallPadding
|
2021-10-20 22:47:23 +00:00
|
|
|
image.width: 32
|
|
|
|
image.height: 32
|
|
|
|
image.source: model.identicon
|
|
|
|
image.isIdenticon: true
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|