functora-egui 0.4.2

shadcn/ui-inspired widget library for egui - buttons, inputs, selects, dialogs, and 70+ more components with light/dark theming
Documentation
//! Show method for `NavigationMenu` — renders a horizontal nav bar.

impl super::widget::NavigationMenu {
    /// Shows the navigation menu. `active` is the currently selected item index.
    /// Returns the index of the clicked item, if any.
    pub fn show(self, ui: &mut egui::Ui, active: &mut usize) -> Option<usize> {
        let entries: Vec<(usize, String)> = self.items.into_iter().enumerate().collect();
        super::widget::NavigationMenuValue::new(&entries).show(ui, active)
    }
}

impl<T: Clone + PartialEq> super::widget::NavigationMenuValue<'_, T> {
    /// Shows the navigation menu. `active` holds the currently selected value.
    /// Returns the clicked value, if any.
    /// A value missing from `entries` keeps the current selection.
    pub fn show(self, ui: &mut egui::Ui, active: &mut T) -> Option<T> {
        let theme = crate::theme::shadcn_theme_ext::ShadcnThemeExt::shadcn_theme(ui.ctx());
        let mut clicked = None;

        let _ = ui.horizontal_wrapped(|inner_ui| {
            inner_ui.spacing_mut().item_spacing.x = 2.0;
            // Navigation menu items should stay single-line with truncation
            // for consistent row height when wrapping on narrow screens.
            inner_ui.style_mut().wrap_mode = Some(egui::TextWrapMode::Truncate);
            for (value, label) in self.entries {
                let is_active = *value == *active;
                let font_size: f32 = 14.0;
                let h_pad: f32 = 12.0;
                let height = crate::responsive::responsive_ext::ResponsiveExt::responsive_spacing(
                    inner_ui.ctx(),
                )
                .touch_height;

                let fg = if is_active {
                    theme.foreground
                } else {
                    theme.muted_foreground
                };

                let galley = crate::text::layout(
                    inner_ui,
                    label.clone(),
                    egui::FontId::proportional(font_size),
                    fg,
                    (inner_ui.available_width() - h_pad * 2.0).max(0.0),
                    None,
                );

                let desired = egui::vec2(
                    galley.size().x + h_pad * 2.0,
                    crate::text::box_height(&galley, height),
                );
                let (rect, response) = inner_ui.allocate_exact_size(desired, egui::Sense::click());

                if inner_ui.is_rect_visible(rect) {
                    let cr = crate::paint::corner_radius(theme.radius);

                    if is_active {
                        let _ = inner_ui.painter().rect_filled(rect, cr, theme.accent);
                    } else if response.is_pointer_button_down_on() {
                        let _ = inner_ui.painter().rect_filled(
                            rect,
                            cr,
                            crate::paint::interpolate_color::interpolate_color(
                                theme.accent,
                                theme.primary,
                                0.12,
                            ),
                        );
                    } else if response.hovered() {
                        let _ = inner_ui.painter().rect_filled(rect, cr, theme.accent);
                    }

                    let text_pos = egui::pos2(
                        rect.center().x - galley.size().x / 2.0,
                        rect.center().y - galley.size().y / 2.0,
                    );
                    inner_ui.painter().galley(text_pos, galley, fg);
                }

                if response.clicked() {
                    *active = value.clone();
                    clicked = Some(value.clone());
                    inner_ui.ctx().request_repaint();
                }

                if response.hovered() {
                    inner_ui
                        .ctx()
                        .set_cursor_icon(egui::CursorIcon::PointingHand);
                }
            }
        });

        clicked
    }
}