euv-example 0.51.0

Cross-platform native showcase application demonstrating euv fine-grained reactivity, multi-mode rendering, window controls, and euv-ui components.
Documentation
use super::*;

/// Mounts the 2D physics & sprite engine studio with a full-width interactive arena viewport and a single fullscreen toggle button.
///
/// # Arguments
///
/// - `&ShowcaseState` - Reactive showcase state.
/// - `ThemeTokens` - Active design tokens.
///
/// # Returns
///
/// - `NodeId` - Mounted game engine showcase page node.
pub fn mount_game_page(state: &ShowcaseState, tokens: ThemeTokens) -> NodeId {
    let host: GameEngineHost = state.get_game_host().clone();
    let fs_2d_open: Signal<bool> = state.get_game_fullscreen_open();
    let viewport: NodeId = host.mount_viewport(style! {
        Width(Length::Percent(100.0))
        Height(Length::Percent(100.0))
    });
    let fs_enter_btn: NodeId =
        mount_game_fullscreen_enter_button(BTN_GAME_FULLSCREEN, fs_2d_open, tokens);
    let vp_sig: Signal<Size> = use_viewport_size();
    let vp_frame_style = move || -> Style {
        let live: ThemeTokens = use_live_tokens(tokens);
        let height_val: f32 = adaptive_viewport_height(vp_sig.get().get_height());
        style! {
            FlexDirection::Column
            Width(Length::Percent(100.0))
            Height(Length::Px(height_val))
            Overflow::Hidden
            Border::new(live.get_geometry().get_border_width(), live.get_palette().get_border())
            Radius(live.get_geometry().get_radius_md())
        }
    };
    let viewport_frame: NodeId = view! {
        column {
            bind_style: vp_frame_style
            { viewport }
            { fs_enter_btn }
        }
    };
    let fs_2d_portal: NodeId = mount_game_fullscreen_portal(fs_2d_open, &host, viewport, tokens);
    let hint_style = move || -> Style {
        let live: ThemeTokens = use_live_tokens(tokens);
        style! {
            Width(Length::Percent(100.0))
            FontSize(12.0)
            FontFamily::Monospace
            TextColor(live.get_palette().get_muted_foreground())
        }
    };
    let card_col_style: Style = style! {
        Width(Length::Percent(100.0))
        Gap(SPACE_SM)
    };
    let arena_card: NodeId = view! {
        EuvCard {
            title: GAME_CARD_TITLE.to_string()
            tokens: tokens
            subtitle: GAME_CARD_SUB
            column {
                style: card_col_style
                { viewport_frame }
                text {
                    bind_style: hint_style
                    { GAME_HUD_CONTROLS_HINT }
                }
            }
        }
    };
    view! {
        EuvPage {
            title: GAME_TITLE.to_string()
            subtitle: GAME_SUBTITLE.to_string()
            tokens: tokens
            { arena_card }
            { fs_2d_portal }
        }
    }
}

/// Mounts the single corner fullscreen entry button overlaid on a game viewport frame, hidden automatically while fullscreen is active.
///
/// # Arguments
///
/// - `&str` - Accessible and visible fullscreen button label.
/// - `Signal<bool>` - Fullscreen visibility toggle signal.
/// - `ThemeTokens` - Active design tokens.
///
/// # Returns
///
/// - `NodeId` - Mounted corner fullscreen entry button node.
pub(crate) fn mount_game_fullscreen_enter_button(
    label: &str,
    open: Signal<bool>,
    tokens: ThemeTokens,
) -> NodeId {
    let label_text: String = label.to_string();
    let label_a11y: String = label.to_string();
    let on_open_fs = move |_: &UiEvent| {
        let _: bool = open.set_if_changed(true);
    };
    let btn_style = move || -> Style {
        let live: ThemeTokens = use_live_tokens(tokens);
        let live_p: ThemePalette = live.get_palette();
        let live_g: ThemeGeometry = live.get_geometry();
        let is_open: bool = open.get();
        style! {
            if is_open {
                Display::None
            } else {
                Display::Flex
            }
            Position::Absolute
            Inset(Some(SPACE_SM), Some(SPACE_SM), None, None)
            FlexDirection::Row
            Height(Length::Px(FS_EXIT_BTN_SIZE))
            Padding(Edges::symmetric(0.0, SPACE_SM))
            Gap(SPACE_XS)
            JustifyContent::Center
            AlignItems::Center
            Radius(live_g.get_radius_md())
            Background(live_p.get_primary())
            Border::new(live_g.get_border_width(), live_p.get_primary())
            CursorIcon::Pointer
            PointerEvents::Auto
            StopEventPropagation(true)
            ZIndex(2)
        }
    };
    let text_style = move || -> Style {
        let live: ThemeTokens = use_live_tokens(tokens);
        style! {
            FontSize(12.0)
            FontWeight::SemiBold
            TextColor(live.get_palette().get_primary_foreground())
            TextOverflow::Clip
        }
    };
    let paint_enter_icon = move |bounds: Rect, display_list: &mut DisplayList| {
        let live: ThemeTokens = live_tokens_untracked(tokens);
        draw_euv_icon(
            EuvIconName::Maximize,
            bounds,
            display_list,
            live.get_palette().get_primary_foreground(),
        );
    };
    let icon_style: Style = style! {
        Width(Length::Px(FS_ENTER_ICON_SIZE))
        Height(Length::Px(FS_ENTER_ICON_SIZE))
    };
    view! {
        row {
            bind_style: btn_style
            role: A11yRole::Button
            a11y_label: label_a11y
            on_click: on_open_fs
            canvas {
                caching: CanvasCaching::RetainedStatic
                style: icon_style
                painter: paint_enter_icon
            }
            text {
                bind_style: text_style
                { label_text }
            }
        }
    }
}

/// Mounts a reusable edge-to-edge fullscreen portal overlay hosting any 2D/3D `GameEngineHost` with a single safe-area-aware exit button and Escape/Back dismissal.
///
/// # Arguments
///
/// - `Signal<bool>` - Fullscreen visibility toggle signal.
/// - `&GameEngineHost` - Target game engine host.
/// - `NodeId` - Embedded inline viewport hidden while fullscreen is active to avoid redundant rendering.
/// - `ThemeTokens` - Active design tokens.
///
/// # Returns
///
/// - `NodeId` - Mounted fullscreen portal handle.
pub(crate) fn mount_game_fullscreen_portal(
    open: Signal<bool>,
    host: &GameEngineHost,
    inline_viewport: NodeId,
    tokens: ThemeTokens,
) -> NodeId {
    let ctx: Context = current_context();
    bind_fullscreen_context(&ctx);
    let safe_sig: Signal<SafeAreaInsets> = ctx.get_safe_area();
    let vp_sig: Signal<Size> = ctx.get_viewport_size();
    let fs_viewport: NodeId = host.mount_viewport(style! {
        Position::Absolute
        Inset(Some(0.0), Some(0.0), Some(0.0), Some(0.0))
        Width(Length::Percent(100.0))
        Height(Length::Percent(100.0))
        Overflow::Hidden
        StopScrollPropagation(true)
        StopEventPropagation(true)
    });
    let on_exit = move |_: &UiEvent| {
        let _: bool = open.set_if_changed(false);
    };
    let init_dark: ThemeTokens =
        ThemeTokens::resolve(live_tokens_untracked(tokens).get_scheme(), ThemeMode::Dark);
    let init_dark_p: ThemePalette = init_dark.get_palette();
    let paint_exit_icon = move |bounds: Rect, display_list: &mut DisplayList| {
        let dark_tok: ThemeTokens =
            ThemeTokens::resolve(live_tokens_untracked(tokens).get_scheme(), ThemeMode::Dark);
        draw_euv_icon(
            EuvIconName::Minimize,
            bounds,
            display_list,
            dark_tok.get_palette().get_foreground(),
        );
    };
    let exit_btn_style = move || -> Style {
        let dark_tok: ThemeTokens =
            ThemeTokens::resolve(use_live_tokens(tokens).get_scheme(), ThemeMode::Dark);
        let dark_p: ThemePalette = dark_tok.get_palette();
        let dark_g: ThemeGeometry = dark_tok.get_geometry();
        let fs_lay: FullscreenSafeLayout =
            resolve_fullscreen_safe_layout(vp_sig.get(), safe_sig.get());
        let hp: Edges = fs_lay.get_header_padding();
        style! {
            Position::Absolute
            Inset(Some(hp.get_top()), Some(hp.get_right()), None, None)
            FlexDirection::Row
            Width(Length::Px(FS_EXIT_BTN_SIZE))
            Height(Length::Px(FS_EXIT_BTN_SIZE))
            JustifyContent::Center
            AlignItems::Center
            Radius(FS_PILL_RADIUS)
            Background(dark_p.get_card().with_alpha(232))
            Border::new(dark_g.get_border_width(), dark_p.get_border())
            CursorIcon::Pointer
            PointerEvents::Auto
            StopEventPropagation(true)
            ZIndex(2)
        }
    };
    let portal_style: Style = style! {
        FlexDirection::Column
        Display::None
        Position::Fixed
        Inset(Some(0.0), Some(0.0), Some(0.0), Some(0.0))
        Width(Length::Percent(100.0))
        Height(Length::Percent(100.0))
        Padding(Edges::all(0.0))
        Margin(Edges::all(0.0))
        Background(init_dark_p.get_background())
        Border::new(0.0, COLOR_TRANSPARENT)
        Radius(0.0)
        Overflow::Hidden
        PointerEvents::None
        StopScrollPropagation(true)
        StopEventPropagation(true)
        ZIndex(Z_INDEX_FULLSCREEN)
    };
    let canvas_style: Style = style! {
        Width(Length::Px(FS_EXIT_ICON_SIZE))
        Height(Length::Px(FS_EXIT_ICON_SIZE))
    };
    let portal: NodeId = view! {
        portal {
            style: portal_style
            { fs_viewport }
            row {
                bind_style: exit_btn_style
                role: A11yRole::Button
                a11y_label: BTN_EXIT_FULLSCREEN.to_string()
                on_click: on_exit
                canvas {
                    caching: CanvasCaching::RetainedStatic
                    style: canvas_style
                    painter: paint_exit_icon
                }
            }
        }
    };
    let handle: SceneHandle = ctx.get_scene().clone();
    let prev_fs: Rc<Cell<bool>> = Rc::new(Cell::new(false));
    let fs_acquired: Rc<Cell<bool>> = Rc::new(Cell::new(false));
    let fs_cleanup: Rc<Cell<bool>> = Rc::clone(&fs_acquired);
    let (scope, _): (Scope, Effect) = Scope::run(|_: Scope| {
        on_cleanup(move || {
            if fs_cleanup.replace(false) {
                exit_fullscreen();
            }
        });
        let _: Effect = EuvOverlayStack::bind_signal(open, FS_OVERLAY_NAME, Z_INDEX_FULLSCREEN);
        Effect::new(move || {
            let is_open: bool = open.get();
            if is_open != prev_fs.replace(is_open) {
                if is_open {
                    if !fs_acquired.replace(true) {
                        enter_fullscreen();
                    }
                } else if fs_acquired.replace(false) {
                    exit_fullscreen();
                }
            }
            handle.with_write(
                |tree: &mut SceneTree| {
                    tree.cancel_subtree_transitions(portal);
                    if let Some(item_count) = tree.get_node_mut(inline_viewport) {
                        let mut computed_style: Style = item_count.get_style();
                        computed_style.get_mut_layout().set_display(if is_open {
                            Display::None
                        } else {
                            Display::Flex
                        });
                        item_count.set_style(computed_style);
                        tree.mark_dirty(inline_viewport, DIRTY_LAYOUT | DIRTY_PAINT);
                    }
                    if let Some(node_entry) = tree.get_node_mut(portal) {
                        let mut element_style: Style = node_entry.get_style();
                        element_style.get_mut_layout().set_display(if is_open {
                            Display::Flex
                        } else {
                            Display::None
                        });
                        element_style
                            .get_mut_paint()
                            .set_pointer_events(if is_open {
                                PointerEvents::Auto
                            } else {
                                PointerEvents::None
                            })
                            .set_opacity(1.0)
                            .set_transform(Transform2D::default());
                        node_entry.set_style(element_style);
                    }
                    let dirty_l: u16 = DIRTY_LAYOUT | DIRTY_SUBTREE_LAYOUT;
                    tree.mark_dirty(portal, dirty_l | DIRTY_PAINT);
                },
                (),
            );
        })
    });
    ctx.get_scene().with_write(
        |tree: &mut SceneTree| {
            tree.attach_scope(portal, scope);
        },
        (),
    );
    portal
}