Skip to content

Commit 75e3ec0

Browse files
committed
feat: FluAutoSuggestBox支持方向键循环移动列表项
1 parent f4c1c15 commit 75e3ec0

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

src/Qt5/imports/FluentUI/Controls/FluAutoSuggestBox.qml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,44 @@ FluTextBox{
3737
return
3838
}
3939
list_view.model = items.filter(item => control.filter(item))
40+
list_view.currentIndex = -1
41+
}
42+
function navigateList(step) {
43+
if (list_view.count < 1) {
44+
return
45+
}
46+
let newIndex = list_view.currentIndex + step
47+
if (newIndex >= list_view.count) {
48+
newIndex = 0
49+
} else if (newIndex < 0) {
50+
newIndex = list_view.count - 1
51+
}
52+
list_view.currentIndex = newIndex
53+
list_view.positionViewAtIndex(newIndex, ListView.Contain)
54+
}
55+
function handleEnterReturn() {
56+
if (list_view.count > 0 && list_view.currentIndex !== -1) {
57+
d.handleClick(list_view.model[list_view.currentIndex])
58+
}
4059
}
4160
}
4261
onActiveFocusChanged: {
4362
if(!activeFocus){
4463
control_popup.visible = false
4564
}
4665
}
66+
Keys.onDownPressed: {
67+
d.navigateList(1)
68+
}
69+
Keys.onUpPressed: {
70+
d.navigateList(-1)
71+
}
72+
Keys.onEnterPressed: {
73+
d.handleEnterReturn()
74+
}
75+
Keys.onReturnPressed: {
76+
d.handleEnterReturn()
77+
}
4778
Popup{
4879
id:control_popup
4980
focus: false
@@ -77,6 +108,9 @@ FluTextBox{
77108
}
78109
}
79110
}
111+
highlight: Rectangle {
112+
color: FluTheme.itemHoverColor
113+
}
80114
delegate:FluControl{
81115
id: item_control
82116
height: control.itemHeight

src/Qt6/imports/FluentUI/Controls/FluAutoSuggestBox.qml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,44 @@ FluTextBox{
3636
return
3737
}
3838
list_view.model = items.filter(item => control.filter(item))
39+
list_view.currentIndex = -1
40+
}
41+
function navigateList(step) {
42+
if (list_view.count < 1) {
43+
return
44+
}
45+
let newIndex = list_view.currentIndex + step
46+
if (newIndex >= list_view.count) {
47+
newIndex = 0
48+
} else if (newIndex < 0) {
49+
newIndex = list_view.count - 1
50+
}
51+
list_view.currentIndex = newIndex
52+
list_view.positionViewAtIndex(newIndex, ListView.Contain)
53+
}
54+
function handleEnterReturn() {
55+
if (list_view.count > 0 && list_view.currentIndex !== -1) {
56+
d.handleClick(list_view.model[list_view.currentIndex])
57+
}
3958
}
4059
}
4160
onActiveFocusChanged: {
4261
if(!activeFocus){
4362
control_popup.visible = false
4463
}
4564
}
65+
Keys.onDownPressed: {
66+
d.navigateList(1)
67+
}
68+
Keys.onUpPressed: {
69+
d.navigateList(-1)
70+
}
71+
Keys.onEnterPressed: {
72+
d.handleEnterReturn()
73+
}
74+
Keys.onReturnPressed: {
75+
d.handleEnterReturn()
76+
}
4677
Popup{
4778
id:control_popup
4879
focus: false
@@ -76,6 +107,9 @@ FluTextBox{
76107
}
77108
}
78109
}
110+
highlight: Rectangle {
111+
color: FluTheme.itemHoverColor
112+
}
79113
delegate:FluControl{
80114
id: item_control
81115
height: control.itemHeight

0 commit comments

Comments
 (0)