417 lines
15 KiB
Bash
417 lines
15 KiB
Bash
import QtQuick
|
|
import QtQuick.Controls
|
|
|
|
// A styled dropdown selection menu with built-in search and scrolling.
|
|
//
|
|
// Shows a dropdown toggle button with the current selection. Clicking it opens
|
|
// a scrollable menu of all available options (with instant search filtering).
|
|
// `options` may be plain strings or `{value, label}` pairs.
|
|
Column {
|
|
id: combo
|
|
|
|
property string label: ""
|
|
property string placeholder: "Select an option..."
|
|
property string error: ""
|
|
property var options: []
|
|
property bool popupOpen: false
|
|
property string currentValue: ""
|
|
property string currentLabel: ""
|
|
property int highlightIndex: -1
|
|
|
|
property var filteredOptions: []
|
|
|
|
// Compatibility aliases for existing bridge and harness code
|
|
property alias text: mainInput.text
|
|
property alias inputItem: mainInput
|
|
|
|
signal edited(string value)
|
|
signal selected(string value)
|
|
signal opened
|
|
|
|
width: parent ? parent.width : implicitWidth
|
|
spacing: 4
|
|
|
|
function optionValue(option) {
|
|
return (option !== null && typeof option === "object") ? ("" + option.value) : ("" + option);
|
|
}
|
|
|
|
function optionLabel(option) {
|
|
return (option !== null && typeof option === "object" && option.label !== undefined)
|
|
? ("" + option.label) : ("" + option);
|
|
}
|
|
|
|
function updateCurrentLabel() {
|
|
const all = combo.options || [];
|
|
for (let i = 0; i < all.length; i++) {
|
|
if (combo.optionValue(all[i]) === combo.currentValue) {
|
|
combo.currentLabel = combo.optionLabel(all[i]);
|
|
return;
|
|
}
|
|
}
|
|
combo.currentLabel = combo.currentValue;
|
|
}
|
|
|
|
function refreshFilter() {
|
|
const query = (searchField.text || "").trim().toLowerCase();
|
|
const all = combo.options || [];
|
|
const out = [];
|
|
for (let i = 0; i < all.length; i++) {
|
|
const val = combo.optionValue(all[i]);
|
|
const lbl = combo.optionLabel(all[i]);
|
|
if (query.length === 0
|
|
|| val.toLowerCase().indexOf(query) !== -1
|
|
|| lbl.toLowerCase().indexOf(query) !== -1) {
|
|
out.push({ value: val, label: lbl });
|
|
}
|
|
}
|
|
combo.filteredOptions = out;
|
|
combo.highlightIndex = out.length > 0 ? 0 : -1;
|
|
}
|
|
|
|
function choose(val, lbl) {
|
|
combo.currentValue = val;
|
|
combo.currentLabel = lbl;
|
|
if (mainInput.text !== lbl) {
|
|
mainInput.text = lbl;
|
|
}
|
|
combo.popupOpen = false;
|
|
searchField.text = "";
|
|
mainInput.forceActiveFocus();
|
|
combo.edited(val);
|
|
combo.selected(val);
|
|
}
|
|
|
|
function openPopup() {
|
|
combo.popupOpen = true;
|
|
combo.opened();
|
|
combo.refreshFilter();
|
|
searchField.forceActiveFocus();
|
|
}
|
|
|
|
function forceFocus() {
|
|
mainInput.forceActiveFocus();
|
|
}
|
|
|
|
onCurrentValueChanged: {
|
|
updateCurrentLabel();
|
|
const display = combo.currentLabel.length > 0 ? combo.currentLabel : combo.currentValue;
|
|
if (mainInput.text !== display && display.length > 0) {
|
|
mainInput.text = display;
|
|
}
|
|
}
|
|
onOptionsChanged: {
|
|
updateCurrentLabel();
|
|
refreshFilter();
|
|
}
|
|
Component.onCompleted: {
|
|
updateCurrentLabel();
|
|
refreshFilter();
|
|
}
|
|
|
|
// Label
|
|
Text {
|
|
width: parent.width
|
|
text: combo.label
|
|
color: Theme.warmWhite
|
|
font.family: Theme.family
|
|
font.pixelSize: Theme.fontSmall
|
|
font.weight: Font.DemiBold
|
|
visible: combo.label.length > 0
|
|
}
|
|
|
|
// Main Selection Box (Clickable & Keyboard-Focusable)
|
|
Rectangle {
|
|
id: selectorBox
|
|
width: parent.width
|
|
height: Theme.controlHeight
|
|
radius: Theme.radiusSmall
|
|
color: Theme.charcoal
|
|
border.width: (combo.popupOpen || mainInput.activeFocus) ? 2 : 1
|
|
border.color: combo.error.length > 0 ? Theme.danger
|
|
: (combo.popupOpen || mainInput.activeFocus) ? Theme.focusRing
|
|
: Theme.border
|
|
|
|
Rectangle {
|
|
anchors.fill: parent
|
|
anchors.margins: -3
|
|
radius: parent.radius + 3
|
|
color: "transparent"
|
|
border.width: 2
|
|
border.color: Theme.focusRing
|
|
visible: mainInput.activeFocus && !combo.popupOpen
|
|
}
|
|
|
|
TextInput {
|
|
id: mainInput
|
|
anchors.verticalCenter: parent.verticalCenter
|
|
anchors.left: parent.left
|
|
anchors.right: chevronBox.left
|
|
anchors.leftMargin: 12
|
|
anchors.rightMargin: 4
|
|
text: combo.currentLabel.length > 0 ? combo.currentLabel : combo.currentValue
|
|
color: Theme.warmWhite
|
|
font.family: Theme.family
|
|
font.pixelSize: Theme.fontBody
|
|
clip: true
|
|
selectByMouse: true
|
|
selectionColor: Theme.amberDeep
|
|
selectedTextColor: Theme.warmWhite
|
|
activeFocusOnTab: true
|
|
Accessible.role: Accessible.ComboBox
|
|
Accessible.name: combo.label
|
|
|
|
Text {
|
|
anchors.fill: parent
|
|
verticalAlignment: Text.AlignVCenter
|
|
text: combo.placeholder
|
|
color: Theme.muted
|
|
font.family: Theme.family
|
|
font.pixelSize: Theme.fontBody
|
|
visible: mainInput.text.length === 0
|
|
}
|
|
|
|
onTextEdited: {
|
|
combo.currentValue = mainInput.text;
|
|
combo.currentLabel = mainInput.text;
|
|
searchField.text = mainInput.text;
|
|
combo.refreshFilter();
|
|
combo.edited(mainInput.text);
|
|
}
|
|
|
|
Keys.onDownPressed: event => {
|
|
event.accepted = true;
|
|
if (!combo.popupOpen) {
|
|
combo.openPopup();
|
|
} else if (combo.filteredOptions.length > 0) {
|
|
combo.highlightIndex = (combo.highlightIndex + 1) % combo.filteredOptions.length;
|
|
listView.positionViewAtIndex(combo.highlightIndex, ListView.Contain);
|
|
}
|
|
}
|
|
Keys.onUpPressed: event => {
|
|
event.accepted = true;
|
|
if (combo.popupOpen && combo.filteredOptions.length > 0) {
|
|
combo.highlightIndex = combo.highlightIndex <= 0 ? combo.filteredOptions.length - 1 : combo.highlightIndex - 1;
|
|
listView.positionViewAtIndex(combo.highlightIndex, ListView.Contain);
|
|
}
|
|
}
|
|
Keys.onReturnPressed: event => {
|
|
if (combo.popupOpen && combo.highlightIndex >= 0 && combo.highlightIndex < combo.filteredOptions.length) {
|
|
event.accepted = true;
|
|
const chosen = combo.filteredOptions[combo.highlightIndex];
|
|
combo.choose(chosen.value, chosen.label);
|
|
} else {
|
|
combo.popupOpen = false;
|
|
}
|
|
}
|
|
Keys.onEnterPressed: event => {
|
|
if (combo.popupOpen && combo.highlightIndex >= 0 && combo.highlightIndex < combo.filteredOptions.length) {
|
|
event.accepted = true;
|
|
const chosen = combo.filteredOptions[combo.highlightIndex];
|
|
combo.choose(chosen.value, chosen.label);
|
|
} else {
|
|
combo.popupOpen = false;
|
|
}
|
|
}
|
|
Keys.onEscapePressed: event => {
|
|
event.accepted = true;
|
|
combo.popupOpen = false;
|
|
}
|
|
}
|
|
|
|
Item {
|
|
id: chevronBox
|
|
width: 36
|
|
height: parent.height
|
|
anchors.right: parent.right
|
|
|
|
Text {
|
|
id: chevron
|
|
anchors.centerIn: parent
|
|
text: combo.popupOpen ? "\u25B4" : "\u25BE"
|
|
color: Theme.muted
|
|
font.pixelSize: Theme.fontHeading
|
|
}
|
|
|
|
MouseArea {
|
|
anchors.fill: parent
|
|
cursorShape: Qt.PointingHandCursor
|
|
onClicked: {
|
|
if (combo.popupOpen) {
|
|
combo.popupOpen = false;
|
|
} else {
|
|
combo.openPopup();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Inline Error
|
|
Text {
|
|
width: parent.width
|
|
visible: combo.error.length > 0
|
|
text: combo.error
|
|
color: Theme.danger
|
|
font.family: Theme.family
|
|
font.pixelSize: Theme.fontSmall
|
|
}
|
|
|
|
// Dropdown Menu Panel
|
|
Rectangle {
|
|
id: dropdownMenu
|
|
width: parent.width
|
|
visible: combo.popupOpen
|
|
height: visible ? (searchBoxRect.height + listContainer.height + 16) : 0
|
|
radius: Theme.radiusSmall
|
|
color: Theme.surfaceRaised
|
|
border.width: 1
|
|
border.color: Theme.border
|
|
|
|
Column {
|
|
anchors.fill: parent
|
|
anchors.margins: 8
|
|
spacing: 6
|
|
|
|
// Search filter box inside dropdown
|
|
Rectangle {
|
|
id: searchBoxRect
|
|
width: parent.width
|
|
height: 34
|
|
radius: 4
|
|
color: Theme.surface
|
|
border.width: 1
|
|
border.color: searchField.activeFocus ? Theme.focusRing : Theme.border
|
|
|
|
TextInput {
|
|
id: searchField
|
|
anchors.fill: parent
|
|
anchors.leftMargin: 8
|
|
anchors.rightMargin: 8
|
|
verticalAlignment: TextInput.AlignVCenter
|
|
color: Theme.warmWhite
|
|
font.family: Theme.family
|
|
font.pixelSize: Theme.fontSmall
|
|
selectionColor: Theme.amberDeep
|
|
selectedTextColor: Theme.warmWhite
|
|
clip: true
|
|
|
|
Text {
|
|
anchors.fill: parent
|
|
verticalAlignment: Text.AlignVCenter
|
|
text: "Type to filter options..."
|
|
color: Theme.muted
|
|
font.family: Theme.family
|
|
font.pixelSize: Theme.fontSmall
|
|
visible: searchField.text.length === 0
|
|
}
|
|
|
|
onTextChanged: combo.refreshFilter()
|
|
|
|
Keys.onUpPressed: event => {
|
|
event.accepted = true;
|
|
if (combo.filteredOptions.length === 0) return;
|
|
combo.highlightIndex = combo.highlightIndex <= 0 ? combo.filteredOptions.length - 1 : combo.highlightIndex - 1;
|
|
listView.positionViewAtIndex(combo.highlightIndex, ListView.Contain);
|
|
}
|
|
Keys.onDownPressed: event => {
|
|
event.accepted = true;
|
|
if (combo.filteredOptions.length === 0) return;
|
|
combo.highlightIndex = combo.highlightIndex >= combo.filteredOptions.length - 1 ? 0 : combo.highlightIndex + 1;
|
|
listView.positionViewAtIndex(combo.highlightIndex, ListView.Contain);
|
|
}
|
|
Keys.onReturnPressed: event => {
|
|
event.accepted = true;
|
|
if (combo.highlightIndex >= 0 && combo.highlightIndex < combo.filteredOptions.length) {
|
|
const chosen = combo.filteredOptions[combo.highlightIndex];
|
|
combo.choose(chosen.value, chosen.label);
|
|
} else if (searchField.text.trim().length > 0) {
|
|
const custom = searchField.text.trim();
|
|
combo.choose(custom, custom);
|
|
} else {
|
|
combo.popupOpen = false;
|
|
selectorBox.forceActiveFocus();
|
|
}
|
|
}
|
|
Keys.onEnterPressed: event => {
|
|
event.accepted = true;
|
|
if (combo.highlightIndex >= 0 && combo.highlightIndex < combo.filteredOptions.length) {
|
|
const chosen = combo.filteredOptions[combo.highlightIndex];
|
|
combo.choose(chosen.value, chosen.label);
|
|
} else if (searchField.text.trim().length > 0) {
|
|
const custom = searchField.text.trim();
|
|
combo.choose(custom, custom);
|
|
} else {
|
|
combo.popupOpen = false;
|
|
selectorBox.forceActiveFocus();
|
|
}
|
|
}
|
|
Keys.onEscapePressed: event => {
|
|
event.accepted = true;
|
|
combo.popupOpen = false;
|
|
selectorBox.forceActiveFocus();
|
|
}
|
|
}
|
|
}
|
|
|
|
// Scrollable list of options
|
|
Item {
|
|
id: listContainer
|
|
width: parent.width
|
|
height: Math.min(220, listView.contentHeight > 0 ? listView.contentHeight : 40)
|
|
clip: true
|
|
|
|
ListView {
|
|
id: listView
|
|
anchors.fill: parent
|
|
model: combo.filteredOptions
|
|
boundsBehavior: Flickable.StopAtBounds
|
|
ScrollBar.vertical: ScrollBar {
|
|
active: true
|
|
policy: listView.contentHeight > listView.height ? ScrollBar.AlwaysOn : ScrollBar.AsNeeded
|
|
}
|
|
|
|
delegate: Rectangle {
|
|
id: optionRow
|
|
required property var modelData
|
|
required property int index
|
|
width: listView.width
|
|
height: 32
|
|
radius: 4
|
|
color: index === combo.highlightIndex ? Theme.amberDeep
|
|
: (rowHover.containsMouse ? Theme.surface : "transparent")
|
|
|
|
Text {
|
|
anchors.verticalCenter: parent.verticalCenter
|
|
anchors.left: parent.left
|
|
anchors.right: parent.right
|
|
anchors.leftMargin: 10
|
|
anchors.rightMargin: 10
|
|
text: optionRow.modelData.label
|
|
color: index === combo.highlightIndex ? Theme.charcoal : Theme.warmWhite
|
|
font.family: Theme.family
|
|
font.pixelSize: Theme.fontSmall
|
|
elide: Text.ElideRight
|
|
}
|
|
|
|
MouseArea {
|
|
id: rowHover
|
|
anchors.fill: parent
|
|
hoverEnabled: true
|
|
cursorShape: Qt.PointingHandCursor
|
|
onClicked: combo.choose(optionRow.modelData.value, optionRow.modelData.label)
|
|
}
|
|
}
|
|
}
|
|
|
|
Text {
|
|
anchors.centerIn: parent
|
|
text: "No matching options found"
|
|
color: Theme.muted
|
|
font.family: Theme.family
|
|
font.pixelSize: Theme.fontSmall
|
|
visible: combo.filteredOptions.length === 0
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|