functora-egui 0.2.0

shadcn/ui-inspired widget library for egui - buttons, inputs, selects, dialogs, and 70+ more components with light/dark theming
Documentation
//! Widget trait implementation for Button.

impl egui::Widget for super::widget::Button<'_> {
    fn ui(self, ui: &mut egui::Ui) -> egui::Response {
        let theme = crate::theme::shadcn_theme_ext::ShadcnThemeExt::shadcn_theme(ui.ctx());

        // Check if inside a ButtonGroup and claim an index
        let group_key = crate::widgets::button_group::widget::ButtonGroup::context_key();
        let group_info = ui.ctx().data_mut(|d| {
            d.get_temp::<crate::widgets::button_group::button_group_context::ButtonGroupContext>(
                group_key,
            )
            .and_then(|mut ctx| {
                if !ctx.active {
                    return None;
                }
                let index = ctx.current_index;
                let cached_count = ctx.cached_count;
                let cr = ctx.corner_radius;
                ctx.current_index += 1;
                let _ = d.insert_temp(group_key, ctx);
                Some((index, cached_count, cr))
            })
        });
        let in_group = group_info.is_some();

        let mut style = super::button_variant_style::resolve_button_style(
            &theme,
            &crate::responsive::responsive_ext::ResponsiveExt::responsive_spacing(ui.ctx()),
            self.variant,
            self.size,
            false,
            false,
            !ui.is_enabled(),
        );

        if self.full_width {
            let spacing =
                crate::responsive::responsive_ext::ResponsiveExt::responsive_spacing(ui.ctx());
            let (h, pad, fs) = self.size.metrics_for(&spacing);
            style.height = h;
            style.h_padding = pad;
            style.font_size = fs;
        }

        let is_loader = matches!(
            self.icon,
            Some(
                crate::icons::lucide_icon::LucideIcon::LoaderCircle
                    | crate::icons::lucide_icon::LucideIcon::Loader
                    | crate::icons::lucide_icon::LucideIcon::LoaderPinwheel
            )
        );
        if is_loader {
            ui.ctx().request_repaint();
        }

        let text_string = self.text.text().to_owned();
        let is_icon_only = text_string.is_empty() && self.icon.is_some();
        let has_icon = self.icon.is_some();
        let has_text = !text_string.is_empty();
        let has_shortcut = self.shortcut_text.is_some();

        let text_galley = ui.painter().layout_no_wrap(
            text_string.clone(),
            egui::FontId::proportional(style.font_size),
            style.fg,
        );

        let shortcut_galley = self.shortcut_text.as_ref().map(|st| {
            ui.painter().layout_no_wrap(
                st.clone(),
                egui::FontId::proportional(style.font_size - 1.0),
                theme.muted_foreground,
            )
        });

        let icon_size = style.height * 0.5;
        let icon_gap = 6.0;
        let shortcut_gap = 16.0;

        let shortcut_width = shortcut_galley
            .as_ref()
            .map_or(0.0, |g| shortcut_gap + g.size().x);

        let content_width = if is_icon_only {
            style.height
        } else if has_icon && has_text {
            style.h_padding
                + icon_size
                + icon_gap
                + text_galley.size().x
                + shortcut_width
                + style.h_padding
        } else {
            text_galley.size().x + shortcut_width + style.h_padding * 2.0
        };

        let width = self.fixed_width.unwrap_or(content_width);
        let desired = egui::vec2(width, style.height);

        // fixed_width: exact size for equal-width strips (e.g. stretched tabs).
        // full_width: use allocate_at_least so the button stretches to fill
        // the menu/list width without inflating the menu's own desired size.
        let (rect, response) = if self.fixed_width.is_some() {
            ui.allocate_exact_size(desired, egui::Sense::click())
        } else if self.full_width {
            ui.allocate_at_least(desired, egui::Sense::click())
        } else {
            ui.allocate_exact_size(desired, egui::Sense::click())
        };

        // Record boundary and rect in group context
        if in_group {
            ui.ctx().data_mut(|d| {
                if let Some(mut ctx) = d.get_temp::<crate::widgets::button_group::button_group_context::ButtonGroupContext>(group_key)
                    && ctx.active {
                        ctx.boundaries.push(rect.max.x);
                        ctx.group_rect = Some(match ctx.group_rect {
                            Some(r) => r.union(rect),
                            None => rect,
                        });
                        let _ = d.insert_temp(group_key, ctx);
                    }
            });
        }

        // Re-resolve with actual interaction state
        let mut active_style = super::button_variant_style::resolve_button_style(
            &theme,
            &crate::responsive::responsive_ext::ResponsiveExt::responsive_spacing(ui.ctx()),
            self.variant,
            self.size,
            response.hovered(),
            response.is_pointer_button_down_on(),
            !ui.is_enabled(),
        );

        if self.full_width {
            let spacing =
                crate::responsive::responsive_ext::ResponsiveExt::responsive_spacing(ui.ctx());
            let (h, pad, fs) = self.size.metrics_for(&spacing);
            active_style.height = h;
            active_style.h_padding = pad;
            active_style.font_size = fs;
        }

        if self.selected {
            active_style.fg = theme.accent_foreground;
            active_style.bg = if response.is_pointer_button_down_on() && ui.is_enabled() {
                crate::paint::interpolate_color::interpolate_color(
                    theme.accent,
                    theme.primary,
                    0.12,
                )
            } else {
                theme.accent
            };
        }

        if ui.is_rect_visible(rect) {
            let painter = ui.painter();
            let cr = group_corner_radius(group_info.as_ref(), active_style.corner_radius);

            // Background
            let _ = painter.rect_filled(rect, cr, active_style.bg);

            // Border (skip when inside a button group — the group draws borders)
            if !in_group && let Some(border_color) = active_style.border {
                let _ = painter.rect_stroke(
                    rect,
                    cr,
                    egui::Stroke::new(1.0, border_color),
                    egui::epaint::StrokeKind::Inside,
                );
            }

            if is_icon_only {
                if let Some(ref icon) = self.icon {
                    let icon_rect = egui::Rect::from_center_size(
                        rect.center(),
                        egui::vec2(icon_size, icon_size),
                    );
                    if is_loader {
                        let time = crate::utils::f64_to_f32(ui.input(|i| i.time));
                        let angle = time * std::f32::consts::TAU;
                        let radius = icon_rect.width().min(icon_rect.height()) / 2.0 - 1.0;
                        crate::paint::paint_arc::paint_arc(
                            painter,
                            icon_rect.center(),
                            radius,
                            angle,
                            egui::Stroke::new(2.0, active_style.fg),
                        );
                    } else {
                        crate::icons::paint_icon::paint_icon(
                            painter,
                            icon_rect,
                            icon,
                            active_style.fg,
                        );
                    }
                }
            } else if has_icon && has_text {
                if has_shortcut {
                    let x = rect.min.x + active_style.h_padding;
                    if let Some(ref icon) = self.icon {
                        let icon_rect = egui::Rect::from_min_size(
                            egui::pos2(x, rect.center().y - icon_size / 2.0),
                            egui::vec2(icon_size, icon_size),
                        );
                        if is_loader {
                            let time = crate::utils::f64_to_f32(ui.input(|i| i.time));
                            let angle = time * std::f32::consts::TAU;
                            let radius = icon_rect.width().min(icon_rect.height()) / 2.0 - 1.0;
                            crate::paint::paint_arc::paint_arc(
                                painter,
                                icon_rect.center(),
                                radius,
                                angle,
                                egui::Stroke::new(2.0, active_style.fg),
                            );
                        } else {
                            crate::icons::paint_icon::paint_icon(
                                painter,
                                icon_rect,
                                icon,
                                active_style.fg,
                            );
                        }
                    }
                    let text_pos = egui::pos2(
                        x + icon_size + icon_gap,
                        rect.center().y - text_galley.size().y / 2.0,
                    );
                    painter.galley_with_override_text_color(
                        text_pos,
                        text_galley.clone(),
                        active_style.fg,
                    );
                } else {
                    let total_w = icon_size + icon_gap + text_galley.size().x;
                    let start_x = rect.center().x - total_w / 2.0;
                    if let Some(ref icon) = self.icon {
                        let icon_rect = egui::Rect::from_min_size(
                            egui::pos2(start_x, rect.center().y - icon_size / 2.0),
                            egui::vec2(icon_size, icon_size),
                        );
                        if is_loader {
                            let time = crate::utils::f64_to_f32(ui.input(|i| i.time));
                            let angle = time * std::f32::consts::TAU;
                            let radius = icon_rect.width().min(icon_rect.height()) / 2.0 - 1.0;
                            crate::paint::paint_arc::paint_arc(
                                painter,
                                icon_rect.center(),
                                radius,
                                angle,
                                egui::Stroke::new(2.0, active_style.fg),
                            );
                        } else {
                            crate::icons::paint_icon::paint_icon(
                                painter,
                                icon_rect,
                                icon,
                                active_style.fg,
                            );
                        }
                    }
                    let text_pos = egui::pos2(
                        start_x + icon_size + icon_gap,
                        rect.center().y - text_galley.size().y / 2.0,
                    );
                    painter.galley_with_override_text_color(
                        text_pos,
                        text_galley.clone(),
                        active_style.fg,
                    );
                }
            } else if has_shortcut || self.full_width {
                let text_pos = egui::pos2(
                    rect.min.x + active_style.h_padding,
                    rect.center().y - text_galley.size().y / 2.0,
                );
                painter.galley_with_override_text_color(
                    text_pos,
                    text_galley.clone(),
                    active_style.fg,
                );
            } else {
                let text_pos = egui::pos2(
                    rect.center().x - text_galley.size().x / 2.0,
                    rect.center().y - text_galley.size().y / 2.0,
                );
                painter.galley_with_override_text_color(
                    text_pos,
                    text_galley.clone(),
                    active_style.fg,
                );
            }

            if let Some(shortcut_galley_val) = shortcut_galley {
                let shortcut_pos = egui::pos2(
                    rect.max.x - active_style.h_padding - shortcut_galley_val.size().x,
                    rect.center().y - shortcut_galley_val.size().y / 2.0,
                );
                let shortcut_fg = if self.selected
                    || active_style.bg == theme.accent
                    || response.is_pointer_button_down_on()
                {
                    theme.accent_foreground
                } else {
                    theme.muted_foreground
                };
                let shortcut_color = if ui.is_enabled() {
                    shortcut_fg
                } else {
                    theme.muted_foreground
                };
                painter.galley_with_override_text_color(
                    shortcut_pos,
                    shortcut_galley_val,
                    shortcut_color,
                );
            }

            // Underline for Link variant
            if active_style.underline && response.hovered() {
                let galley2 = painter.layout_no_wrap(
                    text_string,
                    egui::FontId::proportional(active_style.font_size),
                    active_style.fg,
                );
                let text_pos = egui::pos2(
                    rect.center().x - galley2.size().x / 2.0,
                    rect.center().y - galley2.size().y / 2.0,
                );
                let underline_y = text_pos.y + galley2.size().y;
                let _ = painter.hline(
                    text_pos.x..=text_pos.x + galley2.size().x,
                    underline_y,
                    egui::Stroke::new(1.0, active_style.fg),
                );
            }

            // Focus ring
            if response.has_focus() {
                crate::paint::paint_focus_ring::paint_focus_ring(
                    painter,
                    rect,
                    active_style.corner_radius,
                    theme.ring,
                );
            }
        }

        if ui.is_enabled() {
            response.on_hover_cursor(egui::CursorIcon::PointingHand)
        } else {
            response
        }
    }
}

fn group_corner_radius(
    group_info: Option<&(usize, usize, f32)>,
    default_cr: f32,
) -> egui::CornerRadius {
    match group_info {
        Some((index, cached_count, group_cr)) => {
            let r = crate::utils::f32_to_u8_clamped(*group_cr);
            let is_first = *index == 0;
            let is_last = *cached_count > 0 && *index == *cached_count - 1;
            egui::CornerRadius {
                nw: if is_first { r } else { 0 },
                sw: if is_first { r } else { 0 },
                ne: if is_last { r } else { 0 },
                se: if is_last { r } else { 0 },
            }
        }
        None => egui::CornerRadius::same(crate::utils::f32_to_u8_clamped(default_cr)),
    }
}