#[cfg(feature = "devtools")]
use std::sync::atomic::AtomicUsize;
use std::{
any::{Any, TypeId},
collections::HashSet,
sync::{
Arc,
atomic::{AtomicBool, AtomicU64, Ordering},
},
task::Context as TaskContext,
time::Duration,
};
use parking_lot::Mutex;
#[cfg(feature = "devtools")]
use super::component::{ComponentInfo, DevtoolsInspectable};
use super::{
app_state::App,
component::Component,
events::MouseEvent,
i18n::I18n,
theme::Theme,
window::{Window, WindowWaker},
};
#[cfg(feature = "devtools")]
use crate::time::{SystemTime, UNIX_EPOCH};
use crate::{
core::{
ContextMap, ElementRef, ElementRefMut, ReactiveContext, Store,
cell_ref::Ref,
effect::Effect,
memo::Memo,
signal::{Signal, SignalValue},
tracking,
},
node::{Element, HitTestBehavior, Node},
time::Instant,
};
mod frames;
mod futures;
pub(crate) use frames::WindowFrames;
pub use frames::{Frame, FrameCallback};
pub use futures::{FutureAction, FutureHandle, FutureState, FutureStatus, StreamEmitter, StreamHandle};
use futures::{FutureSlot, RuntimeFutureHandle};
static NEXT_COMPONENT_SLOT_ID: AtomicU64 = AtomicU64::new(1);
fn next_component_slot_id() -> u64 {
NEXT_COMPONENT_SLOT_ID.fetch_add(1, Ordering::Relaxed)
}
static NEXT_RENDER_GENERATION: AtomicU64 = AtomicU64::new(1);
fn next_render_generation() -> u64 {
NEXT_RENDER_GENERATION.fetch_add(1, Ordering::Relaxed)
}
#[cfg(feature = "devtools")]
fn set_component_debug_metadata(node: &mut Node, ctx: &Ctx) {
node.set_component_props_debug(ctx.props_debug());
node.set_component_signals_debug(ctx.signals_debug());
node.set_component_memos_debug(ctx.memos_debug());
node.set_component_effects_debug(ctx.effects_debug());
node.set_component_contexts_debug(ctx.contexts_debug());
node.set_component_render_debug(Some(ctx.render_debug()));
}
fn attach_component_metadata(
mut node: Box<Node>,
tag_name: Arc<str>,
slot_id: u64,
key: Option<&str>,
#[cfg(feature = "devtools")] ctx: &Ctx,
) -> Box<Node> {
if node.component_slot_id().is_some() {
node = Box::new(Node::logical().child(*node));
}
node.set_tag_name(tag_name);
node.set_component_slot_id(slot_id);
node.set_component_key(key);
#[cfg(feature = "devtools")]
set_component_debug_metadata(&mut node, ctx);
node
}
pub(crate) fn component_tag_name<C: 'static>() -> Arc<str> {
let type_name = std::any::type_name::<C>();
let base = type_name.split('<').next().unwrap_or(type_name);
Arc::from(base.rsplit("::").next().unwrap_or(base))
}
pub(crate) type ClickOutsideCallback = Arc<dyn Fn(&MouseEvent) + Send + Sync>;
struct ClickOutsideEntry {
scope_id: u64,
cursor: usize,
element_ref: ElementRef,
callback: ClickOutsideCallback,
}
#[derive(Clone)]
pub enum OpenState {
Static(bool),
Signal(Signal<bool>),
}
impl OpenState {
pub fn is_open(&self) -> bool {
match self {
Self::Static(open) => *open,
Self::Signal(open) => open.get(),
}
}
pub(crate) fn signal(&self) -> Option<Signal<bool>> {
match self {
Self::Static(_) => None,
Self::Signal(open) => Some(open.clone()),
}
}
}
impl From<bool> for OpenState {
fn from(value: bool) -> Self {
Self::Static(value)
}
}
impl From<Signal<bool>> for OpenState {
fn from(value: Signal<bool>) -> Self {
Self::Signal(value)
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
pub enum Placement {
TopStart,
Top,
TopEnd,
#[default]
BottomStart,
Bottom,
BottomEnd,
LeftStart,
Left,
LeftEnd,
RightStart,
Right,
RightEnd,
}
#[cfg(feature = "devtools")]
impl crate::app::component::DevtoolsInspectable for Placement {
fn inspect(&self, formatter: &mut crate::app::component::DevtoolsFormatter<'_>) {
formatter.value_debug(self);
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
pub enum CollisionStrategy {
None,
Clamp,
Flip,
#[default]
FlipThenClamp,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum OutsidePress {
#[default]
Consume,
PassThrough,
}
pub(crate) type OverlayDismiss = std::sync::Arc<dyn Fn() + Send + Sync>;
pub(crate) struct OverlaySpec {
pub(crate) anchor: ElementRef,
pub(crate) node: Node,
pub(crate) placement: Placement,
pub(crate) offset_x: f32,
pub(crate) offset_y: f32,
pub(crate) match_anchor_width: bool,
pub(crate) collision: CollisionStrategy,
pub(crate) hit_test: HitTestBehavior,
pub(crate) open_signal: Option<Signal<bool>>,
pub(crate) dismiss_on_outside_click: bool,
pub(crate) outside_press: OutsidePress,
pub(crate) dismiss_on_escape: bool,
pub(crate) on_dismiss: Option<OverlayDismiss>,
pub(crate) takes_focus: bool,
pub(crate) tab_after_anchor: bool,
pub(crate) traps_tab: bool,
pub(crate) at: Option<(f32, f32)>,
pub(crate) at_placement: Option<Placement>,
pub(crate) clear: [f32; 4],
pub(crate) within: Option<ElementRef>,
pub(crate) within_inset: [f32; 4],
pub(crate) frame: Option<PlacingFrame>,
}
#[derive(Clone)]
pub(crate) enum PlacingFrame {
Box(ElementRef),
ClearOf(ElementRef),
}
impl PlacingFrame {
pub(crate) fn element(&self) -> &ElementRef {
let (Self::Box(element) | Self::ClearOf(element)) = self;
element
}
pub(crate) fn rect(
&self,
anchor: crate::core::ElementRect,
frame: Option<crate::core::ElementRect>,
placement: Placement,
) -> crate::core::ElementRect {
let Some(frame) = frame.filter(|frame| frame.width > 0.0 || frame.height > 0.0) else {
return anchor;
};
let beside = matches!(
placement,
Placement::LeftStart
| Placement::Left
| Placement::LeftEnd
| Placement::RightStart
| Placement::Right
| Placement::RightEnd
);
match self {
Self::Box(_) => frame,
Self::ClearOf(_) if beside => crate::core::ElementRect {
x: frame.x,
relative_x: anchor.relative_x + (frame.x - anchor.x),
width: frame.width,
..anchor
},
Self::ClearOf(_) => crate::core::ElementRect {
y: frame.y,
relative_y: anchor.relative_y + (frame.y - anchor.y),
height: frame.height,
..anchor
},
}
}
}
#[derive(Clone)]
pub enum ModalTarget {
Parent,
Root,
Element(ElementRef),
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct Parent;
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct Root;
impl From<Parent> for ModalTarget {
fn from(_: Parent) -> Self {
Self::Parent
}
}
impl From<Root> for ModalTarget {
fn from(_: Root) -> Self {
Self::Root
}
}
impl From<ElementRef> for ModalTarget {
fn from(value: ElementRef) -> Self {
Self::Element(value)
}
}
impl From<ElementRefMut> for ModalTarget {
fn from(value: ElementRefMut) -> Self {
Self::Element(value.into())
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum ModalLayer {
Dialog,
WindowChrome,
}
pub(crate) struct ModalSpec {
pub(crate) target: ModalTarget,
pub(crate) node: Node,
pub(crate) open_signal: Option<Signal<bool>>,
pub(crate) dismiss_on_escape: bool,
pub(crate) layer: ModalLayer,
pub(crate) takes_focus: bool,
pub(crate) on_dismiss: Option<OverlayDismiss>,
}
impl ModalSpec {
pub(crate) fn clone_for_reuse(&self) -> Self {
self.clone_with_node(self.node.clone_for_reuse())
}
pub(crate) fn clone_with_node(&self, node: Node) -> Self {
Self {
target: self.target.clone(),
node,
open_signal: self.open_signal.clone(),
dismiss_on_escape: self.dismiss_on_escape,
layer: self.layer,
takes_focus: self.takes_focus,
on_dismiss: self.on_dismiss.clone(),
}
}
}
impl OverlaySpec {
pub(crate) fn clone_for_reuse(&self) -> Self {
self.clone_with_node(self.node.clone_for_reuse())
}
pub(crate) fn clone_with_node(&self, node: Node) -> Self {
Self {
anchor: self.anchor.clone(),
node,
placement: self.placement,
offset_x: self.offset_x,
offset_y: self.offset_y,
match_anchor_width: self.match_anchor_width,
collision: self.collision,
hit_test: self.hit_test,
open_signal: self.open_signal.clone(),
dismiss_on_outside_click: self.dismiss_on_outside_click,
outside_press: self.outside_press,
dismiss_on_escape: self.dismiss_on_escape,
on_dismiss: self.on_dismiss.clone(),
takes_focus: self.takes_focus,
tab_after_anchor: self.tab_after_anchor,
traps_tab: self.traps_tab,
at: self.at,
at_placement: self.at_placement,
clear: self.clear,
within: self.within.clone(),
within_inset: self.within_inset,
frame: self.frame.clone(),
}
}
}
pub struct Modal {
node: Box<Node>,
open: OpenState,
target: ModalTarget,
dismiss_on_escape: bool,
layer: ModalLayer,
dismiss: Option<Signal<bool>>,
takes_focus: bool,
on_dismiss: Vec<OverlayDismiss>,
}
impl Modal {
pub fn new(content: impl Into<Element>) -> Self {
Self {
node: content.into().node,
open: OpenState::Static(true),
target: ModalTarget::Parent,
dismiss_on_escape: true,
layer: ModalLayer::Dialog,
dismiss: None,
takes_focus: false,
on_dismiss: Vec::new(),
}
}
pub fn open(mut self, open: impl Into<OpenState>) -> Self {
self.open = open.into();
self
}
pub fn open_when(mut self, open: bool) -> Self {
self.open = OpenState::Static(open);
self
}
pub fn target(mut self, target: impl Into<ModalTarget>) -> Self {
self.target = target.into();
self
}
pub fn dismiss_on_escape(mut self, dismiss: bool) -> Self {
self.dismiss_on_escape = dismiss;
self
}
pub fn takes_focus(mut self, takes: bool) -> Self {
self.takes_focus = takes;
self
}
pub fn on_dismiss(mut self, handler: impl Fn() + Send + Sync + 'static) -> Self {
self.on_dismiss.push(std::sync::Arc::new(handler));
self
}
pub(crate) fn dismisses(mut self, open: Signal<bool>) -> Self {
self.dismiss = Some(open);
self
}
pub(crate) fn window_chrome_layer(mut self) -> Self {
self.layer = ModalLayer::WindowChrome;
self
}
fn into_spec(self) -> Option<ModalSpec> {
if !self.open.is_open() {
return None;
}
let handlers = self.on_dismiss;
let on_dismiss = (!handlers.is_empty()).then(|| {
std::sync::Arc::new(move || {
for handler in &handlers {
handler();
}
}) as OverlayDismiss
});
Some(ModalSpec {
target: self.target,
node: *self.node,
open_signal: self.dismiss.or_else(|| self.open.signal()),
dismiss_on_escape: self.dismiss_on_escape,
layer: self.layer,
takes_focus: self.takes_focus,
on_dismiss,
})
}
}
impl From<Modal> for Element {
fn from(modal: Modal) -> Self {
let mut node = Node::logical();
node.set_layout_neutral(true);
if let Some(spec) = modal.into_spec() {
node.set_modal_declaration(spec);
}
Element::from_node(node)
}
}
pub struct Overlay {
anchor: Option<ElementRef>,
node: Box<Node>,
open: OpenState,
placement: Placement,
offset_x: f32,
offset_y: f32,
match_anchor_width: bool,
collision: CollisionStrategy,
hit_test: HitTestBehavior,
dismiss_on_outside_click: bool,
outside_press: OutsidePress,
dismiss_on_escape: bool,
on_dismiss: Option<OverlayDismiss>,
switch_on_anchor_press: bool,
takes_focus: bool,
tab_after_anchor: bool,
traps_tab: bool,
at: Option<(f32, f32)>,
at_placement: Option<Placement>,
clear: [f32; 4],
within: Option<ElementRef>,
within_inset: [f32; 4],
frame: Option<PlacingFrame>,
}
impl Overlay {
pub fn new(content: impl Into<Element>) -> Self {
Self {
anchor: None,
node: content.into().node,
open: OpenState::Static(true),
placement: Placement::default(),
offset_x: 0.0,
offset_y: 0.0,
match_anchor_width: false,
collision: CollisionStrategy::default(),
hit_test: HitTestBehavior::Auto,
dismiss_on_outside_click: false,
outside_press: OutsidePress::default(),
dismiss_on_escape: false,
on_dismiss: None,
switch_on_anchor_press: true,
takes_focus: false,
tab_after_anchor: false,
traps_tab: false,
at: None,
at_placement: None,
clear: [0.0; 4],
within: None,
within_inset: [0.0; 4],
frame: None,
}
}
pub fn anchor(mut self, anchor: impl Into<ElementRef>) -> Self {
self.anchor = Some(anchor.into());
self
}
pub fn open(mut self, open: impl Into<OpenState>) -> Self {
self.open = open.into();
self
}
pub fn open_when(mut self, open: bool) -> Self {
self.open = OpenState::Static(open);
self
}
pub fn placement(mut self, placement: Placement) -> Self {
self.placement = placement;
self
}
pub fn offset(mut self, x: f32, y: f32) -> Self {
self.offset_x = x;
self.offset_y = y;
self
}
pub fn match_anchor_width(mut self, match_anchor_width: bool) -> Self {
self.match_anchor_width = match_anchor_width;
self
}
pub fn collision(mut self, collision: CollisionStrategy) -> Self {
self.collision = collision;
self
}
pub fn hit_test(mut self, behavior: HitTestBehavior) -> Self {
self.hit_test = behavior;
self
}
pub fn dismiss_on_outside_click(mut self, dismiss: bool) -> Self {
self.dismiss_on_outside_click = dismiss;
self
}
pub fn outside_press(mut self, outside_press: OutsidePress) -> Self {
self.outside_press = outside_press;
self
}
pub fn dismiss_on_escape(mut self, dismiss: bool) -> Self {
self.dismiss_on_escape = dismiss;
self
}
pub fn on_dismiss(mut self, handler: impl Fn() + Send + Sync + 'static) -> Self {
self.on_dismiss = Some(std::sync::Arc::new(handler));
self
}
pub fn switch_on_anchor_press(mut self, switch: bool) -> Self {
self.switch_on_anchor_press = switch;
self
}
pub fn takes_focus(mut self, takes: bool) -> Self {
self.takes_focus = takes;
self
}
pub fn tab_after_anchor(mut self, tab: bool) -> Self {
self.tab_after_anchor = tab;
self
}
pub fn traps_tab(mut self, traps: bool) -> Self {
self.traps_tab = traps;
self
}
pub fn at(mut self, point: Option<(f32, f32)>) -> Self {
self.at = point;
self
}
pub(crate) fn at_placed(mut self, placement: Placement) -> Self {
self.at_placement = Some(placement);
self
}
pub(crate) fn keep_clear(mut self, top: f32, right: f32, bottom: f32, left: f32) -> Self {
self.clear = [top, right, bottom, left];
self
}
pub fn within(mut self, element: impl Into<ElementRef>) -> Self {
self.within = Some(element.into());
self
}
pub fn within_inset(mut self, element: impl Into<ElementRef>, top: f32, right: f32, bottom: f32, left: f32) -> Self {
self.within = Some(element.into());
self.within_inset = [top, right, bottom, left];
self
}
pub fn placed_by(mut self, element: impl Into<ElementRef>) -> Self {
self.frame = Some(PlacingFrame::Box(element.into()));
self
}
pub fn clear_of(mut self, element: impl Into<ElementRef>) -> Self {
self.frame = Some(PlacingFrame::ClearOf(element.into()));
self
}
fn into_spec(self) -> Option<OverlaySpec> {
if !self.open.is_open() {
return None;
}
let open_signal = self.open.signal();
Some(OverlaySpec {
anchor: self.anchor?,
node: *self.node,
placement: self.placement,
offset_x: self.offset_x,
offset_y: self.offset_y,
match_anchor_width: self.match_anchor_width,
collision: self.collision,
hit_test: self.hit_test,
open_signal,
dismiss_on_outside_click: self.dismiss_on_outside_click,
outside_press: self.outside_press,
dismiss_on_escape: self.dismiss_on_escape,
on_dismiss: self.on_dismiss,
takes_focus: self.takes_focus,
tab_after_anchor: self.tab_after_anchor,
traps_tab: self.traps_tab,
at: self.at,
at_placement: self.at_placement,
clear: self.clear,
within: self.within,
within_inset: self.within_inset,
frame: self.frame,
})
}
}
impl From<Overlay> for Element {
fn from(overlay: Overlay) -> Self {
let mut node = Node::logical();
node.set_layout_neutral(true);
if overlay.dismiss_on_outside_click
&& overlay.switch_on_anchor_press
&& matches!(overlay.open, OpenState::Signal(_))
&& let Some(anchor) = &overlay.anchor
{
node.set_dropdown_anchor(anchor.clone());
}
if let Some(spec) = overlay.into_spec() {
node.set_overlay_declaration(spec);
}
Element::from_node(node)
}
}
#[derive(Clone)]
pub struct Timeout {
timer: Timer,
}
#[derive(Clone)]
pub struct Interval {
timer: Timer,
}
#[derive(Clone)]
struct Timer {
inner: Arc<Mutex<TimerInner>>,
}
struct TimerInner {
duration: Duration,
repeat: bool,
next_fire: Option<Instant>,
callback: Arc<dyn Fn() + Send + Sync>,
wake: Option<WindowWaker>,
}
impl Timeout {
#[cfg(feature = "markdown")]
pub(crate) fn detached(f: impl Fn() + Send + Sync + 'static) -> Self {
Self {
timer: Timer::new(Duration::ZERO, false, f, None),
}
}
pub fn start(&self) {
self.timer.start();
}
pub fn restart(&self) {
self.timer.restart();
}
pub fn cancel(&self) {
self.timer.stop();
}
pub(crate) fn set_duration(&self, duration: Duration) {
self.timer.inner.lock().duration = duration;
}
pub fn is_active(&self) -> bool {
self.timer.is_active()
}
}
impl Interval {
pub fn start(&self) {
self.timer.start();
}
pub fn restart(&self) {
self.timer.restart();
}
pub fn stop(&self) {
self.timer.stop();
}
pub fn is_active(&self) -> bool {
self.timer.is_active()
}
}
thread_local! {
static TIMER_CLOCK: std::cell::Cell<Option<Instant>> = const { std::cell::Cell::new(None) };
}
pub(crate) fn set_timer_clock(now: Option<Instant>) {
TIMER_CLOCK.with(|clock| clock.set(now));
}
fn timer_now() -> Instant {
TIMER_CLOCK.with(std::cell::Cell::get).unwrap_or_else(Instant::now)
}
impl Timer {
fn new(
duration: Duration,
repeat: bool,
callback: impl Fn() + Send + Sync + 'static,
wake: Option<WindowWaker>,
) -> Self {
Self {
inner: Arc::new(Mutex::new(TimerInner {
duration,
repeat,
next_fire: None,
callback: Arc::new(callback),
wake,
})),
}
}
fn start(&self) {
let now = timer_now();
let wake = {
let mut inner = self.inner.lock();
if inner.next_fire.is_some() {
return;
}
inner.next_fire = Some(now + inner.duration);
inner.wake.clone()
};
Self::wake(wake);
}
fn restart(&self) {
self.restart_at(timer_now());
}
fn restart_at(&self, now: Instant) {
let wake = {
let mut inner = self.inner.lock();
inner.next_fire = Some(now + inner.duration);
inner.wake.clone()
};
Self::wake(wake);
}
fn wake(wake: Option<WindowWaker>) {
if let Some(wake) = wake {
wake();
}
}
fn next_fire(&self) -> Option<Instant> {
self.inner.lock().next_fire
}
fn stop(&self) {
self.inner.lock().next_fire = None;
}
fn is_active(&self) -> bool {
self.inner.lock().next_fire.is_some()
}
fn tick(&self, now: Instant) -> bool {
let callback = {
let mut inner = self.inner.lock();
let Some(next_fire) = inner.next_fire else {
return false;
};
if now < next_fire {
return false;
}
if inner.repeat {
inner.next_fire = Some(now + inner.duration);
} else {
inner.next_fire = None;
}
inner.callback.clone()
};
callback();
true
}
}
#[derive(Default)]
struct ElementRequests {
focus: Option<ElementRef>,
focus_onward: bool,
scroll_into_view: Option<ElementRef>,
scroll_into_view_start: Option<ElementRef>,
scroll_range_into_view: Option<(ElementRef, std::ops::Range<usize>)>,
blur: bool,
scroll_to_top: bool,
scroll_states_to_top: Vec<crate::layout::layout_kind::ScrollState>,
scroll_states_to: Vec<(crate::layout::layout_kind::ScrollState, f32, f32)>,
}
#[derive(Clone)]
pub struct FocusHandle {
requests: Arc<Mutex<ElementRequests>>,
window: Option<crate::app::window::Window>,
}
impl FocusHandle {
fn wake(&self) {
if let Some(window) = &self.window {
window.wake();
}
}
pub fn focus(&self, element: &ElementRef) {
self.requests.lock().focus = Some(element.clone());
self.wake();
}
pub fn focus_onward(&self) {
self.requests.lock().focus_onward = true;
self.wake();
}
}
#[derive(Clone)]
pub(crate) struct InFront {
own: Signal<bool>,
around: Option<Arc<InFront>>,
}
impl InFront {
pub(crate) fn new(own: Signal<bool>, around: Option<InFront>) -> Self {
Self {
own,
around: around.map(Arc::new),
}
}
fn get(&self) -> bool {
let own = self.own.get();
let around = self.around.as_ref().is_none_or(|around| around.get());
own && around
}
}
#[cfg(feature = "devtools")]
impl crate::app::component::DevtoolsInspectable for InFront {
fn inspect(&self, formatter: &mut crate::app::component::DevtoolsFormatter<'_>) {
formatter.value_debug(&self.own.get_untracked());
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub(crate) enum BottomBarInset {
Over(f32),
}
#[cfg(feature = "devtools")]
impl crate::app::component::DevtoolsInspectable for BottomBarInset {
fn inspect(&self, formatter: &mut crate::app::component::DevtoolsFormatter<'_>) {
formatter.value_debug(self);
}
}
crate::register_inspectable!(BottomBarInset);
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) struct SafeAreaTaken;
#[cfg(feature = "devtools")]
impl crate::app::component::DevtoolsInspectable for SafeAreaTaken {
fn inspect(&self, formatter: &mut crate::app::component::DevtoolsFormatter<'_>) {
formatter.value_debug(self);
}
}
crate::register_inspectable!(SafeAreaTaken);
#[derive(Clone)]
pub struct EditRunner {
window: Option<crate::app::window::Window>,
}
impl EditRunner {
pub fn run(&self, command: crate::app::events::EditCommand) {
if let Some(window) = &self.window {
window.request_edit(command);
}
}
}
#[derive(Clone)]
pub struct HistoryRunner {
window: Option<crate::app::window::Window>,
}
impl HistoryRunner {
pub fn back(&self) {
self.run(crate::app::events::NavigationCommand::Back);
}
pub fn forward(&self) {
self.run(crate::app::events::NavigationCommand::Forward);
}
pub fn run(&self, command: crate::app::events::NavigationCommand) {
self.request(command, crate::app::window::NavigationOrigin::Focus, None);
}
pub fn run_in(&self, part: &crate::core::ElementRef, command: crate::app::events::NavigationCommand) {
self.request(command, crate::app::window::NavigationOrigin::Part(part.clone()), None);
}
#[cfg_attr(not(feature = "router"), allow(dead_code))]
pub(crate) fn run_in_then(
&self,
part: &crate::core::ElementRef,
command: crate::app::events::NavigationCommand,
source: crate::app::events::NavigationSource,
done: impl FnOnce(bool) + Send + Sync + 'static,
) {
let request = crate::app::window::NavigationRequest {
command,
source,
origin: crate::app::window::NavigationOrigin::Part(part.clone()),
done: Some(Box::new(done)),
};
match &self.window {
Some(window) => window.request_navigation(request),
None => {
if let Some(done) = request.done {
done(false);
}
}
}
}
fn request(
&self,
command: crate::app::events::NavigationCommand,
origin: crate::app::window::NavigationOrigin,
done: Option<crate::app::window::NavigationDone>,
) {
if let Some(window) = &self.window {
window.request_navigation(crate::app::window::NavigationRequest {
command,
source: crate::app::events::NavigationSource::App,
origin,
done,
});
}
}
}
pub struct Ctx {
dirty: Arc<AtomicBool>,
subtree_dirty: Arc<AtomicBool>,
dirty_child_slots: Arc<Mutex<Vec<u64>>>,
ancestor_dirty_slots: Vec<DirtyAncestor>,
batch: Arc<BatchState>,
props: Option<Box<dyn Any + Send>>,
#[cfg(feature = "devtools")]
props_debug: Option<DevtoolsInspectableDebug>,
#[cfg(feature = "devtools")]
signals_debug: Vec<ComponentSignalDebug>,
#[cfg(feature = "devtools")]
memos_debug: Vec<ComponentMemoDebug>,
#[cfg(feature = "devtools")]
effects_debug: Vec<ComponentEffectDebug>,
#[cfg(feature = "devtools")]
contexts_debug: Vec<ComponentContextDebug>,
#[cfg(feature = "devtools")]
render_debug: ComponentRenderDebug,
#[cfg(feature = "devtools")]
render_started: Option<Instant>,
theme: Option<Theme>,
window: Option<crate::app::window::Window>,
breakpoint: Option<crate::core::Memo<Option<crate::app::theme::Breakpoint>>>,
i18n: Option<I18n>,
app: Option<App>,
element_requests: Arc<Mutex<ElementRequests>>,
#[cfg(feature = "tokio")]
runtime_future_handle: RuntimeFutureHandle,
context_map: ContextMap,
provided_contexts: ContextMap,
base_contexts: ContextMap,
inherited_context_revision: u64,
context_reads: Vec<TypeId>,
provided_in_render: bool,
previous_render_contexts: Option<ContextMap>,
slot_children: Option<Vec<Element>>,
children: Vec<ChildSlot>,
child_cursor: usize,
scope_id: u64,
element_ref_cursor: usize,
future_cursor: usize,
watch_handles: Vec<Box<dyn Any + Send + Sync>>,
render_watch_handles: Vec<Box<dyn Any + Send + Sync>>,
effects: Vec<Effect>,
timers: Vec<Timer>,
frames: frames::ComponentFrames,
future_slots: Vec<FutureSlot>,
stable_future_slots: Vec<FutureSlot>,
component_name: &'static str,
#[cfg(feature = "query")]
query_registry: crate::query::QueryRegistry,
#[cfg(feature = "query")]
query_slots: Vec<crate::query::Observer>,
#[cfg(feature = "query")]
query_cursor: usize,
element_refs: Vec<ElementRefMut>,
click_outside_registry: Arc<Mutex<Vec<ClickOutsideEntry>>>,
click_outside_cursor: usize,
click_outside_active_cursors: Vec<usize>,
rendering: bool,
holding: bool,
}
#[derive(Clone)]
struct DirtyAncestor {
subtree_dirty: Arc<AtomicBool>,
dirty_child_slots: Arc<Mutex<Vec<u64>>>,
child_slot_id: u64,
}
#[cfg(feature = "devtools")]
#[derive(Clone, Debug, PartialEq)]
pub struct DevtoolsInspectableDebug {
pub type_name: Arc<str>,
pub fields: Vec<ComponentInfo>,
}
#[cfg(feature = "devtools")]
#[derive(Clone, Debug)]
pub struct ComponentSignalDebug {
pub id: usize,
pub type_name: Arc<str>,
value: Arc<Mutex<Option<Arc<str>>>>,
history: Arc<Mutex<Vec<ComponentValueChangeDebug>>>,
subscriber_count: Arc<AtomicUsize>,
}
#[cfg(feature = "devtools")]
#[derive(Clone, Debug)]
pub struct ComponentMemoDebug {
pub id: usize,
pub type_name: Arc<str>,
value: Arc<Mutex<Option<Arc<str>>>>,
history: Arc<Mutex<Vec<ComponentValueChangeDebug>>>,
subscriber_count: Arc<AtomicUsize>,
}
#[cfg(feature = "devtools")]
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ComponentValueChangeDebug {
pub sequence: u64,
pub timestamp: String,
pub from_value: String,
pub to_value: String,
}
#[cfg(feature = "devtools")]
static NEXT_VALUE_CHANGE: AtomicU64 = AtomicU64::new(1);
#[cfg(feature = "devtools")]
#[derive(Clone, Debug, PartialEq)]
pub struct ComponentEffectDebug {
pub id: usize,
}
#[cfg(feature = "devtools")]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct ComponentRenderDebug {
pub count: u64,
pub last: Duration,
}
#[cfg(feature = "devtools")]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ComponentContextKind {
Provided,
Consumed,
}
#[cfg(feature = "devtools")]
#[derive(Clone, Debug)]
pub struct ComponentContextDebug {
pub kind: ComponentContextKind,
pub type_name: Arc<str>,
pub reactive: bool,
value: Arc<Mutex<Option<Vec<ComponentInfo>>>>,
history: Arc<Mutex<Vec<ComponentContextChangeDebug>>>,
}
#[cfg(feature = "devtools")]
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ComponentContextChangeDebug {
pub sequence: u64,
pub timestamp: String,
pub from_value: Vec<ComponentInfo>,
pub to_value: Vec<ComponentInfo>,
}
#[cfg(feature = "devtools")]
impl ComponentContextDebug {
pub fn value(&self) -> Option<Vec<ComponentInfo>> {
self.value.lock().clone()
}
pub fn history(&self) -> Vec<ComponentContextChangeDebug> {
self.history.lock().clone()
}
}
#[cfg(feature = "devtools")]
impl PartialEq for ComponentContextDebug {
fn eq(&self, other: &Self) -> bool {
self.kind == other.kind
&& self.type_name == other.type_name
&& self.reactive == other.reactive
&& self.value() == other.value()
&& self.history() == other.history()
}
}
#[cfg(feature = "devtools")]
fn record_context_value(
held: &Mutex<Option<Vec<ComponentInfo>>>,
history: &Mutex<Vec<ComponentContextChangeDebug>>,
next: Vec<ComponentInfo>,
) {
let mut held = held.lock();
if held.as_ref() == Some(&next) {
return;
}
if let Some(previous) = held.as_ref() {
let mut history = history.lock();
history.push(ComponentContextChangeDebug {
sequence: NEXT_VALUE_CHANGE.fetch_add(1, Ordering::Relaxed),
timestamp: current_compact_timestamp(),
from_value: previous.clone(),
to_value: next.clone(),
});
if history.len() > 64 {
let overflow = history.len() - 64;
history.drain(0..overflow);
}
}
*held = Some(next);
}
#[cfg(feature = "devtools")]
fn inspected<T: 'static>(value: &T) -> Vec<ComponentInfo> {
crate::app::component::inspect_registered(value).unwrap_or_default()
}
#[cfg(feature = "devtools")]
impl DevtoolsInspectableDebug {
fn from_props<T: DevtoolsInspectable + 'static>(props: &T) -> Self {
let mut fields = Vec::new();
props.write_info(&mut fields);
Self {
type_name: Arc::from(std::any::type_name::<T>()),
fields,
}
}
}
#[cfg(feature = "devtools")]
impl ComponentSignalDebug {
pub fn formatted_value(&self) -> Option<String> {
self.value.lock().as_ref().map(|value| value.to_string())
}
pub fn history(&self) -> Vec<ComponentValueChangeDebug> {
self.history.lock().clone()
}
pub fn subscriber_count(&self) -> usize {
self.subscriber_count.load(Ordering::Relaxed)
}
#[cfg_attr(not(feature = "perf_profile"), allow(dead_code))]
pub(crate) fn estimated_memory_bytes(&self) -> usize {
let history = self.history.lock();
self.value.lock().as_ref().map(|value| value.len()).unwrap_or(0)
+ history.capacity() * std::mem::size_of::<ComponentValueChangeDebug>()
+ history
.iter()
.map(ComponentValueChangeDebug::estimated_memory_bytes)
.sum::<usize>()
}
}
#[cfg(feature = "devtools")]
impl ComponentMemoDebug {
pub fn formatted_value(&self) -> Option<String> {
self.value.lock().as_ref().map(|value| value.to_string())
}
pub fn history(&self) -> Vec<ComponentValueChangeDebug> {
self.history.lock().clone()
}
pub fn subscriber_count(&self) -> usize {
self.subscriber_count.load(Ordering::Relaxed)
}
pub(crate) fn estimated_memory_bytes(&self) -> usize {
let history = self.history.lock();
self.value.lock().as_ref().map(|value| value.len()).unwrap_or(0)
+ history.capacity() * std::mem::size_of::<ComponentValueChangeDebug>()
+ history
.iter()
.map(ComponentValueChangeDebug::estimated_memory_bytes)
.sum::<usize>()
}
}
#[cfg(feature = "devtools")]
impl ComponentValueChangeDebug {
#[cfg_attr(not(feature = "perf_profile"), allow(dead_code))]
fn estimated_memory_bytes(&self) -> usize {
self.timestamp.capacity() + self.from_value.capacity() + self.to_value.capacity()
}
}
#[cfg(feature = "devtools")]
impl PartialEq for ComponentSignalDebug {
fn eq(&self, other: &Self) -> bool {
self.id == other.id
&& self.type_name == other.type_name
&& self.formatted_value() == other.formatted_value()
&& self.history() == other.history()
&& self.subscriber_count() == other.subscriber_count()
}
}
#[cfg(feature = "devtools")]
impl PartialEq for ComponentMemoDebug {
fn eq(&self, other: &Self) -> bool {
self.id == other.id
&& self.type_name == other.type_name
&& self.formatted_value() == other.formatted_value()
&& self.history() == other.history()
&& self.subscriber_count() == other.subscriber_count()
}
}
#[cfg(feature = "devtools")]
fn update_debug_value_history(
current_value: &Arc<Mutex<Option<Arc<str>>>>,
history: &Arc<Mutex<Vec<ComponentValueChangeDebug>>>,
next_value: Option<Arc<str>>,
) {
let mut current_value = current_value.lock();
if *current_value == next_value {
return;
}
if let Some(previous) = current_value.as_ref() {
let mut history = history.lock();
history.push(ComponentValueChangeDebug {
sequence: NEXT_VALUE_CHANGE.fetch_add(1, Ordering::Relaxed),
timestamp: current_compact_timestamp(),
from_value: previous.to_string(),
to_value: next_value
.as_ref()
.map(|value| value.to_string())
.unwrap_or_else(|| "<unknown>".to_owned()),
});
if history.len() > 64 {
let overflow = history.len() - 64;
history.drain(0..overflow);
}
}
*current_value = next_value;
}
#[cfg(feature = "devtools")]
fn current_compact_timestamp() -> String {
let duration = SystemTime::now().duration_since(UNIX_EPOCH).unwrap_or_default();
compact_timestamp(
duration.as_secs(),
duration.subsec_millis(),
crate::core::Time::now().seconds(),
)
}
#[cfg(feature = "devtools")]
fn compact_timestamp(utc_seconds: u64, millis: u32, local_seconds: i64) -> String {
const DAY: i64 = 86_400;
const QUARTER: i64 = 900;
let utc_of_day = (utc_seconds % DAY as u64) as i64;
let zone = ((local_seconds - utc_of_day) as f64 / QUARTER as f64).round() as i64 * QUARTER;
let seconds_of_day = (utc_of_day + zone).rem_euclid(DAY);
let hour = seconds_of_day / 3_600;
let minute = seconds_of_day % 3_600 / 60;
let second = seconds_of_day % 60;
format!("{hour:02}:{minute:02}:{second:02}.{millis:03}")
}
#[cfg(all(test, feature = "devtools"))]
mod timestamp_tests {
use super::compact_timestamp;
#[test]
fn a_change_is_timed_by_the_clock_of_the_zone() {
let utc = 9 * 3_600 + 29 * 60 + 40;
assert_eq!(compact_timestamp(utc, 5, (utc + 2 * 3_600) as i64), "11:29:40.005");
assert_eq!(compact_timestamp(utc, 5, (utc + 2 * 3_600 + 1) as i64), "11:29:40.005");
assert_eq!(compact_timestamp(3_600, 0, 20 * 3_600), "20:00:00.000");
assert_eq!(
compact_timestamp(utc, 0, (utc + 5 * 3_600 + 45 * 60) as i64),
"15:14:40.000"
);
assert_eq!(compact_timestamp(utc, 250, utc as i64), "09:29:40.250");
assert_eq!(compact_timestamp(20_000 * 86_400 + utc, 0, utc as i64), "09:29:40.000");
}
}
#[cfg(feature = "devtools")]
fn format_debug_value<T: DevtoolsInspectable + 'static>(value: &T) -> Option<Arc<str>> {
let mut fields = Vec::new();
value.write_info(&mut fields);
format_debug_fields(&fields).map(Arc::from)
}
#[cfg(feature = "devtools")]
fn format_debug_fields(fields: &[ComponentInfo]) -> Option<String> {
if fields.is_empty() {
return None;
}
if fields.len() == 1 {
let info = &fields[0];
if matches!(info.name(), "value" | "variant")
&& let Some(value) = info.formatted_value()
{
return Some(value.to_owned());
}
}
Some(fields.iter().map(format_debug_info).collect::<Vec<_>>().join(", "))
}
#[cfg(feature = "devtools")]
fn format_debug_info(info: &ComponentInfo) -> String {
if let Some(value) = info.formatted_value() {
if info.name() == "value" {
value.to_owned()
} else {
format!("{}: {}", info.name(), value)
}
} else if let Some(children) = format_debug_fields(info.children()) {
if info.name() == "Some" {
format!("Some({children})")
} else {
format!("{}: {{{children}}}", info.name())
}
} else {
format!("{}: {}", info.name(), info.type_name())
}
}
#[derive(Default)]
struct BatchState {
inner: Mutex<BatchStateInner>,
off_loop_wake: Option<WindowWaker>,
}
#[derive(Default)]
struct BatchStateInner {
depth: usize,
pending_dirty: Vec<Arc<AtomicBool>>,
}
struct BatchGuard<'a> {
state: &'a BatchState,
}
impl BatchState {
fn waking(off_loop_wake: WindowWaker) -> Self {
Self {
inner: Mutex::default(),
off_loop_wake: Some(off_loop_wake),
}
}
fn batch<R>(&self, f: impl FnOnce() -> R) -> R {
self.inner.lock().depth += 1;
let guard = BatchGuard { state: self };
let result = f();
drop(guard);
result
}
fn mark_dirty(&self, dirty: &Arc<AtomicBool>) -> bool {
let mut inner = self.inner.lock();
if inner.depth == 0 {
drop(inner);
return !dirty.swap(true, Ordering::Relaxed);
}
inner.pending_dirty.push(dirty.clone());
false
}
fn end_batch(&self) {
let pending = {
let mut inner = self.inner.lock();
debug_assert!(inner.depth > 0);
inner.depth = inner.depth.saturating_sub(1);
if inner.depth > 0 {
return;
}
std::mem::take(&mut inner.pending_dirty)
};
let mut seen = HashSet::new();
let mut newly_dirty = false;
for dirty in pending {
let ptr = Arc::as_ptr(&dirty) as usize;
if seen.insert(ptr) {
newly_dirty |= !dirty.swap(true, Ordering::Relaxed);
}
}
if newly_dirty {
self.wake_loop();
}
}
fn wake_loop(&self) {
if let Some(wake) = &self.off_loop_wake {
wake();
}
}
}
impl Drop for BatchGuard<'_> {
fn drop(&mut self) {
self.state.end_batch();
}
}
struct ChildSlot {
id: u64,
key: Option<String>,
component: Box<dyn AnyComponent>,
ctx: Ctx,
rendered: Option<Box<Node>>,
rendered_at: u64,
mounted: bool,
offstage: bool,
offstage_dirty: bool,
}
trait AnyComponent: Send + Sync + 'static {
fn render(&self, ctx: &mut Ctx) -> Element;
fn after_layout(&self);
fn on_mounted(&self);
fn on_unmounted(&self);
fn type_name(&self) -> &'static str;
fn tag_name(&self) -> Arc<str>;
}
struct ComponentWrapper<C: Component> {
component: C,
}
impl<C: Component> AnyComponent for ComponentWrapper<C> {
fn render(&self, ctx: &mut Ctx) -> Element {
let mut element: Element = self.component.render(ctx).into();
element.node.mount_declared_components(ctx);
element
}
fn after_layout(&self) {
self.component.after_layout();
}
fn on_mounted(&self) {
self.component.on_mounted();
}
fn on_unmounted(&self) {
self.component.on_unmounted();
}
fn type_name(&self) -> &'static str {
std::any::type_name::<C>()
}
fn tag_name(&self) -> Arc<str> {
component_tag_name::<C>()
}
}
impl Ctx {
pub fn new_root() -> Self {
Self::new()
}
pub(crate) fn new() -> Self {
Self {
dirty: Arc::new(AtomicBool::new(true)),
subtree_dirty: Arc::new(AtomicBool::new(true)),
dirty_child_slots: Arc::new(Mutex::new(Vec::new())),
ancestor_dirty_slots: Vec::new(),
batch: Arc::new(BatchState::default()),
props: None,
#[cfg(feature = "devtools")]
props_debug: None,
#[cfg(feature = "devtools")]
signals_debug: Vec::new(),
#[cfg(feature = "devtools")]
memos_debug: Vec::new(),
#[cfg(feature = "devtools")]
effects_debug: Vec::new(),
#[cfg(feature = "devtools")]
contexts_debug: Vec::new(),
#[cfg(feature = "devtools")]
render_debug: ComponentRenderDebug::default(),
#[cfg(feature = "devtools")]
render_started: None,
theme: None,
window: None,
breakpoint: None,
i18n: None,
app: None,
element_requests: Arc::default(),
#[cfg(feature = "tokio")]
runtime_future_handle: None,
context_map: ContextMap::default(),
provided_contexts: ContextMap::default(),
base_contexts: ContextMap::default(),
inherited_context_revision: 0,
context_reads: Vec::new(),
provided_in_render: false,
previous_render_contexts: None,
slot_children: None,
children: Vec::new(),
child_cursor: 0,
scope_id: 0,
element_ref_cursor: 0,
future_cursor: 0,
watch_handles: Vec::new(),
render_watch_handles: Vec::new(),
effects: Vec::new(),
timers: Vec::new(),
frames: frames::ComponentFrames::default(),
future_slots: Vec::new(),
stable_future_slots: Vec::new(),
component_name: "root",
#[cfg(feature = "query")]
query_registry: crate::query::QueryRegistry::default(),
#[cfg(feature = "query")]
query_slots: Vec::new(),
#[cfg(feature = "query")]
query_cursor: 0,
element_refs: Vec::new(),
click_outside_registry: Arc::new(Mutex::new(Vec::new())),
click_outside_cursor: 0,
click_outside_active_cursors: Vec::new(),
rendering: false,
holding: false,
}
}
pub(crate) fn with_theme(mut self, theme: Theme) -> Self {
self.theme = Some(theme);
self
}
pub(crate) fn with_window(mut self, window: crate::app::window::Window) -> Self {
self.batch = Arc::new(BatchState::waking(window.off_loop_waker()));
self.window = Some(window);
self
}
pub(crate) fn with_breakpoint(mut self) -> Self {
if let (Some(window), Some(theme)) = (self.window.clone(), self.theme.clone()) {
self.breakpoint = Some(crate::core::Memo::new(move || {
window.track_access();
theme.track_access();
theme.breakpoints().resolve(window.info().logical_width())
}));
}
self
}
pub(crate) fn with_i18n(mut self, i18n: I18n) -> Self {
self.i18n = Some(i18n);
self
}
pub(crate) fn set_app_ref(&mut self, app: &mut App) {
self.app = Some(app.clone());
#[cfg(feature = "tokio")]
{
self.runtime_future_handle = app.tokio_handle();
}
for slot in &mut self.children {
slot.ctx.set_app_ref(app);
}
}
fn runtime_future_handle(&self) -> RuntimeFutureHandle {
#[cfg(feature = "tokio")]
{
self.runtime_future_handle.clone()
}
#[cfg(not(feature = "tokio"))]
{}
}
pub fn is_dirty(&self) -> bool {
self.dirty.load(Ordering::Relaxed)
}
fn mark_dirty_targets(
batch: &BatchState,
dirty: &Arc<AtomicBool>,
subtree_dirty: &Arc<AtomicBool>,
ancestor_dirty_slots: &[DirtyAncestor],
) {
let mut newly_dirty = batch.mark_dirty(dirty);
newly_dirty |= batch.mark_dirty(subtree_dirty);
for ancestor in ancestor_dirty_slots {
newly_dirty |= batch.mark_dirty(&ancestor.subtree_dirty);
Self::mark_dirty_child_slot(&ancestor.dirty_child_slots, ancestor.child_slot_id);
}
if newly_dirty {
batch.wake_loop();
}
}
fn mark_self_dirty(&self) {
self.dirty.store(true, Ordering::Relaxed);
self.subtree_dirty.store(true, Ordering::Relaxed);
for ancestor in &self.ancestor_dirty_slots {
ancestor.subtree_dirty.store(true, Ordering::Relaxed);
Self::mark_dirty_child_slot(&ancestor.dirty_child_slots, ancestor.child_slot_id);
}
}
fn mark_dirty_child_slot(dirty_child_slots: &Mutex<Vec<u64>>, slot_id: u64) {
let mut dirty_child_slots = dirty_child_slots.lock();
if !dirty_child_slots.contains(&slot_id) {
dirty_child_slots.push(slot_id);
}
}
fn inherit_dirty_ancestors_from(&mut self, parent: &Ctx, slot_id: u64) {
self.ancestor_dirty_slots = parent.ancestor_dirty_slots.clone();
self.ancestor_dirty_slots.push(DirtyAncestor {
subtree_dirty: parent.subtree_dirty.clone(),
dirty_child_slots: parent.dirty_child_slots.clone(),
child_slot_id: slot_id,
});
}
fn update_subtree_dirty_from_children(&self) {
let dirty = self.is_dirty() || !self.dirty_child_slots.lock().is_empty();
self.subtree_dirty.store(dirty, Ordering::Relaxed);
}
fn take_dirty_child_slot_ids(&self) -> Vec<u64> {
let mut dirty_child_slots = self.dirty_child_slots.lock();
std::mem::take(&mut *dirty_child_slots)
}
pub(crate) fn clear_dirty(&self) {
self.dirty.store(false, Ordering::Relaxed);
self.subtree_dirty.store(false, Ordering::Relaxed);
self.dirty_child_slots.lock().clear();
}
pub fn batch<R>(&self, f: impl FnOnce() -> R) -> R {
self.batch.batch(f)
}
pub fn props<T: Send + PartialEq + 'static>(&self) -> &T {
self
.props
.as_ref()
.and_then(|props| props.downcast_ref::<T>())
.expect("component props are not available for this type")
}
#[cfg(feature = "devtools")]
fn set_props<T: Send + PartialEq + DevtoolsInspectable + 'static>(&mut self, props: T) {
self.props_debug = Some(DevtoolsInspectableDebug::from_props(&props));
self.props = Some(Box::new(props));
}
#[cfg(not(feature = "devtools"))]
fn set_props<T: Send + PartialEq + 'static>(&mut self, props: T) {
self.props = Some(Box::new(props));
}
pub(crate) fn set_component_name(&mut self, name: &'static str) {
self.component_name = name;
}
#[cfg(feature = "perf_profile")]
pub(crate) fn component_name(&self) -> &'static str {
self.component_name
}
fn dirty_notifier(
&self,
#[cfg(feature = "perf_profile")] source: crate::app::profiler::RenderSource,
) -> impl Fn() + Send + Sync + 'static {
let dirty = self.dirty.clone();
let subtree_dirty = self.subtree_dirty.clone();
let ancestor_dirty_slots = self.ancestor_dirty_slots.clone();
let batch = self.batch.clone();
#[cfg(feature = "perf_profile")]
let component = self.component_name;
move || {
#[cfg(feature = "perf_profile")]
crate::app::profiler::detail::note(&dirty, component, source);
Self::mark_dirty_targets(&batch, &dirty, &subtree_dirty, &ancestor_dirty_slots);
}
}
#[cfg(feature = "perf_profile")]
pub(crate) fn render_span(
&self,
trigger: impl FnOnce() -> Option<crate::app::profiler::RenderTrigger>,
) -> Option<crate::app::profiler::detail::ComponentSpan> {
crate::app::profiler::detail::component_render(&self.dirty, self.component_name, trigger)
}
#[cfg(feature = "devtools")]
pub(crate) fn set_root_props<T: Send + PartialEq + DevtoolsInspectable + 'static>(&mut self, props: T) {
self.set_props(props);
}
#[cfg(not(feature = "devtools"))]
pub(crate) fn set_root_props<T: Send + PartialEq + 'static>(&mut self, props: T) {
self.set_props(props);
}
#[cfg(feature = "devtools")]
pub(crate) fn update_root_props<T: Send + PartialEq + DevtoolsInspectable + 'static>(&mut self, props: T) -> bool {
if !self.props_changed(&props) {
return false;
}
self.set_props(props);
self.mark_self_dirty();
true
}
#[cfg(not(feature = "devtools"))]
pub(crate) fn update_root_props<T: Send + PartialEq + 'static>(&mut self, props: T) -> bool {
if !self.props_changed(&props) {
return false;
}
self.set_props(props);
self.mark_self_dirty();
true
}
fn props_changed<T: Send + PartialEq + 'static>(&self, props: &T) -> bool {
self.props.as_ref().and_then(|existing| existing.downcast_ref::<T>()) != Some(props)
}
#[cfg(feature = "devtools")]
pub(crate) fn props_debug(&self) -> Option<DevtoolsInspectableDebug> {
self.props_debug.clone()
}
#[cfg(feature = "devtools")]
pub(crate) fn render_debug(&self) -> ComponentRenderDebug {
self.render_debug
}
#[cfg(feature = "devtools")]
pub(crate) fn signals_debug(&self) -> Vec<ComponentSignalDebug> {
self.signals_debug.clone()
}
#[cfg(feature = "devtools")]
pub(crate) fn memos_debug(&self) -> Vec<ComponentMemoDebug> {
self.memos_debug.clone()
}
#[cfg(feature = "devtools")]
pub(crate) fn effects_debug(&self) -> Vec<ComponentEffectDebug> {
self.effects_debug.clone()
}
#[cfg(feature = "devtools")]
pub(crate) fn contexts_debug(&self) -> Vec<ComponentContextDebug> {
self.contexts_debug.clone()
}
#[cfg(feature = "devtools")]
pub fn signal<T: Send + Sync + DevtoolsInspectable + 'static>(&mut self, initial: T) -> Signal<T> {
let debug_value = Arc::new(Mutex::new(format_debug_value(&initial)));
let debug_history = Arc::new(Mutex::new(Vec::new()));
let sig = Signal::new(initial);
self.signals_debug.push(ComponentSignalDebug {
id: sig.id(),
type_name: Arc::from(std::any::type_name::<T>()),
value: debug_value.clone(),
history: debug_history.clone(),
subscriber_count: sig.devtools_subscriber_count(),
});
let debug_handle = sig.subscribe_debug(move |value| {
update_debug_value_history(&debug_value, &debug_history, format_debug_value(value));
});
let handle = sig.watch(self.dirty_notifier(crate::app::profiler::detail::source_of::<T>(
crate::app::profiler::RenderTrigger::Signal,
)));
self.watch_handles.push(Box::new(debug_handle));
self.watch_handles.push(Box::new(handle));
sig
}
#[cfg(not(feature = "devtools"))]
pub fn signal<T: SignalValue + Send + Sync + 'static>(&mut self, initial: T) -> Signal<T> {
let sig = Signal::new(initial);
let handle = sig.watch(self.dirty_notifier(
#[cfg(feature = "perf_profile")]
crate::app::profiler::detail::source_of::<T>(crate::app::profiler::RenderTrigger::Signal),
));
self.watch_handles.push(Box::new(handle));
sig
}
#[cfg(feature = "devtools")]
pub fn memo<T: Clone + PartialEq + Send + Sync + DevtoolsInspectable + 'static>(
&mut self,
f: impl Fn() -> T + Send + Sync + 'static,
) -> Memo<T> {
let memo = Memo::new(f);
let debug_value = Arc::new(Mutex::new(memo.with(|value| format_debug_value(value))));
let debug_history = Arc::new(Mutex::new(Vec::new()));
self.memos_debug.push(ComponentMemoDebug {
id: memo.id(),
type_name: Arc::from(std::any::type_name::<T>()),
value: debug_value.clone(),
history: debug_history.clone(),
subscriber_count: memo.devtools_subscriber_count(),
});
let debug_handle = memo.subscribe(move |value| {
update_debug_value_history(&debug_value, &debug_history, format_debug_value(value));
});
self.watch_handles.push(Box::new(debug_handle));
memo
}
#[cfg(not(feature = "devtools"))]
pub fn memo<T: SignalValue + Clone + PartialEq + Send + Sync + 'static>(
&mut self,
f: impl Fn() -> T + Send + Sync + 'static,
) -> Memo<T> {
Memo::new(f)
}
pub fn create_ref<T: Send + Sync + 'static>(&self, initial: T) -> Ref<T> {
Ref::new(initial)
}
pub fn on_effect(&mut self, f: impl Fn() + Send + Sync + 'static) {
let effect = Effect::new(f);
#[cfg(feature = "devtools")]
self.effects_debug.push(ComponentEffectDebug { id: effect.id() });
self.effects.push(effect);
}
pub fn create_timeout(&mut self, duration: Duration, f: impl Fn() + Send + Sync + 'static) -> Timeout {
let timer = Timer::new(duration, false, f, self.window.as_ref().map(Window::loop_waker));
self.timers.push(timer.clone());
Timeout { timer }
}
pub fn create_interval(&mut self, duration: Duration, f: impl Fn() + Send + Sync + 'static) -> Interval {
let timer = Timer::new(duration, true, f, self.window.as_ref().map(Window::loop_waker));
self.timers.push(timer.clone());
Interval { timer }
}
#[cfg(feature = "form")]
pub fn form(&mut self, options: crate::components::FormOptions) -> crate::components::FormHandle {
crate::components::FormHandle::with_dirty(
options,
Arc::new(self.dirty_notifier(
#[cfg(feature = "perf_profile")]
crate::app::profiler::RenderSource {
trigger: crate::app::profiler::RenderTrigger::Form,
value_type: None,
},
)),
)
}
#[cfg(feature = "form")]
pub fn form_view<R>(&mut self, form: crate::components::FormHandle, render: impl FnOnce(&mut Ctx) -> R) -> Element
where
R: Into<Element>,
{
self.form_view_with(crate::components::FormProps::new(form), render)
}
#[cfg(feature = "form")]
pub fn form_view_with<R>(
&mut self,
props: crate::components::FormProps,
render: impl FnOnce(&mut Ctx) -> R,
) -> Element
where
R: Into<Element>,
{
let previous_context = self.context_map.clone();
if let Some(form) = props.form.clone() {
self.provide(crate::components::FormContext::new(form));
}
let child = render(self).into();
self.context_map = previous_context;
crate::components::Form::element(props, child)
}
#[cfg(feature = "form")]
pub fn form_control<T>(&mut self, control: &crate::components::Control<T>) -> crate::components::ResolvedControl<T>
where
T: SignalValue + Clone + PartialEq + Send + Sync + 'static,
{
control.resolve()
}
#[cfg(feature = "form")]
pub fn string_control(&mut self, name: impl Into<Arc<str>>) -> crate::components::ResolvedControl<String> {
self.current_form().string_control(name).resolve()
}
#[cfg(feature = "form")]
pub fn number_control(&mut self, name: impl Into<Arc<str>>) -> crate::components::ResolvedControl<f64> {
self.current_form().number_control(name).resolve()
}
#[cfg(feature = "form")]
pub fn bool_control(&mut self, name: impl Into<Arc<str>>) -> crate::components::ResolvedControl<bool> {
self.current_form().bool_control(name).resolve()
}
#[cfg(feature = "form")]
pub fn range_control(&mut self, name: impl Into<Arc<str>>) -> crate::components::ResolvedControl<(f64, f64)> {
self.current_form().range_control(name).resolve()
}
#[cfg(feature = "form")]
fn current_form(&mut self) -> crate::components::FormHandle {
self
.use_context::<crate::components::FormContext>()
.map(|ctx| ctx.form())
.expect("form controls must be resolved inside a Form render context")
}
#[cfg(feature = "router")]
pub fn router(&mut self, routes: crate::router::Routes) -> crate::router::RouterHandle {
crate::router::RouterHandle::new(routes)
}
#[cfg(feature = "router")]
pub fn navigator(&mut self) -> Option<crate::router::Navigator> {
self.use_context::<crate::router::Navigator>()
}
#[cfg(feature = "router")]
pub fn route_params(&mut self) -> crate::router::Params {
self
.use_context::<crate::router::route_match::RouterMatches>()
.and_then(|matches| matches.0.last().map(|m| m.params().clone()))
.or_else(|| {
self
.use_context::<crate::router::route_match::OutletDepth>()
.and_then(|depth| {
self
.use_context::<crate::router::route_match::RouterMatches>()
.and_then(|matches| matches.0.get(depth.0).map(|m| m.params().clone()))
})
})
.unwrap_or_default()
}
#[cfg(feature = "router")]
pub fn route_path(&mut self) -> String {
self
.use_context::<crate::router::Navigator>()
.map(|nav| nav.path().get())
.unwrap_or_default()
}
#[cfg(feature = "router")]
pub fn route_query(&mut self) -> crate::router::Query {
self
.use_context::<crate::router::Navigator>()
.map(|nav| crate::router::Query::from_path(&nav.path().get()))
.unwrap_or_default()
}
#[cfg(feature = "router")]
pub fn route_state<T: std::any::Any + Send + Sync>(&mut self) -> Option<std::sync::Arc<T>> {
if let Some(state) = self.use_context::<crate::router::route_match::StackState>() {
return state.0?.downcast::<T>().ok();
}
let nav = self.use_context::<crate::router::Navigator>()?;
let _ = nav.path().get();
nav.state::<T>()
}
#[cfg(feature = "router")]
pub fn navigation_item(&mut self, item: crate::components::NavigationItem) {
if let Some(slot) = self.use_context::<crate::components::stack_outlet::NavigationSlot>() {
slot.set(item);
}
}
#[cfg(feature = "query")]
pub fn query_client(&mut self) -> crate::query::QueryClient {
let client = self
.use_context::<crate::query::QueryClient>()
.expect("QueryClient is missing: call ctx.provide(QueryClient::new()) in the root component's create method");
self.query_registry.attach(
&client,
self.window.as_ref().map(|window| window.loop_waker()),
#[cfg(feature = "tokio")]
self.runtime_future_handle.clone(),
);
client
}
#[cfg(feature = "query")]
pub fn query<D: crate::query::QueryDefinition>(
&mut self,
query: crate::query::Query<D>,
) -> crate::query::QueryHandle<D::Data, D::Error> {
assert!(self.rendering, "ctx.query must be called during render");
let client = self.query_client();
let cursor = self.query_cursor;
self.query_cursor += 1;
if let Some(observer) = self.query_slots.get(cursor)
&& observer.matches(&client, &query)
{
return observer.handle::<D>();
}
let (handle, observer) = client.observe(query, Instant::now());
if cursor < self.query_slots.len() {
self.query_slots[cursor] = observer;
} else {
self.query_slots.push(observer);
}
handle
}
pub fn watch<T: SignalValue + Send + Sync + 'static>(
&mut self,
signal: &Signal<T>,
f: impl Fn(&T) + Send + Sync + 'static,
) {
let sub = signal.subscribe(f);
self.watch_handles.push(Box::new(sub));
}
pub fn store<T: SignalValue + Clone + Send + Sync + 'static>(&mut self, initial: T) -> Store<T> {
let store = Store::new(initial);
let handle = store.signal().watch(self.dirty_notifier(
#[cfg(feature = "perf_profile")]
crate::app::profiler::detail::source_of::<T>(crate::app::profiler::RenderTrigger::Store),
));
self.watch_handles.push(Box::new(handle));
store
}
pub fn provide<T: Clone + Send + Sync + 'static>(&mut self, value: T) {
#[cfg(feature = "devtools")]
self.note_provided(std::any::type_name::<T>(), false, inspected(&value));
#[cfg(feature = "router")]
if self.rendering
&& let Some(navigator) = (&value as &dyn std::any::Any).downcast_ref::<crate::router::Navigator>()
{
crate::router::navigator::provide_in_render(navigator.clone());
}
self.store_context(value);
}
pub fn provide_if_changed<T: Clone + PartialEq + Send + Sync + 'static>(&mut self, value: T) {
#[cfg(feature = "devtools")]
self.note_provided(std::any::type_name::<T>(), false, inspected(&value));
let id = TypeId::of::<T>();
if self.rendering {
let unchanged = self
.previous_render_contexts
.as_ref()
.and_then(|previous| previous.entry(id))
.filter(|entry| entry.value::<T>() == Some(&value))
.cloned();
match unchanged {
Some(entry) => {
self.context_map.insert(id, entry);
if let Some(previous) = &self.previous_render_contexts
&& self.context_map.has_same_entries(previous)
{
self.context_map.set_revision(previous.revision());
}
}
None => self.context_map.provide(value),
}
self.provided_in_render = true;
return;
}
let unchanged = self
.provided_contexts
.entry(id)
.is_some_and(|entry| entry.value::<T>() == Some(&value));
if !unchanged {
self.store_context(value);
}
}
fn store_context<T: Clone + Send + Sync + 'static>(&mut self, value: T) {
if self.rendering {
self.context_map.provide(value);
self.provided_in_render = true;
return;
}
self.provided_contexts.provide(value.clone());
self.base_contexts.provide(value);
self.context_map = self.base_contexts.clone();
}
fn note_context_read(&mut self, id: TypeId) {
if !self.context_reads.contains(&id) {
self.context_reads.push(id);
}
}
pub fn use_context<T: Clone + Send + Sync + 'static>(&mut self) -> Option<T> {
#[cfg(feature = "devtools")]
self.note_consumed(std::any::type_name::<T>(), false);
self.note_context_read(TypeId::of::<T>());
self.context_map.get::<T>()
}
pub fn create_context<T: Clone + std::hash::Hash + Send + Sync + 'static>(&mut self, value: T) -> ReactiveContext<T> {
#[cfg(feature = "devtools")]
let (held, history) = self.note_provided(std::any::type_name::<ReactiveContext<T>>(), true, inspected(&value));
let ctx = ReactiveContext::new(value);
#[cfg(feature = "devtools")]
ctx.inspect_with(move |value: &T| record_context_value(&held, &history, inspected(value)));
self.store_context(ctx.clone());
ctx.subscribe(self.dirty_notifier(
#[cfg(feature = "perf_profile")]
crate::app::profiler::detail::source_of::<T>(crate::app::profiler::RenderTrigger::Context),
));
ctx
}
pub fn consume_context<T: Clone + std::hash::Hash + Send + Sync + 'static>(&mut self) -> Option<ReactiveContext<T>> {
#[cfg(feature = "devtools")]
self.note_consumed(std::any::type_name::<ReactiveContext<T>>(), true);
self.note_context_read(TypeId::of::<ReactiveContext<T>>());
let ctx = self.context_map.get::<ReactiveContext<T>>()?;
ctx.subscribe(self.dirty_notifier(
#[cfg(feature = "perf_profile")]
crate::app::profiler::detail::source_of::<T>(crate::app::profiler::RenderTrigger::Context),
));
Some(ctx)
}
fn inherit_contexts(&mut self, parent: &ContextMap) -> Inherited {
if self.inherited_context_revision == parent.revision() {
return Inherited::Same;
}
self.inherited_context_revision = parent.revision();
let left_by_render = self.context_map.revision() != self.base_contexts.revision();
let previous = std::mem::replace(
&mut self.base_contexts,
ContextMap::layered(parent, &self.provided_contexts),
);
if left_by_render {
self.previous_render_contexts = Some(std::mem::take(&mut self.context_map));
}
self.context_map = self.base_contexts.clone();
let read_changed = self
.context_reads
.iter()
.any(|id| previous.entry_revision(*id) != self.base_contexts.entry_revision(*id));
if read_changed || self.provided_in_render {
Inherited::Changed
} else {
Inherited::Refreshed
}
}
fn propagate_contexts(&mut self) -> bool {
let contexts = std::mem::take(&mut self.context_map);
let mut render_again = false;
for slot in self.children.iter_mut().filter(|slot| !slot.offstage) {
let for_each_item = slot.component.type_name() == ForEachSlot::TYPE_NAME;
let changed = match slot.ctx.inherit_contexts(&contexts) {
Inherited::Same => false,
Inherited::Refreshed => slot.ctx.propagate_contexts(),
Inherited::Changed => true,
};
if !changed {
continue;
}
if for_each_item {
render_again = true;
} else {
#[cfg(feature = "perf_profile")]
crate::app::profiler::detail::note(
&slot.ctx.dirty,
slot.ctx.component_name,
crate::app::profiler::RenderSource {
trigger: crate::app::profiler::RenderTrigger::Context,
value_type: None,
},
);
slot.ctx.mark_dirty();
}
}
self.context_map = contexts;
render_again
}
fn mark_dirty(&self) {
Self::mark_dirty_targets(
&self.batch,
&self.dirty,
&self.subtree_dirty,
&self.ancestor_dirty_slots,
);
}
#[cfg(feature = "devtools")]
fn context_debug(
&mut self,
kind: ComponentContextKind,
type_name: &'static str,
reactive: bool,
) -> &ComponentContextDebug {
let at = self
.contexts_debug
.iter()
.position(|ctx| ctx.kind == kind && ctx.type_name.as_ref() == type_name);
let at = at.unwrap_or_else(|| {
self.contexts_debug.push(ComponentContextDebug {
kind,
type_name: Arc::from(type_name),
reactive,
value: Arc::default(),
history: Arc::default(),
});
self.contexts_debug.len() - 1
});
&self.contexts_debug[at]
}
#[cfg(feature = "devtools")]
#[allow(clippy::type_complexity)]
fn note_provided(
&mut self,
type_name: &'static str,
reactive: bool,
value: Vec<ComponentInfo>,
) -> (
Arc<Mutex<Option<Vec<ComponentInfo>>>>,
Arc<Mutex<Vec<ComponentContextChangeDebug>>>,
) {
let debug = self.context_debug(ComponentContextKind::Provided, type_name, reactive);
let (held, history) = (debug.value.clone(), debug.history.clone());
record_context_value(&held, &history, value);
(held, history)
}
#[cfg(feature = "devtools")]
fn note_consumed(&mut self, type_name: &'static str, reactive: bool) {
self.context_debug(ComponentContextKind::Consumed, type_name, reactive);
}
pub fn in_theme<E: Into<Element>>(&mut self, theme: &Theme, build: impl FnOnce(&mut Ctx) -> E) -> Element {
theme.track_access();
let outer = self.theme.replace(theme.clone());
let mut element: Element = crate::ui::in_theme(theme, || build(self).into());
self.theme = outer;
element.node.set_theme_scope(theme);
element
}
pub fn theme(&self) -> &Theme {
let theme = self.theme.as_ref().expect("theme not set");
theme.track_access();
theme
}
pub fn reduced_motion(&self) -> bool {
self.theme().reduced_motion()
}
pub(crate) fn window_untracked(&self) -> Option<&crate::app::window::Window> {
self.window.as_ref()
}
pub fn window(&self) -> crate::app::window::WindowHandle {
let window = self.window.as_ref().expect("window not set");
window.track_access();
window.handle()
}
pub fn breakpoint(&self) -> Option<crate::app::theme::Breakpoint> {
self.breakpoint.as_ref().and_then(|memo| memo.get())
}
pub fn responsive<T: Clone>(&self, value: &crate::responsive::Responsive<T>) -> T {
value.resolve(self.breakpoint()).clone()
}
pub fn app_ref(&self) -> &App {
self.app.as_ref().expect("app ref not set")
}
pub(crate) fn app_if_set(&self) -> Option<&App> {
self.app.as_ref()
}
pub(crate) fn app_handle(&self) -> Option<App> {
self.app.clone()
}
pub fn window_opener(&self) -> crate::app::WindowOpener {
self.app_ref().window_opener()
}
pub fn open_url(&self, url: &str) -> Result<(), crate::app::OpenUrlError> {
crate::app::open_url(url)
}
pub fn share(&self, share: crate::app::Share) -> Result<(), crate::app::ShareError> {
self.window().share(share)
}
pub fn app_ref_mut(&mut self) -> &mut App {
self.app.as_mut().expect("app ref not set")
}
#[cfg(feature = "persistent_storage")]
pub fn persistent_value<T: crate::persistent_storage::PersistentValue>(&self, key: &str) -> Option<T> {
self.app_ref().persistent_value(key)
}
#[cfg(feature = "persistent_storage")]
pub fn set_persistent_value<T: crate::persistent_storage::IntoPersistentValue>(
&self,
key: &str,
value: T,
) -> Result<(), crate::persistent_storage::PersistentStorageError> {
self.app_ref().set_persistent_value(key, value)
}
#[cfg(feature = "persistent_storage")]
pub fn read_bulk<I, K>(
&self,
keys: I,
) -> Result<crate::persistent_storage::PersistentReadBatch, crate::persistent_storage::PersistentStorageError>
where
I: IntoIterator<Item = K>,
K: AsRef<str>,
{
self.app_ref().read_bulk(keys)
}
#[cfg(feature = "persistent_storage")]
pub fn read_bulk_values<T, I, K>(
&self,
keys: I,
) -> Result<Vec<Option<T>>, crate::persistent_storage::PersistentStorageError>
where
T: crate::persistent_storage::PersistentValue,
I: IntoIterator<Item = K>,
K: AsRef<str>,
{
self.app_ref().read_bulk_values(keys)
}
#[cfg(feature = "persistent_storage")]
pub fn write_bulk<I, E>(&self, entries: I) -> Result<(), crate::persistent_storage::PersistentStorageError>
where
I: IntoIterator<Item = E>,
E: crate::persistent_storage::IntoPersistentWrite,
{
self.app_ref().write_bulk(entries)
}
pub fn i18n(&self) -> &I18n {
let i18n = self.i18n.as_ref().expect("i18n not set");
i18n.track_access();
i18n
}
pub fn t(&self, key: &str) -> Arc<str> {
self.i18n().t(key)
}
pub fn t_ns(&self, namespace: &str, key: &str) -> Arc<str> {
self.i18n().t_ns(namespace, key)
}
pub fn t_args<I, K, V>(&self, key: &str, args: I) -> Arc<str>
where
I: IntoIterator<Item = (K, V)>,
K: AsRef<str>,
V: ToString,
{
self.i18n().t_args(key, args)
}
pub fn t_ns_args<I, K, V>(&self, namespace: &str, key: &str, args: I) -> Arc<str>
where
I: IntoIterator<Item = (K, V)>,
K: AsRef<str>,
V: ToString,
{
self.i18n().t_ns_args(namespace, key, args)
}
pub fn children(&self) -> &[Element] {
self.slot_children.as_deref().unwrap_or(&[])
}
pub fn has_children(&self) -> bool {
self.slot_children.as_ref().is_some_and(|c| !c.is_empty())
}
pub fn element_ref(&mut self) -> ElementRef {
self.element_ref_mut().as_ref()
}
pub fn focus(&self, element: &ElementRef) {
self.element_requests.lock().focus = Some(element.clone());
self.wake_window();
}
pub fn scroll_into_view(&self, element: &ElementRef) {
self.element_requests.lock().scroll_into_view = Some(element.clone());
self.wake_window();
}
pub fn scroll_into_view_start(&self, element: &ElementRef) {
self.element_requests.lock().scroll_into_view_start = Some(element.clone());
self.wake_window();
}
pub fn scroll_range_into_view(&self, element: &ElementRef, range: std::ops::Range<usize>) {
self.element_requests.lock().scroll_range_into_view = Some((element.clone(), range));
self.wake_window();
}
pub(crate) fn take_scroll_range_into_view_request(&self) -> Option<(ElementRef, std::ops::Range<usize>)> {
self.element_requests.lock().scroll_range_into_view.take()
}
fn wake_window(&self) {
if let Some(window) = &self.window {
window.wake();
}
}
pub(crate) fn has_element_request(&self) -> bool {
let requests = self.element_requests.lock();
requests.focus.is_some()
|| requests.scroll_into_view.is_some()
|| requests.scroll_into_view_start.is_some()
|| requests.scroll_range_into_view.is_some()
|| requests.blur
|| requests.scroll_to_top
|| requests.focus_onward
}
pub fn edit(&self, command: crate::app::events::EditCommand) {
self.edit_runner().run(command);
}
pub fn edit_runner(&self) -> EditRunner {
EditRunner {
window: self.window.clone(),
}
}
pub fn history_back(&self) {
self.history_runner().back();
}
pub fn history_forward(&self) {
self.history_runner().forward();
}
pub fn history_runner(&self) -> HistoryRunner {
HistoryRunner {
window: self.window.clone(),
}
}
pub fn blur(&self) {
self.element_requests.lock().blur = true;
self.wake_window();
}
pub(crate) fn take_blur_request(&self) -> bool {
std::mem::take(&mut self.element_requests.lock().blur)
}
pub fn scroll_to_top(&self) {
self.element_requests.lock().scroll_to_top = true;
self.wake_window();
}
pub(crate) fn take_scroll_to_top_request(&self) -> bool {
std::mem::take(&mut self.element_requests.lock().scroll_to_top)
}
pub fn scroll_to_top_of(&self, state: &crate::layout::layout_kind::ScrollState) {
let mut requests = self.element_requests.lock();
if !requests.scroll_states_to_top.iter().any(|asked| asked.same(state)) {
requests.scroll_states_to_top.push(state.clone());
}
drop(requests);
self.wake_window();
}
pub(crate) fn take_scroll_states_to_top(&self) -> Vec<crate::layout::layout_kind::ScrollState> {
std::mem::take(&mut self.element_requests.lock().scroll_states_to_top)
}
pub fn scroll_state_to(&self, state: &crate::layout::layout_kind::ScrollState, x: f32, y: f32) {
let mut requests = self.element_requests.lock();
requests.scroll_states_to.retain(|(asked, ..)| !asked.same(state));
requests.scroll_states_to.push((state.clone(), x, y));
drop(requests);
self.wake_window();
}
pub(crate) fn take_scroll_states_to(&self) -> Vec<(crate::layout::layout_kind::ScrollState, f32, f32)> {
std::mem::take(&mut self.element_requests.lock().scroll_states_to)
}
pub fn is_in_front(&mut self) -> bool {
self.use_context::<InFront>().is_none_or(|front| front.get())
}
pub fn safe_area(&mut self) -> crate::app::window::SafeAreaInsets {
if self.use_context::<SafeAreaTaken>().is_some() {
return crate::app::window::SafeAreaInsets::default();
}
let mut safe_area = self.window().safe_area;
match self.use_context::<BottomBarInset>() {
Some(BottomBarInset::Over(bar)) => safe_area.bottom += bar,
None => {}
}
safe_area
}
pub fn focus_handle(&self) -> FocusHandle {
FocusHandle {
requests: self.element_requests.clone(),
window: self.window.clone(),
}
}
pub(crate) fn take_focus_onward_request(&self) -> bool {
std::mem::take(&mut self.element_requests.lock().focus_onward)
}
pub(crate) fn focus_request(&self) -> Option<ElementRef> {
self.element_requests.lock().focus.clone()
}
pub(crate) fn take_focus_request(&self) -> Option<ElementRef> {
self.element_requests.lock().focus.take()
}
pub(crate) fn take_scroll_into_view_request(&self) -> Option<ElementRef> {
self.element_requests.lock().scroll_into_view.take()
}
pub(crate) fn take_scroll_into_view_start_request(&self) -> Option<ElementRef> {
self.element_requests.lock().scroll_into_view_start.take()
}
pub fn element_ref_mut(&mut self) -> ElementRefMut {
if !self.rendering {
return ElementRefMut::new();
}
let cursor = self.element_ref_cursor;
self.element_ref_cursor += 1;
if cursor == self.element_refs.len() {
self.element_refs.push(ElementRefMut::new());
}
self.element_refs[cursor].clone()
}
pub fn interaction(&self) -> crate::node::interaction_state::InteractionState {
crate::node::interaction_state::InteractionState::new()
}
pub fn on_click_outside(
&mut self,
element_ref: impl Into<ElementRef>,
f: impl Fn(&MouseEvent) + Send + Sync + 'static,
) {
if !self.rendering {
return;
}
let cursor = self.click_outside_cursor;
self.click_outside_cursor += 1;
self.click_outside_active_cursors.push(cursor);
let element_ref = element_ref.into();
let callback: ClickOutsideCallback = Arc::new(f);
let mut registry = self.click_outside_registry.lock();
if let Some(entry) = registry
.iter_mut()
.find(|entry| entry.scope_id == self.scope_id && entry.cursor == cursor)
{
entry.element_ref = element_ref;
entry.callback = callback;
return;
}
registry.push(ClickOutsideEntry {
scope_id: self.scope_id,
cursor,
element_ref,
callback,
});
}
pub(crate) fn click_outside_callbacks_at(&self, x: f32, y: f32) -> Vec<ClickOutsideCallback> {
self
.click_outside_registry
.lock()
.iter()
.filter(|entry| element_ref_is_click_outside(&entry.element_ref, x, y))
.map(|entry| entry.callback.clone())
.collect()
}
pub fn mount<C: Component>(&mut self, props: C::Props) -> Element {
self.mount_inner::<C>(None, props, None, true)
}
pub fn mount_keyed<C: Component>(&mut self, key: &str, props: C::Props) -> Element {
self.mount_inner::<C>(Some(key), props, None, true)
}
pub fn mount_offstage<C: Component>(&mut self, props: C::Props, active: bool) -> Element {
self.mount_inner::<C>(None, props, None, active)
}
pub fn mount_keyed_offstage<C: Component>(&mut self, key: &str, props: C::Props, active: bool) -> Element {
self.mount_inner::<C>(Some(key), props, None, active)
}
pub fn mount_with<C: Component>(&mut self, props: C::Props, slot_children: Vec<Element>) -> Element {
self.mount_inner::<C>(None, props, Some(slot_children), true)
}
pub fn mount_keyed_with<C: Component>(&mut self, key: &str, props: C::Props, slot_children: Vec<Element>) -> Element {
self.mount_inner::<C>(Some(key), props, Some(slot_children), true)
}
pub(crate) fn mount_holding(&mut self, mount: impl FnOnce(&mut Ctx) -> Element) -> Element {
let was = std::mem::replace(&mut self.holding, true);
let element = mount(self);
self.holding = was;
element
}
pub(crate) fn cached_output_of(&self, slot: u64) -> Option<Box<Node>> {
self.descendant_slot(slot)?.assembled_output()
}
fn mount_inner<C: Component>(
&mut self,
key: Option<&str>,
props: C::Props,
slot_children: Option<Vec<Element>>,
active: bool,
) -> Element {
let holding = self.holding;
let cursor = self.child_cursor;
self.child_cursor += 1;
let type_name = std::any::type_name::<C>();
if let Some(key) = key
&& let Some(found) = self.children[cursor..]
.iter()
.position(|slot| slot.key.as_deref() == Some(key) && slot.component.type_name() == type_name)
.map(|offset| cursor + offset)
&& found != cursor
{
let slot = self.children.remove(found);
self.children.insert(cursor, slot);
}
let can_reuse = self.children.get(cursor).is_some_and(|slot| {
let key_match = match key {
Some(k) => slot.key.as_deref() == Some(k),
None => slot.key.is_none(),
};
key_match && slot.component.type_name() == type_name
});
if can_reuse {
let slot = &mut self.children[cursor];
let has_slot_children = slot.ctx.slot_children.is_some() || slot_children.is_some();
let props_changed = slot.ctx.props_changed(&props);
let inherited = slot.ctx.inherit_contexts(&self.context_map);
let theme_changed = match (&slot.ctx.theme, &self.theme) {
(Some(held), Some(now)) => !held.same_theme(now),
(None, Some(_)) => true,
_ => false,
};
if theme_changed {
slot.ctx.theme = self.theme.clone();
}
slot.ctx.slot_children = slot_children;
if props_changed {
slot.ctx.set_props(props);
}
if !active {
if !slot.offstage && slot.rendered.is_some() && slot.ctx.any_dirty() {
slot.render_as_it_leaves();
}
slot.offstage = true;
slot.offstage_dirty = true;
slot.ctx.clear_modal_entries_recursive();
slot.ctx.clear_dirty();
return Element::new();
}
slot.offstage = false;
#[cfg(feature = "perf_profile")]
let resumed = slot.rendered.is_none() || slot.offstage_dirty;
#[cfg(feature = "perf_profile")]
let mut context_changed = inherited == Inherited::Changed;
let mut needs_render = has_slot_children
|| props_changed
|| theme_changed
|| inherited == Inherited::Changed
|| slot.ctx.any_dirty()
|| slot.rendered.is_none()
|| slot.offstage_dirty;
slot.offstage_dirty = false;
if !needs_render && inherited == Inherited::Refreshed && slot.ctx.propagate_contexts() {
needs_render = true;
#[cfg(feature = "perf_profile")]
{
context_changed = true;
}
}
if needs_render {
#[cfg(feature = "perf_profile")]
let _span = slot.ctx.render_span(|| {
use crate::app::profiler::RenderTrigger;
if resumed {
Some(RenderTrigger::Mount)
} else if props_changed {
Some(RenderTrigger::Props)
} else if context_changed {
Some(RenderTrigger::Context)
} else if has_slot_children {
Some(RenderTrigger::SlotChildren)
} else {
None
}
});
let previous = slot.rendered.take();
slot.ctx.begin_render();
let mut element = slot.component.render(&mut slot.ctx);
slot.ctx.end_render();
element.node = attach_component_metadata(
element.node,
slot.component.tag_name(),
slot.id,
slot.key.as_deref(),
#[cfg(feature = "devtools")]
&slot.ctx,
);
if let Some(previous) = previous.as_ref() {
element.node.preserve_runtime_state_from(previous);
}
slot.keep_rendered(&element.node);
return element;
}
if holding {
return Element::from_node(Node::held_component(slot.id, slot.key.as_deref()));
}
let output = slot
.assembled_output()
.expect("a component that is not rendered again has kept its output");
return Element::from_boxed_node(output);
}
let slot_id = next_component_slot_id();
let mut child_ctx = Ctx::new();
child_ctx.inherit_dirty_ancestors_from(self, slot_id);
child_ctx.batch = self.batch.clone();
child_ctx.theme = self.theme.clone();
child_ctx.window = self.window.clone();
child_ctx.breakpoint = self.breakpoint.clone();
child_ctx.app = self.app.clone();
child_ctx.element_requests = self.element_requests.clone();
#[cfg(feature = "tokio")]
{
child_ctx.runtime_future_handle = self.runtime_future_handle.clone();
}
#[cfg(feature = "query")]
{
child_ctx.query_registry = self.query_registry.clone();
}
child_ctx.click_outside_registry = self.click_outside_registry.clone();
{
child_ctx.i18n = self.i18n.clone();
}
child_ctx.inherit_contexts(&self.context_map);
child_ctx.slot_children = slot_children;
child_ctx.set_props(props);
child_ctx.scope_id = slot_id;
child_ctx.component_name = type_name;
#[cfg(feature = "perf_profile")]
let _span = active
.then(|| child_ctx.render_span(|| Some(crate::app::profiler::RenderTrigger::Mount)))
.flatten();
let component = C::create(&mut child_ctx);
let wrapper = ComponentWrapper { component };
let mut element = Element::new();
if active {
child_ctx.begin_render();
element = wrapper.render(&mut child_ctx);
child_ctx.end_render();
element.node = attach_component_metadata(
element.node,
wrapper.tag_name(),
slot_id,
key,
#[cfg(feature = "devtools")]
&child_ctx,
);
} else {
child_ctx.clear_dirty();
}
let mut slot = ChildSlot {
id: slot_id,
key: key.map(str::to_owned),
component: Box::new(wrapper),
ctx: child_ctx,
rendered: None,
rendered_at: 0,
mounted: false,
offstage: !active,
offstage_dirty: !active,
};
if active {
slot.keep_rendered(&element.node);
}
if key.is_some() {
self.insert_child_slot(cursor, slot);
} else {
self.set_child_slot(cursor, slot);
}
element
}
pub fn for_each<T, K, KF, CF>(
&mut self,
items: impl IntoIterator<Item = T>,
key_fn: KF,
component_fn: CF,
) -> Vec<Element>
where
K: std::fmt::Display,
KF: Fn(&T) -> K,
CF: Fn(&mut Ctx, T) -> Element,
{
let cursor = self.child_cursor;
self.child_cursor += 1;
let can_reuse_group = self
.children
.get(cursor)
.is_some_and(|slot| slot.key.is_none() && slot.component.type_name() == ForEachSlot::TYPE_NAME);
if !can_reuse_group {
let slot_id = next_component_slot_id();
let mut group_ctx = Ctx::new();
group_ctx.inherit_dirty_ancestors_from(self, slot_id);
group_ctx.batch = self.batch.clone();
group_ctx.theme = self.theme.clone();
group_ctx.window = self.window.clone();
group_ctx.breakpoint = self.breakpoint.clone();
group_ctx.app = self.app.clone();
group_ctx.element_requests = self.element_requests.clone();
#[cfg(feature = "tokio")]
{
group_ctx.runtime_future_handle = self.runtime_future_handle.clone();
}
#[cfg(feature = "query")]
{
group_ctx.query_registry = self.query_registry.clone();
}
group_ctx.click_outside_registry = self.click_outside_registry.clone();
{
group_ctx.i18n = self.i18n.clone();
}
group_ctx.inherit_contexts(&self.context_map);
group_ctx.scope_id = slot_id;
group_ctx.component_name = ForEachSlot::TYPE_NAME;
let slot = ChildSlot {
id: slot_id,
key: None,
component: Box::new(ForEachSlot),
ctx: group_ctx,
rendered: None,
rendered_at: 0,
mounted: false,
offstage: false,
offstage_dirty: false,
};
self.set_child_slot(cursor, slot);
}
let slot = &mut self.children[cursor];
slot.ctx.inherit_contexts(&self.context_map);
slot.ctx.begin_render();
let elements = items
.into_iter()
.map(|item| {
let key = format!("{}", key_fn(&item));
slot.ctx.render_for_each_item(key, item, &component_fn)
})
.collect();
slot.ctx.end_render();
elements
}
fn render_for_each_item<T, CF>(&mut self, key: String, item: T, component_fn: &CF) -> Element
where
CF: Fn(&mut Ctx, T) -> Element,
{
let cursor = self.child_cursor;
self.child_cursor += 1;
if let Some(found) = self.children[cursor..]
.iter()
.position(|slot| {
slot.key.as_deref() == Some(key.as_str()) && slot.component.type_name() == ForEachSlot::TYPE_NAME
})
.map(|offset| cursor + offset)
&& found != cursor
{
let slot = self.children.remove(found);
self.children.insert(cursor, slot);
}
let can_reuse = self.children.get(cursor).is_some_and(|slot| {
slot.key.as_deref() == Some(key.as_str()) && slot.component.type_name() == ForEachSlot::TYPE_NAME
});
if can_reuse {
let slot = &mut self.children[cursor];
slot.ctx.inherit_contexts(&self.context_map);
#[cfg(feature = "perf_profile")]
let _span = slot
.ctx
.render_span(|| Some(crate::app::profiler::RenderTrigger::ListItem));
slot.ctx.begin_render();
let mut element = component_fn(&mut slot.ctx, item);
slot.ctx.end_render();
element.node = attach_component_metadata(
element.node,
slot.component.tag_name(),
slot.id,
slot.key.as_deref(),
#[cfg(feature = "devtools")]
&slot.ctx,
);
slot.keep_rendered(&element.node);
return element;
}
let slot_id = next_component_slot_id();
let mut child_ctx = Ctx::new();
child_ctx.inherit_dirty_ancestors_from(self, slot_id);
child_ctx.batch = self.batch.clone();
child_ctx.theme = self.theme.clone();
child_ctx.window = self.window.clone();
child_ctx.breakpoint = self.breakpoint.clone();
child_ctx.app = self.app.clone();
child_ctx.element_requests = self.element_requests.clone();
#[cfg(feature = "tokio")]
{
child_ctx.runtime_future_handle = self.runtime_future_handle.clone();
}
#[cfg(feature = "query")]
{
child_ctx.query_registry = self.query_registry.clone();
}
child_ctx.click_outside_registry = self.click_outside_registry.clone();
{
child_ctx.i18n = self.i18n.clone();
}
child_ctx.inherit_contexts(&self.context_map);
child_ctx.scope_id = slot_id;
child_ctx.component_name = ForEachSlot::TYPE_NAME;
#[cfg(feature = "perf_profile")]
let _span = child_ctx.render_span(|| Some(crate::app::profiler::RenderTrigger::Mount));
child_ctx.begin_render();
let mut element = component_fn(&mut child_ctx, item);
child_ctx.end_render();
element.node = attach_component_metadata(
element.node,
component_tag_name::<ForEachSlot>(),
slot_id,
Some(key.as_str()),
#[cfg(feature = "devtools")]
&child_ctx,
);
let mut slot = ChildSlot {
id: slot_id,
key: Some(key),
component: Box::new(ForEachSlot),
ctx: child_ctx,
rendered: None,
rendered_at: 0,
mounted: false,
offstage: false,
offstage_dirty: false,
};
slot.keep_rendered(&element.node);
self.insert_child_slot(cursor, slot);
element
}
pub fn error_boundary(
&mut self,
component_fn: impl FnOnce(&mut Ctx) -> Element,
fallback_fn: impl FnOnce() -> Element,
) -> Element {
let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| component_fn(self)));
match result {
Ok(node) => node,
Err(_) => fallback_fn(),
}
}
pub fn begin_render(&mut self) {
if self.context_map.revision() != self.base_contexts.revision() {
self.previous_render_contexts = Some(std::mem::replace(&mut self.context_map, self.base_contexts.clone()));
}
self.provided_in_render = false;
self.clear_dirty();
#[cfg(feature = "devtools")]
{
self.render_started = Some(Instant::now());
}
self.child_cursor = 0;
self.element_ref_cursor = 0;
self.click_outside_cursor = 0;
self.click_outside_active_cursors.clear();
self.future_cursor = 0;
self.frames.begin_render();
#[cfg(feature = "query")]
{
self.query_cursor = 0;
}
tracking::start_tracking();
self.rendering = true;
#[cfg(feature = "router")]
crate::router::navigator::enter_render(self.context_map.get::<crate::router::Navigator>());
if let Some(theme) = &self.theme {
crate::ui::theme::enter_theme(
theme.clone(),
self.breakpoint.clone(),
self.app.as_ref().map(|app| app.shared.dates.clone()),
self.app.as_ref().map(|app| app.shared.locale.clone()),
self.app.as_ref().map(|app| app.shared.pointer.clone()),
self.window.clone(),
);
}
}
fn set_child_slot(&mut self, cursor: usize, slot: ChildSlot) {
if cursor < self.children.len() {
self.children[cursor].ctx.clear_modal_entries_recursive();
if self.children[cursor].mounted {
self.children[cursor].component.on_unmounted();
}
self.children[cursor] = slot;
} else {
self.children.push(slot);
}
}
fn insert_child_slot(&mut self, cursor: usize, slot: ChildSlot) {
if cursor < self.children.len() {
self.children.insert(cursor, slot);
} else {
self.children.push(slot);
}
}
fn clear_modal_entries_recursive(&mut self) {
self
.click_outside_registry
.lock()
.retain(|entry| entry.scope_id != self.scope_id);
for slot in &mut self.children {
slot.ctx.clear_modal_entries_recursive();
}
}
pub(crate) fn end_render(&mut self) {
#[cfg(feature = "devtools")]
if let Some(started) = self.render_started.take() {
self.render_debug.count += 1;
self.render_debug.last = started.elapsed();
}
self
.click_outside_registry
.lock()
.retain(|entry| entry.scope_id != self.scope_id || self.click_outside_active_cursors.contains(&entry.cursor));
for slot in &self.children[self.child_cursor..] {
if slot.mounted {
slot.component.on_unmounted();
}
}
for slot in &mut self.children[self.child_cursor..] {
slot.ctx.clear_modal_entries_recursive();
}
self.children.truncate(self.child_cursor);
for slot in &mut self.children {
if !slot.mounted && !slot.offstage {
slot.component.on_mounted();
slot.mounted = true;
}
}
self.element_refs.truncate(self.element_ref_cursor);
self.drop_unreached_future_slots();
self.frames.end_render();
#[cfg(feature = "query")]
self.query_slots.truncate(self.query_cursor);
if self.rendering && self.theme.is_some() {
crate::ui::theme::leave_theme();
}
#[cfg(feature = "router")]
if self.rendering {
crate::router::navigator::leave_render();
}
self.rendering = false;
if let Some(previous) = self.previous_render_contexts.take()
&& self.context_map.has_same_entries(&previous)
{
self.context_map.set_revision(previous.revision());
}
let deps = tracking::stop_tracking();
let handles = deps
.into_iter()
.map(|dep| {
let notifier = self.dirty_notifier(
#[cfg(feature = "perf_profile")]
dep.source,
);
let handle = (dep.subscribe_fn)(Arc::new(notifier));
Box::new(handle) as Box<dyn Any + Send + Sync>
})
.collect();
self.render_watch_handles = handles;
}
pub(crate) fn any_dirty(&self) -> bool {
self.subtree_dirty.load(Ordering::Relaxed)
}
pub(crate) fn after_layout_recursive(&self) {
for slot in self.children.iter().filter(|slot| !slot.offstage) {
slot.component.after_layout();
slot.ctx.after_layout_recursive();
}
}
#[cfg(feature = "perf_profile")]
pub(crate) fn after_layout_recursive_spans(&self, spans: &crate::app::profiler::detail::HookSpans) {
for slot in self.children.iter().filter(|slot| !slot.offstage) {
let _span = spans.enter(slot.component.type_name());
slot.component.after_layout();
slot.ctx.after_layout_recursive_spans(spans);
}
}
#[cfg_attr(not(feature = "winit"), allow(dead_code))]
pub(crate) fn has_active_timers(&self) -> bool {
self.timers.iter().any(Timer::is_active)
|| self
.children
.iter()
.any(|slot| !slot.offstage && slot.ctx.has_active_timers())
}
#[cfg_attr(not(feature = "winit"), allow(dead_code))]
pub(crate) fn next_timer_fire(&self) -> Option<Instant> {
let own = self.timers.iter().filter_map(Timer::next_fire).min();
let children = self
.children
.iter()
.filter(|slot| !slot.offstage)
.filter_map(|slot| slot.ctx.next_timer_fire())
.min();
own.into_iter().chain(children).min()
}
#[cfg(feature = "query")]
#[cfg_attr(not(feature = "winit"), allow(dead_code))]
pub(crate) fn has_ready_queries(&self) -> bool {
self.query_registry.ready()
|| self
.children
.iter()
.any(|slot| !slot.offstage && slot.ctx.has_ready_queries())
}
#[cfg_attr(not(feature = "winit"), allow(dead_code))]
pub(crate) fn has_active_futures(&self) -> bool {
#[cfg(feature = "query")]
if self.query_registry.ready() {
return true;
}
self.future_slots().any(|slot| slot.task.is_active())
|| self
.children
.iter()
.any(|slot| !slot.offstage && slot.ctx.has_active_futures())
}
pub(crate) fn tick_timers(&mut self, now: Instant) -> bool {
let mut fired = false;
for timer in &self.timers {
fired |= timer.tick(now);
}
for slot in self.children.iter_mut().filter(|slot| !slot.offstage) {
fired |= slot.ctx.tick_timers(now);
}
fired
}
pub(crate) fn tick_futures(&mut self) -> bool {
let waker = self.loop_waker();
let mut cx = TaskContext::from_waker(&waker);
#[cfg(feature = "query")]
let query_changed = self.query_registry.tick(Instant::now(), &mut cx) | self.publish_query_updates();
#[cfg(not(feature = "query"))]
let query_changed = false;
self.poll_futures(&mut cx) | query_changed
}
#[cfg(feature = "query")]
pub(crate) fn next_query_deadline(&self) -> Option<Instant> {
self.query_registry.deadline()
}
#[cfg(feature = "query")]
fn publish_query_updates(&self) -> bool {
let mut changed = false;
for observer in &self.query_slots {
changed |= observer.publish();
}
for slot in &self.children {
changed |= slot.ctx.publish_query_updates();
}
changed
}
fn poll_futures(&mut self, cx: &mut TaskContext<'_>) -> bool {
let mut completed = false;
for slot in self.future_slots() {
completed |= slot.task.poll(cx);
}
for slot in self.children.iter_mut().filter(|slot| !slot.offstage) {
completed |= slot.ctx.poll_futures(cx);
}
completed
}
pub(crate) fn refresh_dirty_subtrees(&mut self) -> Vec<(u64, Box<Node>)> {
let mut replacements = Vec::new();
let dirty_child_slot_ids = self.take_dirty_child_slot_ids();
for dirty_slot_id in dirty_child_slot_ids {
let Some(index) = self.children.iter().position(|slot| slot.id == dirty_slot_id) else {
continue;
};
let handed = self.fresh_slot_children(index);
let slot = &mut self.children[index];
if !slot.ctx.any_dirty() {
continue;
}
if slot.offstage {
slot.offstage_dirty = true;
slot.ctx.clear_dirty();
continue;
}
if let Some(handed) = handed {
slot.ctx.slot_children = Some(handed);
}
if slot.ctx.is_dirty() {
#[cfg(feature = "perf_profile")]
let _span = slot.ctx.render_span(|| None);
let old_rendered = slot.rendered.take();
slot.ctx.begin_render();
let mut element = slot.component.render(&mut slot.ctx);
slot.ctx.end_render();
let needs_followup_refresh = slot.ctx.any_dirty();
element.node = attach_component_metadata(
element.node,
slot.component.tag_name(),
slot.id,
slot.key.as_deref(),
#[cfg(feature = "devtools")]
&slot.ctx,
);
if let Some(old) = old_rendered.as_ref() {
element.node.preserve_runtime_state_from(old);
}
slot.keep_rendered(&element.node);
replacements.push((slot.id, element.node));
if needs_followup_refresh {
Self::mark_dirty_child_slot(&self.dirty_child_slots, dirty_slot_id);
}
continue;
} else {
replacements.extend(slot.ctx.refresh_dirty_subtrees());
}
}
self.update_subtree_dirty_from_children();
replacements
}
fn fresh_slot_children(&self, index: usize) -> Option<Vec<Element>> {
let holder = &self.children[index];
if !holder.ctx.is_dirty() || holder.offstage {
return None;
}
let handed = holder.ctx.slot_children.as_ref()?;
if !self.any_slot_rendered_after(holder.rendered_at, holder.id) {
return None;
}
let mut any = false;
let mut fresh = |root: &Node| -> Option<Node> {
let id = root.component_slot_id()?;
let slot = self.descendant_slot(id).filter(|slot| slot.id != holder.id)?;
if slot.rendered_at <= holder.rendered_at {
return None;
}
let mut output = slot.assemble(self, None)?;
output.adopt_outer_decoration(root);
any = true;
Some(output)
};
let children: Vec<Element> = handed
.iter()
.map(|child| {
let node = match fresh(&child.node) {
Some(node) => node,
None => child.node.clone_with_component_roots(&mut fresh),
};
Element::from_node(node)
})
.collect();
any.then_some(children)
}
fn any_slot_rendered_after(&self, since: u64, but: u64) -> bool {
self
.children
.iter()
.filter(|slot| slot.id != but)
.any(|slot| slot.rendered_at > since || slot.ctx.any_slot_rendered_after(since, but))
}
fn child_slot(&self, id: u64) -> Option<&ChildSlot> {
for slot in &self.children {
if slot.id == id {
return Some(slot);
}
if slot.is_for_each_group()
&& let Some(item) = slot.ctx.children.iter().find(|item| item.id == id)
{
return Some(item);
}
}
None
}
fn descendant_slot(&self, id: u64) -> Option<&ChildSlot> {
for slot in &self.children {
if slot.id == id {
return Some(slot);
}
if let Some(found) = slot.ctx.descendant_slot(id) {
return Some(found);
}
}
None
}
#[cfg_attr(not(feature = "perf_profile"), allow(dead_code))]
pub(crate) fn estimated_memory_bytes(&self) -> usize {
std::mem::size_of::<Self>()
+ self
.props
.as_ref()
.map(|_| std::mem::size_of::<Box<dyn Any + Send>>())
.unwrap_or(0)
+ self.children.capacity() * std::mem::size_of::<ChildSlot>()
+ self.watch_handles.capacity() * std::mem::size_of::<Box<dyn Any + Send + Sync>>()
+ self.render_watch_handles.capacity() * std::mem::size_of::<Box<dyn Any + Send + Sync>>()
+ {
#[cfg(feature = "devtools")]
{
self.memos_debug.capacity() * std::mem::size_of::<ComponentMemoDebug>()
+ self.effects_debug.capacity() * std::mem::size_of::<ComponentEffectDebug>()
}
#[cfg(not(feature = "devtools"))]
{
0
}
}
+ self.effects.capacity() * std::mem::size_of::<Effect>()
+ self.timers.capacity() * std::mem::size_of::<Timer>()
+ self.frames.capacity_bytes()
+ (self.future_slots.capacity() + self.stable_future_slots.capacity()) * std::mem::size_of::<FutureSlot>()
+ self.element_refs.capacity() * std::mem::size_of::<ElementRefMut>()
+ self.click_outside_active_cursors.capacity() * std::mem::size_of::<usize>()
+ self
.children
.iter()
.map(ChildSlot::estimated_memory_bytes)
.sum::<usize>()
}
}
#[derive(Clone, Copy, PartialEq, Eq)]
enum Inherited {
Same,
Refreshed,
Changed,
}
fn element_ref_is_click_outside(element_ref: &ElementRef, x: f32, y: f32) -> bool {
if !element_ref.is_attached() {
return false;
}
let rect = element_ref.bounds();
x < rect.x || x > rect.x + rect.width || y < rect.y || y > rect.y + rect.height
}
struct ForEachSlot;
impl ForEachSlot {
const TYPE_NAME: &'static str = "ForEachSlot";
}
impl AnyComponent for ForEachSlot {
fn render(&self, _ctx: &mut Ctx) -> Element {
Element::new()
}
fn after_layout(&self) {}
fn on_mounted(&self) {}
fn on_unmounted(&self) {}
fn type_name(&self) -> &'static str {
Self::TYPE_NAME
}
fn tag_name(&self) -> Arc<str> {
Arc::from("ForEachSlot")
}
}
impl ChildSlot {
#[cfg_attr(not(feature = "perf_profile"), allow(dead_code))]
fn estimated_memory_bytes(&self) -> usize {
self.key.as_ref().map(|key| key.capacity()).unwrap_or(0)
+ std::mem::size_of::<Box<dyn AnyComponent>>()
+ self.ctx.estimated_memory_bytes()
}
fn is_for_each_group(&self) -> bool {
self.key.is_none() && self.component.type_name() == ForEachSlot::TYPE_NAME
}
fn render_as_it_leaves(&mut self) {
if !self.ctx.is_dirty() {
let _ = self.ctx.refresh_dirty_subtrees();
return;
}
let previous = self.rendered.take();
self.ctx.begin_render();
let mut element = self.component.render(&mut self.ctx);
self.ctx.end_render();
element.node = attach_component_metadata(
element.node,
self.component.tag_name(),
self.id,
self.key.as_deref(),
#[cfg(feature = "devtools")]
&self.ctx,
);
if let Some(previous) = previous.as_ref() {
element.node.preserve_runtime_state_from(previous);
}
self.keep_rendered(&element.node);
}
fn keep_rendered(&mut self, node: &Node) {
let mut own = Vec::new();
for child in &self.ctx.children {
if child.is_for_each_group() {
own.extend(child.ctx.children.iter().map(|item| item.id));
} else {
own.push(child.id);
}
}
own.sort_unstable();
self.rendered = Some(node.clone_own_output(&|id| own.binary_search(&id).is_ok()));
self.rendered_at = next_render_generation();
}
fn assembled_output(&self) -> Option<Box<Node>> {
self.assemble(&self.ctx, None).map(Box::new)
}
fn assemble(&self, scope: &Ctx, enclosing: Option<&SlotOwners<'_>>) -> Option<Node> {
let rendered = self.rendered.as_deref()?;
let owners = SlotOwners {
ctx: &self.ctx,
enclosing,
};
Some(rendered.clone_with_component_roots(&mut |root: &Node| {
let id = root.component_slot_id()?;
if root.is_component_slot_placeholder() {
let mut output = self.ctx.child_slot(id)?.assemble(scope, Some(&owners))?;
output.adopt_outer_decoration(root);
return Some(output);
}
let slot = enclosing
.and_then(|owners| owners.slot(id))
.or_else(|| scope.descendant_slot(id))?;
if slot.rendered_at <= self.rendered_at {
return None;
}
let mut output = slot.assemble(scope, Some(&owners))?;
output.adopt_outer_decoration(root);
Some(output)
}))
}
}
struct SlotOwners<'a> {
ctx: &'a Ctx,
enclosing: Option<&'a SlotOwners<'a>>,
}
impl<'a> SlotOwners<'a> {
fn slot(&self, id: u64) -> Option<&'a ChildSlot> {
self
.ctx
.child_slot(id)
.or_else(|| self.enclosing.and_then(|owners| owners.slot(id)))
}
}
impl Drop for Ctx {
fn drop(&mut self) {
for slot in &self.children {
if slot.mounted {
slot.component.on_unmounted();
}
}
}
}
#[cfg(test)]
mod tests {
use super::Ctx;
use crate::{
app::component::Component,
components::{Column, Rect},
node::Element,
};
struct CachedChild;
impl Component for CachedChild {
type Props = ();
fn create(_ctx: &mut Ctx) -> Self {
Self
}
fn render(&self, _ctx: &mut Ctx) -> impl Into<Element> {
Column::new()
.child(Rect::new(1.0, 1.0))
.child(Rect::new(2.0, 2.0))
.child(Rect::new(3.0, 3.0))
}
}
struct CachedParent;
impl Component for CachedParent {
type Props = ();
fn create(_ctx: &mut Ctx) -> Self {
Self
}
fn render(&self, ctx: &mut Ctx) -> impl Into<Element> {
Column::new().child(ctx.mount::<CachedChild>(()).id("child"))
}
}
#[test]
fn a_slot_keeps_a_placeholder_for_each_component_it_mounted() {
let mut root = Ctx::new_root();
let output = root.mount::<CachedParent>(());
assert_eq!(output.node.children()[0].children().len(), 3);
let parent = &root.children[0];
let kept = &parent
.rendered
.as_deref()
.expect("the parent kept its output")
.children()[0];
assert!(kept.is_component_slot_placeholder());
assert!(kept.children().is_empty());
assert_eq!(kept.component_slot_id(), Some(parent.ctx.children[0].id));
let assembled = parent.assembled_output().expect("the parent's output");
let child = &assembled.children()[0];
assert!(!child.is_component_slot_placeholder());
assert_eq!(child.children().len(), 3);
assert_eq!(child.element_id(), Some("child"), "the mounting code's id is put back");
}
#[test]
fn batch_defers_dirty_marking_until_end() {
let mut ctx = Ctx::new_root();
let signal = ctx.signal(0);
ctx.clear_dirty();
ctx.batch(|| {
signal.set(1);
assert!(!ctx.is_dirty());
assert!(!ctx.any_dirty());
});
assert!(ctx.is_dirty());
assert!(ctx.any_dirty());
}
#[test]
fn dirty_mark_during_render_survives_end_render() {
let mut ctx = Ctx::new_root();
let signal = ctx.signal(0);
ctx.clear_dirty();
ctx.begin_render();
signal.set(1);
ctx.end_render();
assert!(ctx.is_dirty());
assert!(ctx.any_dirty());
}
#[test]
fn dirty_mark_for_previous_render_dependency_survives_rerender() {
let mut ctx = Ctx::new_root();
let signal = ctx.signal(0);
ctx.begin_render();
let _ = signal.get();
ctx.end_render();
ctx.clear_dirty();
ctx.begin_render();
let _ = signal.get();
signal.set(1);
ctx.end_render();
assert!(ctx.is_dirty());
assert!(ctx.any_dirty());
}
#[test]
fn descendant_dirty_marks_ancestor_subtrees_without_marking_ancestors_dirty() {
let root = Ctx::new_root();
let mut child = Ctx::new();
child.inherit_dirty_ancestors_from(&root, 10);
let mut grandchild = Ctx::new();
grandchild.inherit_dirty_ancestors_from(&child, 20);
root.clear_dirty();
child.clear_dirty();
grandchild.clear_dirty();
grandchild.mark_self_dirty();
assert!(!root.is_dirty());
assert!(!child.is_dirty());
assert!(grandchild.is_dirty());
assert!(root.any_dirty());
assert!(child.any_dirty());
assert!(grandchild.any_dirty());
assert_eq!(*root.dirty_child_slots.lock(), vec![10]);
assert_eq!(*child.dirty_child_slots.lock(), vec![20]);
grandchild.clear_dirty();
let _ = child.take_dirty_child_slot_ids();
let _ = root.take_dirty_child_slot_ids();
child.update_subtree_dirty_from_children();
root.update_subtree_dirty_from_children();
assert!(!root.any_dirty());
assert!(!child.any_dirty());
assert!(!grandchild.any_dirty());
}
#[cfg(feature = "devtools")]
#[test]
fn signal_debug_value_updates_when_signal_changes() {
let mut ctx = Ctx::new_root();
let signal = ctx.signal(0_i32);
let debug = ctx.signals_debug();
assert_eq!(debug[0].formatted_value().as_deref(), Some("0"));
signal.set(42);
assert_eq!(debug[0].formatted_value().as_deref(), Some("42"));
}
#[cfg(feature = "devtools")]
#[test]
fn signal_debug_history_records_value_changes() {
let mut ctx = Ctx::new_root();
let signal = ctx.signal(0_i32);
let debug = ctx.signals_debug();
signal.set(1);
signal.set(2);
let history = debug[0].history();
assert_eq!(history.len(), 2);
assert_eq!(history[0].from_value, "0");
assert_eq!(history[0].to_value, "1");
assert_eq!(history[1].from_value, "1");
assert_eq!(history[1].to_value, "2");
}
#[cfg(feature = "devtools")]
#[test]
fn signal_debug_subscriber_count_excludes_runtime_and_debug_hooks() {
let mut ctx = Ctx::new_root();
let signal = ctx.signal(0_i32);
let debug = ctx.signals_debug();
assert_eq!(debug[0].subscriber_count(), 0);
ctx.watch(&signal, |_| {});
assert_eq!(debug[0].subscriber_count(), 1);
let memo_signal = signal.clone();
let _memo = ctx.memo(move || memo_signal.get() + 1);
assert_eq!(debug[0].subscriber_count(), 2);
}
#[cfg(feature = "devtools")]
#[derive(crate::DevtoolsInspectable)]
struct DebugSignalValue {
count: i32,
active: bool,
}
#[cfg(feature = "devtools")]
#[test]
fn signal_debug_value_uses_devtools_inspectable_fields() {
let mut ctx = Ctx::new_root();
let signal = ctx.signal(DebugSignalValue { count: 2, active: true });
let debug = ctx.signals_debug();
assert_eq!(debug[0].formatted_value().as_deref(), Some("count: 2, active: true"));
signal.set(DebugSignalValue {
count: 3,
active: false,
});
assert_eq!(debug[0].formatted_value().as_deref(), Some("count: 3, active: false"));
}
#[cfg(feature = "devtools")]
#[test]
fn memo_debug_value_updates_when_dependencies_change() {
let mut ctx = Ctx::new_root();
let signal = ctx.signal(2_i32);
let memo_signal = signal.clone();
let _memo = ctx.memo(move || memo_signal.get() * 2);
let debug = ctx.memos_debug();
assert_eq!(debug[0].formatted_value().as_deref(), Some("4"));
signal.set(3);
assert_eq!(debug[0].formatted_value().as_deref(), Some("6"));
}
}