use super::*;
#[must_use]
#[cfg_attr(not(target_arch = "wasm32"), inline)]
pub fn mount_view_default() -> NodeId {
mount_view(Style::default())
}
#[must_use]
#[cfg_attr(not(target_arch = "wasm32"), inline)]
pub fn mount_view(style: Style) -> NodeId {
with_current_context(|context: &Context| {
context.get_scene().with_write(
|tree: &mut SceneTree| tree.create_view(style),
NodeId::default(),
)
})
}
#[must_use]
#[cfg_attr(not(target_arch = "wasm32"), inline)]
pub fn mount_row_default() -> NodeId {
mount_view(Style::default())
}
#[must_use]
#[cfg_attr(not(target_arch = "wasm32"), inline)]
pub fn mount_row(mut style: Style) -> NodeId {
let layout: &mut LayoutStyle = style.get_mut_layout();
if layout.get_display() != Display::None {
layout.set_display(Display::Flex);
}
layout.set_flex_direction(FlexDirection::Row);
mount_view(style)
}
#[must_use]
#[cfg_attr(not(target_arch = "wasm32"), inline)]
pub fn mount_column_default() -> NodeId {
let mut style: Style = Style::default();
style
.get_mut_layout()
.set_flex_direction(FlexDirection::Column);
mount_view(style)
}
#[must_use]
#[cfg_attr(not(target_arch = "wasm32"), inline)]
pub fn mount_column(mut style: Style) -> NodeId {
let layout: &mut LayoutStyle = style.get_mut_layout();
if layout.get_display() != Display::None {
layout.set_display(Display::Flex);
}
layout.set_flex_direction(FlexDirection::Column);
mount_view(style)
}
#[must_use]
#[cfg_attr(not(target_arch = "wasm32"), inline)]
pub fn mount_grid_default() -> NodeId {
let mut style: Style = Style::default();
style
.get_mut_layout()
.set_display(Display::Grid)
.set_grid_columns(2);
mount_view(style)
}
#[must_use]
#[cfg_attr(not(target_arch = "wasm32"), inline)]
pub fn mount_grid(mut style: Style) -> NodeId {
let layout: &mut LayoutStyle = style.get_mut_layout();
if layout.get_display() != Display::Grid && layout.get_display() != Display::None {
layout.set_display(Display::Grid).set_grid_columns(2);
}
mount_view(style)
}
#[must_use]
#[cfg_attr(not(target_arch = "wasm32"), inline)]
pub fn mount_scroll_default() -> NodeId {
let mut style: Style = Style::default();
style
.get_mut_layout()
.set_flex_direction(FlexDirection::Column);
mount_scroll(style)
}
#[must_use]
#[cfg_attr(not(target_arch = "wasm32"), inline)]
pub fn mount_scroll(style: Style) -> NodeId {
with_current_context(|context: &Context| {
context.get_scene().with_write(
|tree: &mut SceneTree| tree.create_scroll_view(style),
NodeId::default(),
)
})
}
#[must_use]
#[cfg_attr(not(target_arch = "wasm32"), inline)]
pub fn mount_portal_default() -> NodeId {
mount_portal(Style::default())
}
#[must_use]
#[cfg_attr(not(target_arch = "wasm32"), inline)]
pub fn mount_portal(style: Style) -> NodeId {
with_current_context(|context: &Context| {
context.get_scene().with_write(
|tree: &mut SceneTree| tree.create_portal(style),
NodeId::default(),
)
})
}
#[must_use]
#[cfg_attr(not(target_arch = "wasm32"), inline)]
pub fn mount_fragment() -> NodeId {
with_current_context(|context: &Context| {
context.get_scene().with_write(
|tree: &mut SceneTree| tree.create_fragment(),
NodeId::default(),
)
})
}
#[must_use]
#[cfg_attr(not(target_arch = "wasm32"), inline)]
pub fn mount_plain_text_str(content: &str) -> NodeId {
mount_text_str(content, Style::default())
}
#[must_use]
#[cfg_attr(not(target_arch = "wasm32"), inline)]
pub fn mount_text_str(content: &str, style: Style) -> NodeId {
let owned_text: String = content.to_string();
with_current_context(|context: &Context| {
context.get_scene().with_write(
|tree: &mut SceneTree| tree.create_text(owned_text, style),
NodeId::default(),
)
})
}
#[must_use]
#[cfg_attr(not(target_arch = "wasm32"), inline)]
pub fn mount_input_node(state: InputNodeState, style: Style) -> NodeId {
with_current_context(|context: &Context| {
context.get_scene().with_write(
|tree: &mut SceneTree| tree.create_input(state, style),
NodeId::default(),
)
})
}
#[must_use]
#[cfg_attr(not(target_arch = "wasm32"), inline)]
pub fn mount_image_node(buffer: ImageBuffer, style: Style) -> NodeId {
with_current_context(|context: &Context| {
context.get_scene().with_write(
|tree: &mut SceneTree| tree.create_image(buffer, style),
NodeId::default(),
)
})
}
#[must_use]
#[cfg_attr(not(target_arch = "wasm32"), inline)]
pub fn mount_canvas_node(
style: Style,
caching: CanvasCaching,
painter: CanvasDrawCallback,
) -> NodeId {
with_current_context(|context: &Context| {
context.get_scene().with_write(
|tree: &mut SceneTree| tree.create_canvas_with_caching(style, caching, painter),
NodeId::default(),
)
})
}
#[must_use]
pub fn mount_dynamic_slot(
slot_id: String,
style: Style,
hub: Option<DynamicUpdateHub>,
fallback: DynamicSlotFallback,
) -> NodeId {
let mut slot: DynamicComponentSlot = DynamicComponentSlot::new(slot_id, fallback);
let _: &mut DynamicComponentSlot = slot.set_style(style);
if hub.is_some() {
let _: &mut DynamicComponentSlot = slot.set_hub(hub);
}
Component::mount(slot)
}
#[must_use]
pub fn mount_dynamic_page_slot(
route: String,
style: Style,
hub: Option<DynamicUpdateHub>,
fallback: DynamicSlotFallback,
) -> NodeId {
let mut slot: DynamicPageSlot = DynamicPageSlot::new(route, fallback);
let _: &mut DynamicPageSlot = slot.set_style(style);
if hub.is_some() {
let _: &mut DynamicPageSlot = slot.set_hub(hub);
}
Component::mount(slot)
}
#[must_use]
pub fn empty_slot_fallback() -> DynamicSlotFallback {
Rc::new(mount_view_default)
}
#[must_use]
pub fn mount_slot_fallback_column() -> NodeId {
let mut style: Style = Style::default();
style
.get_mut_layout()
.set_flex_direction(FlexDirection::Column)
.set_width(Length::Percent(100.0));
mount_view(style)
}
#[cfg_attr(not(target_arch = "wasm32"), inline)]
pub fn append_view_child(parent: NodeId, child: NodeId) {
with_current_context(|context: &Context| {
context.get_scene().with_write(
|tree: &mut SceneTree| {
tree.append_child(parent, child);
},
(),
);
});
}
#[cfg_attr(not(target_arch = "wasm32"), inline)]
pub fn append_view_children(parent: NodeId, children: &[NodeId]) {
if children.is_empty() {
return;
}
with_current_context(|context: &Context| {
context.get_scene().with_write(
|tree: &mut SceneTree| {
tree.append_children(parent, children);
},
(),
);
});
}
pub fn mount_spread_into<Iter, Item>(parent: NodeId, items: Iter)
where
Iter: IntoIterator<Item = Item>,
Item: Component,
{
let mounted: Vec<NodeId> = items.into_iter().map(Component::mount).collect();
append_view_children(parent, &mounted);
}
pub fn extend_spread_into<Iter, Item>(batch: &mut Vec<NodeId>, items: Iter)
where
Iter: IntoIterator<Item = Item>,
Item: Component,
{
batch.extend(items.into_iter().map(Component::mount));
}
#[must_use]
#[cfg_attr(not(target_arch = "wasm32"), inline)]
pub fn into_ui_event_handler<HandlerFn>(handler: HandlerFn) -> UiEventHandler
where
HandlerFn: FnMut(&UiEvent) + 'static,
{
Rc::new(RefCell::new(handler))
}
#[must_use]
#[cfg_attr(not(target_arch = "wasm32"), inline)]
pub fn into_canvas_painter<PainterFn>(painter: PainterFn) -> CanvasDrawCallback
where
PainterFn: FnMut(Rect, &mut DisplayList) + 'static,
{
Rc::new(RefCell::new(painter))
}
#[cfg_attr(not(target_arch = "wasm32"), inline)]
pub fn bind_node_on_click(node: NodeId, handler: UiEventHandler) {
with_current_context(|context: &Context| {
context.get_scene().with_write(
|tree: &mut SceneTree| {
if let Some(scene_node) = tree.get_node_mut(node) {
scene_node.get_mut_handlers().set_on_click(Some(handler));
scene_node.get_mut_interaction().set_focusable(true);
let paint: &mut PaintStyle = scene_node.get_mut_style().get_mut_paint();
if paint.get_cursor() == CursorIcon::Default {
paint.set_cursor(CursorIcon::Pointer);
}
}
},
(),
);
});
}
#[must_use]
#[cfg_attr(not(target_arch = "wasm32"), inline)]
pub fn finish_node_child(parent: NodeId, child: NodeId) -> NodeId {
append_view_child(parent, child);
parent
}
#[must_use]
#[cfg_attr(not(target_arch = "wasm32"), inline)]
pub fn finish_node_children(parent: NodeId, children: &[NodeId]) -> NodeId {
append_view_children(parent, children);
parent
}
#[must_use]
#[cfg_attr(not(target_arch = "wasm32"), inline)]
pub fn finish_node_on_click(node: NodeId, handler: UiEventHandler) -> NodeId {
bind_node_on_click(node, handler);
node
}
#[must_use]
#[cfg_attr(not(target_arch = "wasm32"), inline)]
pub fn finish_node_patch(node: NodeId, patch: NodeInitPatch) -> NodeId {
apply_node_init_patch(node, patch);
node
}
pub fn apply_node_init_patch(node: NodeId, patch: NodeInitPatch) {
let NodeInitPatch {
focusable,
on_click,
on_change,
on_scroll,
on_pointer_down,
on_pointer_move,
on_pointer_up,
on_pointer_enter,
on_pointer_leave,
on_key_down,
on_focus,
on_blur,
a11y,
} = patch;
with_current_context(|context: &Context| {
context.get_scene().with_write(
|tree: &mut SceneTree| {
let Some(scene_node) = tree.get_node_mut(node) else {
return;
};
if let Some(is_focusable) = focusable {
scene_node.get_mut_interaction().set_focusable(is_focusable);
}
if let Some(handler) = on_click {
scene_node.get_mut_handlers().set_on_click(Some(handler));
scene_node.get_mut_interaction().set_focusable(true);
let paint: &mut PaintStyle = scene_node.get_mut_style().get_mut_paint();
if paint.get_cursor() == CursorIcon::Default {
paint.set_cursor(CursorIcon::Pointer);
}
}
if let Some(handler) = on_change {
scene_node.get_mut_handlers().set_on_change(Some(handler));
}
if let Some(handler) = on_scroll {
scene_node.get_mut_handlers().set_on_scroll(Some(handler));
}
if let Some(handler) = on_pointer_down {
scene_node
.get_mut_handlers()
.set_on_pointer_down(Some(handler));
let paint: &mut PaintStyle = scene_node.get_mut_style().get_mut_paint();
if paint.get_cursor() == CursorIcon::Default {
paint.set_cursor(CursorIcon::Pointer);
}
}
if let Some(handler) = on_pointer_move {
scene_node
.get_mut_handlers()
.set_on_pointer_move(Some(handler));
}
if let Some(handler) = on_pointer_up {
scene_node
.get_mut_handlers()
.set_on_pointer_up(Some(handler));
}
if let Some(handler) = on_pointer_enter {
scene_node
.get_mut_handlers()
.set_on_pointer_enter(Some(handler));
}
if let Some(handler) = on_pointer_leave {
scene_node
.get_mut_handlers()
.set_on_pointer_leave(Some(handler));
}
if let Some(handler) = on_key_down {
scene_node.get_mut_handlers().set_on_key_down(Some(handler));
scene_node.get_mut_interaction().set_focusable(true);
}
if let Some(handler) = on_focus {
scene_node.get_mut_handlers().set_on_focus(Some(handler));
scene_node.get_mut_interaction().set_focusable(true);
}
if let Some(handler) = on_blur {
scene_node.get_mut_handlers().set_on_blur(Some(handler));
}
if let Some(props) = a11y {
scene_node.set_a11y(Some(props));
}
},
(),
);
});
}