Merge pull request #2221 from nZo-sp/feat/scroll-with-keyboard-on-dropdown

Added scroll with keyboard in searchable dropdown
This commit is contained in:
Lemmy
2026-03-24 19:50:29 -04:00
committed by GitHub
+66 -1
View File
@@ -10,6 +10,7 @@ RowLayout {
property real minimumWidth: 280
property real popupHeight: 180
property bool selectOnNavigation: true
property string label: ""
property string description: ""
property ListModel model: {}
@@ -152,7 +153,10 @@ RowLayout {
}
}
onSearchTextChanged: filterModel()
onSearchTextChanged: {
filterModel();
listView.currentIndex = 0;
}
NLabel {
label: root.label
@@ -236,6 +240,59 @@ RowLayout {
text: root.searchText
onTextChanged: root.searchText = text
fontSize: Style.fontSizeS
Keys.onPressed: (event) => {
if (Keybinds.checkKey(event, 'enter', Settings)) {
selectHighlighted()
combo.popup.close();
event.accepted = true;
return;
}
if (Keybinds.checkKey(event, 'escape', Settings)) {
combo.popup.close()
event.accepted = true;
return;
}
if (Keybinds.checkKey(event, 'up', Settings)) {
if (listView.currentIndex > 0) {
listView.currentIndex--;
listView.positionViewAtIndex(listView.currentIndex, ListView.Contain);
if (root.selectOnNavigation)
selectHighlighted();
}
event.accepted = true;
return;
}
if (Keybinds.checkKey(event, 'down', Settings)) {
if (listView.currentIndex < listView.count - 1) {
listView.currentIndex++;
listView.positionViewAtIndex(listView.currentIndex, ListView.Contain);
if (root.selectOnNavigation)
selectHighlighted();
}
event.accepted = true;
return;
}
}
function selectHighlighted() {
if (listView.currentIndex >= 0
&& listView.model
&& listView.currentIndex < listView.model.count) {
var selectedKey = listView.model.get(listView.currentIndex).key;
root.selected(selectedKey);
}
}
function pageStep() {
if (listView.count === 0 || listView.contentHeight === 0)
return 1;
var rowHeight = listView.contentHeight / listView.count;
return Math.max(1, Math.floor(listView.height / rowHeight));
}
}
NListView {
@@ -247,6 +304,11 @@ RowLayout {
horizontalPolicy: ScrollBar.AlwaysOff
verticalPolicy: ScrollBar.AsNeeded
onCurrentIndexChanged: {
if (currentIndex >= 0)
positionViewAtIndex(currentIndex, ListView.Contain);
}
delegate: root.delegate ? root.delegate : defaultDelegate
Component {
@@ -378,12 +440,15 @@ RowLayout {
if (combo.popup.visible) {
// Ensure the model is filtered when popup opens
filterModel();
listView.currentIndex = Math.max(0, root.findIndexInActiveModel(root.currentKey));
// Small delay to ensure the popup is fully rendered
Qt.callLater(() => {
if (searchInput && searchInput.inputItem) {
searchInput.inputItem.forceActiveFocus();
}
});
} else {
root.searchText = "";
}
}
}