1use crate::*;
2
3#[derive(Clone, Copy, Debug)]
5pub struct FabMenuAction<'a> {
6 pub label: &'a str,
7 pub glyph: &'a str,
8 pub enabled: bool,
9}
10
11#[derive(Default)]
13pub struct FabMenuState {
14 was_open: bool,
15 changed_at: f64,
16 scroll: f32,
17 pending_focus: Option<usize>,
18 dragging: bool,
19}
20impl FabMenuState {
21 pub fn scroll_offset(&self) -> f32 {
22 self.scroll
23 }
24}
25
26#[derive(Clone, Copy, Debug, Default)]
27pub struct FabMenuOptions {
28 pub rtl: bool,
29}
30
31impl Frame<'_> {
32 pub fn fab_menu(
34 &mut self,
35 id: u64,
36 bounds: Rect,
37 open: &mut bool,
38 actions: &[FabMenuAction<'_>],
39 state: &mut FabMenuState,
40 ) -> Option<usize> {
41 self.fab_menu_with(id, bounds, open, actions, state, FabMenuOptions::default())
42 }
43 pub fn fab_menu_with(
45 &mut self,
46 id: u64,
47 bounds: Rect,
48 open: &mut bool,
49 actions: &[FabMenuAction<'_>],
50 state: &mut FabMenuState,
51 options: FabMenuOptions,
52 ) -> Option<usize> {
53 let count = actions.len();
54 if count == 0
55 || count > 8
56 || id > u64::MAX - count as u64
57 || !bounds.x.is_finite()
58 || !bounds.y.is_finite()
59 || !bounds.width.is_finite()
60 || !bounds.height.is_finite()
61 || bounds.width < 56.0
62 || bounds.height < 120.0
63 {
64 return None;
65 }
66 let visible = bounds.intersect(self.clip);
67 if visible.width <= 0.0 || visible.height <= 0.0 {
68 return None;
69 }
70 let trigger = Rect {
71 x: if options.rtl {
72 bounds.x
73 } else {
74 bounds.x + bounds.width - 56.0
75 },
76 y: bounds.y + bounds.height - 56.0,
77 width: 56.0,
78 height: 56.0,
79 };
80 if *open && self.input.pressed && self.input.pointer.is_some_and(|p| !bounds.contains(p)) {
81 *open = false;
82 }
83 if *open
84 && self.pressed_key(Key::Escape)
85 && self
86 .ui
87 .focused
88 .is_some_and(|focus| focus >= id && focus <= id + count as u64)
89 {
90 *open = false;
91 self.request_focus(id);
92 self.key_consumed = true;
93 }
94 let r = self.target(
95 id,
96 trigger,
97 if *open {
98 "Close actions"
99 } else {
100 "Open actions"
101 },
102 true,
103 Role::Button,
104 );
105 if r.clicked {
106 *open = !*open;
107 }
108 let focus_first = *open
109 && r.focused
110 && !self.key_consumed
111 && self
112 .input
113 .key
114 .is_some_and(|k| k.pressed && k.key == Key::Down);
115 let items_window = Rect {
116 x: bounds.x,
117 y: bounds.y,
118 width: bounds.width,
119 height: bounds.height - 64.0,
120 };
121 let content_height = count as f32 * 56.0 + (count - 1) as f32 * 4.0;
122 let maximum_scroll = (content_height - items_window.height).max(0.0);
123 if state.was_open != *open {
124 state.was_open = *open;
125 state.changed_at = self.ui.time;
126 if *open {
127 state.scroll = maximum_scroll;
128 } else {
129 state.pending_focus = None;
130 }
131 }
132 if *open {
133 state.dragging=self.scroll_viewport(id.wrapping_add(0x100000),items_window,&mut state.scroll,maximum_scroll);
134 if state.dragging {self.ui.active=None;}
135 }
136 if focus_first && let Some(index) = actions.iter().position(|action| action.enabled) {
137 state.scroll = ((index as f32 * 60.0).min(maximum_scroll)).max(0.0);
138 state.pending_focus = Some(index);
139 self.key_consumed = true;
140 self.ui.continuous = true;
141 }
142 if *open
143 && !self.key_consumed
144 && let Some(key) = self.input.key
145 && key.pressed
146 && matches!(key.key, Key::Up | Key::Down)
147 && let Some(current) = self
148 .ui
149 .focused
150 .and_then(|focus| focus.checked_sub(id + 1))
151 .filter(|index| *index < count as u64)
152 {
153 let current = current as usize;
154 let next = (1..=count)
155 .map(|step| {
156 if key.key == Key::Down {
157 (current + step) % count
158 } else {
159 (current + count - step) % count
160 }
161 })
162 .find(|&index| actions[index].enabled);
163 if let Some(index) = next {
164 let top = index as f32 * 60.0;
165 if top < state.scroll {
166 state.scroll = top;
167 } else if top + 56.0 > state.scroll + items_window.height {
168 state.scroll = (top + 56.0 - items_window.height).min(maximum_scroll);
169 }
170 state.pending_focus = Some(index);
171 self.request_focus(id);
172 self.key_consumed = true;
173 self.ui.continuous = true;
174 }
175 }
176 let toggle = self.spatial(id, 68, f32::from(*open), 0.0).clamp(0.0, 1.0);
177 let t = self.ui.theme;
178 let radius = 16.0 + 12.0 * toggle;
179 if r.enabled {
180 self.elevation(trigger, radius, 3);
181 }
182 self.paint_rect(trigger, radius, t.primary_container);
183 self.state_layer(r, radius, t.on_primary_container);
184 self.focus_ring(r);
185 let angle = toggle * std::f32::consts::FRAC_PI_4;
186 let (sin, cos) = angle.sin_cos();
187 let cx = trigger.x + 28.0;
188 let cy = trigger.y + 28.0;
189 let point = |x: f32, y: f32| [cx + x * cos - y * sin, cy + x * sin + y * cos];
190 self.paint_line(
191 point(-9.0, 0.0),
192 point(9.0, 0.0),
193 2.0,
194 t.on_primary_container,
195 );
196 self.paint_line(
197 point(0.0, -9.0),
198 point(0.0, 9.0),
199 2.0,
200 t.on_primary_container,
201 );
202 let mut chosen = None;
203 let previous_clip = self.clip;
204 let content_clip = previous_clip.intersect(items_window);
205 self.clip = previous_clip.intersect(Rect {
208 x: items_window.x - 8.0,
209 width: items_window.width + 16.0,
210 height: items_window.height + 8.0,
211 ..items_window
212 });
213 let shadow_clip = self.clip;
214 self.commands.push(DrawCommand::PushClip(self.clip));
215 for (i, action) in actions.iter().enumerate() {
216 let delay = (count - i - 1) as f64 * 0.045;
217 let pending =
218 *open && !self.reduced_motion() && self.ui.time - state.changed_at < delay;
219 if pending {
220 self.ui.continuous = true;
221 }
222 let amount = self
223 .spatial(id + i as u64 + 1, 69, f32::from(*open && !pending), 0.0)
224 .clamp(0.0, 1.0);
225 if amount <= 0.001 {
226 continue;
227 }
228 let full_width = (self.text_width(
229 action.label,
230 TextStyle {
231 size: 16.0,
232 weight: 500,
233 ..TextStyle::default()
234 },
235 ) + 80.0)
236 .max(112.0)
237 .min(bounds.width);
238 let item = Rect {
239 x: if options.rtl {
240 bounds.x
241 } else {
242 bounds.x + bounds.width - full_width
243 },
244 y: items_window.y
245 + (items_window.height - content_height).max(0.0)
246 + i as f32 * 60.0
247 - state.scroll,
248 width: full_width,
249 height: 56.0,
250 };
251 let item_visible = item.intersect(content_clip);
252 if item_visible.width <= 0.0 || item_visible.height <= 0.0 {
253 continue;
254 }
255 self.clip = content_clip;
256 let response = self.target(
257 id + i as u64 + 1,
258 item,
259 action.label,
260 *open && action.enabled,
261 Role::Button,
262 );
263 self.clip = shadow_clip;
264 if response.clicked {
265 chosen = Some(i);
266 *open = false;
267 self.request_focus(id);
268 }
269 let clip = Rect {
270 x: if options.rtl {
271 item.x
272 } else {
273 item.x + item.width * (1.0 - amount)
274 },
275 width: item.width * amount,
276 ..item
277 };
278 let partially_scrolled = item.y < content_clip.y
279 || item.y + item.height > content_clip.y + content_clip.height;
280 let draw_shadow = |f: &mut Self| {
281 f.with_opacity(amount, |f| {
282 f.elevation(clip, 28.0_f32.min(clip.width / 2.0), 3);
283 });
284 };
285 if partially_scrolled {
286 self.within(content_clip, draw_shadow);
287 } else {
288 draw_shadow(self);
289 }
290 self.within(clip.intersect(content_clip), |f| {
291 f.with_opacity(amount, |f| {
292 f.paint_rect(item, 28.0, t.primary_container);
293 f.state_layer(response, 28.0, t.on_primary_container);
294 f.styled_text(
295 Rect {
296 x: if options.rtl {
297 item.x + item.width - 48.0
298 } else {
299 item.x + 16.0
300 },
301 width: 32.0,
302 ..item
303 },
304 action.glyph,
305 t.on_primary_container,
306 TextStyle {
307 size: 20.0,
308 align: 1,
309 ..TextStyle::default()
310 },
311 );
312 f.styled_text(
313 Rect {
314 x: if options.rtl {
315 item.x + 16.0
316 } else {
317 item.x + 56.0
318 },
319 width: (item.width - 72.0).max(0.0),
320 ..item
321 },
322 action.label,
323 t.on_primary_container,
324 TextStyle {
325 size: 16.0,
326 weight: 500,
327 ..TextStyle::default()
328 },
329 );
330 })
331 });
332 self.within(content_clip, |f| f.focus_ring(response));
333 }
334 self.commands.push(DrawCommand::PopClip);
335 self.clip = previous_clip;
336 if let Some(next) = state
337 .pending_focus
338 .map(|index| id + index as u64 + 1)
339 .filter(|next| {
340 self.ui
341 .semantics
342 .iter()
343 .any(|node| node.id == *next && node.enabled)
344 })
345 {
346 self.request_focus(next);
347 state.pending_focus = None;
348 self.key_consumed = true;
349 self.ui.continuous = true;
350 for node in &mut self.ui.semantics {
351 if node.id >= id && node.id <= id + count as u64 {
352 node.focused = node.id == next;
353 }
354 }
355 }
356 chosen
357 }
358}
359
360#[cfg(test)]
361#[path = "fab_menu.spec.rs"]
362mod tests;