107 lines
2.7 KiB
QML
107 lines
2.7 KiB
QML
import QtQuick 2.2
|
|
import QtQuick.Controls 2.2
|
|
import Nymea 1.0
|
|
|
|
Rectangle {
|
|
id: root
|
|
border.width: 1
|
|
border.color: app.foregroundColor
|
|
color: app.backgroundColor
|
|
height: (Math.min(model.count, 10) * d.entryHeight) + (border.width * 2)
|
|
width: 200
|
|
x: textArea.cursorRectangle.x
|
|
y: textArea.cursorRectangle.y + textArea.cursorRectangle.height
|
|
|
|
visible: model.count > 0 && !d.hidden
|
|
&& (model.filter.length >= 3 || d.manuallyInvoked)
|
|
|
|
property TextArea textArea: null
|
|
property CompletionModel model: null
|
|
property alias font: dummyLabel.font
|
|
|
|
signal complete(int index)
|
|
|
|
Connections {
|
|
target: root.model
|
|
onCountChanged: {
|
|
d.hidden = false;
|
|
d.currentIndex = 0;
|
|
if (root.model.count == 0) {
|
|
d.manuallyInvoked = false;
|
|
}
|
|
}
|
|
}
|
|
|
|
readonly property alias currentIndex: d.currentIndex
|
|
|
|
function next() {
|
|
d.currentIndex = (d.currentIndex + 1) % root.model.count
|
|
}
|
|
|
|
function previous() {
|
|
d.currentIndex--;
|
|
if (d.currentIndex < 0) {
|
|
d.currentIndex = root.model.count - 1
|
|
}
|
|
}
|
|
|
|
function show() {
|
|
d.hidden = false;
|
|
d.manuallyInvoked = true;
|
|
}
|
|
|
|
function hide() {
|
|
d.hidden = true;
|
|
d.manuallyInvoked = false;
|
|
}
|
|
|
|
onCurrentIndexChanged: {
|
|
listView.positionViewAtIndex(currentIndex, ListView.Contain)
|
|
}
|
|
|
|
Label {
|
|
id: dummyLabel
|
|
}
|
|
|
|
QtObject {
|
|
id: d
|
|
property int entryHeight: dummyLabel.font.pixelSize + 4
|
|
|
|
property int currentIndex: 0
|
|
property bool hidden: false
|
|
property bool manuallyInvoked: false
|
|
}
|
|
|
|
ListView {
|
|
id: listView
|
|
anchors.fill: parent
|
|
anchors.margins: root.border.width
|
|
model: root.model
|
|
ScrollBar.vertical: ScrollBar {
|
|
policy: root.model.count > 10 ? ScrollBar.AlwaysOn : ScrollBar.AlwaysOff
|
|
}
|
|
clip: true
|
|
|
|
delegate: Rectangle {
|
|
height: d.entryHeight
|
|
width: parent.width
|
|
color: index == root.currentIndex ? app.accentColor : "transparent"
|
|
Label {
|
|
anchors.verticalCenter: parent.verticalCenter
|
|
anchors { left: parent.left; right: parent.right; margins: 4}
|
|
text: model.displayText
|
|
color: app.foregroundColor
|
|
width: parent.width
|
|
elide: Text.ElideRight
|
|
font: root.font
|
|
}
|
|
MouseArea {
|
|
anchors.fill: parent
|
|
onClicked: {
|
|
root.complete(index)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|