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
//! High level UI building block/widget abstraction ported from [MoonZoon](https://github.com/MoonZoon/MoonZoon)'s [`Element`](https://github.com/MoonZoon/MoonZoon/blob/f8fc31065f65bdb3ab7b94faf5e3916bc5550dd9/crates/zoon/src/element.rs#L84).

use std::borrow::Cow;

use super::align::Alignable;
use bevy_ecs::{component::*, lifecycle::HookContext, prelude::*, system::RunSystemOnce, world::DeferredWorld};
use bevy_log::warn;
use jonmo::prelude::*;

use bevy_ecs::system::IntoObserverSystem;

/// [`Element`]s are types that wrap [`jonmo::Builder`] and can be aligned using
/// [haalka](crate)'s [simple alignability semantics](super::align::Align) and granted UI-specific
/// abilities like [pointer event awareness](super::pointer_event_aware::PointerEventAware),
/// [viewport mutability](super::viewport_mutable::ViewportMutable),
/// [scrollability](super::mouse_wheel_scrollable::MouseWheelScrollable), etc.
pub trait Element: BuilderWrapper + Alignable {}

impl<E: BuilderWrapper + Alignable> Element for E {}

/// Allows consumers to pass non-[`ElementWrapper`] types to the child methods of all alignable
/// types.
pub trait IntoElement {
    /// The type of the [`Element`] that this type is converted into.
    type EL: Element;
    /// Convert this type into an [`Element`].
    fn into_element(self) -> Self::EL;
}

impl<T: Element> IntoElement for T {
    type EL = T;
    fn into_element(self) -> Self::EL {
        self
    }
}

// impl IntoElement for &'static str {
//     type EL = El<Text>;
//     fn into_element(self) -> Self::EL {
//         El::<Text>::new().text(Text::from_section(
//             self.to_string(),
//             TextStyle::default(),
//         ))
//     }
// }

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

impl<E: Element, IE: IntoElement<EL = E>> IntoOptionElement for Option<IE> {
    type EL = E;
    fn into_option_element(self) -> Option<Self::EL> {
        self.map(|into_element| into_element.into_element())
    }
}

impl<E: Element, IE: IntoElement<EL = E>> IntoOptionElement for IE {
    type EL = E;
    fn into_option_element(self) -> Option<Self::EL> {
        Some(self.into_element())
    }
}

/// The core trait for all UI element types in haalka. Types implementing this trait wrap a
/// [`jonmo::Builder`] and can be used with haalka's UI abilities like
/// [`PointerEventAware`](super::pointer_event_aware::PointerEventAware),
/// [`ViewportMutable`](super::viewport_mutable::ViewportMutable), etc.
///
/// **For primitive elements** (like [`El`](super::el::El), [`Column`](super::column::Column), etc.)
/// that directly hold a [`jonmo::Builder`], implement this trait directly.
///
/// **For widgets** that wrap other elements, implement [`ElementWrapper`] instead, which provides
/// a blanket implementation of `BuilderWrapper` automatically.
pub trait BuilderWrapper: Sized {
    /// Mutable reference to the [`jonmo::Builder`] that this wrapper wraps.
    fn builder_mut(&mut self) -> &mut jonmo::Builder;

    /// Process the wrapped [`jonmo::Builder`] directly.
    fn with_builder(mut self, f: impl FnOnce(jonmo::Builder) -> jonmo::Builder) -> Self {
        let builder = std::mem::take(self.builder_mut());
        *self.builder_mut() = f(builder);
        self
    }

    /// Consume this wrapper, returning the wrapped [`jonmo::Builder`].
    fn into_builder(mut self) -> jonmo::Builder {
        std::mem::take(self.builder_mut())
    }
}

/// Allows [`BuilderWrapper`]s to be spawned into the world.
pub trait Spawnable: BuilderWrapper {
    /// Spawn the element into the world.
    fn spawn(self, world: &mut World) -> Entity {
        self.into_builder().spawn(world)
    }
}

impl<T: BuilderWrapper> Spawnable for T {}

/// [`ElementWrapper`]s can be passed to the child methods of all alignable types, e.g.
/// [`.child`](super::el::El::child), [`.item_signal`](super::column::Column::item_signal),
/// [`.layers`](super::stack::Stack::layers),
/// [`.cells_signal_vec`](super::grid::Grid::cells_signal_vec), etc. This trait provides the
/// foundation for building "widgets" using [haalka](crate).
///
/// # Example
/// ```
/// use bevy::prelude::*;
/// use haalka::prelude::*;
///
/// struct MyWidget {
///     el: El<Node>,
///     data: usize,
/// }
///
/// impl ElementWrapper for MyWidget {
///     type EL = El<Node>;
///     fn element_mut(&mut self) -> &mut Self::EL {
///         &mut self.el
///     }
/// }
///
/// impl GlobalEventAware for MyWidget {}
/// impl PointerEventAware for MyWidget {}
/// impl ViewportMutable for MyWidget {}
/// impl MouseWheelScrollable for MyWidget {}
/// ```
pub trait ElementWrapper: Sized {
    /// The type of the [`Element`] that this wrapper wraps; this can be another [`ElementWrapper`].
    type EL: Element + Default;
    /// Mutable reference to the [`Element`] that this wrapper wraps.
    fn element_mut(&mut self) -> &mut Self::EL;

    /// Indirection which allows trait consumers to define custom "build" or "render" logic outside
    /// the body of the [`ElementWrapper`] itself, allowing the [`ElementWrapper`] to be more
    /// ergonomically used as a configuration builder.
    ///
    /// Couldn't figure out how to do this without the [`Default`] constraint since it is required
    /// by [`mem::take`], and was led to the [`mem::take`] solution since there didn't seem to
    /// be a viable unsafe way to take ownership of a single field of a struct with only a
    /// mutable reference to the field, i.e. via [`.element_mut()`](ElementWrapper::element_mut).
    ///
    /// [`mem::take`]: std::mem::take
    fn into_el(mut self) -> Self::EL {
        std::mem::take(self.element_mut())
    }
}

impl<EW: ElementWrapper> BuilderWrapper for EW {
    fn builder_mut(&mut self) -> &mut jonmo::Builder {
        self.element_mut().builder_mut()
    }

    fn into_builder(self) -> jonmo::Builder {
        self.into_el().into_builder()
    }
}

impl<EW: ElementWrapper> Alignable for EW {}

fn warn_non_orphan_ui_root(mut world: DeferredWorld, HookContext { entity, .. }: HookContext) {
    world.commands().queue(move |world: &mut World| {
        let _ = world.run_system_once(move |child_ofs: Query<&ChildOf>| {
            if child_ofs.iter_ancestors(entity).count() > 0 {
                warn!(
                    "entity {:?} is registered as a UiRoot but is not an orphan (has a parent); this may lead to unexpected behavior",
                    entity
                );
            }
        });
    })
}

/// Marker component for the root of the UI tree. Use [`UiRootable::ui_root`] to
/// register an [`Element`] as the [`UiRoot`].
///
/// Used to register global event listeners.
#[derive(Component)]
#[component(on_add = warn_non_orphan_ui_root)]
pub struct UiRoot;

/// Allows [`Element`]s to be marked as the root of the UI tree.
pub trait UiRootable: BuilderWrapper {
    /// Mark this node as the root of the UI tree.
    fn ui_root(self) -> Self {
        self.with_builder(|builder| builder.insert(UiRoot))
    }
}

/// Convenience trait for adding a [`Name`] to an [`Element`].
pub trait Nameable: BuilderWrapper {
    /// Set the [`Name`] of this element.
    fn name<T: Into<Cow<'static, str>>>(mut self, name_option: impl Into<Option<T>>) -> Self {
        if let Some(name) = name_option.into() {
            self = self.with_builder(|builder| builder.insert(Name::new(name)));
        }
        self
    }

    /// Reactively set the name of this element. If the signal outputs [`None`] the [`Name`] is
    /// removed.
    fn name_signal<T, S>(mut self, name_option_signal_option: impl Into<Option<S>>) -> Self
    where
        T: Into<Cow<'static, str>> + Clone + 'static,
        S: Signal<Item = Option<T>> + Send + Sync + 'static,
    {
        if let Some(name_option_signal) = name_option_signal_option.into() {
            self = self.with_builder(|builder| {
                builder.component_signal(name_option_signal.map_in(|name_option| name_option.map(Name::new)))
            });
        }
        self
    }
}

/// Pass-through convenience trait for commonly used [`jonmo::Builder`] methods.
pub trait BuilderPassThrough: BuilderWrapper {
    /// Pass-through for [`jonmo::Builder::lazy_entity`].
    fn lazy_entity(self, entity: jonmo::utils::LazyEntity) -> Self {
        self.with_builder(|builder| builder.lazy_entity(entity))
    }

    /// Pass-through for [`jonmo::Builder::insert`].
    fn insert<T: Bundle>(self, bundle: T) -> Self {
        self.with_builder(|builder| builder.insert(bundle))
    }

    /// Pass-through for [`jonmo::Builder::observe`].
    fn observe<E: EntityEvent, B: Bundle, Marker>(
        self,
        observer: impl IntoObserverSystem<E, B, Marker> + Sync,
    ) -> Self {
        self.with_builder(|builder| builder.observe(observer))
    }

    /// Pass-through for [`jonmo::Builder::component_signal`].
    fn component_signal<C>(self, signal: impl Signal<Item = Option<C>>) -> Self
    where
        C: Component + Clone,
    {
        self.with_builder(|builder| builder.component_signal(signal))
    }
}

/// Generates the `# Clone semantics` doc section for element struct documentation.
///
/// # Usage
/// ```ignore
/// #[doc = clone_semantics_doc!("El")]
/// pub struct El<NodeType> { ... }
/// ```
#[doc(hidden)]
#[macro_export]
macro_rules! clone_semantics_doc {
    ($type_name:literal) => {
        concat!(
            "# `Clone` semantics\n\n",
            "This type implements [`Clone`] **only** to satisfy trait bounds required by signal combinators.\n",
            "**Cloning `",
            $type_name,
            "`s at runtime is a bug.** See [`",
            $type_name,
            "::clone`] for details."
        )
    };
}

/// Generates the base error text for clone documentation (without trailing punctuation).
#[doc(hidden)]
#[macro_export]
macro_rules! clone_error_doc_base {
    ($type_name:literal) => {
        concat!(
            "# Error\n\n",
            "This clone implementation exists **only** to satisfy trait bounds required by signal\n",
            "combinators. **Cloning `",
            $type_name,
            "`s at runtime is a bug and will lead to unexpected behavior.**\n\n",
            "Clones produce an empty element with no on-spawn hooks.\n\n",
            "Use factory functions instead if you need reusable UI templates"
        )
    };
}

/// Generates the error docstring for a `Clone::clone` method on element types.
///
/// # Usage
/// ```ignore
/// impl Clone for MyType {
///     #[doc = clone_error_doc!("MyType")]
///     fn clone(&self) -> Self { ... }
/// }
/// ```
///
/// Or with a code example:
/// ```ignore
/// impl Clone for MyType {
///     #[doc = clone_error_doc!("MyType", my_fn, ".method()")]
///     fn clone(&self) -> Self { ... }
/// }
/// ```
#[doc(hidden)]
#[macro_export]
macro_rules! clone_error_doc {
    // Without example
    ($type_name:literal) => {
        concat!($crate::clone_error_doc_base!($type_name), ".")
    };
    // With example
    ($type_name:literal, $example_fn:ident, $example_method:literal) => {
        concat!(
            $crate::clone_error_doc_base!($type_name),
            ":\n\n",
            "```ignore\n",
            "use bevy_ui::prelude::*;\n",
            "use haalka::prelude::*;\n\n",
            "fn ",
            stringify!($example_fn),
            "(label: &str) -> ",
            $type_name,
            "<Node> {\n",
            "    ",
            $type_name,
            "::new()",
            $example_method,
            "\n",
            "}\n\n",
            "// Correct: each call creates a fresh element\n",
            "let el1 = ",
            stringify!($example_fn),
            "(\"First\");\n",
            "let el2 = ",
            stringify!($example_fn),
            "(\"Second\");\n",
            "```"
        )
    };
}

/// Generates the runtime error message for cloning an element type.
#[doc(hidden)]
#[macro_export]
macro_rules! clone_error_msg {
    ($type_name:literal) => {
        concat!(
            "Cloning `",
            $type_name,
            "` at {} is a bug! Use a factory function instead."
        )
    };
}

/// Implements [`Clone`] for element types with appropriate errors.
///
/// This macro generates a `Clone` implementation that:
/// - Logs an error at runtime when cloned (since cloning elements is typically a bug)
/// - Has complete documentation explaining the clone semantics
///
/// # Forms
///
/// ## Generic element types with `builder` and `_node_type` fields:
/// ```ignore
/// impl_element_clone! {
///     "El",
///     El<NodeType>,
///     my_el,
///     ".name(\"my_el\")"
/// }
/// ```
///
/// ## Non-generic tuple struct wrapping `jonmo::Builder`:
/// ```ignore
/// impl_element_clone!(simple "AlignabilityFacade", AlignabilityFacade);
/// ```
#[doc(hidden)]
#[macro_export]
macro_rules! impl_element_clone {
    // Generic element type with builder + _node_type fields and example
    ($type_name:literal, $type:ty, $example_fn:ident, $example_method:literal) => {
        impl<NodeType> Clone for $type {
            #[doc = $crate::clone_error_doc!($type_name, $example_fn, $example_method)]
            #[track_caller]
            fn clone(&self) -> Self {
                let msg = format!(
                    $crate::clone_error_msg!($type_name),
                    std::panic::Location::caller()
                );
                if cfg!(debug_assertions) {
                    let backtrace = std::backtrace::Backtrace::force_capture();
                    panic!("{}\nBacktrace:\n{}", msg, backtrace);
                }
                bevy_log::error!("{}", msg);

                Self {
                    builder: self.builder.clone(),
                    _node_type: std::marker::PhantomData,
                }
            }
        }
    };
    // Simple non-generic tuple struct wrapping jonmo::Builder
    (simple $type_name:literal, $type:ty) => {
        impl Clone for $type {
            #[doc = $crate::clone_error_doc!($type_name)]
            #[track_caller]
            fn clone(&self) -> Self {
                let msg = format!(
                    $crate::clone_error_msg!($type_name),
                    std::panic::Location::caller()
                );
                if cfg!(debug_assertions) {
                    let backtrace = std::backtrace::Backtrace::force_capture();
                    panic!("{}\nBacktrace:\n{}", msg, backtrace);
                }
                bevy_log::error!("{}", msg);

                Self(self.0.clone())
            }
        }
    };
}

/// Enables mixing of different types of [`Element`]s.
///
/// Since [`Element`]s or [`ElementWrapper::EL`]s can be of different concrete types (e.g.
/// `El<Node>`, `El<ImageBundle>`, `Column<Node>`, etc.), one will run into unfortunate
/// type issues when doing things like returning different [`ElementWrapper`]s (read: widgets) from
/// diverging branches of logic, or creating a collection of [`ElementWrapper`]s of different types.
/// This trait allows collapsing all [`Element`]s and [`ElementWrapper`]s into a single
/// "type erased" [`AlignabilityFacade`] type that still implements [`Element`].
pub trait TypeEraseable {
    /// Convert this type into an [`AlignabilityFacade`], allowing it to mix with other types of
    /// [`Element`]s and [`ElementWrapper`]s.
    fn type_erase(self) -> AlignabilityFacade;
}

impl<T: Alignable> TypeEraseable for T {
    fn type_erase(self) -> AlignabilityFacade {
        AlignabilityFacade(self.into_builder())
    }
}

/// A type-erased [`Element`] that provides a facade of alignability.
///
/// Created via [`TypeEraseable::type_erase`]. The underlying alignment components
/// are preserved from the original element, so alignment behavior works correctly.
#[doc = crate::clone_semantics_doc!("AlignabilityFacade")]
pub struct AlignabilityFacade(jonmo::Builder);

impl_element_clone!(simple "AlignabilityFacade", AlignabilityFacade);

impl BuilderWrapper for AlignabilityFacade {
    fn builder_mut(&mut self) -> &mut jonmo::Builder {
        &mut self.0
    }
}

impl Alignable for AlignabilityFacade {}

/// Enables returning different concrete [`Element`] types from branching logic without type
/// erasure via [`TypeEraseable::type_erase`].
///
/// Inspired by <https://github.com/rayon-rs/either>.
///
/// # Example
///
/// ```
/// use bevy::prelude::*;
/// use haalka::prelude::*;
///
/// fn conditional_element(use_column: bool) -> impl Element {
///     if use_column {
///         Column::<Node>::new().left_either()
///     } else {
///         Row::<Node>::new().right_either()
///     }
/// }
/// ```
#[allow(missing_docs)]
pub enum ElementEither<L, R>
where
    L: Element,
    R: Element,
{
    Left(L),
    Right(R),
}

impl<L, R> Clone for ElementEither<L, R>
where
    L: Element + Clone,
    R: Element + Clone,
{
    #[doc = crate::clone_error_doc!("ElementEither")]
    #[track_caller]
    fn clone(&self) -> Self {
        let msg = format!(crate::clone_error_msg!("ElementEither"), std::panic::Location::caller());
        if cfg!(debug_assertions) {
            panic!("{}", msg);
        }
        bevy_log::error!("{}", msg);

        match self {
            Self::Left(left) => Self::Left(left.clone()),
            Self::Right(right) => Self::Right(right.clone()),
        }
    }
}

impl<L, R> BuilderWrapper for ElementEither<L, R>
where
    L: Element,
    R: Element,
{
    fn builder_mut(&mut self) -> &mut jonmo::Builder {
        match self {
            ElementEither::Left(left) => left.builder_mut(),
            ElementEither::Right(right) => right.builder_mut(),
        }
    }

    fn into_builder(self) -> jonmo::Builder {
        match self {
            ElementEither::Left(left) => left.into_builder(),
            ElementEither::Right(right) => right.into_builder(),
        }
    }
}

impl<L, R> Alignable for ElementEither<L, R>
where
    L: Element,
    R: Element,
{
}

/// Blanket trait for transforming [`Element`]s into [`ElementEither::Left`] or
/// [`ElementEither::Right`].
pub trait IntoElementEither: Sized
where
    Self: Element,
{
    /// Wrap this [`Element`] in the [`ElementEither::Left`] variant.
    ///
    /// Useful for conditional branching where different [`Element`] types need to be returned
    /// from the same function or closure.
    ///
    /// # Example
    ///
    /// ```
    /// use bevy::prelude::*;
    /// use haalka::prelude::*;
    ///
    /// fn conditional_child(use_column: bool) -> impl Element {
    ///     if use_column {
    ///         Column::<Node>::new()
    ///             .item(El::<Node>::new())
    ///             .left_either()
    ///     } else {
    ///         Row::<Node>::new()
    ///             .item(El::<Node>::new())
    ///             .right_either()
    ///     }
    /// }
    /// ```
    fn left_either<R>(self) -> ElementEither<Self, R>
    where
        R: Element,
    {
        ElementEither::Left(self)
    }

    /// Wrap this [`Element`] in the [`ElementEither::Right`] variant.
    ///
    /// Useful for conditional branching where different [`Element`] types need to be returned
    /// from the same function or closure.
    ///
    /// # Example
    ///
    /// ```
    /// use bevy::prelude::*;
    /// use haalka::prelude::*;
    ///
    /// fn conditional_child(use_stack: bool) -> impl Element {
    ///     if use_stack {
    ///         Stack::<Node>::new()
    ///             .layer(El::<Node>::new())
    ///             .left_either()
    ///     } else {
    ///         El::<Node>::new()
    ///             .right_either()
    ///     }
    /// }
    /// ```
    fn right_either<L>(self) -> ElementEither<L, Self>
    where
        L: Element,
    {
        ElementEither::Right(self)
    }
}

impl<T: Element> IntoElementEither for T {}