nheko/resources/qml/Completer.qml

285 lines
8.5 KiB
QML
Raw Normal View History

2021-03-05 00:35:15 +01:00
// SPDX-FileCopyrightText: 2021 Nheko Contributors
2021-03-14 02:45:20 +01:00
//
2021-03-05 00:35:15 +01:00
// SPDX-License-Identifier: GPL-3.0-or-later
2021-01-12 02:22:40 +01:00
import "./ui"
import QtQuick 2.9
2020-11-20 01:22:36 +01:00
import QtQuick.Controls 2.3
import QtQuick.Layouts 1.2
2020-11-20 01:22:36 +01:00
import im.nheko 1.0
Popup {
id: popup
property int currentIndex: -1
property string completerName
property var completer
2020-11-24 02:35:38 +01:00
property bool bottomToTop: true
property bool fullWidth: false
2021-02-24 09:08:01 +01:00
property bool centerRowContent: true
2021-02-23 17:06:21 +01:00
property int avatarHeight: 24
property int avatarWidth: 24
property int rowMargin: 0
property int rowSpacing: 5
property alias count: listView.count
2020-11-20 01:22:36 +01:00
2020-11-24 17:32:45 +01:00
signal completionClicked(string completion)
signal completionSelected(string id)
2020-11-24 17:32:45 +01:00
2020-11-20 01:22:36 +01:00
function up() {
2020-11-24 02:35:38 +01:00
if (bottomToTop)
down_();
else
up_();
}
function down() {
if (bottomToTop)
up_();
else
down_();
}
function up_() {
2020-11-20 01:22:36 +01:00
currentIndex = currentIndex - 1;
if (currentIndex == -2)
2020-11-24 02:35:38 +01:00
currentIndex = listView.count - 1;
2020-11-20 01:22:36 +01:00
}
2020-11-24 02:35:38 +01:00
function down_() {
2020-11-20 01:22:36 +01:00
currentIndex = currentIndex + 1;
2020-11-24 02:35:38 +01:00
if (currentIndex >= listView.count)
2020-11-20 01:22:36 +01:00
currentIndex = -1;
}
function currentCompletion() {
2020-11-24 02:35:38 +01:00
if (currentIndex > -1 && currentIndex < listView.count)
2020-11-20 01:22:36 +01:00
return completer.completionAt(currentIndex);
else
return null;
}
function finishCompletion() {
if (popup.completerName == "room")
popup.completionSelected(listView.itemAtIndex(currentIndex).modelData.roomid);
}
2020-11-20 01:22:36 +01:00
onCompleterNameChanged: {
2020-11-24 02:35:38 +01:00
if (completerName) {
if (completerName == "user")
2021-07-17 22:56:56 +02:00
completer = TimelineManager.completerFor(completerName, room.roomId);
else
completer = TimelineManager.completerFor(completerName);
2020-11-24 02:35:38 +01:00
completer.setSearchString("");
} else {
2020-11-20 02:38:08 +01:00
completer = undefined;
2020-11-24 02:35:38 +01:00
}
2020-11-20 01:22:36 +01:00
}
padding: 1
2020-11-20 01:22:36 +01:00
onAboutToShow: currentIndex = -1
height: listView.contentHeight + 2 // + 2 for the padding on top and bottom
2020-11-20 01:22:36 +01:00
2020-11-24 02:35:38 +01:00
ListView {
id: listView
2020-11-20 01:22:36 +01:00
2020-11-24 02:35:38 +01:00
anchors.fill: parent
implicitWidth: fullWidth ? parent.width : contentItem.childrenRect.width
2020-11-24 02:35:38 +01:00
model: completer
verticalLayoutDirection: popup.bottomToTop ? ListView.BottomToTop : ListView.TopToBottom
spacing: rowSpacing
pixelAligned: true
2020-11-20 01:22:36 +01:00
2020-11-24 02:35:38 +01:00
delegate: Rectangle {
property variant modelData: model
2021-05-13 08:23:56 +02:00
color: model.index == popup.currentIndex ? Nheko.colors.highlight : Nheko.colors.base
height: chooser.childrenRect.height + 2 * popup.rowMargin
implicitWidth: fullWidth ? popup.width : chooser.childrenRect.width + 4
2020-11-20 01:22:36 +01:00
2020-11-24 17:32:45 +01:00
MouseArea {
2021-01-12 02:22:40 +01:00
id: mouseArea
2020-11-24 17:32:45 +01:00
anchors.fill: parent
hoverEnabled: true
onPositionChanged: popup.currentIndex = model.index
onClicked: {
popup.completionClicked(completer.completionAt(model.index));
if (popup.completerName == "room")
popup.completionSelected(model.roomid);
}
2021-01-12 02:22:40 +01:00
Ripple {
rippleTarget: mouseArea
2021-05-13 08:23:56 +02:00
color: Qt.rgba(Nheko.colors.base.r, Nheko.colors.base.g, Nheko.colors.base.b, 0.5)
2021-01-12 02:22:40 +01:00
}
2020-11-24 17:32:45 +01:00
}
2020-11-24 02:35:38 +01:00
DelegateChooser {
id: chooser
2020-11-20 01:22:36 +01:00
2020-11-24 02:35:38 +01:00
roleValue: popup.completerName
anchors.fill: parent
anchors.margins: popup.rowMargin
2020-11-20 04:33:11 +01:00
2020-11-24 02:35:38 +01:00
DelegateChoice {
roleValue: "user"
2020-11-20 04:33:11 +01:00
2020-11-24 02:35:38 +01:00
RowLayout {
id: del
2020-11-20 04:33:11 +01:00
2020-11-24 02:35:38 +01:00
anchors.centerIn: parent
spacing: rowSpacing
2020-11-20 04:33:11 +01:00
2020-11-24 02:35:38 +01:00
Avatar {
2021-02-23 17:06:21 +01:00
height: popup.avatarHeight
width: popup.avatarWidth
2020-11-24 02:35:38 +01:00
displayName: model.displayName
userid: model.userid
2020-11-24 02:35:38 +01:00
url: model.avatarUrl.replace("mxc://", "image://MxcImage/")
2021-01-12 02:02:39 +01:00
onClicked: popup.completionClicked(completer.completionAt(model.index))
2020-11-24 02:35:38 +01:00
}
2020-11-20 04:33:11 +01:00
2020-11-24 02:35:38 +01:00
Label {
text: model.displayName
2021-05-13 08:23:56 +02:00
color: model.index == popup.currentIndex ? Nheko.colors.highlightedText : Nheko.colors.text
2020-11-20 04:33:11 +01:00
}
2020-11-24 19:06:31 +01:00
Label {
text: "(" + model.userid + ")"
2021-05-13 08:23:56 +02:00
color: model.index == popup.currentIndex ? Nheko.colors.highlightedText : Nheko.colors.buttonText
2020-11-24 19:06:31 +01:00
}
2020-11-20 01:22:36 +01:00
}
2020-11-24 02:35:38 +01:00
}
2020-11-20 04:33:11 +01:00
2020-11-24 02:35:38 +01:00
DelegateChoice {
roleValue: "emoji"
2020-11-20 04:33:11 +01:00
2020-11-24 02:35:38 +01:00
RowLayout {
id: del
2020-11-20 04:33:11 +01:00
2020-11-24 02:35:38 +01:00
anchors.centerIn: parent
spacing: rowSpacing
2020-11-20 04:33:11 +01:00
2020-11-24 02:35:38 +01:00
Label {
text: model.unicode
2021-05-13 08:23:56 +02:00
color: model.index == popup.currentIndex ? Nheko.colors.highlightedText : Nheko.colors.text
2020-11-24 02:35:38 +01:00
font: Settings.emojiFont
}
2020-11-20 04:33:11 +01:00
2020-11-24 02:35:38 +01:00
Label {
text: model.shortName
2021-05-13 08:23:56 +02:00
color: model.index == popup.currentIndex ? Nheko.colors.highlightedText : Nheko.colors.text
2020-11-20 04:33:11 +01:00
}
2020-11-20 01:22:36 +01:00
}
}
DelegateChoice {
roleValue: "room"
RowLayout {
id: del
anchors.centerIn: centerRowContent ? parent : undefined
spacing: rowSpacing
Avatar {
2021-02-23 17:06:21 +01:00
height: popup.avatarHeight
width: popup.avatarWidth
displayName: model.roomName
roomid: model.roomid
url: model.avatarUrl.replace("mxc://", "image://MxcImage/")
onClicked: {
popup.completionClicked(completer.completionAt(model.index));
popup.completionSelected(model.roomid);
}
}
Label {
text: model.roomName
font.pixelSize: popup.avatarHeight * 0.5
2021-05-13 08:23:56 +02:00
color: model.index == popup.currentIndex ? Nheko.colors.highlightedText : Nheko.colors.text
2021-06-05 23:20:23 +02:00
textFormat: Text.RichText
}
}
}
DelegateChoice {
roleValue: "roomAliases"
RowLayout {
id: del
anchors.centerIn: parent
spacing: rowSpacing
Avatar {
2021-02-23 17:06:21 +01:00
height: popup.avatarHeight
width: popup.avatarWidth
displayName: model.roomName
roomid: model.roomid
url: model.avatarUrl.replace("mxc://", "image://MxcImage/")
onClicked: popup.completionClicked(completer.completionAt(model.index))
}
Label {
text: model.roomName
2021-05-13 08:23:56 +02:00
color: model.index == popup.currentIndex ? Nheko.colors.highlightedText : Nheko.colors.text
2021-06-05 23:20:23 +02:00
textFormat: Text.RichText
}
Label {
text: "(" + model.roomAlias + ")"
2021-05-13 08:23:56 +02:00
color: model.index == popup.currentIndex ? Nheko.colors.highlightedText : Nheko.colors.buttonText
2021-06-05 23:20:23 +02:00
textFormat: Text.RichText
}
}
}
2020-11-20 01:22:36 +01:00
}
}
}
enter: Transition {
NumberAnimation {
property: "opacity"
from: 0
to: 1
duration: 100
}
}
exit: Transition {
NumberAnimation {
property: "opacity"
from: 1
to: 0
duration: 100
}
}
background: Rectangle {
2021-05-13 08:23:56 +02:00
color: Nheko.colors.base
2020-11-20 01:22:36 +01:00
implicitHeight: popup.contentHeight
implicitWidth: popup.contentWidth
2021-05-13 08:23:56 +02:00
border.color: Nheko.colors.mid
2020-11-20 01:22:36 +01:00
}
}