Skip to main content

egui_reactor/
lib.rs

1//! Core of egui-reactor: `View`, `Cx`, `Store`, `State`, `Handle`, `Dispatch`,
2//! the hooks and the layout and paint attribute types.
3//!
4//! `rsx!`, `#[component]` and `#[hook]` come from `egui-reactor-macros` and are
5//! re-exported here, so `use egui_reactor::prelude::*` is the only import a
6//! component file needs.
7
8mod context;
9mod cx;
10mod dispatch;
11mod engine;
12mod events;
13mod future;
14mod hooks;
15pub mod layout;
16pub mod paint;
17mod state;
18mod store;
19mod view;
20
21pub use context::{provide_context, use_context};
22pub use cx::Cx;
23pub use dispatch::{Dispatch, use_reducer};
24pub use events::{Arity0, Arity1, Emitter, EventSink, Handler};
25pub use future::{SpawnFuture, spawn, use_future};
26pub use hooks::{
27    FnCleanup, IntoCleanup, NoCleanup, use_animate, use_animate_with, use_effect, use_handle,
28    use_memo, use_persisted, use_state,
29};
30pub use layout::{
31    Align, AlignSelf, ContainerStyle, Direction, Display, Gap, ItemStyle, Justify, Length,
32    PaintStyle,
33};
34pub use state::{Handle, State};
35pub use store::{Collision, Store};
36pub use view::{View, view};
37
38pub use egui_reactor_macros::{component, hook, rsx};
39
40/// The taffy types the layout attributes convert into.
41pub use taffy;
42
43#[doc(hidden)]
44pub mod __private {
45    pub use typed_builder;
46    pub use typed_builder::TypedBuilder;
47
48    /// Implemented by every `#[component]` props struct.
49    pub trait Props {
50        /// The typed-builder builder type for this props struct.
51        type Builder;
52        /// Whether the component draws into its parent's `Ui`.
53        ///
54        /// Set by `#[component(shares_ui)]`. Such a component still gets a hook
55        /// scope of its own, but no `Ui::push_id`, so it can reach the parent's
56        /// layout: that is what docked panels and grid rows need.
57        const SHARES_UI: bool = false;
58        /// Start building.
59        fn builder() -> Self::Builder;
60    }
61
62    /// Enter one element's scope and call it. This is what `rsx!` emits.
63    ///
64    /// `P` is fixed by the `props` value, so the component's generic arguments
65    /// are already settled by the time this is called.
66    pub fn enter_scope<P, F, S>(cx: &mut super::Cx<'_, '_>, source: S, props: P, component: F)
67    where
68        P: Props,
69        S: ::core::hash::Hash + ::core::fmt::Debug,
70        F: for<'a, 'b, 'c> Fn(&'a mut super::Cx<'b, 'c>, P),
71    {
72        if P::SHARES_UI {
73            cx.scope_sharing_ui(source, move |cx| component(cx, props));
74        } else {
75            cx.scope(source, move |cx| component(cx, props));
76        }
77    }
78}
79
80pub use __private::Props;
81
82/// Get the props builder for a component function.
83///
84/// A function item's type cannot be named, so `rsx!` passes the function by
85/// reference and lets the `Fn` bound infer the props type: `<Counter/>` becomes
86/// `Counter(cx, props_builder(&Counter)..build())` and the user only has to
87/// `use` the component itself.
88pub fn props_builder<P, F>(_component: &F) -> P::Builder
89where
90    P: Props,
91    F: for<'a, 's, 'u> Fn(&'a mut Cx<'s, 'u>, P),
92{
93    P::builder()
94}
95
96/// Everything a component needs, in one `use`.
97pub mod prelude {
98    pub use crate::context::{provide_context, use_context};
99    pub use crate::cx::Cx;
100    pub use crate::dispatch::{Dispatch, use_reducer};
101    pub use crate::events::{Emitter, EventSink, Handler};
102    pub use crate::future::{spawn, use_future};
103    pub use crate::hooks::{
104        use_animate, use_animate_with, use_effect, use_handle, use_memo, use_persisted, use_state,
105    };
106    pub use crate::layout::{
107        Align, AlignSelf, ContainerStyle, Direction, Display, Gap, ItemStyle, Justify, Length,
108        PaintStyle,
109    };
110    pub use crate::state::{Handle, State};
111    pub use crate::store::{Collision, Store};
112    pub use crate::view::{View, view};
113    pub use egui_reactor_macros::{component, hook, rsx};
114
115    /// What `use_future` returns. Re-exported so a component file needs no
116    /// `use std::task::Poll` of its own.
117    pub use std::task::Poll;
118}