1use core::any::Any;
5use core::cell::{Cell, RefCell};
6
7use egui::Ui;
8
9use crate::extract::{FromView, Nav, NavState, PageScroll, ViewStore};
10use crate::navigation::Navigation;
11use crate::remote::RemoteStore;
12use crate::request::Request;
13use crate::transition::TransitionHandle;
14
15pub(crate) struct Frame<'a> {
17 pub(crate) request: &'a Request,
18 pub(crate) url: &'a str,
19 pub(crate) ctx: egui::Context,
20 pub(crate) history_state: Option<&'a (dyn Any + 'static)>,
21 pub(crate) navigation: &'a Navigation,
22 pub(crate) page_id: u64,
23 pub(crate) scroll: &'a PageScroll,
24 pub(crate) outgoing: bool,
26 pub(crate) nav: &'a RefCell<NavState>,
27 pub(crate) remote: &'a RemoteStore,
28}
29
30pub struct ViewContext<'a, S> {
32 state: &'a S,
33 frame: &'a Frame<'a>,
34 data: Cell<Option<&'a mut (dyn Any + 'static)>>,
36 store: &'a ViewStore,
37}
38
39impl<'a, S> ViewContext<'a, S> {
40 pub(crate) fn new(
41 state: &'a S,
42 frame: &'a Frame<'a>,
43 data: &'a mut (dyn Any + 'static),
44 store: &'a ViewStore,
45 ) -> Self {
46 Self {
47 state,
48 frame,
49 data: Cell::new(Some(data)),
50 store,
51 }
52 }
53
54 pub fn state(&self) -> &S {
55 self.state
56 }
57
58 pub fn nav(&self) -> Nav<'_> {
59 Nav { frame: self.frame }
60 }
61
62 pub(crate) fn frame(&self) -> &Frame<'a> {
63 self.frame
64 }
65
66 pub(crate) fn store(&self) -> &'a ViewStore {
68 self.store
69 }
70
71 pub(crate) fn take_data(&self) -> &'a mut (dyn Any + 'static) {
73 self.data
74 .take()
75 .expect("egui-kit: a view can only extract its data once")
76 }
77}
78
79#[diagnostic::on_unimplemented(
96 message = "`{Self}` is not a view: `fn(&mut Ui, extractors...)` with up to 16 view extractors",
97 note = "views borrow the app state: `State<&T>` rather than `State<T>`, and `HistoryState<&T>`",
98 note = "the data must be what the `load` returned: `&mut {D}` or `&{D}`",
99 note = "closures need annotated arguments, e.g. `|ui: &mut Ui, nav: Nav| ...`",
100 note = "see the `egui_kit::extract` module for the available extractors"
101)]
102pub trait View<T, S, D>: 'static {
103 fn call(&self, ui: &mut Ui, cx: &ViewContext<'_, S>);
104}
105
106#[diagnostic::on_unimplemented(
108 message = "`{Self}` is not a layout view: `fn(&mut Ui, Outlet, extractors...)` with up to 16 view extractors",
109 note = "views borrow the app state: `State<&T>` rather than `State<T>`, and `HistoryState<&T>`",
110 note = "the data must be what the `load` returned: `&mut {D}` or `&{D}`",
111 note = "closures need annotated arguments, e.g. `|ui: &mut Ui, nav: Nav| ...`",
112 note = "see the `egui_kit::extract` module for the available extractors"
113)]
114pub trait LayoutView<T, S, D>: 'static {
115 fn call(&self, ui: &mut Ui, outlet: Outlet<'_>, cx: &ViewContext<'_, S>);
116}
117
118macro_rules! impl_views {
121 ($($ty:ident),*) => {
122 #[expect(
123 clippy::allow_attributes,
124 reason = "whether the lints fire depends on the arity, so `expect` would be unfulfilled for some"
125 )]
126 impl<F, S: 'static, D, $($ty,)*> View<($($ty,)*), S, D> for F
127 where
128 F: Fn(&mut Ui, $($ty),*) + 'static,
129 F: for<'f> Fn(&mut Ui, $(<$ty as FromView<S, D>>::Item<'f>),*),
130 $($ty: FromView<S, D>,)*
131 {
132 #[allow(non_snake_case, unused_variables)]
133 fn call(&self, ui: &mut Ui, cx: &ViewContext<'_, S>) {
134 #[allow(clippy::too_many_arguments)]
135 fn call_inner<$($ty),*>(f: impl Fn(&mut Ui, $($ty),*), ui: &mut Ui, $($ty: $ty),*) {
136 f(ui, $($ty),*)
137 }
138 $(let $ty = $ty::from_view(cx);)*
139 call_inner(self, ui, $($ty),*)
140 }
141 }
142
143 #[expect(
144 clippy::allow_attributes,
145 reason = "whether the lints fire depends on the arity, so `expect` would be unfulfilled for some"
146 )]
147 impl<F, S: 'static, D, $($ty,)*> LayoutView<($($ty,)*), S, D> for F
148 where
149 F: Fn(&mut Ui, Outlet<'_>, $($ty),*) + 'static,
150 F: for<'f> Fn(&mut Ui, Outlet<'_>, $(<$ty as FromView<S, D>>::Item<'f>),*),
151 $($ty: FromView<S, D>,)*
152 {
153 #[allow(non_snake_case, unused_variables)]
154 fn call(&self, ui: &mut Ui, outlet: Outlet<'_>, cx: &ViewContext<'_, S>) {
155 #[allow(clippy::too_many_arguments)]
156 fn call_inner<$($ty),*>(
157 f: impl Fn(&mut Ui, Outlet<'_>, $($ty),*),
158 ui: &mut Ui,
159 outlet: Outlet<'_>,
160 $($ty: $ty),*
161 ) {
162 f(ui, outlet, $($ty),*)
163 }
164 $(let $ty = $ty::from_view(cx);)*
165 call_inner(self, ui, outlet, $($ty),*)
166 }
167 }
168 };
169}
170
171crate::all_tuples!(impl_views);
172
173pub struct Outlet<'o> {
178 pub(crate) inner: &'o mut dyn FnMut(&mut Ui),
179 pub(crate) uses_transitions: &'o Cell<bool>,
181 pub(crate) transition: Option<&'o mut dyn TransitionHandle>,
182}
183
184impl Outlet<'_> {
185 pub fn show(&mut self, ui: &mut Ui) {
186 (self.inner)(ui);
187 }
188}