Skip to content

Commit fece71e

Browse files
authored
Menus: make it possible to have items with different height
Can be usefull for separator (Required for slint-ui#8339)
1 parent beef5d9 commit fece71e

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

internal/compiler/widgets/common/menus.slint

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ export component PopupMenuImpl inherits Window {
99
in property <[MenuEntry]> entries: [];
1010

1111
property <int> current-highlight: -1;
12+
// `y` position of the currently highlighted menu entry
13+
property <length> current-highlight-y-pos;
1214
property <int> current-open: -1;
1315
property <length> px: 1rem / 14;
1416

@@ -25,6 +27,12 @@ export component PopupMenuImpl inherits Window {
2527
entry: entry;
2628
is-current: current-highlight == index;
2729

30+
changed is-current => {
31+
if self.is-current {
32+
current-highlight-y-pos = self.absolute-position.y - root.absolute-position.y;
33+
}
34+
}
35+
2836
set-current => {
2937
focus-scope.focus();
3038
root.current-highlight = index;
@@ -50,7 +58,7 @@ export component PopupMenuImpl inherits Window {
5058

5159
if current-highlight >= 0 {
5260
if entries[current-highlight].has-sub-menu {
53-
activate(entries[current-highlight], y-pos(current-highlight), current-highlight);
61+
activate(entries[current-highlight], current-highlight-y-pos, current-highlight);
5462
} else {
5563
current-open = -1;
5664
sub-menu.close();
@@ -59,10 +67,6 @@ export component PopupMenuImpl inherits Window {
5967
}
6068
}
6169

62-
function y-pos(idx: int) -> length {
63-
frame.padding + idx * (frame.height - 2 * frame.padding) / entries.length
64-
}
65-
6670
key-pressed(event) => {
6771
open-sub-menu-after-timeout.running = false;
6872

@@ -86,12 +90,12 @@ export component PopupMenuImpl inherits Window {
8690
return accept;
8791
} else if event.text == Key.Return {
8892
if current-highlight >= 0 && current-highlight < entries.length && entries[current-highlight].enabled {
89-
activate(entries[current-highlight], y-pos(current-highlight), current-highlight);
93+
activate(entries[current-highlight], current-highlight-y-pos, current-highlight);
9094
}
9195
return accept;
9296
} else if event.text == Key.RightArrow {
9397
if current-highlight >= 0 && current-highlight < entries.length && entries[current-highlight].has-sub-menu && entries[current-highlight].enabled {
94-
activate(entries[current-highlight], y-pos(current-highlight), current-highlight);
98+
activate(entries[current-highlight], current-highlight-y-pos, current-highlight);
9599
}
96100
return accept;
97101
} else if event.text == Key.LeftArrow {

0 commit comments

Comments
 (0)