matrixion/resources/qml/Completer.qml

299 lines
11 KiB
QML
Raw Normal View History

// SPDX-FileCopyrightText: 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.15
import QtQuick.Controls 2.15
import QtQuick.Layouts 1.15
2020-11-20 03:22:36 +03:00
import im.nheko 1.0
2022-02-21 06:06:49 +03:00
Control {
2020-11-20 03:22:36 +03:00
id: popup
2021-02-23 19:06:21 +03:00
property int avatarHeight: 24
property int avatarWidth: 24
2023-06-02 02:45:24 +03:00
property bool bottomToTop: true
property bool centerRowContent: true
property var completer
property string completerName
property alias count: listView.count
property alias currentIndex: listView.currentIndex
property bool fullWidth: false
property string roomId
property int rowMargin: 0
property int rowSpacing: Nheko.paddingSmall
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
2023-06-02 02:45:24 +03:00
function changeCompleter() {
if (completerName) {
completer = TimelineManager.completerFor(completerName, completerName == "room" ? "" : (popup.roomId != "" ? popup.roomId : room.roomId));
completer.setSearchString("");
} else {
completer = undefined;
}
currentIndex = -1;
}
function currentCompletion() {
if (currentIndex > -1 && currentIndex < listView.count)
return completer.completionAt(currentIndex);
2020-11-24 04:35:38 +03:00
else
2023-06-02 02:45:24 +03:00
return null;
2020-11-24 04:35:38 +03:00
}
function down() {
if (bottomToTop)
up_();
else
down_();
}
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 finishCompletion() {
if (popup.completerName == "room")
popup.completionSelected(listView.itemAtIndex(currentIndex).modelData.roomid);
2022-05-27 17:31:54 +03:00
else if (popup.completerName == "user")
popup.completionSelected(listView.itemAtIndex(currentIndex).modelData.userid);
}
2023-06-02 02:45:24 +03:00
function up() {
if (bottomToTop)
down_();
else
up_();
}
function up_() {
currentIndex = currentIndex - 1;
if (currentIndex == -2)
currentIndex = listView.count - 1;
2020-11-20 03:22:36 +03:00
}
2022-03-01 03:59:06 +03:00
bottomPadding: 1
leftPadding: 1
2023-06-08 02:51:27 +03:00
// Workaround palettes not inheriting for popups
palette: timelineRoot.palette
2022-03-01 03:59:06 +03:00
rightPadding: 1
2023-06-02 02:45:24 +03:00
topPadding: 1
2022-02-21 06:06:49 +03:00
2023-06-02 02:45:24 +03:00
background: Rectangle {
border.color: palette.mid
color: palette.base
}
2022-02-21 06:06:49 +03:00
contentItem: ListView {
2020-11-24 04:35:38 +03:00
id: listView
2020-11-20 03:22:36 +03:00
2023-06-02 02:45:24 +03:00
clip: true
displayMarginBeginning: height / 2
displayMarginEnd: height / 2
highlightFollowsCurrentItem: true
// If we have fewer than 7 items, just use the list view's content height.
2022-02-21 06:06:49 +03:00
// Otherwise, we want to show 7 items. Each item consists of row spacing between rows, row margins
// on each side of a row, 1px of padding above the first item and below the last item, and nominally
// some kind of content height. avatarHeight is used for just about every delegate, so we're using
// that until we find something better. Put is all together and you have the formula below!
2023-06-02 02:45:24 +03:00
implicitHeight: Math.min(contentHeight, 6 * rowSpacing + 7 * (popup.avatarHeight + 2 * rowMargin))
// Broken, see https://bugreports.qt.io/browse/QTBUG-102811
//reuseItems: true
2022-02-21 06:06:49 +03:00
implicitWidth: listView.contentItem.childrenRect.width
2020-11-24 04:35:38 +03:00
model: completer
pixelAligned: true
2023-06-02 02:45:24 +03:00
spacing: rowSpacing
verticalLayoutDirection: popup.bottomToTop ? ListView.BottomToTop : ListView.TopToBottom
2023-02-08 22:19:35 +03:00
2020-11-24 04:35:38 +03:00
delegate: Rectangle {
property variant modelData: model
ListView.delayRemove: true
color: model.index == popup.currentIndex ? palette.highlight : palette.base
height: (chooser.child?.implicitHeight ?? 0) + 2 * popup.rowMargin
2022-03-01 03:59:06 +03:00
implicitWidth: fullWidth ? ListView.view.width : chooser.child.implicitWidth + 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
2023-06-02 02:45:24 +03:00
onClicked: {
2023-06-02 02:45:24 +03:00
popup.completionClicked(completer.completionAt(model.index));
if (popup.completerName == "room")
popup.completionSelected(model.roomid);
else if (popup.completerName == "user")
popup.completionSelected(model.userid);
}
2023-06-02 02:45:24 +03:00
onPositionChanged: if (!listView.moving && !deadTimer.running)
popup.currentIndex = model.index
}
Ripple {
color: Qt.rgba(palette.base.r, palette.base.g, palette.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
anchors.fill: parent
anchors.margins: popup.rowMargin
enabled: false
2023-06-02 02:45:24 +03:00
roleValue: popup.completerName
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
2022-05-27 17:31:54 +03:00
anchors.centerIn: centerRowContent ? parent : undefined
spacing: rowSpacing
2020-11-20 06:33:11 +03:00
2020-11-24 04:35:38 +03:00
Avatar {
displayName: model.displayName
2022-05-27 17:31:54 +03:00
enabled: false
2023-06-02 02:45:24 +03:00
height: popup.avatarHeight
url: model.avatarUrl.replace("mxc://", "image://MxcImage/")
userid: model.userid
width: popup.avatarWidth
2020-11-24 04:35:38 +03:00
}
Label {
color: model.index == popup.currentIndex ? palette.highlightedText : palette.text
2023-06-02 02:45:24 +03:00
text: model.displayName
2020-11-20 06:33:11 +03:00
}
2020-11-24 21:06:31 +03:00
Label {
color: model.index == popup.currentIndex ? palette.highlightedText : palette.buttonText
2023-06-02 02:45:24 +03:00
text: "(" + model.userid + ")"
2020-11-24 21:06:31 +03:00
}
2020-11-20 03:22:36 +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 {
color: model.index == popup.currentIndex ? palette.highlightedText : palette.text
2020-11-24 04:35:38 +03:00
font: Settings.emojiFont
2023-06-02 02:45:24 +03:00
text: model.unicode
visible: !!model.unicode
2020-11-24 04:35:38 +03:00
}
Avatar {
2023-06-02 02:45:24 +03:00
crop: false
displayName: model.shortcode
2023-06-02 02:45:24 +03:00
enabled: false
height: popup.avatarHeight
//userid: model.shortcode
url: (model.url ? model.url : "").replace("mxc://", "image://MxcImage/")
2023-06-02 02:45:24 +03:00
visible: !model.unicode
width: popup.avatarWidth
2020-11-20 06:33:11 +03:00
}
Label {
Layout.leftMargin: Nheko.paddingSmall
Layout.rightMargin: Nheko.paddingSmall
color: model.index == popup.currentIndex ? palette.highlightedText : palette.text
2023-06-02 02:45:24 +03:00
text: model.shortcode
}
Label {
color: model.index == popup.currentIndex ? palette.highlightedText : palette.buttonText
2023-06-02 02:45:24 +03:00
text: "(" + model.packname + ")"
}
}
}
DelegateChoice {
roleValue: "command"
RowLayout {
id: del
anchors.centerIn: parent
spacing: rowSpacing
Label {
color: model.index == popup.currentIndex ? palette.highlightedText : palette.text
font.bold: true
2023-06-02 02:45:24 +03:00
text: model.name
}
Label {
color: model.index == popup.currentIndex ? palette.highlightedText : palette.buttonText
2023-06-02 02:45:24 +03:00
text: model.description
}
}
}
DelegateChoice {
roleValue: "room"
RowLayout {
id: del
anchors.centerIn: centerRowContent ? parent : undefined
spacing: rowSpacing
Avatar {
displayName: model.roomName
2023-06-02 02:45:24 +03:00
enabled: false
height: popup.avatarHeight
roomid: model.roomid
url: model.avatarUrl.replace("mxc://", "image://MxcImage/")
2023-06-02 02:45:24 +03:00
width: popup.avatarWidth
}
Label {
color: model.index == popup.currentIndex ? palette.highlightedText : palette.text
font.italic: model.isTombstoned
2023-06-02 02:45:24 +03:00
font.pixelSize: popup.avatarHeight * 0.5
text: model.roomName
2021-06-06 00:20:23 +03:00
textFormat: Text.RichText
}
}
}
DelegateChoice {
roleValue: "roomAliases"
RowLayout {
id: del
anchors.centerIn: parent
spacing: rowSpacing
Avatar {
displayName: model.roomName
2023-06-02 02:45:24 +03:00
enabled: false
height: popup.avatarHeight
roomid: model.roomid
url: model.avatarUrl.replace("mxc://", "image://MxcImage/")
2023-06-02 02:45:24 +03:00
width: popup.avatarWidth
}
Label {
color: model.index == popup.currentIndex ? palette.highlightedText : palette.text
font.italic: model.isTombstoned
2023-06-02 02:45:24 +03:00
text: model.roomName
2021-06-06 00:20:23 +03:00
textFormat: Text.RichText
}
Label {
color: model.index == popup.currentIndex ? palette.highlightedText : palette.buttonText
2023-06-02 02:45:24 +03:00
text: "(" + model.roomAlias + ")"
2021-06-06 00:20:23 +03:00
textFormat: Text.RichText
}
}
}
2020-11-20 03:22:36 +03:00
}
}
2023-06-02 02:45:24 +03:00
onContentYChanged: deadTimer.restart()
2020-11-20 03:22:36 +03:00
2023-06-02 02:45:24 +03:00
Timer {
id: deadTimer
2020-11-20 03:22:36 +03:00
2023-06-02 02:45:24 +03:00
interval: 50
}
2020-11-20 03:22:36 +03:00
}
2023-06-02 02:45:24 +03:00
onCompleterNameChanged: changeCompleter()
onRoomIdChanged: changeCompleter()
2020-11-20 03:22:36 +03:00
}