1use crate::*;
2
3#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
4pub enum TopAppBarVariant {
5 #[default]
6 Small,
7 CenterAligned,
8 Medium,
9 Large,
10}
11impl TopAppBarVariant {
12 pub fn expanded_height(self) -> f32 {
13 match self {
14 Self::Small | Self::CenterAligned => 64.0,
15 Self::Medium => 112.0,
16 Self::Large => 152.0,
17 }
18 }
19}
20#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
21pub enum TopAppBarScrollBehavior {
22 #[default]
23 Pinned,
24 EnterAlways,
25 ExitUntilCollapsed,
26}
27
28#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
29pub enum BottomAppBarScrollBehavior {
30 #[default]
31 Pinned,
32 ExitAlways,
33}
34
35#[derive(Clone, Copy, Debug, Default)]
37pub struct BottomAppBarState {
38 hidden: f32,
39 settle: crate::timing::Tween,
40 settle_target: Option<f32>,
41 settle_height: f32,
42}
43
44#[derive(Default)]
46pub struct BottomAppBarOverflowState {
47 pub overlay: OverlayState,
48 offset: f32,
49 drag_origin: Option<f32>,
50 drag_last: Option<f32>,
51 dragging: bool,
52 drag_velocity: f32,
53 drag_sample_time: f64,
54 drag_motion_time: f64,
55 fling_velocity: f32,
56 fling_curve: crate::scroll::SplineFling,
57 typeahead: MenuTypeaheadState,
58}
59
60impl BottomAppBarState {
61 pub fn hidden_offset(&self) -> f32 {
62 self.hidden
63 }
64 pub fn apply_scroll(
66 &mut self,
67 behavior: BottomAppBarScrollBehavior,
68 consumed: f32,
69 height: f32,
70 ) {
71 let height = if height.is_finite() {
72 height.max(0.0)
73 } else {
74 0.0
75 };
76 if behavior == BottomAppBarScrollBehavior::Pinned {
77 self.hidden = 0.0;
78 self.settle_target = None;
79 } else if consumed.is_finite() {
80 if consumed != 0.0 {
81 self.settle_target = None;
82 }
83 self.hidden = (self.hidden + consumed).clamp(0.0, height);
84 } else {
85 self.hidden = self.hidden.clamp(0.0, height);
86 }
87 }
88}
89#[derive(Clone, Copy, Debug, Default)]
91pub struct TopAppBarState {
92 offset: f32,
93 drag_origin: Option<f32>,
94 drag_last: Option<f32>,
95 dragging: bool,
96 drag_sample_time: f64,
97 drag_motion_time: f64,
98 drag_velocity: f32,
99 fling_velocity: f32,
100 fling_curve: crate::scroll::SplineFling,
101 fling_variant: Option<TopAppBarVariant>,
102 snap: crate::timing::Tween,
103 snap_target: Option<f32>,
104 snap_variant: Option<TopAppBarVariant>,
105}
106impl TopAppBarState {
107 fn start_snap(
108 &mut self,
109 variant: TopAppBarVariant,
110 behavior: TopAppBarScrollBehavior,
111 time: f64,
112 reduced: bool,
113 ) -> bool {
114 use crate::timing::{Easing, TweenSpec};
115 if behavior == TopAppBarScrollBehavior::Pinned {
116 return false;
117 }
118 let fraction = self.collapse_fraction(variant);
119 if !(0.001..0.999).contains(&fraction) {
120 return false;
121 }
122 let target = if fraction < 0.5 {
123 0.0
124 } else {
125 variant.expanded_height() - 64.0
126 };
127 self.snap = crate::timing::Tween::at(self.offset);
128 self.snap_target = Some(target);
129 self.snap_variant = Some(variant);
130 let spec = TweenSpec::new(0.25, Easing::Standard);
131 self.offset = self.snap.sample(time, target, spec, spec, reduced);
132 if !self.snap.moving() {
133 self.snap_target = None;
134 }
135 self.snap.moving()
136 }
137 pub fn collapse_fraction(&self, variant: TopAppBarVariant) -> f32 {
138 let range = variant.expanded_height() - 64.0;
139 if range <= 0.0 {
140 0.0
141 } else {
142 (self.offset / range).clamp(0.0, 1.0)
143 }
144 }
145 pub fn set_collapse_fraction(&mut self, variant: TopAppBarVariant, fraction: f32) {
146 self.snap_target = None;
147 self.fling_velocity = 0.0;
148 self.offset = if fraction.is_finite() {
149 fraction.clamp(0.0, 1.0) * (variant.expanded_height() - 64.0)
150 } else {
151 0.0
152 };
153 }
154 pub fn apply_scroll(
156 &mut self,
157 variant: TopAppBarVariant,
158 behavior: TopAppBarScrollBehavior,
159 delta: f32,
160 content_at_start: bool,
161 ) -> f32 {
162 let max = variant.expanded_height() - 64.0;
163 if delta.is_finite() && delta != 0.0 {
164 self.snap_target = None;
165 }
166 self.offset = self.offset.clamp(0.0, max);
167 if !delta.is_finite()
168 || behavior == TopAppBarScrollBehavior::Pinned
169 || (delta < 0.0
170 && behavior == TopAppBarScrollBehavior::ExitUntilCollapsed
171 && !content_at_start)
172 {
173 return 0.0;
174 }
175 let old = self.offset;
176 self.offset = (self.offset + delta).clamp(0.0, max);
177 self.offset - old
178 }
179
180 pub fn route_scroll(
184 &mut self,
185 variant: TopAppBarVariant,
186 behavior: TopAppBarScrollBehavior,
187 delta: f32,
188 content_offset: &mut f32,
189 content_max: f32,
190 ) -> f32 {
191 if !delta.is_finite() {
192 return 0.0;
193 }
194 let maximum = if content_max.is_finite() {
195 content_max.max(0.0)
196 } else {
197 0.0
198 };
199 *content_offset = if content_offset.is_finite() {
200 content_offset.clamp(0.0, maximum)
201 } else {
202 0.0
203 };
204 let mut remaining = delta;
205 let expand_first = delta >= 0.0 || behavior == TopAppBarScrollBehavior::EnterAlways;
206 if expand_first {
207 remaining -= self.apply_scroll(variant, behavior, remaining, *content_offset <= 0.0);
208 }
209 let previous = *content_offset;
210 *content_offset = (*content_offset + remaining).clamp(0.0, maximum);
211 remaining -= *content_offset - previous;
212 if !expand_first {
213 remaining -= self.apply_scroll(variant, behavior, remaining, *content_offset <= 0.0);
214 }
215 delta - remaining
216 }
217}
218#[derive(Clone, Copy, Debug)]
219pub struct AppBarAction<'a> {
220 pub label: &'a str,
221 pub glyph: &'a str,
222 pub enabled: bool,
223}
224
225impl Frame<'_> {
226 #[allow(clippy::too_many_arguments)]
230 pub fn top_app_bar_scroll_input(
231 &mut self,
232 bounds: Rect,
233 state: &mut TopAppBarState,
234 variant: TopAppBarVariant,
235 behavior: TopAppBarScrollBehavior,
236 content_offset: &mut f32,
237 content_max: f32,
238 ) -> f32 {
239 use crate::timing::{Easing, TweenSpec};
240 if state.snap_variant != Some(variant) || behavior == TopAppBarScrollBehavior::Pinned {
241 state.snap_target = None;
242 }
243 if let Some(target) = state.snap_target {
244 let spec = TweenSpec::new(0.25, Easing::Standard);
245 state.offset =
246 state
247 .snap
248 .sample(self.ui.time, target, spec, spec, self.reduced_motion());
249 if state.snap.moving() {
250 self.ui.continuous = true;
251 } else {
252 state.snap_target = None;
253 }
254 }
255 let mut delta = self.scroll_y(bounds);
256 let mut fling_consumed = 0.0;
257 if self.input.pressed
258 || self.input.cancelled
259 || self.input.blurred
260 || !self.enabled
261 || delta != 0.0
262 || self.reduced_motion()
263 || state.fling_variant != Some(variant)
264 {
265 state.fling_velocity = 0.0;
266 }
267 if state.fling_velocity != 0.0 {
268 let (movement, moving) = state.fling_curve.step(self.ui.time);
270 fling_consumed =
271 state.route_scroll(variant, behavior, movement, content_offset, content_max);
272 if !moving { state.fling_velocity = 0.; }
273 if state.fling_velocity.abs() < 20.0 || (movement - fling_consumed).abs() > 0.5 {
274 state.fling_velocity = 0.0;
275 self.ui.continuous |=
276 state.start_snap(variant, behavior, self.ui.time, self.reduced_motion());
277 } else {
278 self.ui.continuous = true;
279 }
280 }
281 if self.enabled && self.input.pointer_kind == PointerKind::Touch {
282 if self.input.pressed && self.pointer_in(bounds) {
283 state.drag_origin = self.input.pointer.map(|p| p[1]);
284 state.drag_last = state.drag_origin;
285 state.dragging = false;
286 state.drag_velocity = 0.0;
287 state.drag_sample_time = self.ui.time;
288 state.drag_motion_time = self.ui.time;
289 }
290 if let (Some(origin), Some(last), Some(pointer)) =
291 (state.drag_origin, state.drag_last, self.input.pointer)
292 {
293 if !state.dragging && (pointer[1] - origin).abs() > 4.0 {
294 state.dragging = true;
295 }
296 if state.dragging {
297 let movement = last - pointer[1];
298 delta += movement;
299 if movement != 0.0 {
300 let elapsed = self.ui.time - state.drag_sample_time;
301 state.drag_velocity = if (0.005..=0.15).contains(&elapsed) {
302 (movement / elapsed as f32).clamp(-4000.0, 4000.0)
303 } else {
304 0.0
305 };
306 state.drag_sample_time = self.ui.time;
307 state.drag_motion_time = self.ui.time;
308 }
309 self.ui.active = None;
310 }
311 if state.dragging {
312 state.drag_last = Some(pointer[1]);
313 }
314 }
315 }
316 let finish_drag = state.dragging
317 && self.input.released
318 && !self.input.cancelled
319 && !self.input.blurred
320 && self.enabled;
321 if self.input.pressed || delta != 0.0 {
322 state.snap_target = None;
323 }
324 if self.input.released || self.input.cancelled || self.input.blurred || !self.enabled {
325 state.drag_origin = None;
326 state.drag_last = None;
327 state.dragging = false;
328 }
329 let consumed = state.route_scroll(variant, behavior, delta, content_offset, content_max);
330 if finish_drag {
331 if !self.reduced_motion()
332 && (self.ui.time - state.drag_motion_time) <= 0.1
333 && state.drag_velocity.abs() >= 350.0
334 {
335 state.fling_velocity = state.drag_velocity;
336 state.fling_curve.start(state.fling_velocity,self.ui.time);
337 state.fling_variant = Some(variant);
338 self.ui.continuous = true;
339 } else {
340 self.ui.continuous |=
341 state.start_snap(variant, behavior, self.ui.time, self.reduced_motion());
342 }
343 }
344 if consumed != 0.0 {
345 self.ui.continuous = true;
346 }
347 consumed + fling_consumed
348 }
349
350 #[allow(clippy::too_many_arguments)]
353 pub fn top_app_bar_scroll_keyboard(
354 &mut self,
355 target_id: u64,
356 bounds: Rect,
357 state: &mut TopAppBarState,
358 variant: TopAppBarVariant,
359 behavior: TopAppBarScrollBehavior,
360 content_offset: &mut f32,
361 content_max: f32,
362 ) -> f32 {
363 let visible = bounds.intersect(self.clip);
364 if !self.enabled
365 || self.key_consumed
366 || self.ui.focused != Some(target_id)
367 || visible.width <= 0.0
368 || visible.height <= 0.0
369 {
370 return 0.0;
371 }
372 let Some(key) = self.input.key.filter(|k| k.pressed) else {
373 return 0.0;
374 };
375 let range = if content_max.is_finite() {
376 content_max.max(0.0)
377 } else {
378 0.0
379 };
380 let full = range + variant.expanded_height() - 64.0;
381 let delta = match key.key {
382 Key::Down => 48.0,
383 Key::Up => -48.0,
384 Key::PageDown => visible.height,
385 Key::PageUp => -visible.height,
386 Key::Home => -full,
387 Key::End => full,
388 _ => return 0.0,
389 };
390 self.key_consumed = true;
391 state.snap_target = None;
392 state.fling_velocity = 0.0;
393 let consumed = state.route_scroll(variant, behavior, delta, content_offset, content_max);
394 if consumed != 0.0 {
395 self.ui.continuous = true;
396 }
397 consumed
398 }
399
400 #[allow(clippy::too_many_arguments)]
403 pub fn top_app_bar(
404 &mut self,
405 id: u64,
406 title: &str,
407 variant: TopAppBarVariant,
408 state: &TopAppBarState,
409 navigation: Option<AppBarAction<'_>>,
410 actions: &[AppBarAction<'_>],
411 rtl: bool,
412 ) -> Option<usize> {
413 self.top_app_bar_with_icons(id, title, variant, state, navigation, actions, rtl, &[])
414 }
415
416 #[allow(clippy::too_many_arguments)]
419 pub fn top_app_bar_with_icons(
420 &mut self,
421 id: u64,
422 title: &str,
423 variant: TopAppBarVariant,
424 state: &TopAppBarState,
425 navigation: Option<AppBarAction<'_>>,
426 actions: &[AppBarAction<'_>],
427 rtl: bool,
428 icons: &[Option<AppBarIcon>],
429 ) -> Option<usize> {
430 let fraction = state.collapse_fraction(variant);
431 let expanded_height = variant.expanded_height();
432 let height = expanded_height - (expanded_height - 64.0) * fraction;
433 let bar = self.reserve(height);
434 let t = self.ui.theme;
435 self.paint_rect(
436 bar,
437 0.0,
438 t.background.mix(t.surface_highest, 0.12 * fraction),
439 );
440 let mut selected = None;
441 let nav_width = if navigation.is_some() && bar.width >= 48.0 {
442 48.0
443 } else {
444 0.0
445 };
446 let count = actions
447 .len()
448 .min(((bar.width - nav_width).max(0.0) / 48.0) as usize);
449 let paint_action = |f: &mut Self, index: usize, action: AppBarAction<'_>, x: f32| -> bool {
450 let bounds = Rect {
451 x,
452 y: bar.y + 8.0,
453 width: 48.0,
454 height: 48.0,
455 };
456 let response = f.interact(id.wrapping_add(index as u64), bounds, action.enabled);
457 let color = t
458 .on_surface_variant
459 .opacity(if response.enabled { 1.0 } else { 0.38 });
460 f.state_layer(response, 24.0, color);
461 if let Some(Some(icon)) = icons.get(index) {
462 f.commands.push(DrawCommand::Shape {
463 bounds: bounds.inset(12.0),
464 index: icon.shape_index(rtl),
465 color,
466 rotation: 0.0,
467 });
468 } else {
469 f.styled_text(
470 bounds,
471 action.glyph,
472 color,
473 TextStyle {
474 size: 22.0,
475 align: 1,
476 ..TextStyle::default()
477 },
478 );
479 }
480 f.semantic(
481 response,
482 Role::Button,
483 action.label,
484 false,
485 None,
486 None,
487 None,
488 );
489 f.focus_ring(response);
490 response.clicked
491 };
492 if let Some(nav) = navigation.filter(|_| nav_width > 0.0) {
493 let x = if rtl { bar.x + bar.width - 48.0 } else { bar.x };
494 if paint_action(self, 0, nav, x) {
495 selected = Some(0);
496 }
497 }
498 for (i, action) in actions.iter().take(count).enumerate() {
499 let x = if rtl {
500 bar.x + i as f32 * 48.0
501 } else {
502 bar.x + bar.width - (i + 1) as f32 * 48.0
503 };
504 if paint_action(self, i + 1, *action, x) {
505 selected = Some(i + 1);
506 }
507 }
508 let left = if rtl { count as f32 * 48.0 } else { nav_width };
509 let right = if rtl { nav_width } else { count as f32 * 48.0 };
510 let centered = variant == TopAppBarVariant::CenterAligned;
511 let title_left = if centered { left.max(right) } else { left };
512 let title_right = if centered { left.max(right) } else { right };
513 let small_title = Rect {
514 x: bar.x + title_left + 16.0,
515 y: bar.y + 8.0,
516 width: (bar.width - title_left - title_right - 32.0).max(0.0),
517 height: 48.0,
518 };
519 let is_large = matches!(variant, TopAppBarVariant::Medium | TopAppBarVariant::Large);
520 let small_alpha = if is_large { fraction } else { 1.0 };
521 if small_alpha > 0.001 {
522 self.styled_text(
523 small_title,
524 title,
525 t.text.opacity(small_alpha),
526 TextStyle {
527 size: 22.0,
528 align: if variant == TopAppBarVariant::CenterAligned {
529 1
530 } else if rtl {
531 2
532 } else {
533 0
534 },
535 ..TextStyle::default()
536 },
537 );
538 }
539 if is_large && fraction < 0.999 {
540 let title_bounds = Rect {
541 x: bar.x + 16.0,
542 y: bar.y + height - 52.0,
543 width: (bar.width - 32.0).max(0.0),
544 height: 40.0,
545 };
546 self.styled_text(
547 title_bounds,
548 title,
549 t.text.opacity(1.0 - fraction),
550 TextStyle {
551 size: if variant == TopAppBarVariant::Large {
552 28.0
553 } else {
554 24.0
555 },
556 align: if rtl { 2 } else { 0 },
557 ..TextStyle::default()
558 },
559 );
560 }
561 selected
562 }
563
564 #[allow(clippy::too_many_arguments)]
567 pub fn bottom_app_bar_scroll_input(
568 &mut self,
569 target_id: u64,
570 bounds: Rect,
571 bar: &mut BottomAppBarState,
572 behavior: BottomAppBarScrollBehavior,
573 scroll: &mut TopAppBarState,
574 content_offset: &mut f32,
575 content_max: f32,
576 bar_height: f32,
577 ) -> f32 {
578 use crate::timing::{Easing, Tween, TweenSpec};
579 let height = if bar_height.is_finite() {
580 bar_height.max(0.0)
581 } else {
582 0.0
583 };
584 if behavior == BottomAppBarScrollBehavior::Pinned || bar.settle_height != height {
585 bar.settle_target = None;
586 }
587 if let Some(target) = bar.settle_target {
588 let spec = TweenSpec::new(0.25, Easing::Standard);
589 bar.hidden = bar
590 .settle
591 .sample(self.ui.time, target, spec, spec, self.reduced_motion())
592 .clamp(0.0, height);
593 if bar.settle.moving() {
594 self.ui.continuous = true;
595 } else {
596 bar.settle_target = None;
597 }
598 }
599 let was_dragging = scroll.dragging;
600 let was_flinging = scroll.fling_velocity != 0.0;
601 let key_scroll = self.ui.focused == Some(target_id)
602 && self.input.key.is_some_and(|k| {
603 k.pressed
604 && matches!(
605 k.key,
606 Key::Up | Key::Down | Key::Home | Key::End | Key::PageUp | Key::PageDown
607 )
608 });
609 let direct_input = self.input.pressed
610 || self.input.cancelled
611 || self.input.blurred
612 || key_scroll
613 || self.scroll_y(bounds) != 0.0
614 || !self.enabled;
615 let mut consumed = self.top_app_bar_scroll_input(
616 bounds,
617 scroll,
618 TopAppBarVariant::Small,
619 TopAppBarScrollBehavior::Pinned,
620 content_offset,
621 content_max,
622 );
623 consumed += self.top_app_bar_scroll_keyboard(
624 target_id,
625 bounds,
626 scroll,
627 TopAppBarVariant::Small,
628 TopAppBarScrollBehavior::Pinned,
629 content_offset,
630 content_max,
631 );
632 if direct_input {
633 bar.settle_target = None;
634 }
635 bar.apply_scroll(behavior, consumed, height);
636 let released =
637 was_dragging && self.input.released && !self.input.cancelled && !self.input.blurred;
638 let ended_fling = was_flinging && scroll.fling_velocity == 0.0 && !direct_input;
639 if behavior == BottomAppBarScrollBehavior::ExitAlways
640 && height > 0.0
641 && (released || ended_fling)
642 && scroll.fling_velocity == 0.0
643 && bar.hidden > 0.001
644 && bar.hidden < height - 0.001
645 {
646 let target = if bar.hidden / height < 0.5 {
647 0.0
648 } else {
649 height
650 };
651 bar.settle = Tween::at(bar.hidden);
652 bar.settle_target = Some(target);
653 bar.settle_height = height;
654 let spec = TweenSpec::new(0.25, Easing::Standard);
655 bar.hidden = bar
656 .settle
657 .sample(self.ui.time, target, spec, spec, self.reduced_motion())
658 .clamp(0.0, height);
659 if bar.settle.moving() {
660 self.ui.continuous = true;
661 } else {
662 bar.settle_target = None;
663 }
664 }
665 consumed
666 }
667
668 #[allow(clippy::too_many_arguments)]
671 pub fn scrolling_bottom_app_bar(
672 &mut self,
673 id: u64,
674 viewport: Rect,
675 state: &BottomAppBarState,
676 actions: &[AppBarAction<'_>],
677 icons: &[Option<AppBarIcon>],
678 fab: Option<(AppBarAction<'_>, Option<AppBarIcon>)>,
679 rtl: bool,
680 ) -> Option<usize> {
681 let height = viewport.height.clamp(0.0, 80.0);
682 if height < 56.0 || state.hidden >= height {
683 return None;
684 }
685 let bar = Rect {
686 y: viewport.y + viewport.height - height + state.hidden.clamp(0.0, height),
687 height,
688 ..viewport
689 };
690 let mut chosen = None;
691 self.within(viewport, |f| {
692 chosen = f.bottom_app_bar_at(id, bar, actions, icons, fab, rtl);
693 });
694 chosen
695 }
696 #[allow(clippy::too_many_arguments)]
701 pub fn scrolling_bottom_app_bar_with_overflow(
702 &mut self,
703 id: u64,
704 viewport: Rect,
705 bar: &BottomAppBarState,
706 menu: &mut BottomAppBarOverflowState,
707 actions: &[AppBarAction<'_>],
708 icons: &[Option<AppBarIcon>],
709 fab: Option<(AppBarAction<'_>, Option<AppBarIcon>)>,
710 rtl: bool,
711 ) -> Option<usize> {
712 if actions.len() > 64 || id.checked_add(actions.len() as u64 * 2 + 5).is_none() {
713 return None;
714 }
715 let height = viewport.height.clamp(0.0, 80.0);
716 let bar_bounds = Rect {
717 y: viewport.y + viewport.height - height + bar.hidden.clamp(0.0, height),
718 height,
719 ..viewport
720 };
721 let mut layout = (None, 0, false, None);
722 if height >= 56.0 && bar.hidden < height {
723 self.without_input(menu.overlay.open, |f| {
724 f.within(viewport, |f| {
725 layout = f.bottom_app_bar_inner(id, bar_bounds, actions, icons, fab, rtl, true);
726 })
727 });
728 }
729 let (mut chosen, visible, fab_visible, anchor) = layout;
730 let total =
731 actions.len().saturating_sub(visible) + usize::from(fab.is_some() && !fab_visible);
732 if anchor.is_none() || total == 0 {
733 menu.overlay.open = false;
734 }
735 if chosen == Some(actions.len() + 1) {
736 menu.overlay.open = true;
737 menu.offset = 0.0;
738 chosen = None;
739 }
740 let anchor = anchor.unwrap_or(bar_bounds);
741 let item_id = id + actions.len() as u64 + 2;
742 let overlay_id = item_id + total as u64 + 1;
743 let labels = |i: usize| -> AppBarAction<'_> {
744 let index = visible + i;
745 if index < actions.len() {
746 actions[index]
747 } else {
748 fab.expect("overflowed FAB").0
749 }
750 };
751 let width = (0..total)
752 .map(|i| self.text_width(labels(i).label, TextStyle::LABEL) + 48.0)
753 .fold(112.0_f32, f32::max)
754 .min(280.0)
755 .min(viewport.width);
756 let height = (total as f32 * 48.0 + 16.0).min((anchor.y - viewport.y - 4.0).max(0.0));
757 let bounds = Rect {
758 x: if rtl {
759 (anchor.x + anchor.width - width).max(viewport.x)
760 } else {
761 anchor.x.min(viewport.x + viewport.width - width)
762 },
763 y: anchor.y - height,
764 width,
765 height,
766 };
767 let body_height = (height - 16.0).max(0.0);
768 let max_offset = (total as f32 * 48.0 - body_height).max(0.0);
769 menu.offset = menu.offset.clamp(0.0, max_offset);
770 if !menu.overlay.open
771 || self.input.pressed
772 || self.input.cancelled
773 || self.input.blurred
774 || self.reduced_motion()
775 || self.input.scroll[1] != 0.0
776 || self.input.key.is_some()
777 {
778 menu.fling_velocity = 0.0;
779 }
780 if menu.fling_velocity != 0.0 {
781 let (movement, moving) = menu.fling_curve.step(self.ui.time);
782 let old = menu.offset;
783 menu.offset = (old + movement).clamp(0.0, max_offset);
784 if !moving { menu.fling_velocity = 0.; }
785 if menu.fling_velocity.abs() < 20.0 || (menu.offset - old - movement).abs() > 0.5 {
786 menu.fling_velocity = 0.0;
787 } else {
788 self.ui.continuous = true;
789 }
790 }
791 if menu.overlay.open {
792 let scroll = self.scroll_y(bounds);
793 if scroll.is_finite() {
794 menu.offset = (menu.offset + scroll).clamp(0.0, max_offset);
795 }
796 if max_offset > 0.0 && self.input.pointer_kind == PointerKind::Touch {
797 if self.input.pressed && self.pointer_in(bounds) {
798 menu.drag_origin = self.input.pointer.map(|p| p[1]);
799 menu.drag_last = menu.drag_origin;
800 menu.dragging = false;
801 menu.drag_velocity = 0.0;
802 menu.drag_sample_time = self.ui.time;
803 menu.drag_motion_time = self.ui.time;
804 } else if let (Some(origin), Some(last), Some(pointer)) =
805 (menu.drag_origin, menu.drag_last, self.input.pointer)
806 {
807 if (pointer[1] - origin).abs() > 4.0 {
808 menu.dragging = true;
809 }
810 if menu.dragging {
811 let movement = last - pointer[1];
812 menu.offset = (menu.offset + movement).clamp(0.0, max_offset);
813 if movement != 0.0 {
814 let elapsed = self.ui.time - menu.drag_sample_time;
815 menu.drag_velocity = if (0.005..=0.15).contains(&elapsed) {
816 (movement / elapsed as f32).clamp(-4000.0, 4000.0)
817 } else {
818 0.0
819 };
820 menu.drag_sample_time = self.ui.time;
821 menu.drag_motion_time = self.ui.time;
822 }
823 self.ui.active = None;
824 menu.drag_last = Some(pointer[1]);
825 }
826 }
827 }
828 if self.input.key.is_some_and(|key| key.pressed)
829 || self.input.blurred
830 || self.input.cancelled
831 {
832 let typeahead_items: Vec<_> = (0..total)
833 .map(|i| {
834 let action = labels(i);
835 (action.label, action.enabled)
836 })
837 .collect();
838 if let Some(next) =
839 self.menu_typeahead(&mut menu.typeahead, item_id, &typeahead_items)
840 {
841 let top = next as f32 * 48.0;
842 if top < menu.offset {
843 menu.offset = top;
844 } else if top + 48.0 > menu.offset + body_height {
845 menu.offset = (top + 48.0 - body_height).min(max_offset);
846 }
847 }
848 }
849 if let Some(current) = self
850 .focused_id()
851 .and_then(|v| v.checked_sub(item_id))
852 .map(|v| v as usize)
853 .filter(|i| *i < total)
854 {
855 let forward = self.pressed_key(Key::Down);
856 let backward = self.pressed_key(Key::Up);
857 let home = self.pressed_key(Key::Home);
858 let end = self.pressed_key(Key::End);
859 if forward || backward || home || end {
860 let candidate = (0..total)
861 .map(|step| {
862 if home {
863 step
864 } else if end {
865 total - 1 - step
866 } else if forward {
867 (current + step + 1) % total
868 } else {
869 (current + total - 1 - step) % total
870 }
871 })
872 .find(|i| labels(*i).enabled);
873 if let Some(next) = candidate {
874 let top = next as f32 * 48.0;
875 if top < menu.offset {
876 menu.offset = top;
877 } else if top + 48.0 > menu.offset + body_height {
878 menu.offset = (top + 48.0 - body_height).min(max_offset);
879 }
880 self.request_focus(item_id + next as u64);
881 }
882 }
883 }
884 }
885 if !menu.overlay.open {
886 menu.typeahead.reset();
887 }
888 if self.input.released || self.input.cancelled || self.input.blurred || !menu.overlay.open {
889 if menu.dragging {
890 self.ui.active = None;
891 if self.input.released
892 && !self.input.cancelled
893 && !self.input.blurred
894 && menu.overlay.open
895 && !self.reduced_motion()
896 && self.ui.time - menu.drag_motion_time <= 0.1
897 && menu.drag_velocity.abs() >= 350.0
898 {
899 menu.fling_velocity = menu.drag_velocity;
900 menu.fling_curve.start(menu.fling_velocity,self.ui.time);
901 self.ui.continuous = true;
902 }
903 }
904 menu.drag_origin = None;
905 menu.drag_last = None;
906 menu.dragging = false;
907 }
908 menu.overlay.kind = OverlayKind::Menu;
909 menu.overlay.style = OverlayStyle::AngularMaterial;
910 self.overlay(overlay_id, &mut menu.overlay, bounds, false, |f, open| {
911 let content = f.available();
912 f.within(content, |f| {
913 let rows = Rect {
914 y: content.y - menu.offset,
915 height: total as f32 * 48.0,
916 ..content
917 };
918 f.within(rows, |f| {
919 f.with_gap(0.0, |f| {
920 for i in 0..total {
921 let action = labels(i);
922 if f.menu_item(item_id + i as u64, action.label, action.enabled)
923 .clicked
924 {
925 chosen = Some(visible + i);
926 *open = false;
927 }
928 }
929 })
930 });
931 });
932 });
933 chosen
934 }
935
936 #[allow(clippy::too_many_arguments)]
940 pub fn bottom_app_bar_at(
941 &mut self,
942 id: u64,
943 bounds: Rect,
944 actions: &[AppBarAction<'_>],
945 icons: &[Option<AppBarIcon>],
946 fab: Option<(AppBarAction<'_>, Option<AppBarIcon>)>,
947 rtl: bool,
948 ) -> Option<usize> {
949 self.bottom_app_bar_inner(id, bounds, actions, icons, fab, rtl, false)
950 .0
951 }
952
953 #[allow(clippy::too_many_arguments)]
954 fn bottom_app_bar_inner(
955 &mut self,
956 id: u64,
957 bounds: Rect,
958 actions: &[AppBarAction<'_>],
959 icons: &[Option<AppBarIcon>],
960 fab: Option<(AppBarAction<'_>, Option<AppBarIcon>)>,
961 rtl: bool,
962 use_overflow: bool,
963 ) -> (Option<usize>, usize, bool, Option<Rect>) {
964 if !bounds.x.is_finite()
965 || !bounds.y.is_finite()
966 || !bounds.width.is_finite()
967 || !bounds.height.is_finite()
968 || bounds.width <= 0.0
969 || bounds.height < 56.0
970 {
971 return (None, 0, false, None);
972 }
973 let t = self.ui.theme;
974 self.paint_rect(bounds, 0.0, t.surface_low);
975 let mut fab_visible = fab.is_some() && bounds.width >= 88.0;
976 let mut slots =
977 ((bounds.width - if fab_visible { 80.0 } else { 8.0 }).max(0.0) / 48.0) as usize;
978 let mut needs_overflow =
979 use_overflow && (actions.len() > slots || fab.is_some() && !fab_visible);
980 if needs_overflow && slots == 0 && fab_visible {
981 fab_visible = false;
982 slots = ((bounds.width - 8.0).max(0.0) / 48.0) as usize;
983 }
984 needs_overflow &= slots > 0;
985 let count = actions
986 .len()
987 .min(slots.saturating_sub(usize::from(needs_overflow)));
988 let mut chosen = None;
989 for (i, action) in actions.iter().take(count).enumerate() {
990 let b = Rect {
991 x: if rtl {
992 bounds.x + bounds.width - 4.0 - (i + 1) as f32 * 48.0
993 } else {
994 bounds.x + 4.0 + i as f32 * 48.0
995 },
996 y: bounds.y + (bounds.height - 48.0) / 2.0,
997 width: 48.0,
998 height: 48.0,
999 };
1000 let r = self.interact(id.wrapping_add(i as u64), b, action.enabled);
1001 let color = t
1002 .on_surface_variant
1003 .opacity(if r.enabled { 1.0 } else { 0.38 });
1004 self.state_layer(r, 24.0, color);
1005 if let Some(Some(icon)) = icons.get(i) {
1006 self.commands.push(DrawCommand::Shape {
1007 bounds: b.inset(12.0),
1008 index: icon.shape_index(rtl),
1009 color,
1010 rotation: 0.0,
1011 });
1012 } else {
1013 self.styled_text(
1014 b,
1015 action.glyph,
1016 color,
1017 TextStyle {
1018 size: 22.0,
1019 align: 1,
1020 ..TextStyle::default()
1021 },
1022 );
1023 }
1024 self.semantic(r, Role::Button, action.label, false, None, None, None);
1025 self.focus_ring(r);
1026 if r.clicked {
1027 chosen = Some(i);
1028 }
1029 }
1030 let overflow_bounds = if needs_overflow {
1031 let b = Rect {
1032 x: if rtl {
1033 bounds.x + bounds.width - 4.0 - (count + 1) as f32 * 48.0
1034 } else {
1035 bounds.x + 4.0 + count as f32 * 48.0
1036 },
1037 y: bounds.y + (bounds.height - 48.0) / 2.0,
1038 width: 48.0,
1039 height: 48.0,
1040 };
1041 let r = self.interact(id.wrapping_add(actions.len() as u64 + 1), b, true);
1042 self.state_layer(r, 24.0, t.on_surface_variant);
1043 self.commands.push(DrawCommand::Shape {
1044 bounds: b.inset(12.0),
1045 index: AppBarIcon::More.shape_index(rtl),
1046 color: t
1047 .on_surface_variant
1048 .opacity(if r.enabled { 1.0 } else { 0.38 }),
1049 rotation: 0.0,
1050 });
1051 self.semantic(r, Role::Button, "More actions", false, None, None, None);
1052 self.focus_ring(r);
1053 if r.clicked {
1054 chosen = Some(actions.len() + 1);
1055 }
1056 Some(b)
1057 } else {
1058 None
1059 };
1060 if let Some((action, icon)) = fab.filter(|_| fab_visible) {
1061 let b = Rect {
1062 x: if rtl {
1063 bounds.x + 16.0
1064 } else {
1065 bounds.x + bounds.width - 72.0
1066 },
1067 y: bounds.y + (bounds.height - 56.0) / 2.0,
1068 width: 56.0,
1069 height: 56.0,
1070 };
1071 let r = self.interact(id.wrapping_add(actions.len() as u64), b, action.enabled);
1072 let fg = if r.enabled {
1073 t.on_secondary
1074 } else {
1075 t.text.opacity(0.38)
1076 };
1077 self.paint_rect(
1078 b,
1079 16.0,
1080 if r.enabled {
1081 t.secondary
1082 } else {
1083 t.text.opacity(0.12)
1084 },
1085 );
1086 self.state_layer(r, 16.0, fg);
1087 if let Some(icon) = icon {
1088 self.commands.push(DrawCommand::Shape {
1089 bounds: b.inset(16.0),
1090 index: icon.shape_index(rtl),
1091 color: fg,
1092 rotation: 0.0,
1093 });
1094 } else {
1095 self.styled_text(
1096 b,
1097 action.glyph,
1098 fg,
1099 TextStyle {
1100 size: 24.0,
1101 align: 1,
1102 ..TextStyle::default()
1103 },
1104 );
1105 }
1106 self.semantic(r, Role::Button, action.label, false, None, None, None);
1107 self.focus_ring(r);
1108 if r.clicked {
1109 chosen = Some(actions.len());
1110 }
1111 }
1112 (chosen, count, fab_visible, overflow_bounds)
1113 }
1114
1115 #[allow(clippy::too_many_arguments)]
1118 pub fn top_app_bar_at(
1119 &mut self,
1120 id: u64,
1121 bounds: Rect,
1122 title: &str,
1123 centered: bool,
1124 navigation: Option<TopAppBarMaterialAction<'_>>,
1125 actions: &[TopAppBarMaterialAction<'_>],
1126 ) -> Option<usize> {
1127 let t = self.theme();
1128 self.paint_rect(bounds, 0.0, t.surface_low);
1129 let mut clicked = None;
1130 let btn_y = bounds.y + (bounds.height - 48.0) * 0.5;
1131 let nav_w: f32 = if let Some(nav) = navigation {
1132 let hit = Rect { x: bounds.x + 4.0, y: btn_y, width: 48.0, height: 48.0 };
1133 let r = self.material_icon_button_at(
1134 id,
1135 hit,
1136 nav.icon,
1137 nav.label,
1138 Options { enabled: nav.enabled, variant: ButtonVariant::Text, ..Options::default() },
1139 );
1140 if r.clicked {
1141 clicked = Some(0);
1142 }
1143 56.0
1144 } else {
1145 16.0
1146 };
1147 let count = actions.len();
1148 for (i, act) in actions.iter().enumerate() {
1149 let from_right = (count - i) as f32;
1150 let hit = Rect {
1151 x: bounds.x + bounds.width - 4.0 - from_right * 48.0,
1152 y: btn_y,
1153 width: 48.0,
1154 height: 48.0,
1155 };
1156 let r = self.material_icon_button_at(
1157 id.wrapping_add(1 + i as u64),
1158 hit,
1159 act.icon,
1160 act.label,
1161 Options { enabled: act.enabled, variant: ButtonVariant::Text, ..Options::default() },
1162 );
1163 if r.clicked {
1164 clicked = Some(1 + i);
1165 }
1166 }
1167 let trailing_w = if count > 0 { 8.0 + count as f32 * 48.0 } else { 16.0 };
1168 let (title_x, title_w, align) = if centered {
1169 let inset = nav_w.max(trailing_w);
1170 (bounds.x + inset, (bounds.width - inset * 2.0).max(0.0), 1)
1171 } else {
1172 (bounds.x + nav_w + 4.0, (bounds.width - nav_w - trailing_w - 8.0).max(0.0), 0)
1173 };
1174 self.styled_text(
1175 Rect { x: title_x, y: bounds.y, width: title_w, height: bounds.height },
1176 title,
1177 t.text,
1178 TextStyle { size: 20.0, weight: if centered { 600 } else { 500 }, align, ..TextStyle::default() },
1179 );
1180 clicked
1181 }
1182
1183 #[allow(clippy::too_many_arguments)]
1188 pub fn top_app_bar_collapsing(
1189 &mut self,
1190 id: u64,
1191 bounds: Rect,
1192 title: &str,
1193 expanded_h: f32,
1194 collapsed_h: f32,
1195 scroll_y: f32,
1196 back_icon: Option<MaterialIcon>,
1197 ) -> (f32, bool) {
1198 let collapsed_h = collapsed_h.max(48.0);
1199 let expanded_h = expanded_h.max(collapsed_h);
1200 let range = (expanded_h - collapsed_h).max(1.0);
1201 let collapse = (scroll_y / range).clamp(0.0, 1.0);
1202 let bar_h = expanded_h + (collapsed_h - expanded_h) * collapse;
1203 let t = self.theme();
1204 let bg = t.background.mix(t.surface_high, collapse);
1205 let bar = Rect { height: bar_h, ..bounds };
1206 self.paint_rect(bar, 0.0, bg);
1207 let mut back_clicked = false;
1208 if let Some(icon) = back_icon {
1209 let hit = Rect { x: bar.x + 4.0, y: bar.y + (collapsed_h - 48.0) * 0.5, width: 48.0, height: 48.0 };
1210 let r = self.material_icon_button_at(
1211 id,
1212 hit,
1213 icon,
1214 "Navigate up",
1215 Options { variant: ButtonVariant::Text, ..Options::default() },
1216 );
1217 back_clicked = r.clicked;
1218 }
1219 let start_x = bar.x + 20.0;
1220 let end_x = bar.x + if back_icon.is_some() { 60.0 } else { 20.0 };
1221 let title_x = start_x + (end_x - start_x) * collapse;
1222 let start_y = bar.y + expanded_h - 48.0;
1223 let end_y = bar.y + (collapsed_h - 32.0) * 0.5;
1224 let title_y = start_y + (end_y - start_y) * collapse;
1225 let title_size = 32.0 + (20.0 - 32.0) * collapse;
1226 self.styled_text(
1227 Rect {
1228 x: title_x,
1229 y: title_y,
1230 width: (bar.x + bar.width - title_x - 16.0).max(0.0),
1231 height: 36.0,
1232 },
1233 title,
1234 t.text,
1235 TextStyle { size: title_size, weight: 500, ..TextStyle::default() },
1236 );
1237 (bar_h, back_clicked)
1238 }
1239
1240 pub fn floating_pill_app_bar(
1244 &mut self,
1245 id: u64,
1246 bounds: Rect,
1247 slots: &[FloatingPillSlot<'_>],
1248 mut custom_slot: impl FnMut(&mut Self, usize, Rect),
1249 ) -> Option<usize> {
1250 let t = self.theme();
1251 let radius = bounds.height * 0.5;
1252 self.shadow(bounds, radius, [0.0, 2.0], 5.0, 0.0, Color(0, 0, 0, 35));
1253 self.paint_rect(bounds, radius, t.primary);
1254 let mut clicked = None;
1255 let btn_sz = 48.0_f32.min(bounds.height);
1256 let pad_x = 4.0;
1257 let pad_y = (bounds.height - btn_sz) * 0.5;
1258 for (i, slot) in slots.iter().enumerate() {
1259 let hit = Rect {
1260 x: bounds.x + pad_x + i as f32 * btn_sz,
1261 y: bounds.y + pad_y,
1262 width: btn_sz,
1263 height: btn_sz,
1264 };
1265 let r = self.target(id.wrapping_add(i as u64), hit, slot.label, true, Role::Button);
1266 if r.hovered || (r.focused && self.ui.keyboard_focus) {
1267 self.paint_rect(hit, btn_sz * 0.5, t.on_primary.opacity(0.12));
1268 }
1269 self.ink(r, btn_sz * 0.5, t.on_primary);
1270 self.focus_ring(r);
1271 if let Some(icon) = slot.icon {
1272 self.paint_material_icon(hit.inset(12.0), icon, t.on_primary);
1273 } else {
1274 custom_slot(self, i, hit.inset(8.0));
1275 }
1276 if r.clicked {
1277 clicked = Some(i);
1278 }
1279 }
1280 clicked
1281 }
1282}
1283
1284#[derive(Clone, Copy, Debug)]
1286pub struct TopAppBarMaterialAction<'a> {
1287 pub icon: MaterialIcon,
1288 pub label: &'a str,
1289 pub enabled: bool,
1290}
1291
1292#[derive(Clone, Copy, Debug)]
1294pub struct FloatingPillSlot<'a> {
1295 pub label: &'a str,
1296 pub icon: Option<MaterialIcon>,
1297}
1298
1299#[cfg(test)]
1300#[path = "app_bar.spec.rs"]
1301mod tests;