1+ import { Palette , ComboBox } from "std-widgets.slint" ;
2+ import { Constants } from "../constants.slint" ;
3+ import { VerticalScrollable , VerticalStart , VerticalScrollbar , VerticalStretch } from "vertical.slint" ;
4+ import { HorizontalStart } from "horizontal.slint" ;
5+ import { ActiveUi } from "../state.slint" ;
6+
7+ export component Dropdown inherits Rectangle {
8+ in property <string > text ;
9+ in property <[string ]> options ;
10+ in property <length > element-length : 50px ;
11+ out property <bool > is-open : false ;
12+
13+ callback selected (option : string , index : int );
14+
15+ border-color : Palette.accent-background;
16+ border-width : 2px ;
17+ border-radius : Constants.radius-md;
18+
19+ GridLayout {
20+ padding-left : Constants.padding;
21+ padding-right : Constants.padding;
22+ Text {
23+ text : text;
24+ vertical-alignment : center;
25+ horizontal-alignment : center;
26+ font-weight : Constants.font-weight-bold;
27+ }
28+ }
29+
30+ TouchArea {
31+ clicked => {
32+ popup.show ();
33+ }
34+ }
35+
36+ popup := PopupWindow {
37+ x : 0 ;
38+ y : root .height;
39+ width : max(root .width, 100px );
40+ height : min(options.length * element-length, ActiveUi.height);
41+
42+ Rectangle {
43+ background : Palette.alternate-background;
44+ drop-shadow-color : #0000004C ;
45+ drop-shadow-blur : 2px ;
46+ drop-shadow-offset-y : 1px ;
47+ border-radius : Constants.radius-md;
48+ border-color : Palette.accent-background;
49+ border-width : 1px ;
50+ }
51+
52+ FocusScope {
53+ changed has-focus => {
54+ if !self .has-focus {
55+ popup.close ();
56+ }
57+ }
58+
59+ VerticalStretch {
60+ padding : 0 ;
61+ spacing : 0 ;
62+ for i in options.length: Rectangle {
63+ bg := Rectangle {
64+ HorizontalStart {
65+ padding-left : Constants.padding;
66+ padding-right : Constants.padding;
67+ Text {
68+ text : options[i];
69+ color : Palette.alternate-foreground;
70+ vertical-alignment : center;
71+ }
72+ }
73+
74+ states [
75+ pressed when touch-area.pressed : {
76+ bg.background : Palette.selection-background;
77+ }
78+ ]
79+
80+ touch-area := TouchArea {
81+ clicked => {
82+ selected (options[i], i);
83+ popup.close ();
84+ }
85+ }
86+ }
87+ }
88+ }
89+ }
90+ }
91+ }
92+
93+ // For testing in the slint live preview
94+ component LivePreviewTest {
95+ width : 480px ;
96+ height : 272px ;
97+
98+ VerticalStart {
99+ Dropdown {
100+ text : "Test Test Test" ;
101+ options : ["One" , "Two" , "Three" ];
102+ }
103+ Dropdown {
104+ text : "Long" ;
105+ options : ["One" , "Two" , "Three" , "Four" , "Five" , "Six" , "Seven" , "Eight" , "Nine" , "Ten" ];
106+ }
107+
108+ ComboBox {
109+ model : ["One" , "Two" , "Three" , "Four" , "Five" , "Six" , "Seven" , "Eight" , "Nine" , "Ten" ];
110+ }
111+ }
112+ }
0 commit comments