matrixion/resources/qml/Completer.qml

191 lines
4.8 KiB
QML
Raw Permalink Normal View History

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 alias count: listView.count
2020-11-20 03:22:36 +03:00
2020-11-24 19:32:45 +03:00
signal completionClicked(string completion)
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;
}
onCompleterNameChanged: {
2020-11-24 04:35:38 +03:00
if (completerName) {
2020-11-20 03:22:36 +03:00
completer = TimelineManager.timeline.input.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: contentItem.childrenRect.width
model: completer
verticalLayoutDirection: popup.bottomToTop ? ListView.BottomToTop : ListView.TopToBottom
2020-11-20 03:22:36 +03:00
2020-11-24 04:35:38 +03:00
delegate: Rectangle {
color: model.index == popup.currentIndex ? colors.highlight : colors.base
height: chooser.childrenRect.height + 4
implicitWidth: 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
2020-11-24 19:32:45 +03:00
onClicked: popup.completionClicked(completer.completionAt(model.index))
2021-01-12 04:22:40 +03:00
Ripple {
rippleTarget: mouseArea
color: Qt.rgba(colors.base.r, colors.base.g, colors.base.b, 0.5)
}
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.centerIn: parent
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
2020-11-20 06:33:11 +03:00
2020-11-24 04:35:38 +03:00
Avatar {
height: 24
width: 24
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
color: model.index == popup.currentIndex ? colors.highlightedText : colors.text
2020-11-20 06:33:11 +03:00
}
2020-11-24 21:06:31 +03:00
Label {
text: "(" + model.userid + ")"
color: model.index == popup.currentIndex ? colors.highlightedText : colors.buttonText
}
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
2020-11-20 06:33:11 +03:00
2020-11-24 04:35:38 +03:00
Label {
text: model.unicode
color: model.index == popup.currentIndex ? colors.highlightedText : colors.text
font: Settings.emojiFont
}
2020-11-20 06:33:11 +03:00
2020-11-24 04:35:38 +03:00
Label {
text: model.shortName
color: model.index == popup.currentIndex ? colors.highlightedText : colors.text
2020-11-20 06:33:11 +03:00
}
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 {
2020-11-24 04:35:38 +03:00
color: colors.base
2020-11-20 03:22:36 +03:00
implicitHeight: popup.contentHeight
implicitWidth: popup.contentWidth
2021-01-23 04:38:09 +03:00
border.color: colors.mid
2020-11-20 03:22:36 +03:00
}
}