freya_components/
floating_tab.rs1use freya_core::prelude::*;
2
3use crate::{
4 activable_route_context::use_activable_route,
5 get_theme,
6 theming::component_themes::{
7 FloatingTabTheme,
8 FloatingTabThemePartial,
9 },
10};
11
12#[derive(Debug, Default, PartialEq, Clone, Copy)]
14pub enum TabStatus {
15 #[default]
17 Idle,
18 Hovering,
20}
21
22#[derive(PartialEq)]
23pub struct FloatingTab {
24 pub(crate) theme: Option<FloatingTabThemePartial>,
25 children: Vec<Element>,
26 on_press: Option<EventHandler<Event<PressEventData>>>,
28}
29
30impl Default for FloatingTab {
31 fn default() -> Self {
32 Self::new()
33 }
34}
35
36impl ChildrenExt for FloatingTab {
37 fn get_children(&mut self) -> &mut Vec<Element> {
38 &mut self.children
39 }
40}
41
42#[cfg_attr(feature = "docs",
64 doc = embed_doc_image::embed_image!("floating_tab", "images/gallery_floating_tab.png")
65)]
66impl FloatingTab {
67 pub fn new() -> Self {
68 Self {
69 children: vec![],
70 theme: None,
71 on_press: None,
72 }
73 }
74}
75
76impl Component for FloatingTab {
77 fn render(&self) -> impl IntoElement {
78 let focus = use_focus();
79 let focus_status = use_focus_status(focus);
80 let mut status = use_state(TabStatus::default);
81 let is_active = use_activable_route();
82
83 let FloatingTabTheme {
84 background,
85 hover_background,
86 padding,
87 width,
88 height,
89 color,
90 } = get_theme!(&self.theme, floating_tab);
91
92 let on_pointer_enter = move |_| {
93 Cursor::set(CursorIcon::Pointer);
94 status.set(TabStatus::Hovering);
95 };
96
97 let on_pointer_leave = move |_| {
98 Cursor::set(CursorIcon::default());
99 status.set(TabStatus::default());
100 };
101
102 let background = match *status.read() {
103 _ if focus_status() == FocusStatus::Keyboard || is_active => hover_background,
104 TabStatus::Hovering => hover_background,
105 TabStatus::Idle => background,
106 };
107
108 rect()
109 .a11y_id(focus.a11y_id())
110 .a11y_focusable(Focusable::Enabled)
111 .a11y_role(AccessibilityRole::Tab)
112 .on_pointer_enter(on_pointer_enter)
113 .on_pointer_leave(on_pointer_leave)
114 .map(self.on_press.clone(), |el, on_press| el.on_press(on_press))
115 .width(width)
116 .height(height)
117 .center()
118 .overflow(Overflow::Clip)
119 .padding(padding)
120 .background(background)
121 .color(color)
122 .corner_radius(99.)
123 .children(self.children.clone())
124 }
125}