1use crate::*;
2
3#[derive(Clone, Copy, Debug)]
6pub struct NavigationDestination<'a> {
7 pub label: &'a str,
8 pub icon: &'a str,
9 pub enabled: bool,
10 pub badge: Option<(BadgeContent<'a>, &'a str)>,
11}
12
13#[derive(Clone, Copy, Debug)]
15pub enum NavigationIcon {
16 Home,
17 Folder,
18 Settings,
19 Alarm,
20 Clock,
21 Timer,
22 Stopwatch,
23 Menu,
24}
25impl NavigationIcon {
26 pub(super) fn shape_index(self, selected: bool) -> u8 {
27 let base = match self {
28 Self::Home => 0,
29 Self::Folder => 2,
30 Self::Settings => 4,
31 Self::Alarm => 6,
32 Self::Clock => 8,
33 Self::Timer => 10,
34 Self::Stopwatch => 12,
35 Self::Menu => 14,
36 };
37 (crate::beercss_shapes::SHAPES.len() + base + usize::from(!selected)) as u8
38 }
39}
40
41impl Frame<'_> {
44 #[allow(clippy::too_many_arguments)]
46 pub fn material_navigation_item(
47 &mut self,
48 id: u64,
49 b: Rect,
50 label: &str,
51 icon: MaterialIcon,
52 selected: bool,
53 expansion: f32,
54 enabled: bool,
55 ) -> Response {
56 let r = self.interact(id, b, enabled);
57 self.semantic(r, Role::ListItem, label, selected, None, None, None);
58 let t = self.theme();
59 let selected_alpha = self.transition(
60 id,
61 90,
62 f32::from(selected),
63 f32::from(selected),
64 0.2,
65 timing::Easing::Standard,
66 );
67 self.paint_rect(b, b.height / 2., t.secondary.opacity(selected_alpha));
68 let color = if selected {
69 t.on_secondary
70 } else {
71 t.on_surface_variant
72 };
73 self.state_layer(r, b.height / 2., color);
74 let x = b.x + (b.width - 24.) / 2. + (16. - (b.width - 24.) / 2.) * expansion;
75 self.paint_material_icon(
76 Rect {
77 x,
78 y: b.y + (b.height - 24.) / 2.,
79 width: 24.,
80 height: 24.,
81 },
82 icon,
83 color,
84 );
85 self.styled_text(
86 Rect {
87 x: b.x + 52.,
88 width: (b.width - 64.).max(0.),
89 ..b
90 },
91 label,
92 color.opacity(expansion),
93 TextStyle {
94 size: 14.,
95 weight: 500,
96 ..TextStyle::default()
97 },
98 );
99 self.focus_ring(r);
100 r
101 }
102 #[allow(clippy::too_many_arguments)]
105 pub fn material_drawer_item(
106 &mut self,
107 id: u64,
108 bounds: Rect,
109 label: &str,
110 icon: MaterialIcon,
111 selected: bool,
112 enabled: bool,
113 ) -> Response {
114 let r = self.interact(id, bounds, enabled);
115 self.semantic(r, Role::ListItem, label, selected, None, None, None);
116 let t = self.theme();
117 let color = if selected {
118 t.on_secondary
119 } else {
120 t.on_surface_variant
121 };
122 let indicator = Rect {
123 x: bounds.x + 12.,
124 width: (bounds.width - 24.).max(0.),
125 ..bounds
126 };
127 let p = self.transition(
128 id,
129 90,
130 f32::from(selected),
131 f32::from(selected),
132 0.2,
133 timing::Easing::Standard,
134 );
135 self.paint_rect(indicator, indicator.height / 2., t.secondary.opacity(p));
136 let state = Response {
137 bounds: indicator,
138 ..r
139 };
140 self.state_layer(state, indicator.height / 2., color);
141 self.paint_material_icon(
142 Rect {
143 x: bounds.x + 28.,
144 y: bounds.y + (bounds.height - 24.) / 2.,
145 width: 24.,
146 height: 24.,
147 },
148 icon,
149 color,
150 );
151 self.styled_text(
152 Rect {
153 x: bounds.x + 64.,
154 width: (bounds.width - 92.).max(0.),
155 ..bounds
156 },
157 label,
158 color,
159 TextStyle {
160 size: 14.,
161 weight: if selected { 700 } else { 500 },
162 ..TextStyle::default()
163 },
164 );
165 self.focus_ring(r);
166 r
167 }
168 #[allow(clippy::too_many_arguments)]
170 pub fn material_navigation_bar_item(
171 &mut self,
172 id: u64,
173 b: Rect,
174 label: &str,
175 icon: MaterialIcon,
176 selected: bool,
177 enabled: bool,
178 ) -> Response {
179 let r = self.interact(id, b, enabled);
180 self.semantic(r, Role::Tab, label, selected, None, None, None);
181 let t = self.theme();
182 let p = self.transition(
183 id,
184 91,
185 f32::from(selected),
186 f32::from(selected),
187 0.3,
188 timing::Easing::Standard,
189 );
190 let indicator = Rect {
191 x: b.x + b.width / 2. - 32.,
192 y: b.y + 8.,
193 width: 64.,
194 height: 32.,
195 };
196 self.paint_rect(
197 Rect {
198 x: indicator.x + 32. * (1. - p),
199 width: 64. * p,
200 ..indicator
201 },
202 16.,
203 t.secondary.opacity(p),
204 );
205 self.state_layer(
206 Response {
207 bounds: indicator,
208 ..r
209 },
210 16.,
211 t.on_surface_variant,
212 );
213 self.paint_material_icon(
214 Rect {
215 x: b.x + b.width / 2. - 12.,
216 y: b.y + 12.,
217 width: 24.,
218 height: 24.,
219 },
220 icon,
221 if selected {
222 t.on_secondary
223 } else {
224 t.on_surface_variant
225 },
226 );
227 self.styled_text(
228 Rect {
229 y: b.y + 44.,
230 height: 24.,
231 ..b
232 },
233 label,
234 t.on_surface_variant,
235 TextStyle {
236 size: 12.,
237 weight: 500,
238 align: 1,
239 ..TextStyle::default()
240 },
241 );
242 self.focus_ring(r);
243 r
244 }
245}
246
247impl Frame<'_> {
248 pub fn navigation_bar(
250 &mut self,
251 id: u64,
252 destinations: &[NavigationDestination<'_>],
253 selected: &mut usize,
254 rtl: bool,
255 ) -> bool {
256 self.navigation_bar_with_labels(id, destinations, selected, rtl, true)
257 }
258
259 pub fn navigation_bar_with_labels(
262 &mut self,
263 id: u64,
264 destinations: &[NavigationDestination<'_>],
265 selected: &mut usize,
266 rtl: bool,
267 always_show_labels: bool,
268 ) -> bool {
269 self.navigation_bar_with_icons(id, destinations, selected, rtl, always_show_labels, &[])
270 }
271
272 pub fn navigation_bar_with_icons(
275 &mut self,
276 id: u64,
277 destinations: &[NavigationDestination<'_>],
278 selected: &mut usize,
279 rtl: bool,
280 always_show_labels: bool,
281 icons: &[NavigationIcon],
282 ) -> bool {
283 let bar = self.reserve(80.0);
284 self.navigation_destinations_at(
285 id,
286 bar,
287 destinations,
288 selected,
289 rtl,
290 always_show_labels,
291 icons,
292 false,
293 0.0,
294 false,
295 )
296 }
297
298 pub fn navigation_rail(
301 &mut self,
302 id: u64,
303 destinations: &[NavigationDestination<'_>],
304 selected: &mut usize,
305 rtl: bool,
306 always_show_labels: bool,
307 icons: &[NavigationIcon],
308 ) -> bool {
309 let allocated = self.reserve(destinations.len() as f32 * 60.0 + 4.0);
310 let bar = Rect {
311 width: allocated.width.min(80.0),
312 ..allocated
313 };
314 self.navigation_destinations_at(
315 id,
316 bar,
317 destinations,
318 selected,
319 rtl,
320 always_show_labels,
321 icons,
322 true,
323 0.0,
324 false,
325 )
326 }
327
328 #[allow(clippy::too_many_arguments)]
331 pub fn animated_navigation_rail(
332 &mut self,
333 id: u64,
334 destinations: &[NavigationDestination<'_>],
335 selected: &mut usize,
336 rtl: bool,
337 always_show_labels: bool,
338 icons: &[NavigationIcon],
339 expanded: bool,
340 ) -> bool {
341 let allocated = self.reserve(destinations.len() as f32 * 60.0 + 4.0);
342 let target = f32::from(expanded);
343 let progress = self.spatial(id, 75, target, target).clamp(0.0, 1.0);
344 let width = (80.0 + 160.0 * progress).min(allocated.width);
345 let rail = Rect { width, ..allocated };
346 self.navigation_destinations_at(
347 id,
348 rail,
349 destinations,
350 selected,
351 rtl,
352 always_show_labels,
353 icons,
354 true,
355 progress,
356 false,
357 )
358 }
359
360 #[allow(clippy::too_many_arguments)]
362 pub fn expressive_navigation_rail(
363 &mut self,
364 id: u64,
365 destinations: &[NavigationDestination<'_>],
366 selected: &mut usize,
367 rtl: bool,
368 icons: &[NavigationIcon],
369 expanded: bool,
370 ) -> bool {
371 let progress = self.navigation_rail_expansion(id, expanded);
372 self.expressive_navigation_rail_at_progress(
373 id,
374 destinations,
375 selected,
376 rtl,
377 icons,
378 progress,
379 )
380 }
381
382 pub fn navigation_rail_expansion(&mut self, id: u64, expanded: bool) -> f32 {
384 let target = f32::from(expanded);
385 self.spatial(id, 75, target, target).clamp(0.0, 1.0)
386 }
387
388 pub fn expressive_navigation_rail_at_progress(
390 &mut self,
391 id: u64,
392 destinations: &[NavigationDestination<'_>],
393 selected: &mut usize,
394 rtl: bool,
395 icons: &[NavigationIcon],
396 progress: f32,
397 ) -> bool {
398 let progress = if progress.is_finite() {
399 progress.clamp(0.0, 1.0)
400 } else {
401 0.0
402 };
403 let allocated = self.reserve(destinations.len() as f32 * 68.0 + 4.0);
404 let width = (96.0 + 144.0 * progress).min(allocated.width);
405 self.navigation_destinations_at(
406 id,
407 Rect { width, ..allocated },
408 destinations,
409 selected,
410 rtl,
411 true,
412 icons,
413 true,
414 progress,
415 true,
416 )
417 }
418
419 pub fn wide_navigation_rail(
422 &mut self,
423 id: u64,
424 destinations: &[NavigationDestination<'_>],
425 selected: &mut usize,
426 rtl: bool,
427 icons: &[NavigationIcon],
428 ) -> bool {
429 let allocation = self.reserve(destinations.len() as f32 * 60.0 + 4.0);
430 let rail = Rect {
431 width: allocation.width.min(240.0),
432 ..allocation
433 };
434 if destinations.is_empty() {
435 return false;
436 }
437 *selected = (*selected).min(destinations.len() - 1);
438 let old = *selected;
439 let t = self.ui.theme;
440 self.paint_rect(rail, 0.0, t.surface_low);
441 let mut responses = Vec::with_capacity(destinations.len());
442 for (i, destination) in destinations.iter().enumerate() {
443 let item = Rect {
444 x: rail.x + 16.0_f32.min(rail.width / 2.0),
445 y: rail.y + 4.0 + i as f32 * 60.0,
446 width: (rail.width - 32.0).max(0.0),
447 height: 56.0,
448 };
449 let response = self.interact(id.wrapping_add(i as u64), item, destination.enabled);
450 if response.clicked {
451 *selected = i;
452 }
453 responses.push(response);
454 }
455 for (i, (destination, response)) in destinations.iter().zip(responses).enumerate() {
456 let active = i == *selected;
457 let item = response.bounds;
458 let p = self
459 .effect(response.id, 74, f32::from(active), f32::from(active))
460 .clamp(0.0, 1.0);
461 self.paint_rect(item, 28.0, t.secondary.opacity(p));
462 let icon_color = if active {
463 t.on_secondary
464 } else {
465 t.on_surface_variant
466 }
467 .opacity(if response.enabled { 1.0 } else { 0.38 });
468 let label_color = if active { t.text } else { t.on_surface_variant }
469 .opacity(if response.enabled { 1.0 } else { 0.38 });
470 self.state_layer(response, 28.0, icon_color);
471 let icon = Rect {
472 x: if rtl {
473 item.x + item.width - 40.0
474 } else {
475 item.x + 16.0
476 },
477 y: item.y + 16.0,
478 width: 24.0,
479 height: 24.0,
480 };
481 if let Some(builtin) = icons.get(i) {
482 self.commands.push(DrawCommand::Shape {
483 bounds: icon,
484 index: builtin.shape_index(active),
485 color: icon_color,
486 rotation: 0.0,
487 });
488 } else {
489 self.styled_text(
490 icon,
491 destination.icon,
492 icon_color,
493 TextStyle {
494 size: 22.0,
495 align: 1,
496 ..TextStyle::default()
497 },
498 );
499 }
500 let label = Rect {
501 x: item.x + if rtl { 16.0 } else { 48.0 },
502 y: item.y,
503 width: (item.width - 64.0).max(0.0),
504 height: item.height,
505 };
506 self.styled_text(
507 label,
508 destination.label,
509 label_color,
510 TextStyle {
511 size: 14.0,
512 weight: 500,
513 align: if rtl { 2 } else { 0 },
514 ..TextStyle::default()
515 },
516 );
517 self.semantic(
518 response,
519 Role::Tab,
520 destination.label,
521 active,
522 None,
523 None,
524 None,
525 );
526 if let Some((badge, description)) = destination.badge {
527 self.badge_at(response.id, icon, badge, description, rtl);
528 }
529 self.focus_ring(response);
530 }
531 old != *selected
532 }
533
534 #[allow(clippy::too_many_arguments)]
535 fn navigation_destinations_at(
536 &mut self,
537 id: u64,
538 bar: Rect,
539 destinations: &[NavigationDestination<'_>],
540 selected: &mut usize,
541 rtl: bool,
542 always_show_labels: bool,
543 icons: &[NavigationIcon],
544 rail: bool,
545 rail_expansion: f32,
546 expressive: bool,
547 ) -> bool {
548 if destinations.is_empty() {
549 return false;
550 }
551 *selected = (*selected).min(destinations.len() - 1);
552 let old = *selected;
553 let width = if rail {
554 bar.width
555 } else {
556 bar.width / destinations.len() as f32
557 };
558 let t = self.ui.theme;
559 self.paint_rect(
560 bar,
561 0.0,
562 if expressive {
563 t.background
564 } else {
565 t.surface_low
566 },
567 );
568 let mut responses = Vec::with_capacity(destinations.len());
569 for (i, destination) in destinations.iter().enumerate() {
570 let bounds = if rail {
571 Rect {
572 x: bar.x + 16.0 * rail_expansion,
573 y: bar.y + 4.0 + i as f32 * if expressive { 68.0 } else { 60.0 },
574 width: (width - 32.0 * rail_expansion).max(0.0),
575 height: if expressive { 64.0 } else { 56.0 },
576 }
577 } else {
578 let column = if rtl { destinations.len() - 1 - i } else { i };
579 Rect {
580 x: bar.x + column as f32 * width,
581 width,
582 ..bar
583 }
584 };
585 let response = self.interact(id.wrapping_add(i as u64), bounds, destination.enabled);
586 if response.clicked {
587 *selected = i;
588 }
589 responses.push(response);
590 }
591 for (i, (destination, response)) in destinations.iter().zip(responses).enumerate() {
592 let active = i == *selected;
593 let center = response.bounds.x + response.bounds.width / 2.0;
594 let indicator_width = 56.0_f32.min(width);
596 let progress = self
597 .spatial(response.id, 72, f32::from(active), f32::from(active))
598 .clamp(0.0, 1.0);
599 let label_opacity = self
600 .effect(response.id, 73, f32::from(active), f32::from(active))
601 .clamp(0.0, 1.0);
602 let offset = if always_show_labels {
603 0.0
604 } else {
605 12.0 * (1.0 - progress) * (1.0 - rail_expansion)
606 };
607 let expanded_width = if expressive {
608 (self.text_width(
609 destination.label,
610 TextStyle {
611 size: 14.0,
612 weight: 500,
613 ..TextStyle::default()
614 },
615 ) + 64.0)
616 .min((response.bounds.width - 8.0).max(0.0))
617 } else {
618 response.bounds.width
619 };
620 let indicator_width =
621 indicator_width + (expanded_width - indicator_width) * rail_expansion;
622 let indicator_height = 32.0 + 24.0 * rail_expansion;
623 let narrow_x = center - indicator_width / 2.0;
624 let expanded_x = if rtl {
625 response.bounds.x + response.bounds.width - 4.0 - indicator_width
626 } else {
627 response.bounds.x + 4.0
628 };
629 let indicator = Rect {
630 x: if expressive {
631 narrow_x + (expanded_x - narrow_x) * rail_expansion
632 } else {
633 narrow_x
634 },
635 y: response.bounds.y
636 + if expressive {
637 2.0 + 2.0 * rail_expansion
638 } else if rail {
639 0.0
640 } else {
641 12.0
642 }
643 + offset,
644 width: indicator_width,
645 height: indicator_height,
646 };
647 self.paint_rect(
648 Rect {
649 x: indicator.x + indicator_width * (1.0 - progress) / 2.0,
650 width: indicator_width * progress,
651 ..indicator
652 },
653 indicator_height / 2.0,
654 t.secondary.opacity(progress),
655 );
656 let color = if active {
657 t.on_secondary
658 } else {
659 t.on_surface_variant
660 }
661 .opacity(if response.enabled { 1.0 } else { 0.38 });
662 let label_color = if active { t.text } else { t.on_surface_variant }
664 .opacity(if response.enabled { 1.0 } else { 0.38 });
665 self.state_layer(
666 Response {
667 bounds: indicator,
668 ..response
669 },
670 indicator_height / 2.0,
671 color,
672 );
673 let narrow_icon_x = center - 12.0;
674 let expanded_icon_x = if rtl {
675 response.bounds.x + response.bounds.width - if expressive { 44.0 } else { 40.0 }
676 } else {
677 response.bounds.x + if expressive { 20.0 } else { 16.0 }
678 };
679 let icon = Rect {
680 x: narrow_icon_x + (expanded_icon_x - narrow_icon_x) * rail_expansion,
681 y: response.bounds.y
682 + if rail {
683 if expressive {
684 6.0 + 14.0 * rail_expansion
685 } else {
686 4.0 + 12.0 * rail_expansion
687 }
688 } else {
689 16.0
690 }
691 + offset,
692 width: 24.0,
693 height: 24.0,
694 };
695 if let Some(builtin) = icons.get(i) {
696 self.commands.push(DrawCommand::Shape {
697 bounds: icon,
698 index: builtin.shape_index(active),
699 color,
700 rotation: 0.0,
701 });
702 } else {
703 self.styled_text(
704 icon,
705 destination.icon,
706 color,
707 TextStyle {
708 size: 22.0,
709 align: 1,
710 ..TextStyle::default()
711 },
712 );
713 }
714 if always_show_labels || label_opacity > 0.001 || rail_expansion > 0.001 {
715 let narrow_label = Rect {
716 y: response.bounds.y
717 + if expressive {
718 42.0
719 } else if rail {
720 36.0
721 } else {
722 48.0
723 }
724 + offset,
725 height: 20.0,
726 ..response.bounds
727 };
728 let wide_label = Rect {
729 x: response.bounds.x
730 + if rtl {
731 16.0
732 } else if expressive {
733 52.0
734 } else {
735 48.0
736 },
737 y: response.bounds.y,
738 width: (response.bounds.width - 64.0).max(0.0),
739 height: response.bounds.height,
740 };
741 let mix = |a: f32, b: f32| a + (b - a) * rail_expansion;
742 self.styled_text(
743 Rect {
744 x: mix(narrow_label.x, wide_label.x),
745 y: mix(narrow_label.y, wide_label.y),
746 width: mix(narrow_label.width, wide_label.width),
747 height: mix(narrow_label.height, wide_label.height),
748 },
749 destination.label,
750 label_color.opacity(if always_show_labels {
751 1.0
752 } else {
753 label_opacity.max(rail_expansion)
754 }),
755 TextStyle {
756 size: 12.0 + 2.0 * rail_expansion,
757 weight: if expressive { 400 } else { 500 },
758 align: if rail_expansion >= 0.5 {
759 if rtl { 2 } else { 0 }
760 } else {
761 1
762 },
763 ..TextStyle::default()
764 },
765 );
766 }
767 self.semantic(
768 response,
769 Role::Tab,
770 destination.label,
771 active,
772 None,
773 None,
774 None,
775 );
776 if let Some((badge, description)) = destination.badge {
777 self.badge_at(response.id, icon, badge, description, rtl);
778 }
779 self.focus_ring(response);
780 }
781 old != *selected
782 }
783 pub fn navigation_item(
784 &mut self,
785 id: u64,
786 b: Rect,
787 label: &str,
788 selected: bool,
789 enabled: bool,
790 ) -> Response {
791 let r = self.interact(id, b, enabled);
792 let t = self.ui.theme;
793 let color = if selected {
794 t.on_secondary
795 } else {
796 t.on_surface_variant
797 }
798 .opacity(if r.enabled { 1.0 } else { 0.38 });
799 if selected {
800 self.rect(b, b.height / 2.0, t.secondary);
801 }
802 self.state_layer(r, b.height / 2.0, color);
803 self.styled_text(
804 b,
805 label,
806 color,
807 TextStyle {
808 padding: 16.0,
809 letter_spacing: 0.5,
810 ..TextStyle::default()
811 },
812 );
813 self.semantic(r, Role::ListItem, label, selected, None, None, None);
814 r
815 }
816
817 pub fn navigation_drawer_header(&mut self, title: &str, subtitle: Option<&str>) {
819 let t = self.theme();
820 let h = if subtitle.is_some() { 112.0 } else { 80.0 };
821 let head = self.reserve(h);
822 self.styled_text(
823 Rect { x: head.x + 16.0, y: head.y + 36.0, width: (head.width - 32.0).max(0.0), height: 32.0 },
824 title,
825 t.text,
826 TextStyle { size: 20.0, weight: 500, ..TextStyle::default() },
827 );
828 if let Some(sub) = subtitle {
829 self.styled_text(
830 Rect { x: head.x + 16.0, y: head.y + 72.0, width: (head.width - 32.0).max(0.0), height: 24.0 },
831 sub,
832 t.on_surface_variant,
833 TextStyle { size: 14.0, ..TextStyle::default() },
834 );
835 }
836 }
837
838 pub fn navigation_drawer_section_header(&mut self, label: &str) {
840 let t = self.theme();
841 let row = self.reserve(36.0);
842 self.styled_text(
843 Rect { x: row.x + 28.0, width: (row.width - 56.0).max(0.0), ..row },
844 label,
845 t.on_surface_variant,
846 TextStyle { size: 14.0, weight: 600, ..TextStyle::default() },
847 );
848 }
849
850 pub fn navigation_drawer(
853 &mut self,
854 id: u64,
855 bounds: Rect,
856 items: &[(&str, Option<MaterialIcon>)],
857 selected: &mut usize,
858 ) -> bool {
859 if items.is_empty() {
860 return false;
861 }
862 *selected = (*selected).min(items.len() - 1);
863 let t = self.theme();
864 self.paint_rect(bounds, 16.0, t.surface_low);
865 let mut changed = false;
866 let row_h = if items.iter().any(|(_, ic)| ic.is_some()) { 56.0 } else { 48.0 };
867 for (i, &(label, icon)) in items.iter().enumerate() {
868 let row = Rect {
869 x: bounds.x,
870 y: bounds.y + 8.0 + i as f32 * row_h,
871 width: bounds.width,
872 height: row_h,
873 };
874 let is_sel = *selected == i;
875 let r = if let Some(ic) = icon {
876 self.material_drawer_item(id.wrapping_add(i as u64), row, label, ic, is_sel, true)
877 } else {
878 self.navigation_item(id.wrapping_add(i as u64), row, label, is_sel, true)
879 };
880 if r.clicked && !is_sel {
881 *selected = i;
882 changed = true;
883 }
884 }
885 changed
886 }
887}
888
889#[cfg(test)]
890#[path = "navigation.spec.rs"]
891mod tests;