Mercurial > demo / src/crates/ui/sidebar.slint
1 import { StyleMetrics } from "std-widgets.slint"; 3 component SideBarItem inherits Rectangle { 4 callback clicked <=> touch.clicked; 5 in-out property<string> text <=> label.text; 6 in property<bool> selected; 7 in property<bool> has-focus; 9 min-height: l.preferred-height; 13 background: StyleMetrics.window-background; 15 animate opacity { duration: 150ms; } 18 l := HorizontalLayout { 19 y: (parent.height - self.height) / 2; 20 padding: StyleMetrics.layout-padding; 24 color: StyleMetrics.default-text-color; 25 vertical-alignment: center; 35 pressed when touch.pressed : { 38 hover when touch.has-hover : { 41 selected when root.selected : { 44 focused when root.has-focus : { 50 export component SideBar inherits Rectangle { 51 in property<[string]> model: []; 52 out property<int> current-item: 0; 53 in property<string> title <=> label.text; 54 out property<int> current-focused: fs.has-focus ? fs.focused-tab : -1; // The currently focused tab 60 accessible-delegate-focus: root.current-focused >= 0 ? root.current-focused : root.current-item; 63 background: StyleMetrics.window-background.darker(0.2); 67 width: 0px; // Do not react on clicks 68 property<int> focused-tab: 0; 70 key-pressed(event) => { 71 if (event.text == "\n") { 72 root.current-item = root.current-focused; 75 if (event.text == Key.UpArrow) { 76 self.focused-tab = Math.max(self.focused-tab - 1, 0); 79 if (event.text == Key.DownArrow) { 80 self.focused-tab = Math.min(self.focused-tab + 1, root.model.length - 1); 86 key-released(event) => { 87 if (event.text == " ") { 88 root.current-item = root.current-focused; 97 padding-top: StyleMetrics.layout-padding; 98 padding-bottom: StyleMetrics.layout-padding; 99 spacing: StyleMetrics.layout-spacing; 104 horizontal-alignment: center; 107 navigation := VerticalLayout { 110 for item[index] in root.model : SideBarItem { 111 has-focus: index == root.current-focused; 113 selected: index == root.current-item; 114 clicked => { root.current-item = index; } 119 bottom := VerticalLayout { 120 padding-left: StyleMetrics.layout-padding; 121 padding-right: StyleMetrics.layout-padding;