use core::any::Any;
use core::cell::{Cell, RefCell};
use egui::Ui;
use crate::extract::{FromView, Nav, NavState, PageScroll, ViewStore};
use crate::load::Loaded;
use crate::navigation::Navigation;
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) enum ViewData<'a> {
Mut(&'a mut (dyn Any + 'static)),
Shared(&'a (dyn Any + 'static)),
}
pub struct ViewContext<'a, S> {
state: &'a S,
frame: &'a Frame<'a>,
data: Cell<Option<ViewData<'a>>>,
store: &'a ViewStore,
layouts: &'a [Option<Loaded>],
}
impl<'a, S> ViewContext<'a, S> {
pub(crate) fn new(
state: &'a S,
frame: &'a Frame<'a>,
data: ViewData<'a>,
store: &'a ViewStore,
layouts: &'a [Option<Loaded>],
) -> Self {
Self {
state,
frame,
data: Cell::new(Some(data)),
store,
layouts,
}
}
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 peek_data<R>(&self, f: impl FnOnce(&dyn Any) -> R) -> R {
let data = self
.data
.take()
.expect("egui-kit: the view's data was already extracted (this is a bug)");
let result = f(match &data {
ViewData::Mut(data) => &**data,
ViewData::Shared(data) => *data,
});
self.data.set(Some(data));
result
}
pub(crate) fn take_data(&self) -> &'a (dyn Any + 'static) {
match self.take() {
ViewData::Mut(data) => data,
ViewData::Shared(data) => data,
}
}
pub(crate) fn take_data_mut(&self) -> &'a mut (dyn Any + 'static) {
match self.take() {
ViewData::Mut(data) => data,
ViewData::Shared(_) => panic!("egui-kit: the data is shared (this is a bug)"),
}
}
fn take(&self) -> ViewData<'a> {
self.data
.take()
.expect("egui-kit: a view can only extract its data once")
}
pub(crate) fn layouts(&self) -> &'a [Option<Loaded>] {
self.layouts
}
}
#[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, Children, 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, children: Children<'_>, cx: &ViewContext<'_, S>);
#[doc(hidden)]
fn borrows_data_mut(&self) -> bool {
true
}
}
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),*)
}
$($ty::prepare(cx);)*
$(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, Children<'_>, $($ty),*) + 'static,
F: for<'f> Fn(&mut Ui, Children<'_>, $(<$ty as FromView<S, D>>::Item<'f>),*),
$($ty: FromView<S, D>,)*
{
#[allow(non_snake_case, unused_variables)]
fn call(&self, ui: &mut Ui, children: Children<'_>, cx: &ViewContext<'_, S>) {
#[allow(clippy::too_many_arguments)]
fn call_inner<$($ty),*>(
f: impl Fn(&mut Ui, Children<'_>, $($ty),*),
ui: &mut Ui,
children: Children<'_>,
$($ty: $ty),*
) {
f(ui, children, $($ty),*)
}
$($ty::prepare(cx);)*
$(let $ty = $ty::from_view(cx);)*
call_inner(self, ui, children, $($ty),*)
}
fn borrows_data_mut(&self) -> bool {
[$(<$ty as FromView<S, D>>::BORROWS_DATA_MUT),*].contains(&true)
}
}
};
}
crate::all_tuples!(impl_views);
pub struct Children<'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 Children<'_> {
pub fn show(&mut self, ui: &mut Ui) {
(self.inner)(ui);
}
}