matrixion/resources/qml/Completer.qml

284 lines
8.3 KiB
QML
Raw Normal View History

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