use super::*;
pub(crate) fn attach_lazy_overlay_portal<CallbackFn>(
slot: NodeId,
open_sig: Signal<bool>,
theme: UiThemeState,
factory: CallbackFn,
) -> Rc<dyn Fn()>
where
CallbackFn: Fn(ThemeTokens) -> NodeId + 'static,
{
let ctx: Context = current_context();
let mounted: Rc<Cell<bool>> = Rc::new(Cell::new(false));
let factory_rc: Rc<CallbackFn> = Rc::new(factory);
let ctx_m: Context = ctx.clone();
let mounted_m: Rc<Cell<bool>> = Rc::clone(&mounted);
let theme_m: UiThemeState = theme.clone();
let factory_m: Rc<CallbackFn> = Rc::clone(&factory_rc);
let ensure_mounted: Rc<dyn Fn()> = Rc::new(move || {
if mounted_m.replace(true) {
return;
}
let live_tok: ThemeTokens = untracked(|| theme_m.get_tokens().get());
let portal_node: NodeId = with_context(ctx_m.clone(), || factory_m(live_tok));
let tok_sig: Memo<ThemeTokens> = theme_m.get_tokens();
let prev_tok: Rc<Cell<ThemeTokens>> = Rc::new(Cell::new(live_tok));
let theme_ctx: Context = ctx_m.clone();
let (theme_scope, _): (Scope, Effect) = Scope::run(|_: Scope| {
Effect::new(move || {
let next_tok: ThemeTokens = tok_sig.get();
let old_tok: ThemeTokens = prev_tok.replace(next_tok);
if old_tok == next_tok {
return;
}
theme_ctx.get_scene().with_write(
|tree: &mut SceneTree| {
remap_subtree_theme_in_place(tree, portal_node, old_tok, next_tok);
},
(),
);
})
});
ctx_m.get_scene().with_write(
|tree: &mut SceneTree| {
tree.attach_scope(portal_node, theme_scope);
let dirty_mask: u16 = DIRTY_CHILDREN + DIRTY_LAYOUT + DIRTY_PAINT;
tree.append_child(slot, portal_node);
tree.mark_dirty(slot, dirty_mask);
},
(),
);
});
let fallback: Rc<dyn Fn()> = Rc::clone(&ensure_mounted);
let (watch_scope, _): (Scope, Effect) = Scope::run(|_: Scope| {
Effect::new(move || {
if open_sig.get() {
fallback();
}
})
});
ctx.get_scene().with_write(
|tree: &mut SceneTree| {
tree.attach_scope(slot, watch_scope);
},
(),
);
ensure_mounted
}
pub(crate) fn build_lazy_modal(modal_open: Signal<bool>, tokens: ThemeTokens) -> NodeId {
let palette: ThemePalette = tokens.get_palette();
let on_close = move |_: &UiEvent| {
let _: bool = modal_open.set_if_changed(false);
};
let column_seventh_style: Style = style! {
Width(Length::Percent(100.0))
Gap(SPACE_MD)
};
let text_sixth_style: Style = style! {
Width(Length::Percent(100.0))
FontSize(13.0)
TextColor(palette.get_muted_foreground())
};
let row_tertiary_style: Style = style! {
Width(Length::Percent(100.0))
JustifyContent::End
};
let modal_body: NodeId = view! {
column {
style: column_seventh_style
text {
style: text_sixth_style
{ OVERLAYS_MODAL_BODY }
}
row {
style: row_tertiary_style
EuvButton {
label: OVERLAYS_BTN_CLOSE_MODAL.to_string()
tokens: tokens
outline()
small()
on_click: on_close
}
}
}
};
view! {
EuvModal {
open: modal_open
title: OVERLAYS_MODAL_TITLE.to_string()
body: modal_body
tokens: tokens
}
}
}
pub(crate) fn build_lazy_drawer(drawer_open: Signal<bool>, tokens: ThemeTokens) -> NodeId {
let palette: ThemePalette = tokens.get_palette();
let on_close = move |_: &UiEvent| {
let _: bool = drawer_open.set_if_changed(false);
};
let column_sixth_style: Style = style! {
Width(Length::Percent(100.0))
Gap(SPACE_MD)
};
let text_fifth_style: Style = style! {
Width(Length::Percent(100.0))
FontSize(13.0)
TextColor(palette.get_muted_foreground())
};
let row_secondary_style: Style = style! {
Width(Length::Percent(100.0))
JustifyContent::End
};
let drawer_body: NodeId = view! {
column {
style: column_sixth_style
text {
style: text_fifth_style
{ OVERLAYS_DRAWER_BODY }
}
row {
style: row_secondary_style
EuvButton {
label: OVERLAYS_BTN_CLOSE_DRAWER.to_string()
tokens: tokens
outline()
small()
on_click: on_close
}
}
}
};
view! {
EuvDrawer {
open: drawer_open
title: OVERLAYS_DRAWER_TITLE.to_string()
body: drawer_body
tokens: tokens
}
}
}
pub(crate) fn build_lazy_sheet(sheet_open: Signal<bool>, tokens: ThemeTokens) -> NodeId {
let palette: ThemePalette = tokens.get_palette();
let on_close = move |_: &UiEvent| {
let _: bool = sheet_open.set_if_changed(false);
};
let column_fifth_style: Style = style! {
Width(Length::Percent(100.0))
Gap(SPACE_MD)
};
let text_quaternary_style: Style = style! {
Width(Length::Percent(100.0))
FontSize(13.0)
TextColor(palette.get_muted_foreground())
};
let row_style: Style = style! {
Width(Length::Percent(100.0))
JustifyContent::End
};
let sheet_body: NodeId = view! {
column {
style: column_fifth_style
text {
style: text_quaternary_style
{ OVERLAYS_SHEET_BODY }
}
row {
style: row_style
EuvButton {
label: OVERLAYS_BTN_CLOSE_SHEET.to_string()
tokens: tokens
outline()
small()
on_click: on_close
}
}
}
};
view! {
EuvSheet {
open: sheet_open
title: OVERLAYS_SHEET_TITLE.to_string()
body: sheet_body
tokens: tokens
}
}
}
pub(crate) fn mount_overlays_anchored_card(state: &ShowcaseState, tokens: ThemeTokens) -> NodeId {
let palette: ThemePalette = tokens.get_palette();
let popover_open: Signal<bool> = state.get_popover_open();
let dropdown_sel: Signal<Option<usize>> = state.get_dropdown_selected();
let ctx_sel: Signal<Option<usize>> = Signal::new(None);
let on_toggle_popover = move |_: &UiEvent| {
let _: bool = popover_open.set(!popover_open.get_untracked());
};
let pop_trigger: NodeId = view! {
EuvButton {
label: OVERLAYS_POPOVER_BTN.to_string()
tokens: tokens
outline()
small()
on_click: on_toggle_popover
}
};
let text_tertiary_style: Style = style! {
Width(Length::Percent(100.0))
FontSize(12.5)
TextColor(palette.get_foreground())
};
let pop_content: NodeId = view! {
text {
style: text_tertiary_style
{ OVERLAYS_POPOVER_BODY }
}
};
let tip_trigger: NodeId = view! {
EuvButton {
label: OVERLAYS_TOOLTIP_BTN.to_string()
tokens: tokens
outline()
small()
}
};
let dropdown_items: Vec<String> = vec![
OVERLAYS_DROPDOWN_ITEM_1.to_string(),
OVERLAYS_DROPDOWN_ITEM_2.to_string(),
OVERLAYS_DROPDOWN_ITEM_3.to_string(),
];
let ctx_items: Vec<String> = vec![
OVERLAYS_CTX_ITEM_1.to_string(),
OVERLAYS_CTX_ITEM_2.to_string(),
OVERLAYS_CTX_ITEM_3.to_string(),
];
let anchored_readout = move || -> String {
let dd: String = dropdown_sel
.get()
.map(|item_index: usize| format!("#{item_index}"))
.unwrap_or_else(|| OVERLAYS_NONE_LABEL.to_string());
let cm: String = ctx_sel
.get()
.map(|outer_index: usize| format!("#{outer_index}"))
.unwrap_or_else(|| OVERLAYS_NONE_LABEL.to_string());
format!(
"Dropdown: {dd} | Popover: {} | Context: {cm}",
popover_open.get()
)
};
let column_quaternary_style: Style = style! {
Width(Length::Percent(100.0))
Gap(SPACE_MD)
};
let text_secondary_style: Style = style! {
Width(Length::Percent(100.0))
FontSize(12.0)
TextColor(palette.get_muted_foreground())
};
view! {
EuvCard {
title: OVERLAYS_ANCHORED_TITLE.to_string()
tokens: tokens
subtitle: OVERLAYS_ANCHORED_SUB
column {
style: column_quaternary_style
EuvDropdown {
label: OVERLAYS_DROPDOWN_LABEL.to_string()
items: dropdown_items
tokens: tokens
open: state.get_dropdown_open()
selected: dropdown_sel
}
row {
style: c_action_row()
EuvPopover {
open: popover_open
trigger: pop_trigger
content: pop_content
tokens: tokens
}
EuvTooltip {
text: OVERLAYS_TOOLTIP_TEXT.to_string()
trigger: tip_trigger
tokens: tokens
}
}
EuvContextMenu {
trigger_label: OVERLAYS_CTX_TRIGGER.to_string()
items: ctx_items
tokens: tokens
selected: ctx_sel
}
text {
style: text_secondary_style
{ anchored_readout }
}
}
}
}
}
pub fn mount_overlays_page(state: &ShowcaseState, tokens: ThemeTokens) -> NodeId {
let palette: ThemePalette = tokens.get_palette();
let theme: UiThemeState = state.get_theme().clone();
let modal_open: Signal<bool> = state.get_modal_open();
let alert_open: Signal<bool> = state.get_alert_dialog_open();
let alert_confirmed: Signal<bool> = state.get_alert_dialog_confirmed();
let drawer_open: Signal<bool> = state.get_drawer_open();
let sheet_open: Signal<bool> = state.get_sheet_open();
let toast_visible: Signal<bool> = state.get_toast_visible();
let notif_visible: Signal<bool> = state.get_notif_visible();
let loading_active: Signal<bool> = state.get_loading_active();
let cmd_open: Signal<bool> = state.get_cmd_palette_open();
let sonner_msgs: Signal<Vec<(String, String)>> = Signal::new(Vec::new());
let sonner_seq: Signal<u32> = Signal::new(0);
let column_tertiary_style: Style = style! {
Position::Absolute
Width(Length::Px(0.0))
Height(Length::Px(0.0))
};
let portal_slot: NodeId = view! {
column {
style: column_tertiary_style
}
};
let ensure_modal: Rc<dyn Fn()> = attach_lazy_overlay_portal(
portal_slot,
modal_open,
theme.clone(),
move |tok: ThemeTokens| build_lazy_modal(modal_open, tok),
);
let ensure_alert: Rc<dyn Fn()> = attach_lazy_overlay_portal(
portal_slot,
alert_open,
theme.clone(),
move |tok: ThemeTokens| {
view! {
EuvAlertDialog {
open: alert_open
title: OVERLAYS_ALERT_TITLE.to_string()
confirmed: alert_confirmed
tokens: tok
description: OVERLAYS_ALERT_DESC
labels(OVERLAYS_ALERT_CANCEL, OVERLAYS_ALERT_CONTINUE)
}
}
},
);
let ensure_drawer: Rc<dyn Fn()> = attach_lazy_overlay_portal(
portal_slot,
drawer_open,
theme.clone(),
move |tok: ThemeTokens| build_lazy_drawer(drawer_open, tok),
);
let ensure_sheet: Rc<dyn Fn()> = attach_lazy_overlay_portal(
portal_slot,
sheet_open,
theme.clone(),
move |tok: ThemeTokens| build_lazy_sheet(sheet_open, tok),
);
let ensure_toast: Rc<dyn Fn()> = attach_lazy_overlay_portal(
portal_slot,
toast_visible,
theme.clone(),
move |tok: ThemeTokens| {
view! {
EuvToast {
visible: toast_visible
message: OVERLAYS_TOAST_TEXT.to_string()
tone: EuvTone::Success
tokens: tok
}
}
},
);
let ensure_notif: Rc<dyn Fn()> = attach_lazy_overlay_portal(
portal_slot,
notif_visible,
theme.clone(),
move |tok: ThemeTokens| {
view! {
EuvNotification {
visible: notif_visible
title: OVERLAYS_NOTIF_TITLE.to_string()
message: OVERLAYS_NOTIF_MSG.to_string()
tokens: tok
tone: EuvTone::Success
}
}
},
);
let ensure_loading: Rc<dyn Fn()> = attach_lazy_overlay_portal(
portal_slot,
loading_active,
theme,
move |tok: ThemeTokens| {
view! {
EuvLoading {
active: loading_active
label: OVERLAYS_LOADING_LABEL.to_string()
tokens: tok
}
}
},
);
let on_open_modal = move |_: &UiEvent| {
ensure_modal();
let _: bool = modal_open.set_if_changed(true);
};
let on_open_alert = move |_: &UiEvent| {
ensure_alert();
let _: bool = alert_open.set_if_changed(true);
};
let on_toggle_drawer = move |_: &UiEvent| {
ensure_drawer();
let _: bool = drawer_open.set(!drawer_open.get_untracked());
};
let on_toggle_sheet = move |_: &UiEvent| {
ensure_sheet();
let _: bool = sheet_open.set(!sheet_open.get_untracked());
};
let on_open_cmd = move |_: &UiEvent| {
let _: bool = cmd_open.set_if_changed(true);
};
let on_toggle_toast = move |_: &UiEvent| {
ensure_toast();
let _: bool = toast_visible.set(!toast_visible.get_untracked());
};
let on_toggle_notif = move |_: &UiEvent| {
ensure_notif();
let _: bool = notif_visible.set(!notif_visible.get_untracked());
};
let on_push_sonner = move |_: &UiEvent| {
let next_n: u32 = sonner_seq.get_untracked().saturating_add(1);
let _: bool = sonner_seq.set(next_n);
let entry: (String, String) = (
format!("{OVERLAYS_SONNER_TITLE} #{next_n}"),
OVERLAYS_SONNER_DESC.to_string(),
);
let _: bool = {
if let Some(mut updated_value) = sonner_msgs.try_get_untracked() {
{
let items: &mut Vec<(String, String)> = &mut updated_value;
items.insert(0, entry);
if items.len() > 3 {
items.truncate(3);
}
}
sonner_msgs.set(updated_value)
} else {
false
}
};
};
let on_clear_sonner = move |_: &UiEvent| {
let _: bool = sonner_msgs.set_if_changed(Vec::new());
};
let on_toggle_loading = move |_: &UiEvent| {
ensure_loading();
let _: bool = loading_active.set(!loading_active.get_untracked());
};
let portals_readout = move || -> String {
format!(
"Modal: {} | Drawer: {} | Sheet: {} | Alert Confirmed: {}",
modal_open.get(),
drawer_open.get(),
sheet_open.get(),
alert_confirmed.get()
)
};
let column_secondary_style: Style = style! {
Width(Length::Percent(100.0))
Gap(SPACE_MD)
};
let text_style: Style = style! {
Width(Length::Percent(100.0))
FontSize(12.0)
TextColor(palette.get_muted_foreground())
};
let portals_card: NodeId = view! {
EuvCard {
title: OVERLAYS_PORTALS_TITLE.to_string()
tokens: tokens
subtitle: OVERLAYS_PORTALS_SUB
column {
style: column_secondary_style
EuvActionRow {
EuvButton {
label: OVERLAYS_BTN_OPEN_MODAL.to_string()
tokens: tokens
small()
on_click: on_open_modal
}
EuvButton {
label: OVERLAYS_BTN_ALERT_DIALOG.to_string()
tokens: tokens
destructive()
small()
on_click: on_open_alert
}
EuvButton {
label: OVERLAYS_BTN_TOGGLE_DRAWER.to_string()
tokens: tokens
outline()
small()
on_click: on_toggle_drawer
}
EuvButton {
label: OVERLAYS_BTN_TOGGLE_SHEET.to_string()
tokens: tokens
outline()
small()
on_click: on_toggle_sheet
}
EuvButton {
label: OVERLAYS_BTN_CMD_PALETTE.to_string()
tokens: tokens
outline()
small()
on_click: on_open_cmd
}
}
text {
style: text_style
{ portals_readout }
}
}
}
};
let column_style: Style = style! {
Width(Length::Percent(100.0))
Gap(SPACE_MD)
};
let feedback_card: NodeId = view! {
EuvCard {
title: OVERLAYS_FEEDBACK_TITLE.to_string()
tokens: tokens
subtitle: OVERLAYS_FEEDBACK_SUB
column {
style: column_style
EuvActionRow {
EuvButton {
label: OVERLAYS_BTN_TOGGLE_TOAST.to_string()
tokens: tokens
outline()
small()
on_click: on_toggle_toast
}
EuvButton {
label: OVERLAYS_BTN_TOGGLE_NOTIF.to_string()
tokens: tokens
outline()
small()
on_click: on_toggle_notif
}
EuvButton {
label: OVERLAYS_BTN_PUSH_SONNER.to_string()
tokens: tokens
small()
on_click: on_push_sonner
}
EuvButton {
label: OVERLAYS_BTN_CLEAR_SONNER.to_string()
tokens: tokens
ghost()
small()
on_click: on_clear_sonner
}
EuvButton {
label: OVERLAYS_BTN_TOGGLE_LOADING.to_string()
tokens: tokens
outline()
small()
on_click: on_toggle_loading
}
}
EuvSonner {
messages: sonner_msgs
tokens: tokens
}
}
}
};
let cards: Vec<NodeId> = vec![
portals_card,
mount_overlays_anchored_card(state, tokens),
feedback_card,
];
let grid: NodeId = mount_responsive_grid(cards, 2);
view! {
EuvPage {
title: OVERLAYS_TITLE.to_string()
subtitle: OVERLAYS_SUBTITLE.to_string()
tokens: tokens
{ grid }
{ portal_slot }
}
}
}