haalka 0.7.1

ergonomic reactive Bevy UI library powered by FRP signals
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
//! [haalka](crate)'s core abstraction, allowing one to rig any [`Entity`] with ergonomic
//! [`futures_signals::Signal`](https://docs.rs/futures-signals/latest/futures_signals/signal/trait.Signal.html) driven reactivity, including
//! methods for registering children, [`Component`]s, event listeners (via observers), and
//! [`System`]s all using a declarative builder pattern/[fluent interface](https://en.wikipedia.org/wiki/Fluent_interface).
//! Port of [MoonZoon](https://github.com/MoonZoon/MoonZoon)'s [`raw_el`](https://github.com/MoonZoon/MoonZoon/tree/fc73b0d90bf39be72e70fdcab4f319ea5b8e6cfc/crates/zoon/src/element/raw_el).

use std::{
    future::Future,
    mem,
    sync::{Arc, OnceLock},
};

use super::{
    node_builder::{NodeBuilder, TaskHolder, async_world},
    raw::utils::remove_system_holder_on_remove,
};
use apply::Apply;
use bevy_ecs::{component::*, error::*, lifecycle::HookContext, prelude::*, system::*, world::*};
use bevy_log::error;
use bevy_tasks::Task;
use bevy_utils::prelude::*;
use enclose::enclose as clone;
use futures_signals::{
    signal::{Signal, SignalExt},
    signal_vec::{SignalVec, SignalVecExt},
};

/// A thin layer over a [`NodeBuilder`] that exposes higher level ECS related methods.
/// Port of [MoonZoon](https://github.com/MoonZoon/MoonZoon)'s [`RawHtmlElement`](https://github.com/MoonZoon/MoonZoon/blob/fc73b0d90bf39be72e70fdcab4f319ea5b8e6cfc/crates/zoon/src/element/raw_el/raw_html_el.rs).
pub struct RawHaalkaEl {
    node_builder: Option<NodeBuilder>,
    deferred_updaters: Vec<Box<dyn FnOnce(RawHaalkaEl) -> RawHaalkaEl + Send + 'static>>,
}

impl From<NodeBuilder> for RawHaalkaEl {
    fn from(node_builder: NodeBuilder) -> Self {
        Self {
            node_builder: Some(node_builder),
            ..Self::new_dummy()
        }
    }
}

impl<NodeType: Bundle> From<NodeType> for RawHaalkaEl {
    fn from(node_bundle: NodeType) -> Self {
        Self {
            node_builder: Some(NodeBuilder::from(node_bundle)),
            ..Self::new_dummy()
        }
    }
}

impl Default for RawHaalkaEl {
    fn default() -> Self {
        Self::new()
    }
}

/// Whether to append the deferred updater to the front or the back *of the back* of the line. See
/// [`RawHaalkaEl::defer_update`].
#[allow(missing_docs)]
pub enum DeferredUpdaterAppendDirection {
    Front,
    Back,
}

impl RawHaalkaEl {
    fn new_dummy() -> Self {
        Self {
            node_builder: None,
            deferred_updaters: default(),
        }
    }

    #[allow(missing_docs)]
    pub fn new() -> Self {
        Self {
            node_builder: Some(default()),
            deferred_updaters: default(),
        }
    }

    // TODO: allow attaching some sortable data to unlimit ordering options (this prolly won't work
    // because it would pollute the struct with a type?)
    /// Force updates to be in the back of the line, with some crude ordering.
    ///
    /// Useful when updates must be applied after some node is wrapped with another.
    ///
    /// # Notes
    /// Deferred updates is a lower level feature and was previously used internally to deal with
    /// update ordering issues in relation to scrollability and sizing. One can freely take
    /// advantage of it to do things like apply wrapping nodes on their own custom elements
    /// built on top of [`RawHaalkaEl`], but should be more wary of using it through
    /// [`.update_raw_el`](RawElWrapper::update_raw_el) on the provided higher level UI elements
    /// like [`El`](super::el::El) and [`Column`](super::column::Column).
    pub fn defer_update(
        mut self,
        append_direction: DeferredUpdaterAppendDirection,
        updater: impl FnOnce(RawHaalkaEl) -> RawHaalkaEl + Send + 'static,
    ) -> Self {
        match append_direction {
            DeferredUpdaterAppendDirection::Front => self.deferred_updaters.insert(0, Box::new(updater)),
            DeferredUpdaterAppendDirection::Back => self.deferred_updaters.push(Box::new(updater)),
        }
        self
    }

    /// Process the underlying [`NodeBuilder`] directly.
    pub fn update_node_builder(mut self, updater: impl FnOnce(NodeBuilder) -> NodeBuilder) -> Self {
        self.node_builder = Some(updater(self.node_builder.unwrap()));
        self
    }

    /// Consume this [`RawHaalkaEl`], running its deferred updaters and returning the underlying
    /// [`NodeBuilder`].
    pub fn into_node_builder(mut self) -> NodeBuilder {
        let deferred_updaters = self.deferred_updaters.drain(..).collect::<Vec<_>>();
        let mut self_ = self;
        for updater in deferred_updaters {
            self_ = updater(self_);
        }
        self_.node_builder.unwrap()
    }

    /// Run a function with mutable access to the [`World`] and this element's [`Entity`].
    pub fn on_spawn(self, on_spawn: impl FnOnce(&mut World, Entity) + Send + 'static) -> Self {
        self.update_node_builder(|node_builder| node_builder.on_spawn(on_spawn))
    }

    /// Run a [`System`] which takes [`In`](`System::In`) this element's [`Entity`].
    pub fn on_spawn_with_system<T: IntoSystem<In<Entity>, (), Marker> + Send + 'static, Marker>(
        self,
        system: T,
    ) -> Self {
        self.on_spawn(|world, entity| {
            if let Err(error) = world.run_system_once_with(system, entity) {
                error!("failed to run system on spawn: {}", error);
            }
        })
    }

    /// Add a [`Bundle`] of components to this element.
    pub fn insert<B: Bundle>(self, bundle: B) -> Self {
        self.update_node_builder(|node_builder| node_builder.insert(bundle))
    }

    /// If the `forwarder` points to [`Some`] [`Entity`], Add a [`Bundle`] of components to that
    /// [`Entity`].
    pub fn insert_forwarded(
        self,
        forwarder: impl FnOnce(&mut EntityWorldMut) -> Option<Entity> + Send + 'static,
        bundle: impl Bundle + 'static,
    ) -> Self {
        self.with_entity_forwarded(forwarder, move |mut entity| {
            entity.insert(bundle);
        })
    }

    /// Run a function with this element's [`EntityWorldMut`].
    pub fn with_entity(self, f: impl FnOnce(EntityWorldMut) + Send + 'static) -> Self {
        self.update_node_builder(|node_builder| node_builder.with_entity(f))
    }

    /// If the `forwarder` points to [`Some`] [`Entity`], run a function with that [`Entity`]'s
    /// [`EntityWorldMut`].
    pub fn with_entity_forwarded(
        self,
        forwarder: impl FnOnce(&mut EntityWorldMut) -> Option<Entity> + Send + 'static,
        f: impl FnOnce(EntityWorldMut) + Send + 'static,
    ) -> Self {
        self.with_entity(move |mut entity| {
            if let Some(forwardee) = forwarder(&mut entity) {
                entity.world_scope(|world| {
                    if let Ok(forwardee) = world.get_entity_mut(forwardee) {
                        f(forwardee)
                    }
                })
            }
        })
    }

    /// Run a function with mutable access (via [`Mut`]) to this element's `C` [`Component`] if it
    /// exists.
    pub fn with_component<C: Component<Mutability = Mutable>>(self, f: impl FnOnce(Mut<C>) + Send + 'static) -> Self {
        self.with_entity(|mut entity| {
            if let Some(component) = entity.get_mut::<C>() {
                f(component);
            }
        })
    }

    /// If the `forwarder` points to [`Some`] [`Entity`], run a function with mutable access (via
    /// [`Mut`]) to that [`Entity`]'s `C` [`Component`] if it exists.
    pub fn with_component_forwarded<C: Component<Mutability = Mutable>>(
        self,
        forwarder: impl FnOnce(&mut EntityWorldMut) -> Option<Entity> + Send + 'static,
        f: impl FnOnce(Mut<C>) + Send + 'static,
    ) -> Self {
        self.with_entity_forwarded(forwarder, move |mut entity| {
            if let Some(component) = entity.get_mut::<C>() {
                f(component)
            }
        })
    }

    /// Attach an [`Observer`] to this element.
    ///
    /// Attaches a special [`HaalkaObserver`] component to the entity, which allows it to be filtered by higher level tools (see [aalo](https://github.com/databasedav/aalo)).
    pub fn observe<E: Event, B: Bundle, Marker>(self, observer: impl IntoObserverSystem<E, B, Marker>) -> Self {
        self.on_spawn(|world, entity| {
            observe(world, entity, observer);
        })
    }

    /// Drop the [`Task`] when it completes or the entity is despawned.
    pub fn hold_tasks(self, tasks: impl IntoIterator<Item = Task<()>> + Send + 'static) -> Self {
        self.with_component::<TaskHolder>(|task_holder| {
            for task in tasks.into_iter() {
                task_holder.hold(task);
            }
        })
    }

    /// When this element is despawned, run a function with mutable access to the [`DeferredWorld`]
    /// and this element's [`Entity`].
    pub fn on_remove(self, on_remove: impl FnOnce(&mut DeferredWorld, Entity) + Send + Sync + 'static) -> Self {
        self.on_spawn(|world, entity| {
            if let Some(mut on_remove_component) = world.entity_mut(entity).get_mut::<OnRemove>() {
                on_remove_component.0.push(Box::new(on_remove));
            } else {
                world.entity_mut(entity).insert(OnRemove(vec![Box::new(on_remove)]));
            }
        })
    }

    /// Reactively run a [`Future`]-returning function with this element's [`Entity`] and the output
    /// of the [`Signal`].
    pub fn on_signal<T, Fut: Future<Output = ()> + Send + 'static>(
        self,
        signal: impl Signal<Item = T> + Send + 'static,
        f: impl FnMut(Entity, T) -> Fut + Send + 'static,
    ) -> Self {
        self.update_node_builder(|node_builder| node_builder.on_signal(signal, f))
    }

    /// Reactively run a function with this element's [`Entity`] and the output of the [`Signal`].
    pub fn on_signal_sync<T>(
        self,
        signal: impl Signal<Item = T> + Send + 'static,
        mut f: impl FnMut(Entity, T) + Send + 'static,
    ) -> Self {
        self.on_signal(signal, move |entity, value| {
            f(entity, value);
            async {}
        })
    }

    /// Reactively run a [`System`] which takes [`In`](`System::In`) this element's [`Entity`] and
    /// the output of the [`Signal`].
    pub fn on_signal_with_system<T: Send + 'static, Marker>(
        self,
        signal: impl Signal<Item = T> + Send + 'static,
        system: impl IntoSystem<In<(Entity, T)>, (), Marker> + Send + 'static,
    ) -> Self {
        let system_holder = Arc::new(OnceLock::new());
        self.on_spawn(clone!((system_holder) move |world, _| {
            let _ = system_holder.set(register_system(world, system));
        }))
        .on_signal(
            signal,
            clone!((system_holder) move |entity, input| {
                async_world().apply(run_system_with_entity(entity, system_holder.get().copied().unwrap(), input).handle_error_with(warn))
            }),
        )
        .apply(remove_system_holder_on_remove(system_holder))
    }

    /// Reactively run a [`System`], if the `forwarder` points to [`Some`] [`Entity`], which takes
    /// [`In`](`System::In`) that element's [`Entity`] and the output of the [`Signal`].
    #[allow(clippy::type_complexity)]
    pub fn on_signal_with_system_forwarded<T: Send + 'static, Marker1, Marker2>(
        self,
        signal: impl Signal<Item = T> + Send + 'static,
        forwarder: impl IntoSystem<In<Entity>, Option<Entity>, Marker1> + Send + 'static,
        system: impl IntoSystem<In<(Entity, T)>, (), Marker2> + Send + 'static,
    ) -> Self {
        let forwarder_system_holder = Arc::new(OnceLock::new());
        let handler_system_holder = Arc::new(OnceLock::new());
        self.on_spawn(clone!((forwarder_system_holder, handler_system_holder) move |world, _| {
            let _ = forwarder_system_holder.set(register_system(world, forwarder));
            let _ = handler_system_holder.set(register_system(world, system));
        }))
        .on_signal_with_system(
            signal,
            clone!((forwarder_system_holder, handler_system_holder) move |In((entity, input)): In<(Entity, T)>, mut commands: Commands| {
                commands.queue(clone!((forwarder_system_holder, handler_system_holder) move |world: &mut World| {
                    if let Ok(Some(forwardee)) = world.run_system_with(forwarder_system_holder.get().copied().unwrap(), entity) {
                        let _ = world.run_system_with(handler_system_holder.get().copied().unwrap(), (forwardee, input));
                    }
                }))
            }),
        )
        .apply(remove_system_holder_on_remove(forwarder_system_holder))
        .apply(remove_system_holder_on_remove(handler_system_holder))
    }

    /// Reactively run a function with this element's [`EntityWorldMut`] and the output of the
    /// [`Signal`].
    pub fn on_signal_with_entity<T: Send + 'static>(
        self,
        signal: impl Signal<Item = T> + Send + 'static,
        mut f: impl FnMut(EntityWorldMut, T) + Send + Sync + 'static,
    ) -> Self {
        self.on_signal_with_system(
            signal,
            move |In((entity, value)): In<(Entity, T)>, world: &mut World| {
                if let Ok(entity) = world.get_entity_mut(entity) {
                    f(entity, value)
                }
            },
        )
    }

    /// Reactively run a function, if the `forwarder` points to [`Some`] [`Entity`],
    /// with that [`Entity`]'s [`EntityWorldMut`] and the output of the [`Signal`].
    pub fn on_signal_with_entity_forwarded<T: Send + 'static, Marker>(
        self,
        signal: impl Signal<Item = T> + Send + 'static,
        forwarder: impl IntoSystem<In<Entity>, Option<Entity>, Marker> + Send + 'static,
        mut f: impl FnMut(EntityWorldMut, T) + Send + Sync + 'static,
    ) -> Self {
        self.on_signal_with_system_forwarded(
            signal,
            forwarder,
            move |In((entity, value)): In<(Entity, T)>, world: &mut World| {
                if let Ok(entity) = world.get_entity_mut(entity) {
                    f(entity, value)
                }
            },
        )
    }

    /// Reactively run a function with mutable access (via [`Mut`]) to this element's `C`
    /// [`Component`] if it exists and the output of the [`Signal`].
    pub fn on_signal_with_component<T: Send + 'static, C: Component<Mutability = Mutable>>(
        self,
        signal: impl Signal<Item = T> + Send + 'static,
        mut f: impl FnMut(Mut<C>, T) + Send + Sync + 'static,
    ) -> Self {
        self.on_signal_with_system(
            signal,
            move |In((entity, value)): In<(Entity, T)>, mut query: Query<&mut C>| {
                if let Ok(component) = query.get_mut(entity) {
                    f(component, value)
                }
            },
        )
    }

    /// Reactively run a function, if the `forwarder` points to [`Some`] [`Entity`], with mutable
    /// access (via [`Mut`]) to that [`Entity`]'s `C` [`Component`] if it exists.
    pub fn on_signal_with_component_forwarded<T: Send + 'static, C: Component<Mutability = Mutable>, Marker>(
        self,
        signal: impl Signal<Item = T> + Send + 'static,
        forwarder: impl IntoSystem<In<Entity>, Option<Entity>, Marker> + Send + 'static,
        mut f: impl FnMut(Mut<C>, T) + Send + Sync + 'static,
    ) -> Self {
        self.on_signal_with_system_forwarded(
            signal,
            forwarder,
            move |In((entity, value)): In<(Entity, T)>, mut query: Query<&mut C>| {
                if let Ok(component) = query.get_mut(entity) {
                    f(component, value)
                }
            },
        )
    }

    /// Reactively set this element's `C` [`Component`]. If the [`Signal`] outputs [`None`], the `C`
    /// [`Component`] is removed.
    pub fn component_signal<C: Component, S: Signal<Item = impl Into<Option<C>>> + Send + 'static>(
        mut self,
        component_option_signal_option: impl Into<Option<S>>,
    ) -> Self {
        if let Some(component_option_signal) = component_option_signal_option.into() {
            // TODO: need partial_eq derivations for all the node related components to minimize updates
            // with .dedupe
            self = self.on_signal_with_entity::<Option<C>>(
                component_option_signal.map(|into_component_option| into_component_option.into()),
                move |mut entity, component_option| {
                    if let Some(component) = component_option {
                        entity.insert(component);
                    } else {
                        entity.remove::<C>();
                    }
                },
            );
        }
        self
    }

    /// Reactively set the `C` [`Component`] of the [`Entity`] that the `forwarder` points to if it
    /// points to [`Some`] [`Entity`]. If the [`Signal`] outputs [`None`], the `C` [`Component`] is
    /// removed.
    pub fn component_signal_forwarded<C: Component, Marker>(
        self,
        forwarder: impl IntoSystem<In<Entity>, Option<Entity>, Marker> + Send + 'static,
        component_option_signal: impl Signal<Item = impl Into<Option<C>>> + Send + 'static,
    ) -> Self {
        self.on_signal_with_entity_forwarded(
            component_option_signal.map(|into_component_option| into_component_option.into()),
            forwarder,
            move |mut entity, component_option| {
                if let Some(component) = component_option {
                    entity.insert(component);
                } else {
                    entity.remove::<C>();
                }
            },
        )
    }

    /// Reactively send an [`Event`] based on this element's [`Entity`] and the output of the
    /// [`Signal`].
    pub fn on_signal_send_message<T, E: Message>(
        self,
        signal: impl Signal<Item = T> + Send + 'static,
        mut f: impl FnMut(Entity, T) -> E + Send + 'static,
    ) -> Self {
        self.on_signal(signal, move |entity, value| {
            async_world().send_message(f(entity, value))
        })
    }

    /// Declare a static child.
    pub fn child<IORE: IntoOptionRawElement>(self, child_option: IORE) -> Self {
        if let Some(child) = child_option.into_option_element() {
            return self.update_node_builder(|node_builder| node_builder.child(child.into_raw().into_node_builder()));
        }
        self
    }

    /// Declare a reactive child. When the [`Signal`] outputs [`None`], the child is removed.
    pub fn child_signal<IORE: IntoOptionRawElement>(
        self,
        child_option_signal: impl Signal<Item = IORE> + Send + 'static,
    ) -> Self {
        self.update_node_builder(|node_builder| {
            node_builder.child_signal(child_option_signal.map(|child_option| {
                child_option
                    .into_option_element()
                    .map(|child| child.into_raw().into_node_builder())
            }))
        })
    }

    /// Declare static children.
    pub fn children<IORE: IntoOptionRawElement, I: IntoIterator<Item = IORE>>(self, children_options: I) -> Self
    where
        I::IntoIter: Send + 'static,
    {
        self.update_node_builder(|node_builder| {
            node_builder.children(
                children_options
                    .into_iter()
                    .filter_map(|child_option| child_option.into_option_element())
                    .map(|child| child.into_raw().into_node_builder()),
            )
        })
    }

    /// Declare reactive children.
    pub fn children_signal_vec<IORE: IntoOptionRawElement>(
        self,
        children_options_signal_vec: impl SignalVec<Item = IORE> + Send + 'static,
    ) -> Self {
        self.update_node_builder(|node_builder| {
            node_builder.children_signal_vec(
                children_options_signal_vec
                    .filter_map(|child_option| child_option.into_option_element())
                    .map(|child| child.into_raw().into_node_builder()),
            )
        })
    }
}

fn run_system_with_entity<I: Send + 'static>(
    entity: Entity,
    id: SystemId<In<(Entity, I)>>,
    input: I,
) -> impl Command<Result> {
    move |world: &mut World| -> Result {
        if world.get_entity(entity).is_ok() {
            world.run_system_with(id, (entity, input))?;
        }
        Ok(())
    }
}

fn on_remove_on_remove(mut world: DeferredWorld, HookContext { entity, .. }: HookContext) {
    let fs = world
        .get_mut::<OnRemove>(entity)
        .unwrap()
        .0
        .drain(..)
        .collect::<Vec<_>>();
    for f in fs {
        f(&mut world, entity);
    }
}

#[allow(clippy::type_complexity)]
#[derive(Component)]
#[component(on_remove = on_remove_on_remove)]
struct OnRemove(Vec<Box<dyn FnOnce(&mut DeferredWorld, Entity) + Send + Sync + 'static>>);

/// Marker [`Component`] for filtering `SystemId` `Entity`s managed by haalka.
#[derive(Component)]
pub struct HaalkaOneShotSystem;

pub(crate) fn register_system<I: SystemInput + 'static, O: 'static, Marker, S: IntoSystem<I, O, Marker> + 'static>(
    world: &mut World,
    system: S,
) -> SystemId<I, O> {
    let system = world.register_system(system);
    if let Ok(mut entity) = world.get_entity_mut(system.entity()) {
        entity.insert(HaalkaOneShotSystem);
    }
    system
}

/// Marker [`Component`] for filtering `Observer` `Entity`s managed by haalka.
#[derive(Component)]
pub struct HaalkaObserver;

pub(crate) fn observe<E: Event, B: Bundle, Marker>(
    world: &mut World,
    entity: Entity,
    observer: impl IntoObserverSystem<E, B, Marker>,
) -> EntityWorldMut<'_> {
    world.spawn((Observer::new(observer).with_entity(entity), HaalkaObserver))
}

/// Thin wrapper trait around [`RawHaalkaEl`] to allow consumers to target custom types when
/// composing [`RawHaalkaEl`]s.
pub trait RawElement: Sized {
    /// Convert this type into a [`RawHaalkaEl`].
    fn into_raw(self) -> RawHaalkaEl;
}

impl<REW: RawElWrapper> RawElement for REW {
    fn into_raw(self) -> RawHaalkaEl {
        self.into_raw_el()
    }
}

// TODO: why is this a conflicting impl ?
// impl<B: Bundle> RawElement for B {
//     fn into_raw(self) -> RawHaalkaEl {
//         RawHaalkaEl::from(self)
//     }
// }

/// Allows consumers to pass non-[`RawElWrapper`] types to the child methods of [`RawHaalkaEl`].
pub trait IntoRawElement {
    /// The type of the [`RawElement`] that this type is converted into
    type EL: RawElement;
    /// Convert this type into an [`RawElement`].
    fn into_raw_element(self) -> Self::EL;
}

impl<T: RawElement> IntoRawElement for T {
    type EL = T;
    fn into_raw_element(self) -> Self::EL {
        self
    }
}

/// Thin wrapper trait around [`RawElement`] that allows consumers to pass [`Option`]s to the child
/// methods of [`RawHaalkaEl`].
pub trait IntoOptionRawElement {
    /// The type of the [`RawElement`] that this type is maybe converted into.
    type EL: RawElement;
    /// Maybe convert this type into a [`RawElement`].
    fn into_option_element(self) -> Option<Self::EL>;
}

impl<E: RawElement, IE: IntoRawElement<EL = E>> IntoOptionRawElement for Option<IE> {
    type EL = E;
    fn into_option_element(self) -> Option<Self::EL> {
        self.map(|into_element| into_element.into_raw_element())
    }
}

impl<E: RawElement, IE: IntoRawElement<EL = E>> IntoOptionRawElement for IE {
    type EL = E;
    fn into_option_element(self) -> Option<Self::EL> {
        Some(self.into_raw_element())
    }
}

// TODO: proc macro for RawElWrapper which scans through the fields of a struct and implements the
// trait for whatever RawHaalkaEl is found first
/// [`RawElWrapper`]s can be passed to the child methods of [`RawHaalkaEl`]. This can be used to
/// create custom non-UI "widgets". See [`ElementWrapper`](super::element::ElementWrapper) for what
/// this looks like in a UI context.
pub trait RawElWrapper: Sized {
    /// Mutable reference to the [`RawHaalkaEl`] that this wrapper wraps.
    fn raw_el_mut(&mut self) -> &mut RawHaalkaEl;

    /// Process the wrapped [`RawHaalkaEl`] directly.
    fn update_raw_el(mut self, updater: impl FnOnce(RawHaalkaEl) -> RawHaalkaEl) -> Self {
        let raw_el = mem::replace(self.raw_el_mut(), RawHaalkaEl::new_dummy());
        *self.raw_el_mut() = updater(raw_el);
        self
    }

    /// Consume this wrapper, returning the wrapped [`RawHaalkaEl`].
    fn into_raw_el(mut self) -> RawHaalkaEl {
        mem::replace(self.raw_el_mut(), RawHaalkaEl::new_dummy())
    }
}

/// Required to allow passing [`RawHaalkaEl`]s to [`RawHaalkaEl`]'s `.child` methods.
impl RawElement for RawHaalkaEl {
    fn into_raw(self) -> RawHaalkaEl {
        self
    }
}

/// Allows [`RawElement`]s and their [wrappers](RawElWrapper) to be spawned into the world.
pub trait Spawnable: RawElement
where
    Self: Sized,
{
    /// Spawn the element into the world.
    fn spawn(self, world: &mut World) -> Entity {
        self.into_raw().into_node_builder().spawn(world)
    }
}

impl<REW: RawElement> Spawnable for REW {}

#[allow(missing_docs)]
pub mod utils {
    use super::*;

    /// If [`Some`] [`System`] is returned by the `getter`, remove it
    /// from the [`World`] on element removal.
    pub fn remove_system_on_remove<I: SystemInput + 'static, O: 'static>(
        getter: impl FnOnce() -> Option<SystemId<I, O>> + Send + Sync + 'static,
    ) -> impl FnOnce(RawHaalkaEl) -> RawHaalkaEl {
        |raw_el| {
            raw_el.on_remove(move |world, _| {
                if let Some(system) = getter() {
                    world.commands().queue(move |world: &mut World| {
                        let _ = world.unregister_system(system);
                    })
                }
            })
        }
    }

    /// Remove the held system from the [`World`] on element removal.
    pub fn remove_system_holder_on_remove<I: SystemInput + 'static, O: 'static>(
        system_holder: Arc<OnceLock<SystemId<I, O>>>,
    ) -> impl FnOnce(RawHaalkaEl) -> RawHaalkaEl {
        remove_system_on_remove(move || system_holder.get().copied())
    }

    /// Run an element's deferred updaters without spawning.
    pub fn flush_deferred_updaters<T: RawElement>(raw_el: T) -> RawHaalkaEl {
        raw_el.into_raw().into_node_builder().apply(RawHaalkaEl::from)
    }
}