use core::any::Any;
use core::cell::{Cell, RefCell};
use egui::Ui;
use crate::extract::{FromView, Nav, NavState, PageScroll, ViewStore};
use crate::navigation::Navigation;
use crate::remote::RemoteStore;
use crate::request::Request;
use crate::transition::TransitionHandle;
pub(crate) struct Frame<'a> {
pub(crate) request: &'a Request,
pub(crate) url: &'a str,
pub(crate) ctx: egui::Context,
pub(crate) history_state: Option<&'a (dyn Any + 'static)>,
pub(crate) navigation: &'a Navigation,
pub(crate) page_id: u64,
pub(crate) scroll: &'a PageScroll,
pub(crate) outgoing: bool,
pub(crate) nav: &'a RefCell<NavState>,
pub(crate) remote: &'a RemoteStore,
}
pub struct ViewContext<'a, S> {
state: &'a S,
frame: &'a Frame<'a>,
data: Cell<Option<&'a mut (dyn Any + 'static)>>,
store: &'a ViewStore,
}
impl<'a, S> ViewContext<'a, S> {
pub(crate) fn new(
state: &'a S,
frame: &'a Frame<'a>,
data: &'a mut (dyn Any + 'static),
store: &'a ViewStore,
) -> Self {
Self {
state,
frame,
data: Cell::new(Some(data)),
store,
}
}
pub fn state(&self) -> &S {
self.state
}
pub fn nav(&self) -> Nav<'_> {
Nav { frame: self.frame }
}
pub(crate) fn frame(&self) -> &Frame<'a> {
self.frame
}
pub(crate) fn store(&self) -> &'a ViewStore {
self.store
}
pub(crate) fn take_data(&self) -> &'a mut (dyn Any + 'static) {
self.data
.take()
.expect("egui-kit: a view can only extract its data once")
}
}
#[diagnostic::on_unimplemented(
message = "`{Self}` is not a view: `fn(&mut Ui, extractors...)` with up to 16 view extractors",
note = "views borrow the app state: `State<&T>` rather than `State<T>`, and `HistoryState<&T>`",
note = "the data must be what the `load` returned: `&mut {D}` or `&{D}`",
note = "closures need annotated arguments, e.g. `|ui: &mut Ui, nav: Nav| ...`",
note = "see the `egui_kit::extract` module for the available extractors"
)]
pub trait View<T, S, D>: 'static {
fn call(&self, ui: &mut Ui, cx: &ViewContext<'_, S>);
}
#[diagnostic::on_unimplemented(
message = "`{Self}` is not a layout view: `fn(&mut Ui, Outlet, extractors...)` with up to 16 view extractors",
note = "views borrow the app state: `State<&T>` rather than `State<T>`, and `HistoryState<&T>`",
note = "the data must be what the `load` returned: `&mut {D}` or `&{D}`",
note = "closures need annotated arguments, e.g. `|ui: &mut Ui, nav: Nav| ...`",
note = "see the `egui_kit::extract` module for the available extractors"
)]
pub trait LayoutView<T, S, D>: 'static {
fn call(&self, ui: &mut Ui, outlet: Outlet<'_>, cx: &ViewContext<'_, S>);
}
macro_rules! impl_views {
($($ty:ident),*) => {
#[expect(
clippy::allow_attributes,
reason = "whether the lints fire depends on the arity, so `expect` would be unfulfilled for some"
)]
impl<F, S: 'static, D, $($ty,)*> View<($($ty,)*), S, D> for F
where
F: Fn(&mut Ui, $($ty),*) + 'static,
F: for<'f> Fn(&mut Ui, $(<$ty as FromView<S, D>>::Item<'f>),*),
$($ty: FromView<S, D>,)*
{
#[allow(non_snake_case, unused_variables)]
fn call(&self, ui: &mut Ui, cx: &ViewContext<'_, S>) {
#[allow(clippy::too_many_arguments)]
fn call_inner<$($ty),*>(f: impl Fn(&mut Ui, $($ty),*), ui: &mut Ui, $($ty: $ty),*) {
f(ui, $($ty),*)
}
$(let $ty = $ty::from_view(cx);)*
call_inner(self, ui, $($ty),*)
}
}
#[expect(
clippy::allow_attributes,
reason = "whether the lints fire depends on the arity, so `expect` would be unfulfilled for some"
)]
impl<F, S: 'static, D, $($ty,)*> LayoutView<($($ty,)*), S, D> for F
where
F: Fn(&mut Ui, Outlet<'_>, $($ty),*) + 'static,
F: for<'f> Fn(&mut Ui, Outlet<'_>, $(<$ty as FromView<S, D>>::Item<'f>),*),
$($ty: FromView<S, D>,)*
{
#[allow(non_snake_case, unused_variables)]
fn call(&self, ui: &mut Ui, outlet: Outlet<'_>, cx: &ViewContext<'_, S>) {
#[allow(clippy::too_many_arguments)]
fn call_inner<$($ty),*>(
f: impl Fn(&mut Ui, Outlet<'_>, $($ty),*),
ui: &mut Ui,
outlet: Outlet<'_>,
$($ty: $ty),*
) {
f(ui, outlet, $($ty),*)
}
$(let $ty = $ty::from_view(cx);)*
call_inner(self, ui, outlet, $($ty),*)
}
}
};
}
crate::all_tuples!(impl_views);
pub struct Outlet<'o> {
pub(crate) inner: &'o mut dyn FnMut(&mut Ui),
pub(crate) uses_transitions: &'o Cell<bool>,
pub(crate) transition: Option<&'o mut dyn TransitionHandle>,
}
impl Outlet<'_> {
pub fn show(&mut self, ui: &mut Ui) {
(self.inner)(ui);
}
}