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 Sheet — renders a slide-in panel overlay with animation.

impl super::widget::Sheet {
    pub fn show(self, ctx: &egui::Context, open: &mut bool, content: impl FnOnce(&mut egui::Ui)) {
        let anim_id = egui::Id::unique("sheet_anim");
        let anim_t = ctx.animate_bool_with_time(anim_id, *open, 0.2);

        if anim_t <= 0.0 {
            return;
        }

        let theme = crate::theme::shadcn_theme_ext::ShadcnThemeExt::shadcn_theme(ctx);
        let screen = ctx.input(egui::InputState::viewport_rect);
        let spacing = crate::responsive::responsive_ext::ResponsiveExt::responsive_spacing(ctx);
        let on_mobile = spacing.is_mobile();
        let ease_t = ease_out_cubic(anim_t);

        // Animated backdrop
        let backdrop_alpha = crate::utils::f32_to_u8_clamped(
            f32::from(crate::widgets::overlay_common::BACKDROP_ALPHA) * ease_t,
        );
        let backdrop_response =
            crate::widgets::overlay_common::paint_backdrop(ctx, "sheet_backdrop", backdrop_alpha);

        if backdrop_response.clicked() {
            *open = false;
            ctx.request_repaint();
        }

        // Compute animated slide offset - on mobile Left/Right become bottom sheet for thumb reach
        let is_horizontal = matches!(
            self.side,
            crate::tokens::sheet_side::SheetSide::Left
                | crate::tokens::sheet_side::SheetSide::Right
        );
        let on_mobile_horizontal = on_mobile && is_horizontal;
        let panel_id = egui::Id::unique("sheet_panel");
        let measured_panel_height = ctx.memory(|m| {
            m.area_rect(panel_id)
                .map_or_else(|| screen.height(), |r| r.height())
        });
        let slide_dist = if on_mobile_horizontal
            || matches!(
                self.side,
                crate::tokens::sheet_side::SheetSide::Top
                    | crate::tokens::sheet_side::SheetSide::Bottom
            ) {
            measured_panel_height + 24.0
        } else {
            self.width + 48.0
        };
        let (anchor, offset, is_bottom_sheet) = match self.side {
            crate::tokens::sheet_side::SheetSide::Right
            | crate::tokens::sheet_side::SheetSide::Left
                if on_mobile_horizontal =>
            {
                (
                    egui::Align2::LEFT_BOTTOM,
                    egui::vec2(0.0, (1.0 - ease_t) * slide_dist),
                    true,
                )
            }
            crate::tokens::sheet_side::SheetSide::Right => (
                egui::Align2::RIGHT_TOP,
                egui::vec2((1.0 - ease_t) * slide_dist, 0.0),
                false,
            ),
            crate::tokens::sheet_side::SheetSide::Left => (
                egui::Align2::LEFT_TOP,
                egui::vec2(-(1.0 - ease_t) * slide_dist, 0.0),
                false,
            ),
            crate::tokens::sheet_side::SheetSide::Top => (
                egui::Align2::LEFT_TOP,
                egui::vec2(0.0, -(1.0 - ease_t) * slide_dist),
                false,
            ),
            crate::tokens::sheet_side::SheetSide::Bottom => (
                egui::Align2::LEFT_BOTTOM,
                egui::vec2(0.0, (1.0 - ease_t) * slide_dist),
                false,
            ),
        };

        let _ = egui::Area::new(panel_id)
            .order(egui::Order::Foreground)
            .anchor(anchor, offset)
            .show(ctx, |inner_ui| {
                let frame = if is_bottom_sheet
                    || matches!(self.side, crate::tokens::sheet_side::SheetSide::Bottom)
                {
                    let cr = egui::CornerRadius {
                        nw: crate::utils::f32_to_u8_clamped(theme.radius + 2.0),
                        ne: crate::utils::f32_to_u8_clamped(theme.radius + 2.0),
                        sw: 0,
                        se: 0,
                    };
                    egui::Frame::NONE
                        .fill(theme.background)
                        .inner_margin(egui::Margin {
                            left: 24,
                            right: 24,
                            top: 16,
                            bottom: 24,
                        })
                        .corner_radius(cr)
                        .stroke(egui::Stroke::new(1.0, theme.border))
                } else if matches!(self.side, crate::tokens::sheet_side::SheetSide::Top) {
                    let cr = egui::CornerRadius {
                        nw: 0,
                        ne: 0,
                        sw: crate::utils::f32_to_u8_clamped(theme.radius + 2.0),
                        se: crate::utils::f32_to_u8_clamped(theme.radius + 2.0),
                    };
                    egui::Frame::NONE
                        .fill(theme.background)
                        .inner_margin(egui::Margin::same(24))
                        .corner_radius(cr)
                        .stroke(egui::Stroke::new(1.0, theme.border))
                } else {
                    egui::Frame::NONE
                        .fill(theme.background)
                        .inner_margin(egui::Margin::same(24))
                        .corner_radius(egui::CornerRadius::ZERO)
                        .stroke(egui::Stroke::new(1.0, theme.border))
                };

                let _ = frame.show(inner_ui, |content_ui| {
                    if is_bottom_sheet {
                        let width = (screen.width() - 50.0).clamp(0.0, 500.0);
                        content_ui.set_min_width(width);
                        content_ui.set_max_width(width);
                    } else if is_horizontal {
                        // Desktop side sheet - full height (inset-y-0)
                        let width = self.width.min((screen.width() - 98.0).max(0.0));
                        let height = (screen.height() - 50.0).max(0.0);
                        content_ui.set_min_size(egui::vec2(width, height));
                        content_ui.set_max_width(width);
                    } else {
                        content_ui.set_min_width((screen.width() - 98.0).max(0.0));
                        content_ui
                            .set_max_height(self.width.min((screen.height() - 98.0).max(0.0)));
                    }

                    // Close button
                    crate::widgets::overlay_common::close_button(content_ui, ctx, &theme, open);

                    // Title & description
                    if let Some(title) = self.title {
                        let _ = content_ui.label(
                            egui::RichText::new(title)
                                .color(theme.foreground)
                                .size(18.0)
                                .strong(),
                        );
                    }

                    if let Some(desc) = self.description {
                        content_ui.add_space(4.0);
                        let _ = content_ui.label(
                            egui::RichText::new(desc)
                                .color(theme.muted_foreground)
                                .size(14.0),
                        );
                    }

                    content_ui.add_space(16.0);
                    content(content_ui);
                });
            });

        ctx.request_repaint();
    }
}

fn ease_out_cubic(t: f32) -> f32 {
    1.0 - (1.0 - t).powi(3)
}