use super::*;
impl EuvNavItem {
#[must_use]
pub fn section(mut self, group: &str) -> Self {
self.set_section(group.to_string());
self
}
}
impl EuvSidebarNav {
#[must_use]
pub fn item_colors(
palette: ThemePalette,
active: bool,
hovered: bool,
) -> (Color, Color, Color) {
let surface: SurfaceColors =
palette.interaction_colors(InteractionRole::NavItem, active, hovered, false);
let indicator_color: Color = if active {
surface.get_border()
} else {
COLOR_TRANSPARENT
};
(surface.get_bg(), indicator_color, surface.get_fg())
}
}
#[component]
impl EuvSidebarNav {
fn render(&self) -> NodeId {
let ctx: Context = current_context();
let base_tokens: ThemeTokens = self.get_tokens();
let init_tok: ThemeTokens = live_tokens_untracked(base_tokens);
let init_p: ThemePalette = init_tok.get_palette();
let init_g: ThemeGeometry = init_tok.get_geometry();
let active_route: Signal<String> = self.get_active_route();
let hovered_idx: Signal<Option<usize>> = Signal::new(None);
let Self { items, .. } = self;
let route_paths: Rc<[String]> = items
.iter()
.map(|item: &EuvNavItem| item.get_path().clone())
.collect();
let init_idx: Option<usize> = {
let cur: String = active_route.get_untracked();
route_paths
.iter()
.position(|text_value: &String| text_value.as_str() == cur.as_str())
};
let row_h: f32 = init_g.get_control_sm().max(34.0);
let row_rad: CornerRadius = CornerRadius::all(init_g.get_radius_md().max(0.0));
let mut root_st: Style = Style::default();
root_st
.get_mut_layout()
.set_flex_direction(FlexDirection::Column)
.set_width(Length::Percent(100.0))
.set_row_gap(2.0)
.set_column_gap(2.0);
let mut base_sec_txt_st: Style = Style::default();
base_sec_txt_st
.get_mut_text()
.set_font_size(11.0)
.set_weight(FontWeight::SemiBold)
.set_color(init_p.get_muted_foreground());
let mut base_row_st: Style = Style::default();
base_row_st
.get_mut_layout()
.set_flex_direction(FlexDirection::Row)
.set_width(Length::Percent(100.0))
.set_height(Length::Px(row_h))
.set_align_items(AlignItems::Center)
.set_row_gap(SPACE_SM)
.set_column_gap(SPACE_SM)
.set_padding(Edges::symmetric(0.0, SPACE_SM))
.set_overflow(Overflow::Hidden);
base_row_st
.get_mut_paint()
.set_radius(row_rad)
.set_cursor(CursorIcon::Pointer);
let mut base_ind_st: Style = Style::default();
base_ind_st
.get_mut_layout()
.set_width(Length::Px(3.5))
.set_height(Length::Px(18.0))
.set_flex_shrink(0.0);
base_ind_st
.get_mut_paint()
.set_radius(CornerRadius::all(RADIUS_PILL));
let mut base_lbl_st: Style = Style::default();
base_lbl_st
.get_mut_layout()
.set_flex_grow(1.0)
.set_flex_shrink(1.0)
.set_min_width(Length::Px(0.0))
.set_overflow(Overflow::Hidden);
base_lbl_st
.get_mut_text()
.set_font_size(13.5)
.set_weight(FontWeight::Normal)
.set_overflow(TextOverflow::Ellipsis);
let scene: SceneHandle = ctx.get_scene().clone();
let (root, section_lbl_nodes, bound_items): (
NodeId,
Vec<NodeId>,
Vec<(NodeId, NodeId, NodeId)>,
) = scene.with_write(
|tree: &mut SceneTree| {
let root_id: NodeId = tree.create_view(root_st);
let mut children: Vec<NodeId> = Vec::with_capacity(items.len() * 2);
let mut sec_lbls: Vec<NodeId> = Vec::new();
let mut bound: Vec<(NodeId, NodeId, NodeId)> = Vec::with_capacity(items.len());
let mut owned: Vec<NodeId> = Vec::with_capacity(items.len() * 3 + 8);
let mut last_section: &str = "";
for (idx, item) in items.iter().enumerate() {
let sec: &str = item.get_section().as_str();
if !sec.is_empty() && sec != last_section {
last_section = sec;
let top_pad: f32 = if idx == 0 { 2.0 } else { 12.0 };
let mut sec_row_st: Style = Style::default();
sec_row_st
.get_mut_layout()
.set_flex_direction(FlexDirection::Row)
.set_width(Length::Percent(100.0))
.set_padding(Edges::new(top_pad, SPACE_SM, 4.0, SPACE_SM));
let sec_row: NodeId = tree.create_view(sec_row_st);
let sec_txt: NodeId = tree.create_text(sec.to_string(), base_sec_txt_st);
tree.append_child(sec_row, sec_txt);
sec_lbls.push(sec_txt);
owned.push(sec_txt);
children.push(sec_row);
}
let is_active: bool = init_idx == Some(idx);
let (row_bg, ind_bg, lbl_fg): (Color, Color, Color) =
Self::item_colors(init_p, is_active, false);
let mut row_st: Style = base_row_st;
row_st.get_mut_paint().set_background(Some(row_bg));
let row_node: NodeId = tree.create_view(row_st);
let click_path: String = item.get_path().clone();
let click_handler: UiEventHandler =
Rc::new(RefCell::new(move |_: &UiEvent| {
let _: bool = active_route.set_if_changed(click_path.clone());
}));
let enter_handler: UiEventHandler =
Rc::new(RefCell::new(move |_: &UiEvent| {
let _: bool = hovered_idx.set_if_changed(Some(idx));
}));
let leave_handler: UiEventHandler =
Rc::new(RefCell::new(move |_: &UiEvent| {
if hovered_idx.get_untracked() == Some(idx) {
let _: bool = hovered_idx.set_if_changed(None);
}
}));
if let Some(node) = tree.get_node_mut(row_node) {
node.get_mut_handlers()
.set_on_click(Some(click_handler))
.set_on_pointer_enter(Some(enter_handler))
.set_on_pointer_leave(Some(leave_handler));
node.get_mut_interaction().set_focusable(true);
}
if let Some(node) = tree.get_node_mut(row_node) {
let mut item_a11y: A11yProps =
A11yProps::new(A11yRole::Link, item.get_label().clone());
item_a11y.set_checked(Some(is_active));
node.set_a11y(Some(item_a11y));
}
let mut ind_st: Style = base_ind_st;
ind_st.get_mut_paint().set_background(Some(ind_bg));
let ind_node: NodeId = tree.create_view(ind_st);
let mut lbl_st: Style = base_lbl_st;
lbl_st.get_mut_text().set_color(lbl_fg);
let lbl_node: NodeId = tree.create_text(item.get_label().clone(), lbl_st);
tree.append_children(row_node, &[ind_node, lbl_node]);
bound.push((row_node, ind_node, lbl_node));
owned.push(row_node);
owned.push(ind_node);
owned.push(lbl_node);
children.push(row_node);
}
if !children.is_empty() {
tree.append_children(root_id, &children);
}
if let Some(active_idx) = init_idx
&& let Some(&(active_row, _, _)) = bound.get(active_idx)
{
tree.request_scroll_into_view(active_row);
}
mark_theme_owned(tree, owned.as_slice());
(root_id, sec_lbls, bound)
},
(NodeId::default(), Vec::new(), Vec::new()),
);
let vp_sig: Signal<Size> = ctx.get_viewport_size();
let init_vp: Size = vp_sig.get_untracked();
let scene_eff: SceneHandle = scene.clone();
let prev_state: Cell<(Option<usize>, Option<usize>, ThemeKey, Size)> =
Cell::new((init_idx, None, init_tok.key(), init_vp));
let (nav_scope, _): (Scope, Effect) = Scope::run(|_: Scope| {
Effect::new(move || {
let next_idx: Option<usize> = active_route
.with(|cur: &String| {
route_paths
.iter()
.position(|label_str: &String| label_str.as_str() == cur.as_str())
})
.flatten();
let next_hover: Option<usize> = hovered_idx.get();
let next_vp: Size = vp_sig.get();
let live_tok: ThemeTokens = use_live_tokens(base_tokens);
let (old_idx, old_hover, old_key, old_vp): (
Option<usize>,
Option<usize>,
ThemeKey,
Size,
) = prev_state.replace((next_idx, next_hover, live_tok.key(), next_vp));
let theme_changed: bool = old_key != live_tok.key();
let route_or_vp_changed: bool = old_idx != next_idx || old_vp != next_vp;
if !theme_changed && !route_or_vp_changed && old_hover == next_hover {
return;
}
let live_p: ThemePalette = live_tok.get_palette();
scene_eff.with_write(
|tree: &mut SceneTree| {
if theme_changed {
let muted_fg: Color = live_p.get_muted_foreground();
for &sec_id in section_lbl_nodes.iter() {
if let Some(node) = tree.get_node_mut(sec_id) {
node.get_mut_style().get_mut_text().set_color(muted_fg);
tree.mark_dirty(sec_id, DIRTY_PAINT);
}
}
}
for (idx, &(row_node, ind_node, lbl_node)) in bound_items.iter().enumerate()
{
let was_affected: bool = theme_changed
|| old_idx == Some(idx)
|| next_idx == Some(idx)
|| old_hover == Some(idx)
|| next_hover == Some(idx);
if !was_affected {
continue;
}
let is_row_active: bool = next_idx == Some(idx);
let (row_bg, ind_bg, lbl_fg): (Color, Color, Color) =
Self::item_colors(live_p, is_row_active, next_hover == Some(idx));
if let Some(node) = tree.get_node_mut(row_node) {
node.get_mut_style()
.get_mut_paint()
.set_background(Some(row_bg));
if let Some(mut a11y) = node.a11y_props() {
a11y.set_checked(Some(is_row_active));
node.set_a11y(Some(a11y));
}
tree.mark_dirty(row_node, DIRTY_PAINT);
}
if let Some(node) = tree.get_node_mut(ind_node) {
node.get_mut_style()
.get_mut_paint()
.set_background(Some(ind_bg));
tree.mark_dirty(ind_node, DIRTY_PAINT);
}
if let Some(node) = tree.get_node_mut(lbl_node) {
node.get_mut_style().get_mut_text().set_color(lbl_fg);
tree.mark_dirty(lbl_node, DIRTY_PAINT);
}
}
if route_or_vp_changed
&& let Some(active_idx) = next_idx
&& let Some(&(active_row, _, _)) = bound_items.get(active_idx)
{
tree.request_scroll_into_view(active_row);
}
},
(),
);
})
});
scene.with_write(
|tree: &mut SceneTree| {
tree.attach_scope(root, nav_scope);
},
(),
);
root
}
}
impl EuvTabs {
#[must_use]
pub fn tab_colors(
tokens: ThemeTokens,
active: bool,
hovered: bool,
) -> (Color, Color, Option<Shadow>, Color) {
let palette: ThemePalette = tokens.get_palette();
let geometry: ThemeGeometry = tokens.get_geometry();
let surface: SurfaceColors =
palette.interaction_colors(InteractionRole::SegmentedPill, active, hovered, false);
let shadow_opt: Option<Shadow> = if active {
Some(geometry.get_shadow_xs())
} else {
None
};
(
surface.get_bg(),
surface.get_border(),
shadow_opt,
surface.get_fg(),
)
}
}
#[component]
impl EuvTabs {
fn render(&self) -> NodeId {
let ctx: Context = current_context();
let base_tokens: ThemeTokens = self.get_tokens();
let init_tok: ThemeTokens = live_tokens_untracked(base_tokens);
let init_p: ThemePalette = init_tok.get_palette();
let init_g: ThemeGeometry = init_tok.get_geometry();
let active: Signal<usize> = self.get_active();
let hovered_tab: Signal<Option<usize>> = Signal::new(None);
let init_active: usize = active.get_untracked();
let Self { labels, .. } = self;
let track_radius: f32 = init_g.get_radius_lg();
let tab_radius: f32 = concentric_radius(track_radius, CONTROL_INSET);
let tab_pad_x: f32 = if labels.len() >= 5 {
SPACE_XS
} else {
SPACE_SM
};
let tab_font_size: f32 = if labels.len() >= 6 { 11.5 } else { 13.0 };
let bar_st: Style = style! {
FlexDirection::Row
FlexWrap::NoWrap
Width(Length::Percent(100.0))
Gap(CONTROL_INSET)
Padding(Edges::all(CONTROL_INSET))
Overflow::Hidden
Background(init_p.get_muted())
Radius(track_radius)
};
let base_tab_st: Style = style! {
FlexDirection::Row
FlexGrow(1.0)
FlexShrink(1.0)
FlexBasis(Length::Px(0.0))
MinWidth(Length::Px(0.0))
Height(Length::Px(init_g.get_control_sm()))
JustifyContent::Center
AlignItems::Center
Padding(Edges::symmetric(0.0, tab_pad_x))
Overflow::Hidden
Radius(tab_radius)
CursorIcon::Pointer
};
let base_lbl_st: Style = style! {
FlexShrink(1.0)
MinWidth(Length::Px(0.0))
MaxWidth(Length::Percent(100.0))
FontSize(tab_font_size)
FontWeight::Medium
TextAlign::Center
TextOverflow::Ellipsis
Overflow::Hidden
};
let scene: SceneHandle = ctx.get_scene().clone();
let (bar_node, bound_tabs): (NodeId, Vec<(NodeId, NodeId)>) = scene.with_write(
|tree: &mut SceneTree| {
let bar_id: NodeId = tree.create_view(bar_st);
if let Some(node) = tree.get_node_mut(bar_id) {
node.set_a11y(Some(A11yProps::new(A11yRole::TabList, String::new())));
}
let mut tab_ids: Vec<NodeId> = Vec::with_capacity(labels.len());
let mut bound: Vec<(NodeId, NodeId)> = Vec::with_capacity(labels.len());
let mut owned: Vec<NodeId> = Vec::with_capacity(labels.len() * 2 + 1);
owned.push(bar_id);
let border_width: f32 = init_g.get_border_width().max(0.0);
for (idx, label) in labels.iter().enumerate() {
let selected: bool = idx == init_active;
let (tab_bg, border_col, shadow_opt, lbl_fg): (
Color,
Color,
Option<Shadow>,
Color,
) = Self::tab_colors(init_tok, selected, false);
let mut tab_st: Style = base_tab_st;
tab_st
.get_mut_paint()
.set_background(Some(tab_bg))
.set_border(Border::new(border_width, border_col))
.set_shadow(shadow_opt);
let tab_id: NodeId = tree.create_view(tab_st);
let click_handler: UiEventHandler =
Rc::new(RefCell::new(move |_: &UiEvent| {
let _: bool = active.set_if_changed(idx);
}));
let enter_handler: UiEventHandler =
Rc::new(RefCell::new(move |_: &UiEvent| {
let _: bool = hovered_tab.set_if_changed(Some(idx));
}));
let leave_handler: UiEventHandler =
Rc::new(RefCell::new(move |_: &UiEvent| {
if hovered_tab.get_untracked() == Some(idx) {
let _: bool = hovered_tab.set_if_changed(None);
}
}));
if let Some(node) = tree.get_node_mut(tab_id) {
node.get_mut_handlers()
.set_on_click(Some(click_handler))
.set_on_pointer_enter(Some(enter_handler))
.set_on_pointer_leave(Some(leave_handler));
node.get_mut_interaction().set_focusable(true);
node.set_a11y(Some(A11yProps::new(A11yRole::Tab, label.clone())));
}
let mut lbl_st: Style = base_lbl_st;
lbl_st.get_mut_text().set_color(lbl_fg);
let lbl_id: NodeId = tree.create_text(label.clone(), lbl_st);
tree.append_child(tab_id, lbl_id);
bound.push((tab_id, lbl_id));
owned.push(tab_id);
owned.push(lbl_id);
tab_ids.push(tab_id);
}
if !tab_ids.is_empty() {
tree.append_children(bar_id, &tab_ids);
}
mark_theme_owned(tree, owned.as_slice());
(bar_id, bound)
},
(NodeId::default(), Vec::new()),
);
let scene_eff: SceneHandle = scene.clone();
let prev_state: Cell<(usize, Option<usize>, ThemeKey)> =
Cell::new((init_active, None, init_tok.key()));
let (tabs_scope, _): (Scope, Effect) = Scope::run(|_: Scope| {
Effect::new(move || {
let next_idx: usize = active.get();
let next_hover: Option<usize> = hovered_tab.get();
let live_tok: ThemeTokens = use_live_tokens(base_tokens);
let (old_idx, old_hover, old_key): (usize, Option<usize>, ThemeKey) =
prev_state.replace((next_idx, next_hover, live_tok.key()));
let theme_changed: bool = old_key != live_tok.key();
if !theme_changed && old_idx == next_idx && old_hover == next_hover {
return;
}
let live_p: ThemePalette = live_tok.get_palette();
let bloom_width: f32 = live_tok.get_geometry().get_border_width().max(0.0);
scene_eff.with_write(
|tree: &mut SceneTree| {
if theme_changed && let Some(node) = tree.get_node_mut(bar_node) {
node.get_mut_style()
.get_mut_paint()
.set_background(Some(live_p.get_muted()));
tree.mark_dirty(bar_node, DIRTY_PAINT);
}
for (idx, &(tab_id, lbl_id)) in bound_tabs.iter().enumerate() {
let affected: bool = theme_changed
|| idx == old_idx
|| idx == next_idx
|| old_hover == Some(idx)
|| next_hover == Some(idx);
if !affected {
continue;
}
let (tab_bg, border_col, shadow_opt, lbl_fg): (
Color,
Color,
Option<Shadow>,
Color,
) = Self::tab_colors(
live_tok,
idx == next_idx,
next_hover == Some(idx),
);
if let Some(node) = tree.get_node_mut(tab_id) {
node.get_mut_style()
.get_mut_paint()
.set_background(Some(tab_bg))
.set_border(Border::new(bloom_width, border_col))
.set_shadow(shadow_opt);
tree.mark_dirty(tab_id, DIRTY_PAINT);
}
if let Some(node) = tree.get_node_mut(lbl_id) {
node.get_mut_style().get_mut_text().set_color(lbl_fg);
tree.mark_dirty(lbl_id, DIRTY_PAINT);
}
}
},
(),
);
})
});
scene.with_write(
|tree: &mut SceneTree| {
tree.attach_scope(bar_node, tabs_scope);
},
(),
);
bar_node
}
}
impl EuvBottomNav {
#[must_use]
pub(crate) fn dock_tile_metrics(
g: ThemeGeometry,
is_vert: bool,
slot_h_pad: f32,
) -> (Edges, f32) {
if is_vert {
(Edges::symmetric(3.5, 2.0), g.get_radius_lg().max(14.0))
} else {
(
Edges::symmetric(0.0, slot_h_pad),
concentric_radius(g.get_radius_lg(), CONTROL_INSET),
)
}
}
#[must_use]
pub fn tab_colors(palette: ThemePalette, active: bool, hovered: bool) -> (Color, Color) {
if active {
let primary_surface: SurfaceColors = palette.surface(SurfaceRole::Primary);
(COLOR_TRANSPARENT, primary_surface.get_fg())
} else {
let surface: SurfaceColors =
palette.interaction_colors(InteractionRole::NavItem, false, hovered, false);
(surface.get_bg(), surface.get_fg())
}
}
}
#[component]
impl EuvBottomNav {
fn render(&self) -> NodeId {
let ctx: Context = current_context();
let base_tokens: ThemeTokens = self.get_tokens();
let init_tok: ThemeTokens = live_tokens_untracked(base_tokens);
let init_p: ThemePalette = init_tok.get_palette();
let init_g: ThemeGeometry = init_tok.get_geometry();
let active_route: Signal<String> = self.get_active_route();
let hovered_tab: Signal<Option<usize>> = Signal::new(None);
let Self { items, .. } = self;
let item_count: usize = items.len().max(1);
let slot_pct: f32 = 100.0 / (item_count as f32);
let route_paths: Rc<[String]> = items
.iter()
.map(|item: &EuvNavItem| item.get_path().clone())
.collect();
let initial_idx: Option<usize> = {
let cur: String = active_route.get_untracked();
route_paths
.iter()
.position(|path: &String| path.as_str() == cur.as_str())
};
let drag_sig: Signal<f32> = ctx.get_swipe_drag_offset();
let vp_sig: Signal<Size> = ctx.get_viewport_size();
let safe_sig: Signal<SafeAreaInsets> = ctx.get_safe_area();
let init_pres: NavDockPresentation =
resolve_nav_dock_presentation(vp_sig.get_untracked(), safe_sig.get_untracked());
let init_vertical: bool = init_pres.get_orientation() == NavDockOrientation::Vertical;
let init_icon_only: bool = init_pres.get_icon_only();
let slot_h_pad: f32 = CONTROL_INSET * 0.5;
let (init_slot_edges, pill_radius): (Edges, f32) =
Self::dock_tile_metrics(init_g, init_vertical, slot_h_pad);
let init_primary: SurfaceColors = init_p.surface(SurfaceRole::Primary);
let initial_display: Display = if initial_idx.is_some() {
Display::Flex
} else {
Display::None
};
let initial_spacer_idx: usize = initial_idx.unwrap_or(0);
let initial_spacer_display: Display = if initial_spacer_idx > 0 {
Display::Flex
} else {
Display::None
};
let initial_spacer_pct: f32 = (initial_spacer_idx as f32) * slot_pct;
let mut pill_node: NodeId = NodeId::default();
let row_quaternary_style: Style = style! {
initial_display
Position::Absolute
Inset(Some(0.0), None, None, Some(0.0))
Width(Length::Percent(100.0))
Height(Length::Percent(100.0))
Padding(init_slot_edges)
ZIndex(0)
PointerEvents::None
};
let view_tertiary_style: Style = style! {
Width(Length::Percent(100.0))
Height(Length::Percent(100.0))
Radius(pill_radius)
Background(init_primary.get_bg())
Overflow::Hidden
};
let indicator_node: NodeId = view! {
row {
style: row_quaternary_style
view {
node_ref: pill_node
style: view_tertiary_style
}
}
};
let mut indicator_spacer: NodeId = NodeId::default();
let mut indicator_slot_anchor: NodeId = NodeId::default();
let row_tertiary_style: Style = style! {
initial_display
if init_vertical { FlexDirection::Column } else { FlexDirection::Row }
Position::Absolute
Inset(Some(0.0), None, None, Some(0.0))
Width(Length::Percent(100.0))
Height(Length::Percent(100.0))
JustifyContent::Start
AlignItems::Center
Gap(0.0)
ZIndex(-1)
PointerEvents::None
};
let view_style: Style = style! {
initial_spacer_display
Width(if init_vertical { Length::Px(1.0) } else { Length::Percent(initial_spacer_pct) })
Height(if init_vertical { Length::Percent(initial_spacer_pct) } else { Length::Px(1.0) })
FlexShrink(0.0)
PointerEvents::None
};
let view_secondary_style: Style = style! {
Width(if init_vertical { Length::Percent(100.0) } else { Length::Percent(slot_pct) })
Height(if init_vertical { Length::Percent(slot_pct) } else { Length::Percent(100.0) })
FlexShrink(0.0)
PointerEvents::None
};
let indicator_track: NodeId = view! {
view {
style: row_tertiary_style
view {
node_ref: indicator_spacer
style: view_style
}
view {
node_ref: indicator_slot_anchor
style: view_secondary_style
{ indicator_node }
}
}
};
let mut tab_slots: Vec<NodeId> = Vec::with_capacity(items.len());
let mut owned_nodes: Vec<NodeId> = Vec::with_capacity(items.len() * 3 + 1);
owned_nodes.push(pill_node);
let mut bound_tabs: Vec<BottomNavTabEntry> = Vec::with_capacity(items.len());
for (tab_idx, item) in items.iter().enumerate() {
let click_path: String = item.get_path().clone();
let icon_path: String = click_path.clone();
let icon_label: String = item.get_label().clone();
let init_under: bool = initial_idx == Some(tab_idx);
let under_cell: Rc<Cell<bool>> = Rc::new(Cell::new(init_under));
let hover_cell: Rc<Cell<bool>> = Rc::new(Cell::new(false));
let under_paint: Rc<Cell<bool>> = Rc::clone(&under_cell);
let hover_paint: Rc<Cell<bool>> = Rc::clone(&hover_cell);
let (init_tab_bg, init_fg): (Color, Color) =
Self::tab_colors(init_p, init_under, false);
let on_tab_click = move |_: &UiEvent| {
let _: usize = EuvOverlayStack::dismiss_all();
let _: bool = active_route.set_if_changed(click_path.clone());
};
let on_tab_enter = move |_: &UiEvent| {
let _: bool = hovered_tab.set_if_changed(Some(tab_idx));
};
let on_tab_leave = move |_: &UiEvent| {
if hovered_tab.get_untracked() == Some(tab_idx) {
let _: bool = hovered_tab.set_if_changed(None);
}
};
let paint_icon = move |bounds: Rect, display_list: &mut DisplayList| {
let live_p: ThemePalette = live_tokens_untracked(base_tokens).get_palette();
let (_, col): (Color, Color) =
Self::tab_colors(live_p, under_paint.get(), hover_paint.get());
draw_bottom_nav_icon(bounds, display_list, &icon_path, &icon_label, col);
};
let canvas_style: Style = style! {
Width(Length::Px(20.0))
Height(Length::Px(18.0))
};
let icon_canvas: NodeId = view! {
canvas {
style: canvas_style
painter: paint_icon
}
};
let mut tab_col: NodeId = NodeId::default();
let mut label_node: NodeId = NodeId::default();
let row_secondary_style: Style = style! {
FlexGrow(1.0)
FlexShrink(1.0)
MinWidth(Length::Px(0.0))
MinHeight(Length::Px(0.0))
Width(if init_vertical { Length::Percent(100.0) } else { Length::Auto })
Height(if init_vertical { Length::Auto } else { Length::Percent(100.0) })
Padding(init_slot_edges)
ZIndex(1)
};
let column_style: Style = style! {
Width(Length::Percent(100.0))
Height(Length::Percent(100.0))
JustifyContent::Center
AlignItems::Center
Gap(2.0)
Padding(Edges::symmetric(2.0, 4.0))
Radius(pill_radius)
Overflow::Hidden
Background(init_tab_bg)
CursorIcon::Pointer
};
let text_style: Style = style! {
if init_icon_only { Display::None } else { Display::Flex }
FontSize(10.5)
if init_under { FontWeight::Bold } else { FontWeight::Medium }
TextColor(init_fg)
TextAlign::Center
TextOverflow::Clip
};
let tab_slot: NodeId = view! {
row {
style: row_secondary_style
column {
node_ref: tab_col
style: column_style
role: A11yRole::Tab
a11y_label: item.get_label().clone()
on_click: on_tab_click
on_pointer_enter: on_tab_enter
on_pointer_leave: on_tab_leave
{ icon_canvas }
text {
node_ref: label_node
style: text_style
{ item.get_label().as_str() }
}
}
}
};
owned_nodes.push(tab_col);
owned_nodes.push(icon_canvas);
owned_nodes.push(label_node);
bound_tabs.push(BottomNavTabEntry {
tab_idx,
tab_col,
icon_canvas,
label_node,
under_cell,
hover_cell,
});
tab_slots.push(tab_slot);
}
let tab_slots_rc: Rc<[NodeId]> = Rc::from(tab_slots.as_slice());
let tab_cols_rc: Rc<[NodeId]> = Rc::from(
bound_tabs
.iter()
.map(|entry: &BottomNavTabEntry| entry.tab_col)
.collect::<Vec<NodeId>>()
.as_slice(),
);
let row_style: Style = style! {
if init_vertical { FlexDirection::Column } else { FlexDirection::Row }
Width(Length::Percent(100.0))
Height(Length::Percent(100.0))
JustifyContent::SpaceBetween
AlignItems::Center
Gap(0.0)
Overflow::Hidden
};
let bar_node: NodeId = view! {
view {
style: row_style
{ indicator_track }
..tab_slots
}
};
let orientation_cell: Rc<Cell<NavDockOrientation>> =
Rc::new(Cell::new(init_pres.get_orientation()));
let scene_tabs: SceneHandle = ctx.get_scene().clone();
let paths_for_pill: Rc<[String]> = Rc::clone(&route_paths);
let prev_tab_state: Cell<(Option<usize>, Option<usize>, ThemeKey, bool)> =
Cell::new((initial_idx, None, init_tok.key(), init_icon_only));
let (tabs_scope, _): (Scope, Effect) = Scope::run(|_: Scope| {
Effect::new(move || {
let cur_idx: Option<usize> = active_route
.with(|cur: &String| {
paths_for_pill
.iter()
.position(|path: &String| path.as_str() == cur.as_str())
})
.flatten();
let vp_sz: Size = vp_sig.get();
let live_safe: SafeAreaInsets = safe_sig.get();
let pres: NavDockPresentation = resolve_nav_dock_presentation(vp_sz, live_safe);
let is_icon_only: bool = pres.get_icon_only();
let dx: f32 = drag_sig.get();
let next_under: Option<usize> = if dx.abs() <= 0.5 {
cur_idx
} else {
let vp_w: f32 = vp_sz.get_width();
(0..item_count).find(|item_index: &usize| {
bottom_nav_tab_under_pill(*item_index, cur_idx, item_count, dx, vp_w)
})
};
let next_hover: Option<usize> = hovered_tab.get();
let live_tok: ThemeTokens = use_live_tokens(base_tokens);
let (old_under, old_hover, old_key, old_compact): (
Option<usize>,
Option<usize>,
ThemeKey,
bool,
) = prev_tab_state.replace((next_under, next_hover, live_tok.key(), is_icon_only));
let theme_changed: bool = old_key != live_tok.key();
let compact_changed: bool = old_compact != is_icon_only;
if !theme_changed
&& !compact_changed
&& next_under == old_under
&& next_hover == old_hover
{
return;
}
let live_p: ThemePalette = live_tok.get_palette();
let primary_bg: Color = live_p.surface(SurfaceRole::Primary).get_bg();
scene_tabs.with_write(
|tree: &mut SceneTree| {
if theme_changed && let Some(node) = tree.get_node_mut(pill_node) {
node.get_mut_style()
.get_mut_paint()
.set_background(Some(primary_bg));
tree.mark_dirty(pill_node, DIRTY_PAINT);
}
for entry in bound_tabs.iter() {
let tab_idx: usize = entry.tab_idx;
let tab_col: NodeId = entry.tab_col;
let icon_canvas: NodeId = entry.icon_canvas;
let label_node: NodeId = entry.label_node;
let affected: bool = theme_changed
|| compact_changed
|| old_under == Some(tab_idx)
|| next_under == Some(tab_idx)
|| old_hover == Some(tab_idx)
|| next_hover == Some(tab_idx);
if !affected {
continue;
}
let under: bool = next_under == Some(tab_idx);
let hovered: bool = !under && next_hover == Some(tab_idx);
entry.under_cell.set(under);
entry.hover_cell.set(hovered);
let (tab_bg, fg): (Color, Color) =
Self::tab_colors(live_p, under, hovered);
if let Some(node) = tree.get_node_mut(tab_col) {
node.get_mut_style()
.get_mut_paint()
.set_background(Some(tab_bg));
tree.mark_dirty(tab_col, DIRTY_PAINT);
}
if let Some(node) = tree.get_node_mut(label_node) {
let computed_style: &mut Style = node.get_mut_style();
computed_style
.get_mut_layout()
.set_display(if is_icon_only {
Display::None
} else {
Display::Flex
});
computed_style
.get_mut_text()
.set_weight(if under {
FontWeight::Bold
} else {
FontWeight::Medium
})
.set_color(fg);
let mut dirty_lbl: u16 = DIRTY_LAYOUT | DIRTY_TEXT;
dirty_lbl |= DIRTY_PAINT;
tree.mark_dirty(label_node, dirty_lbl);
}
tree.mark_dirty(icon_canvas, DIRTY_PAINT);
}
},
(),
);
})
});
let last_idx: Rc<Cell<Option<usize>>> = Rc::new(Cell::new(initial_idx));
let last_drag_tx: Rc<Cell<f32>> = Rc::new(Cell::new(0.0));
let route_paths_anim: Rc<[String]> = Rc::clone(&route_paths);
let ctx_anim: Context = ctx.clone();
let last_idx_route: Rc<Cell<Option<usize>>> = Rc::clone(&last_idx);
let last_drag_route: Rc<Cell<f32>> = Rc::clone(&last_drag_tx);
let tab_slots_anim: Rc<[NodeId]> = Rc::clone(&tab_slots_rc);
let orient_route: Rc<Cell<NavDockOrientation>> = Rc::clone(&orientation_cell);
let (route_scope, _): (Scope, Effect) = Scope::run(|_: Scope| {
let mut initialized: bool = false;
Effect::new(move || {
let next_idx: Option<usize> = active_route
.with(|current_path: &String| {
route_paths_anim
.iter()
.position(|path: &String| path.as_str() == current_path.as_str())
})
.flatten();
let prev_idx: Option<usize> = last_idx_route.replace(next_idx);
if !initialized {
initialized = true;
return;
}
let prev_drag: f32 = last_drag_route.replace(0.0);
let is_vert: bool = orient_route.get() == NavDockOrientation::Vertical;
let dirty_layout: u16 = DIRTY_LAYOUT | DIRTY_SUBTREE_LAYOUT;
let dirty_all: u16 = dirty_layout | DIRTY_PAINT;
ctx_anim.get_scene().with_write(
|tree: &mut SceneTree| {
if let Some(target_i) = next_idx {
let bar_visible: bool = tree.is_node_visible(bar_node);
let cur_visual_t: f32 = if bar_visible {
tree.get_node(indicator_node)
.map(|scene_node: &SceneNode| {
let transform_val: Transform2D =
scene_node.style_ref().get_paint().get_transform();
if is_vert {
transform_val.get_translate_y()
} else {
transform_val.get_translate_x()
}
})
.unwrap_or(prev_drag)
} else {
0.0
};
let from_axis: f32 = if !bar_visible {
0.0
} else if let Some(old_i) = prev_idx
&& old_i != target_i
{
let exact_delta: Option<f32> = match (
tab_slots_anim
.get(old_i)
.and_then(|id: &NodeId| tree.get_node(*id)),
tab_slots_anim
.get(target_i)
.and_then(|id: &NodeId| tree.get_node(*id)),
) {
(Some(old_n), Some(new_n)) => {
let old_b: Rect = old_n.get_layout_box().get_bounds();
let new_b: Rect = new_n.get_layout_box().get_bounds();
if is_vert {
if old_b.get_height() > 1.0 && new_b.get_height() > 1.0
{
Some(old_b.get_y() - new_b.get_y())
} else {
None
}
} else if old_b.get_width() > 1.0 && new_b.get_width() > 1.0
{
Some(old_b.get_x() - new_b.get_x())
} else {
None
}
}
_ => None,
};
let delta_px: f32 = exact_delta.unwrap_or_else(|| {
let bar_span: f32 = tree
.get_node(bar_node)
.map(|target_node: &SceneNode| {
let rect_bounds: Rect =
target_node.get_layout_box().get_content_bounds();
if is_vert {
rect_bounds.get_height()
} else {
rect_bounds.get_width()
}
})
.filter(|source_val: &f32| *source_val > 1.0)
.unwrap_or(0.0);
let slot_span: f32 = bar_span / (item_count as f32);
((old_i as f32) - (target_i as f32)) * slot_span
});
delta_px + cur_visual_t
} else {
cur_visual_t
};
let start_pt: Point = if is_vert {
Point::new(0.0, from_axis)
} else {
Point::new(from_axis, 0.0)
};
tree.cancel_subtree_transitions(indicator_node);
let spacer_disp: Display = if target_i > 0 {
Display::Flex
} else {
Display::None
};
let spacer_pct: f32 = (target_i as f32) * slot_pct;
if let Some(node) = tree.get_node_mut(indicator_track) {
node.get_mut_style()
.get_mut_layout()
.set_display(Display::Flex);
}
if let Some(node) = tree.get_node_mut(indicator_spacer) {
let lay: &mut LayoutStyle = node.get_mut_style().get_mut_layout();
lay.set_display(spacer_disp);
if is_vert {
lay.set_width(Length::Px(1.0))
.set_height(Length::Percent(spacer_pct));
} else {
lay.set_width(Length::Percent(spacer_pct))
.set_height(Length::Px(1.0));
}
}
if let Some(node) = tree.get_node_mut(indicator_node) {
let element_style: &mut Style = node.get_mut_style();
element_style.get_mut_layout().set_display(Display::Flex);
element_style.get_mut_paint().set_transform(
if from_axis.abs() > 0.5 {
Transform2D::new(
start_pt.get_x(),
start_pt.get_y(),
1.0,
1.0,
0.0,
)
} else {
Transform2D::default()
},
);
}
tree.mark_dirty(indicator_spacer, dirty_all);
tree.mark_dirty(indicator_slot_anchor, dirty_all);
tree.mark_dirty(indicator_track, dirty_all);
tree.mark_dirty(indicator_node, dirty_all);
if let Some(old_i) = prev_idx
&& old_i != target_i
&& from_axis.abs() > 0.5
{
tree.start_transition(
NodeTransition::slide_fade(
indicator_node,
start_pt,
Point::new(0.0, 0.0),
1.0,
1.0,
motion_duration(MOTION_STANDARD_SECS),
false,
)
.with_easing(MOTION_EASE_ENTER),
);
} else if from_axis.abs() > 0.5 {
tree.start_transition(
NodeTransition::slide_fade(
indicator_node,
start_pt,
Point::new(0.0, 0.0),
1.0,
1.0,
motion_duration(MOTION_STANDARD_SECS),
false,
)
.with_easing(MOTION_EASE_EXIT),
);
}
} else {
tree.cancel_subtree_transitions(indicator_node);
if let Some(node) = tree.get_node_mut(indicator_track) {
node.get_mut_style()
.get_mut_layout()
.set_display(Display::None);
}
if let Some(node) = tree.get_node_mut(indicator_node) {
let resolved_style: &mut Style = node.get_mut_style();
resolved_style.get_mut_layout().set_display(Display::None);
resolved_style
.get_mut_paint()
.set_transform(Transform2D::default());
}
tree.mark_dirty(indicator_track, dirty_all);
tree.mark_dirty(indicator_node, dirty_all);
}
},
(),
);
})
});
let ctx_resize: Context = ctx.clone();
let last_drag_resize: Rc<Cell<f32>> = Rc::clone(&last_drag_tx);
let last_idx_resize: Rc<Cell<Option<usize>>> = Rc::clone(&last_idx);
let orient_resize: Rc<Cell<NavDockOrientation>> = Rc::clone(&orientation_cell);
let tab_slots_resize: Rc<[NodeId]> = Rc::clone(&tab_slots_rc);
let tab_cols_resize: Rc<[NodeId]> = Rc::clone(&tab_cols_rc);
let (resize_scope, _): (Scope, Effect) = Scope::run(|_: Scope| {
let mut prev_vp: Size = vp_sig.get_untracked();
let mut prev_safe: SafeAreaInsets = safe_sig.get_untracked();
Effect::new(move || {
let next_vp: Size = vp_sig.get();
let next_safe: SafeAreaInsets = safe_sig.get();
if next_vp == prev_vp && next_safe == prev_safe {
return;
}
prev_vp = next_vp;
prev_safe = next_safe;
last_drag_resize.set(0.0);
let pres: NavDockPresentation = resolve_nav_dock_presentation(next_vp, next_safe);
let next_orient: NavDockOrientation = pres.get_orientation();
let is_vert: bool = next_orient == NavDockOrientation::Vertical;
orient_resize.set(next_orient);
let dir: FlexDirection = if is_vert {
FlexDirection::Column
} else {
FlexDirection::Row
};
let live_g: ThemeGeometry = live_tokens_untracked(base_tokens).get_geometry();
let (slot_edges, next_radius): (Edges, f32) =
Self::dock_tile_metrics(live_g, is_vert, slot_h_pad);
let cur_idx: usize = last_idx_resize.get().unwrap_or(0);
let spacer_pct: f32 = (cur_idx as f32) * slot_pct;
let dirty_layout: u16 = DIRTY_LAYOUT | DIRTY_SUBTREE_LAYOUT;
let dirty_all: u16 = dirty_layout | DIRTY_PAINT;
ctx_resize.get_scene().with_write(
|tree: &mut SceneTree| {
tree.cancel_subtree_transitions(indicator_node);
if let Some(node) = tree.get_node_mut(bar_node) {
node.get_mut_style()
.get_mut_layout()
.set_flex_direction(dir);
}
if let Some(node) = tree.get_node_mut(indicator_track) {
node.get_mut_style()
.get_mut_layout()
.set_flex_direction(dir);
}
if let Some(node) = tree.get_node_mut(indicator_spacer) {
let lay: &mut LayoutStyle = node.get_mut_style().get_mut_layout();
if is_vert {
lay.set_width(Length::Px(1.0))
.set_height(Length::Percent(spacer_pct));
} else {
lay.set_width(Length::Percent(spacer_pct))
.set_height(Length::Px(1.0));
}
}
if let Some(node) = tree.get_node_mut(indicator_slot_anchor) {
let lay: &mut LayoutStyle = node.get_mut_style().get_mut_layout();
if is_vert {
lay.set_width(Length::Percent(100.0))
.set_height(Length::Percent(slot_pct));
} else {
lay.set_width(Length::Percent(slot_pct))
.set_height(Length::Percent(100.0));
}
}
if let Some(node) = tree.get_node_mut(indicator_node) {
let node_style: &mut Style = node.get_mut_style();
node_style.get_mut_layout().set_padding(slot_edges);
node_style
.get_mut_paint()
.set_transform(Transform2D::default());
}
if let Some(node) = tree.get_node_mut(pill_node) {
node.get_mut_style()
.get_mut_paint()
.set_radius(CornerRadius::all(next_radius));
tree.mark_dirty(pill_node, DIRTY_PAINT);
}
for &col_id in tab_cols_resize.iter() {
if let Some(node) = tree.get_node_mut(col_id) {
node.get_mut_style()
.get_mut_paint()
.set_radius(CornerRadius::all(next_radius));
tree.mark_dirty(col_id, DIRTY_PAINT);
}
}
for &slot_id in tab_slots_resize.iter() {
if let Some(node) = tree.get_node_mut(slot_id) {
let lay: &mut LayoutStyle = node.get_mut_style().get_mut_layout();
lay.set_padding(slot_edges);
if is_vert {
lay.set_width(Length::Percent(100.0))
.set_height(Length::Auto);
} else {
lay.set_width(Length::Auto)
.set_height(Length::Percent(100.0));
}
tree.mark_dirty(slot_id, dirty_all);
}
}
tree.mark_dirty(bar_node, dirty_all);
tree.mark_dirty(indicator_spacer, dirty_all);
tree.mark_dirty(indicator_slot_anchor, dirty_all);
tree.mark_dirty(indicator_track, dirty_all);
tree.mark_dirty(indicator_node, dirty_all);
},
(),
);
})
});
let ctx_drag: Context = ctx.clone();
let last_idx_drag: Rc<Cell<Option<usize>>> = Rc::clone(&last_idx);
let last_drag_live: Rc<Cell<f32>> = Rc::clone(&last_drag_tx);
let tab_slots_drag: Rc<[NodeId]> = Rc::clone(&tab_slots_rc);
let orient_drag: Rc<Cell<NavDockOrientation>> = Rc::clone(&orientation_cell);
let (drag_scope, _): (Scope, Effect) = Scope::run(|_: Scope| {
Effect::new(move || {
let dx: f32 = drag_sig.get();
let Some(cur_i) = last_idx_drag.get() else {
return;
};
let is_vert: bool = orient_drag.get() == NavDockOrientation::Vertical;
let vp_w: f32 = ctx_drag
.get_viewport_size()
.get_untracked()
.get_width()
.max(1.0);
if dx.abs() > 0.5 {
let mut frac: f32 = (-dx / vp_w).clamp(-1.0, 1.0);
if cur_i == 0 && frac < 0.0 {
frac = 0.0;
}
if cur_i + 1 >= item_count && frac > 0.0 {
frac = 0.0;
}
ctx_drag.get_scene().with_write(
|tree: &mut SceneTree| {
tree.cancel_subtree_transitions(indicator_node);
let slot_span: f32 = tab_slots_drag
.first()
.and_then(|id: &NodeId| tree.get_node(*id))
.map(|node_entry: &SceneNode| {
let clip_bounds: Rect =
node_entry.get_layout_box().get_bounds();
if is_vert {
clip_bounds.get_height()
} else {
clip_bounds.get_width()
}
})
.filter(|scale_val: &f32| *scale_val > 1.0)
.unwrap_or_else(|| {
let bar_span: f32 = tree
.get_node(bar_node)
.map(|normal_vec: &SceneNode| {
let region_bounds: Rect =
normal_vec.get_layout_box().get_content_bounds();
if is_vert {
region_bounds.get_height()
} else {
region_bounds.get_width()
}
})
.filter(|series_item: &f32| *series_item > 1.0)
.unwrap_or(0.0);
bar_span / (item_count as f32)
});
let offset: f32 = frac * slot_span;
last_drag_live.set(offset);
let (tx, ty): (f32, f32) = if is_vert {
(0.0, offset)
} else {
(offset, 0.0)
};
if let Some(node) = tree.get_node_mut(indicator_node) {
node.get_mut_style()
.get_mut_paint()
.set_transform(Transform2D::new(tx, ty, 1.0, 1.0, 0.0));
}
tree.mark_dirty(indicator_node, DIRTY_PAINT);
},
(),
);
} else {
let prev_offset: f32 = last_drag_live.replace(0.0);
if prev_offset.abs() > 0.5 {
let start_pt: Point = if is_vert {
Point::new(0.0, prev_offset)
} else {
Point::new(prev_offset, 0.0)
};
ctx_drag.get_scene().with_write(
|tree: &mut SceneTree| {
tree.start_transition(
NodeTransition::slide_fade(
indicator_node,
start_pt,
Point::new(0.0, 0.0),
1.0,
1.0,
motion_duration(MOTION_STANDARD_SECS),
false,
)
.with_easing(MOTION_EASE_EXIT),
);
},
(),
);
}
}
})
});
ctx.get_scene().with_write(
|tree: &mut SceneTree| {
mark_theme_owned(tree, owned_nodes.as_slice());
tree.attach_scope(bar_node, tabs_scope);
tree.attach_scope(bar_node, route_scope);
tree.attach_scope(bar_node, resize_scope);
tree.attach_scope(bar_node, drag_scope);
},
(),
);
bar_node
}
}