1use crate::*;
2
3impl Frame<'_> {
4 pub fn android_popup_item(&mut self,id:u64,label:&str,enabled:bool,trailing:Option<MaterialIcon>)->Response {
7 let b=self.allocate(48.);let r=self.interact(id,b,enabled);let t=self.ui.theme;
8 let opacity=if r.pressed {0.08} else if r.focused && self.ui.keyboard_focus {0.12} else if r.hovered && self.input.pointer_kind != PointerKind::Touch {0.08} else {0.};
9 if r.enabled && opacity>0. {self.rect(b,0.,t.text.opacity(opacity));}
10 self.ink(r,0.,t.text);
11 self.styled_text(Rect{x:b.x+16.,width:(b.width-if trailing.is_some(){56.}else{32.}).max(0.),..b},label,t.text.opacity(if r.enabled {1.} else {0.38}),TextStyle{size:16.,weight:400,..TextStyle::default()});
12 if let Some(icon)=trailing {self.paint_material_icon(Rect{x:b.x+b.width-40.,y:b.y+12.,width:24.,height:24.},icon,t.on_surface_variant);}
13 self.focus_ring(r);self.semantic(r,Role::MenuItem,label,false,None,None,None);r
14 }
15 pub fn menu_item(&mut self, id: u64, label: &str, enabled: bool) -> Response {
16 self.menu_item_inner(id, label, enabled, None, false, None)
17 }
18 pub fn menu_item_with_icon(
20 &mut self,
21 id: u64,
22 label: &str,
23 enabled: bool,
24 icon: AppBarIcon,
25 rtl: bool,
26 ) -> Response {
27 self.menu_item_inner(id, label, enabled, Some(icon), rtl, None)
28 }
29 pub fn menu_item_with_selection(&mut self,id:u64,label:&str,enabled:bool,selected:bool,rtl:bool) -> Response {
31 self.menu_item_inner(id,label,enabled,Some(AppBarIcon::More),rtl,Some(selected))
32 }
33 #[allow(clippy::too_many_arguments)]
34 fn menu_item_inner(
35 &mut self,
36 id: u64,
37 label: &str,
38 enabled: bool,
39 icon: Option<AppBarIcon>,
40 rtl: bool,
41 selection: Option<bool>,
42 ) -> Response {
43 let b = self.allocate(48.0);
44 let r = self.interact(id, b, enabled);
45 let t = self.ui.theme;
46 let opacity = if r.focused && self.ui.keyboard_focus {
47 0.12
48 } else if r.hovered {
49 0.08
50 } else {
51 0.0
52 };
53 if r.enabled && opacity > 0.0 {
54 self.rect(b, 0.0, t.text.opacity(opacity));
55 }
56 self.ink(r, 0.0, t.text);
57 if let Some(icon) = icon {
58 self.commands.push(DrawCommand::Shape {
59 bounds: Rect {
60 x: if rtl {
61 b.x + b.width - 40.0
62 } else {
63 b.x + 16.0
64 },
65 y: b.y + 12.0,
66 width: 24.0,
67 height: 24.0,
68 },
69 index: if selection.is_some() { (crate::beercss_shapes::SHAPES.len()+crate::icons::material_navigation::ICONS.len()+MaterialIcon::Check as usize) as u8 } else { icon.shape_index(rtl) },
70 color: if selection==Some(false) { Color(0,0,0,0) } else if r.enabled {
71 t.on_surface_variant
72 } else {
73 t.text.opacity(0.38)
74 },
75 rotation: 0.0,
76 });
77 }
78 self.styled_text(
79 if icon.is_some() {
80 Rect {
81 x: b.x + if rtl { 16.0 } else { 56.0 },
82 y: b.y - 1.0,
83 width: (b.width - 72.0).max(0.0),
84 ..b
85 }
86 } else {
87 Rect { y: b.y - 1.0, ..b }
88 },
89 label,
90 t.text.opacity(if r.enabled { 1.0 } else { 0.38 }),
91 TextStyle {
92 align: if rtl && icon.is_some() { 2 } else { 0 },
93 padding: if icon.is_some() { 0.0 } else { 12.0 },
94 ..TextStyle::LABEL
95 },
96 );
97 self.focus_ring(r);
98 self.semantic(r, Role::MenuItem, label, false, None, None, None);
99 r
100 }
101}
102
103#[derive(Default)]
105pub struct MenuTypeaheadState {
106 buffer: String,
107 last_input: f64,
108 last_focus: Option<u64>,
109}
110impl MenuTypeaheadState {
111 pub fn reset(&mut self) {
112 self.buffer.clear();
113 self.last_focus = None;
114 }
115}
116
117impl Frame<'_> {
118 pub fn menu_typeahead(
121 &mut self,
122 state: &mut MenuTypeaheadState,
123 first_id: u64,
124 items: &[(&str, bool)],
125 ) -> Option<usize> {
126 let focus = self.focused_id();
127 if self.input.blurred || self.input.cancelled || !self.enabled || focus.is_none() {
128 state.reset();
129 return None;
130 }
131 let Some(current) = focus
132 .and_then(|id| id.checked_sub(first_id))
133 .and_then(|i| usize::try_from(i).ok())
134 .filter(|i| *i < items.len())
135 else {
136 state.reset();
137 return None;
138 };
139 let found = self.typeahead_match(state, focus.unwrap(), current, items);
140 if let Some(index) = found {
141 self.request_focus(first_id + index as u64);
142 state.last_focus = Some(first_id + index as u64);
143 }
144 found
145 }
146
147 pub fn select_typeahead(&mut self, state: &mut MenuTypeaheadState, trigger_id: u64, current: usize, items: &[(&str, bool)]) -> Option<usize> {
150 if self.input.blurred || self.input.cancelled || !self.enabled || self.focused_id()!=Some(trigger_id) || current>=items.len() {
151 state.reset();
152 return None;
153 }
154 self.typeahead_match(state, trigger_id, current, items)
155 }
156
157 fn typeahead_match(&mut self, state: &mut MenuTypeaheadState, focus: u64, current: usize, items: &[(&str, bool)]) -> Option<usize> {
158 if state.last_focus.is_some_and(|last| last != focus) { state.reset(); }
159 if self.key_consumed { return None; }
160 let key = self.input.key.filter(|key| key.pressed)?;
161 if !self.ui.time.is_finite() || self.ui.time - state.last_input > 0.2 || self.ui.time < state.last_input { state.reset(); }
162 let ch = match key.key {
163 Key::Character(ch) => ch,
164 Key::Space if !state.buffer.is_empty() => ' ',
165 _ => { state.reset(); return None; }
166 };
167 if ch.is_control() || ch.is_whitespace() && state.buffer.is_empty() { return None; }
168 if state.buffer.len() >= 128 { state.reset(); }
169 let fresh = state.buffer.is_empty();
170 state.buffer.extend(ch.to_lowercase());
171 state.last_input = self.ui.time;
172 state.last_focus = Some(focus);
173 self.key_consumed = true;
174 let start = if fresh { (current + 1) % items.len() } else { current };
175 let found = (0..items.len()).map(|step| (start + step) % items.len())
176 .find(|i| items[*i].1 && items[*i].0.trim().to_lowercase().starts_with(&state.buffer));
177 if found.is_none() { state.reset(); }
178 found
179 }
180}
181
182#[cfg(test)]
183#[path = "menu.spec.rs"]
184mod tests;