1use crate::{build_context::BuildCtx, GlobalState, View};
2use std::any::{type_name, Any, TypeId};
3use std::cell::RefCell;
4use std::collections::{HashMap, HashSet};
5use std::marker::PhantomData;
6
7type NextPortalSeq = unsafe fn(*mut ()) -> u64;
8type RegisterRuntimeReducer = unsafe fn(*mut (), crate::ActionId, crate::BoxedReducer);
9
10struct BuildScope {
11 state_type: TypeId,
12 state_name: &'static str,
13 ctx: *mut (),
14 view: *const (),
15 resources: *mut crate::registry::ResourceRegistry,
16 animation_requests: *mut Vec<(crate::WidgetId, crate::AnimationRequest)>,
17 video_nodes: *mut Vec<crate::registry::VideoRegistration>,
18 web_nodes: *mut Vec<crate::registry::WebRegistration>,
19 portals: *mut Vec<crate::registry::PortalEntry>,
20 next_portal_seq: NextPortalSeq,
21 register_runtime_reducer: RegisterRuntimeReducer,
22 runtime: *const crate::RuntimeState,
23 env: *const crate::Env,
24 layout: Option<*const crate::LayoutSnapshot>,
25 local_state_ordinals: HashMap<(&'static str, &'static str), usize>,
26 local_state_seen: HashSet<crate::state::LocalStateKey>,
27 widget_id_stack: Vec<crate::WidgetId>,
28 providers: HashMap<TypeId, Vec<Box<dyn Any + Send + Sync>>>,
29}
30
31thread_local! {
32 static BUILD_SCOPES: RefCell<Vec<BuildScope>> = const { RefCell::new(Vec::new()) };
33}
34
35#[derive(Debug)]
36pub struct BuildCtxHandle<S: GlobalState> {
37 _state: PhantomData<fn() -> S>,
38}
39
40impl<S: GlobalState> Clone for BuildCtxHandle<S> {
41 fn clone(&self) -> Self {
42 *self
43 }
44}
45
46impl<S: GlobalState> Copy for BuildCtxHandle<S> {}
47
48#[derive(Debug)]
49pub struct ViewHandle<S: GlobalState> {
50 _state: PhantomData<fn() -> S>,
51}
52
53impl<S: GlobalState> Clone for ViewHandle<S> {
54 fn clone(&self) -> Self {
55 *self
56 }
57}
58
59impl<S: GlobalState> Copy for ViewHandle<S> {}
60
61#[doc(hidden)]
62pub fn enter<S, R>(ctx: &mut BuildCtx<S>, view: &View<'_, S>, f: impl FnOnce() -> R) -> R
63where
64 S: GlobalState,
65{
66 BUILD_SCOPES.with(|scopes| {
67 scopes.borrow_mut().push(BuildScope {
68 state_type: TypeId::of::<S>(),
69 state_name: type_name::<S>(),
70 ctx: (ctx as *mut BuildCtx<S>).cast::<()>(),
71 view: (view as *const View<'_, S>).cast::<()>(),
72 resources: &mut ctx.resources,
73 animation_requests: &mut ctx.animation_requests,
74 video_nodes: &mut ctx.video_nodes,
75 web_nodes: &mut ctx.web_nodes,
76 portals: &mut ctx.portals,
77 next_portal_seq: next_portal_seq::<S>,
78 register_runtime_reducer: register_runtime_reducer::<S>,
79 runtime: view.runtime(),
80 env: view.env(),
81 layout: view
82 .layout()
83 .map(|layout| layout as *const crate::LayoutSnapshot),
84 local_state_ordinals: HashMap::new(),
85 local_state_seen: HashSet::new(),
86 widget_id_stack: Vec::new(),
87 providers: HashMap::new(),
88 });
89 });
90
91 struct PopGuard;
92 impl Drop for PopGuard {
93 fn drop(&mut self) {
94 BUILD_SCOPES.with(|scopes| {
95 let mut scopes = scopes.borrow_mut();
96 let Some(scope) = scopes.pop() else {
97 return;
98 };
99 if scopes.is_empty() {
100 unsafe {
101 (*scope.runtime)
102 .local_widget_state
103 .retain_active(&scope.local_state_seen);
104 }
105 } else if let Some(parent) = scopes.last_mut() {
106 parent.local_state_seen.extend(scope.local_state_seen);
107 }
108 });
109 }
110 }
111
112 let _guard = PopGuard;
113 f()
114}
115
116unsafe fn next_portal_seq<S: GlobalState>(ctx: *mut ()) -> u64 {
117 let ctx = unsafe { &mut *ctx.cast::<BuildCtx<S>>() };
118 ctx.portal_seq_for_scoped_build()
119}
120
121unsafe fn register_runtime_reducer<S: GlobalState>(
122 ctx: *mut (),
123 action_id: crate::ActionId,
124 reducer: crate::BoxedReducer,
125) {
126 let ctx = unsafe { &mut *ctx.cast::<BuildCtx<S>>() };
127 ctx.register_runtime_reducer(action_id, reducer);
128}
129
130pub(crate) fn resolve_local_state<T>(
131 component: &'static str,
132 field: &'static str,
133 make_default: impl FnOnce() -> T,
134) -> crate::StateField<T>
135where
136 T: Clone + Send + Sync + 'static,
137{
138 let (runtime, key) = BUILD_SCOPES.with(|scopes| {
139 let mut scopes = scopes.borrow_mut();
140 let Some(scope) = scopes.last_mut() else {
141 panic!(
142 "Fission local widget state field `{}` on `{}` was accessed outside an active build pass",
143 field, component
144 );
145 };
146
147 let key_path = scope
148 .widget_id_stack
149 .iter()
150 .map(|id| id.as_u128().to_string())
151 .collect::<Vec<_>>();
152 let ordinal = if key_path.is_empty() {
153 let next = scope
154 .local_state_ordinals
155 .entry((component, field))
156 .and_modify(|next| *next += 1)
157 .or_insert(0);
158 *next
159 } else {
160 0
161 };
162 let key = crate::state::LocalStateKey::new_scoped(component, field, key_path, ordinal);
163 if !scope.local_state_seen.insert(key.clone()) {
164 panic!(
165 "Duplicate Fission local widget state identity for `{}` on `{}`.",
166 field, component
167 );
168 }
169 (scope.runtime, key)
170 });
171 let value = unsafe { &*runtime }
172 .local_widget_state
173 .get_or_insert_with(key.clone(), make_default);
174 crate::StateField::resolved(key, value)
175}
176
177pub fn with_widget_id<R>(id: crate::WidgetId, f: impl FnOnce() -> R) -> R {
178 let pushed = BUILD_SCOPES.with(|scopes| {
179 let mut scopes = scopes.borrow_mut();
180 if let Some(scope) = scopes.last_mut() {
181 scope.widget_id_stack.push(id);
182 true
183 } else {
184 false
185 }
186 });
187
188 struct PopGuard(bool);
189 impl Drop for PopGuard {
190 fn drop(&mut self) {
191 if self.0 {
192 BUILD_SCOPES.with(|scopes| {
193 if let Some(scope) = scopes.borrow_mut().last_mut() {
194 scope.widget_id_stack.pop();
195 }
196 });
197 }
198 }
199 }
200
201 let _guard = PopGuard(pushed);
202 f()
203}
204
205pub fn current_widget_id() -> Option<crate::WidgetId> {
206 BUILD_SCOPES.with(|scopes| {
207 scopes
208 .borrow()
209 .last()
210 .and_then(|scope| scope.widget_id_stack.last().copied())
211 })
212}
213
214pub fn provide<T, R>(value: T, f: impl FnOnce() -> R) -> R
215where
216 T: Clone + Send + Sync + 'static,
217{
218 BUILD_SCOPES.with(|scopes| {
219 let mut scopes = scopes.borrow_mut();
220 let Some(scope) = scopes.last_mut() else {
221 panic!(
222 "Fission build provider `{}` was installed outside an active build pass",
223 type_name::<T>()
224 );
225 };
226 scope
227 .providers
228 .entry(TypeId::of::<T>())
229 .or_default()
230 .push(Box::new(value));
231 });
232
233 struct PopGuard<T: 'static> {
234 _provider: PhantomData<T>,
235 }
236 impl<T: 'static> Drop for PopGuard<T> {
237 fn drop(&mut self) {
238 BUILD_SCOPES.with(|scopes| {
239 if let Some(scope) = scopes.borrow_mut().last_mut() {
240 let provider_type = TypeId::of::<T>();
241 if let Some(values) = scope.providers.get_mut(&provider_type) {
242 values.pop();
243 if values.is_empty() {
244 scope.providers.remove(&provider_type);
245 }
246 }
247 }
248 });
249 }
250 }
251
252 let _guard = PopGuard::<T> {
253 _provider: PhantomData,
254 };
255 f()
256}
257
258pub fn try_read<T>() -> Option<T>
259where
260 T: Clone + Send + Sync + 'static,
261{
262 BUILD_SCOPES.with(|scopes| {
263 let scopes = scopes.borrow();
264 scopes.iter().rev().find_map(|scope| {
265 scope
266 .providers
267 .get(&TypeId::of::<T>())
268 .and_then(|values| values.last())
269 .and_then(|value| value.downcast_ref::<T>())
270 .cloned()
271 })
272 })
273}
274
275pub fn read<T>() -> T
276where
277 T: Clone + Send + Sync + 'static,
278{
279 try_read::<T>().unwrap_or_else(|| {
280 panic!(
281 "Fission build provider `{}` was not found in the active build scope",
282 type_name::<T>()
283 )
284 })
285}
286
287pub fn current<S>() -> (BuildCtxHandle<S>, ViewHandle<S>)
288where
289 S: GlobalState,
290{
291 assert_current_scope::<S>();
292 (
293 BuildCtxHandle {
294 _state: PhantomData,
295 },
296 ViewHandle {
297 _state: PhantomData,
298 },
299 )
300}
301
302pub fn try_register_video(registration: crate::registry::VideoRegistration) {
303 let video_nodes =
304 BUILD_SCOPES.with(|scopes| scopes.borrow().last().map(|scope| scope.video_nodes));
305 if let Some(video_nodes) = video_nodes {
306 unsafe {
307 (*video_nodes).push(registration);
308 }
309 }
310}
311
312fn requested_common_scope<S: GlobalState>() -> bool {
313 TypeId::of::<S>() == TypeId::of::<()>()
314}
315
316fn assert_current_scope<S: GlobalState>() {
317 BUILD_SCOPES.with(|scopes| {
318 let scopes = scopes.borrow();
319 if requested_common_scope::<S>() {
320 if scopes.is_empty() {
321 panic!(
322 "Fission build context for `{}` requested outside an active build pass",
323 type_name::<S>()
324 );
325 }
326 return;
327 }
328
329 let Some(scope) = scopes
330 .iter()
331 .rev()
332 .find(|scope| scope.state_type == TypeId::of::<S>())
333 else {
334 panic!(
335 "Fission build context for `{}` requested outside an active build pass",
336 type_name::<S>()
337 );
338 };
339 let _ = scope.state_name;
340 });
341}
342
343fn exact_scope_index<S: GlobalState>(scopes: &[BuildScope]) -> Option<usize> {
344 scopes
345 .iter()
346 .enumerate()
347 .rev()
348 .find_map(|(index, scope)| (scope.state_type == TypeId::of::<S>()).then_some(index))
349}
350
351impl<S: GlobalState> BuildCtxHandle<S> {
352 fn with_exact_ctx<R>(&self, f: impl FnOnce(&mut BuildCtx<S>) -> R) -> R {
353 let ctx = BUILD_SCOPES.with(|scopes| {
354 let scopes = scopes.borrow();
355 let Some(index) = exact_scope_index::<S>(&scopes) else {
356 panic!(
357 "Fission build context for `{}` requested outside an active build pass",
358 type_name::<S>()
359 );
360 };
361 scopes[index].ctx.cast::<BuildCtx<S>>()
362 });
363 unsafe { f(&mut *ctx) }
367 }
368
369 pub fn bind<A, H>(&self, action: A, handler: H) -> crate::ActionEnvelope
370 where
371 A: crate::Action,
372 H: crate::registry::IntoHandler<S, A> + Send + Sync + 'static,
373 {
374 self.with_exact_ctx(|ctx| ctx.bind(action, handler))
375 }
376
377 pub fn register<A, H>(&self, handler: H)
378 where
379 A: crate::Action,
380 H: crate::registry::IntoHandler<S, A> + Send + Sync + 'static,
381 {
382 self.with_exact_ctx(|ctx| ctx.register::<A, H>(handler));
383 }
384
385 pub fn bind_local<T, A, H>(
386 &self,
387 action: A,
388 field: crate::StateField<T>,
389 handler: H,
390 ) -> crate::ActionEnvelope
391 where
392 T: crate::GlobalState + Clone + 'static,
393 A: crate::Action,
394 H: crate::registry::IntoHandler<T, A> + Send + Sync + 'static,
395 {
396 let action_id = field.action_id::<A>();
397 let field_key = field.key().clone();
398 let reducer: crate::BoxedReducer = Box::new(
399 move |app_states,
400 envelope: &crate::ActionEnvelope,
401 _target,
402 _effects,
403 _input|
404 -> anyhow::Result<()> {
405 let action: A = serde_json::from_slice(&envelope.payload).map_err(|error| {
406 anyhow::anyhow!("Failed to deserialize local action: {error}")
407 })?;
408 let Some(store) = app_states
409 .get_mut(&TypeId::of::<crate::state::LocalStateStore>())
410 .and_then(|state| state.downcast_mut::<crate::state::LocalStateStore>())
411 else {
412 anyhow::bail!("Fission local widget state store is not registered in Runtime");
413 };
414 let mut effects_builder = crate::Effects::<T>::new_headless(0);
415 let mut reducer_ctx = crate::ReducerContext {
416 effects: &mut effects_builder,
417 input: _input,
418 };
419 store.update::<T>(&field_key, |value| {
420 handler.call(value, action, &mut reducer_ctx)
421 })?;
422 _effects.extend(effects_builder.out);
423 Ok(())
424 },
425 );
426
427 let (ctx, register_runtime_reducer) = BUILD_SCOPES.with(|scopes| {
428 let scopes = scopes.borrow();
429 let Some(scope) = scopes.last() else {
430 panic!(
431 "Fission build context for `{}` requested outside an active build pass",
432 type_name::<S>()
433 );
434 };
435 (scope.ctx, scope.register_runtime_reducer)
436 });
437 unsafe {
438 register_runtime_reducer(ctx, action_id, reducer);
439 }
440
441 crate::ActionEnvelope {
442 id: action_id,
443 payload: action.encode(),
444 }
445 }
446
447 pub fn request_animation_for(&self, target: crate::WidgetId, request: crate::AnimationRequest) {
448 let animation_requests = BUILD_SCOPES.with(|scopes| {
449 let scopes = scopes.borrow();
450 let Some(scope) = scopes.last() else {
451 panic!(
452 "Fission build context for `{}` requested outside an active build pass",
453 type_name::<S>()
454 );
455 };
456 scope.animation_requests
457 });
458 unsafe {
459 (*animation_requests).push((target, request));
460 }
461 }
462
463 pub fn register_video(&self, registration: crate::registry::VideoRegistration) {
464 let video_nodes = BUILD_SCOPES.with(|scopes| {
465 let scopes = scopes.borrow();
466 let Some(scope) = scopes.last() else {
467 panic!(
468 "Fission build context for `{}` requested outside an active build pass",
469 type_name::<S>()
470 );
471 };
472 scope.video_nodes
473 });
474 unsafe {
475 (*video_nodes).push(registration);
476 }
477 }
478
479 pub fn register_web_view(&self, registration: crate::registry::WebRegistration) {
480 let web_nodes = BUILD_SCOPES.with(|scopes| {
481 let scopes = scopes.borrow();
482 let Some(scope) = scopes.last() else {
483 panic!(
484 "Fission build context for `{}` requested outside an active build pass",
485 type_name::<S>()
486 );
487 };
488 scope.web_nodes
489 });
490 unsafe {
491 (*web_nodes).push(registration);
492 }
493 }
494
495 pub fn with_resources<R>(
496 &self,
497 f: impl FnOnce(&mut crate::registry::ResourceRegistry) -> R,
498 ) -> R {
499 let resources = BUILD_SCOPES.with(|scopes| {
500 let scopes = scopes.borrow();
501 let Some(scope) = scopes.last() else {
502 panic!(
503 "Fission build context for `{}` requested outside an active build pass",
504 type_name::<S>()
505 );
506 };
507 scope.resources
508 });
509 unsafe { f(&mut *resources) }
510 }
511
512 pub fn register_portal(&self, node: crate::Widget) {
513 self.register_portal_with_layer(crate::PortalLayer::Default, None, node);
514 }
515
516 pub fn register_portal_with_id(&self, id: crate::WidgetId, node: crate::Widget) {
517 self.register_portal_with_layer(crate::PortalLayer::Default, Some(id), node);
518 }
519
520 pub fn register_portal_with_layer(
521 &self,
522 layer: crate::PortalLayer,
523 id: Option<crate::WidgetId>,
524 node: crate::Widget,
525 ) {
526 let (ctx, portals, next_portal_seq) = BUILD_SCOPES.with(|scopes| {
527 let scopes = scopes.borrow();
528 let Some(scope) = scopes.last() else {
529 panic!(
530 "Fission build context for `{}` requested outside an active build pass",
531 type_name::<S>()
532 );
533 };
534 (scope.ctx, scope.portals, scope.next_portal_seq)
535 });
536 unsafe {
537 let seq = next_portal_seq(ctx);
538 (*portals).push(crate::registry::PortalEntry {
539 layer,
540 seq,
541 id,
542 node,
543 });
544 }
545 }
546
547 pub fn anim_for(&self, target: crate::WidgetId) -> ScopedAnimCtx<S> {
548 ScopedAnimCtx {
549 target,
550 _state: PhantomData,
551 }
552 }
553
554 pub fn video_controls(&self, target: crate::WidgetId) -> crate::registry::VideoControlCtx {
555 self.with_exact_ctx(|ctx| ctx.video_controls(target))
556 }
557}
558
559impl<S: GlobalState> ViewHandle<S> {
560 fn with_common_scope<R>(&self, f: impl FnOnce(&BuildScope) -> R) -> R {
561 BUILD_SCOPES.with(|scopes| {
562 let scopes = scopes.borrow();
563 let Some(scope) = scopes.last() else {
564 panic!(
565 "Fission view for `{}` requested outside an active build pass",
566 type_name::<S>()
567 );
568 };
569 f(scope)
570 })
571 }
572
573 pub fn state(&self) -> &S {
574 BUILD_SCOPES.with(|scopes| {
575 let scopes = scopes.borrow();
576 let Some(index) = exact_scope_index::<S>(&scopes) else {
577 panic!(
578 "Fission view state for `{}` requested outside an active build pass",
579 type_name::<S>()
580 );
581 };
582 unsafe { (*scopes[index].view.cast::<View<'_, S>>()).state }
583 })
584 }
585
586 pub fn runtime(&self) -> &crate::RuntimeState {
587 self.with_common_scope(|scope| unsafe { &*scope.runtime })
588 }
589
590 pub fn env(&self) -> &crate::Env {
591 self.with_common_scope(|scope| unsafe { &*scope.env })
592 }
593
594 pub fn layout(&self) -> Option<&crate::LayoutSnapshot> {
595 self.with_common_scope(|scope| unsafe { scope.layout.map(|layout| &*layout) })
596 }
597
598 pub fn theme(&self) -> &fission_theme::Theme {
599 &self.env().theme
600 }
601
602 pub fn i18n(&self) -> &fission_i18n::I18nRegistry {
603 &self.env().i18n
604 }
605
606 pub fn get_rect(&self, id: crate::WidgetId) -> Option<crate::LayoutRect> {
607 let node_id: fission_ir::WidgetId = id.into();
608 self.layout()
609 .and_then(|layout| layout.get_node_rect(node_id))
610 }
611
612 pub fn get_constraints(&self, id: crate::WidgetId) -> Option<crate::BoxConstraints> {
613 let node_id: fission_ir::WidgetId = id.into();
614 self.layout()
615 .and_then(|layout| layout.get_node_constraints(node_id))
616 }
617
618 pub fn viewport_size(&self) -> crate::LayoutSize {
619 self.env().viewport_size
620 }
621
622 pub fn select<R>(&self, selector: impl FnOnce(&S) -> R) -> R {
623 selector(self.state())
624 }
625
626 pub fn select_with<T: crate::view::Selector<S>>(&self) -> T::Output {
627 T::select(*self)
628 }
629
630 pub fn global(&self) -> <S as crate::view::FissionViewField>::View<'_>
631 where
632 S: crate::view::FissionViewField,
633 {
634 <S as crate::view::FissionViewField>::view_field(self.state())
635 }
636
637 pub fn animation_value(
638 &self,
639 widget_id: crate::WidgetId,
640 property: &crate::AnimationPropertyId,
641 ) -> f32 {
642 self.runtime()
643 .animation
644 .values
645 .get(&(widget_id, property.clone()))
646 .copied()
647 .unwrap_or_else(|| property.default_value())
648 }
649
650 pub fn video_state(&self, widget_id: crate::WidgetId) -> Option<&crate::env::VideoState> {
651 self.runtime().video.states.get(&widget_id)
652 }
653}
654
655#[derive(Clone, Copy, Debug)]
656pub struct ScopedAnimCtx<S: GlobalState> {
657 target: crate::WidgetId,
658 _state: PhantomData<fn() -> S>,
659}
660
661impl<S: GlobalState> ScopedAnimCtx<S> {
662 pub fn request(&mut self, request: crate::AnimationRequest) {
663 let (ctx, _) = current::<S>();
664 ctx.request_animation_for(self.target, request);
665 }
666
667 pub fn request_for(&mut self, target: crate::WidgetId, request: crate::AnimationRequest) {
668 let (ctx, _) = current::<S>();
669 ctx.request_animation_for(target, request);
670 }
671}