separate state from rsc

This commit is contained in:
iris committed 2026-01-01 22:18:08 -05:00
1 parent 462c0e6416
commit 5da1e9e767
22 files changed
+373 -492

No files matched your search

+8 -106
View File
@@ -1,116 +1,18 @@
use crate::{HasEvents, HasState, HasUi, StateLike, Ui, Widget, WidgetRef};
use std::ops::{Deref, DerefMut};
use crate::{HasEvents, HasUi, Widget, WidgetRef};
pub struct EventCtx<'a, State, Data> {
pub state: &'a mut State,
pub struct EventCtx<'a, Rsc: HasEvents, Data> {
pub state: &'a mut Rsc::State,
pub data: &'a mut Data,
}
pub struct EventIdCtx<'a, State, Data, W: ?Sized> {
pub struct EventIdCtx<'a, Rsc: HasEvents, Data, W: ?Sized> {
pub widget: WidgetRef<W>,
pub state: &'a mut State,
pub state: &'a mut Rsc::State,
pub data: &'a mut Data,
}
impl<State: HasUi, Data, W: ?Sized> Deref for EventIdCtx<'_, State, Data, W> {
type Target = State;
fn deref(&self) -> &Self::Target {
self.state
impl<Rsc: HasEvents + HasUi, Data, W: Widget> EventIdCtx<'_, Rsc, Data, W> {
pub fn widget<'a>(&self, rsc: &'a mut Rsc) -> &'a mut W {
&mut rsc.ui_mut()[self.widget]
}
}
impl<State: HasUi, Data, W: ?Sized> DerefMut for EventIdCtx<'_, State, Data, W> {
fn deref_mut(&mut self) -> &mut Self::Target {
self.state
}
}
impl<State: HasUi, Data, W: Widget> EventIdCtx<'_, State, Data, W> {
pub fn widget(&mut self) -> &mut W {
&mut self.state.get_mut()[self.widget]
}
}
impl<State: HasUi, Data, W: Widget> HasUi for EventIdCtx<'_, State, Data, W> {
fn get(&self) -> &Ui {
self.state.ui()
}
fn get_mut(&mut self) -> &mut Ui {
self.state.ui_mut()
}
}
impl<State: HasUi, Data, W: Widget> HasState for EventIdCtx<'_, State, Data, W> {
type State = State;
}
impl<State: HasEvents<State = State>, Data, W: Widget> HasEvents
for EventIdCtx<'_, State, Data, W>
{
fn get(&self) -> &super::EventManager<Self::State> {
self.state.events()
}
fn get_mut(&mut self) -> &mut super::EventManager<Self::State> {
self.state.events_mut()
}
}
impl<State, Data, W: Widget> StateLike<State> for EventIdCtx<'_, State, Data, W> {
fn as_state(&mut self) -> &mut State {
self.state
}
}
// fn test() {
// use crate::*;
// struct ClientRsc;
// impl<State, Data> HasUi for EventCtx<'_, State, Data> {
// fn get(&self) -> &Ui {
// todo!()
// }
//
// fn get_mut(&mut self) -> &mut Ui {
// todo!()
// }
// }
// fn on(_: impl for<'a> EventFn<ClientRsc, &'a mut i32>) {}
//
// pub trait WidgetLike<State: HasUi, Tag>: Sized {
// type Widget: Widget + ?Sized + std::marker::Unsize<dyn Widget>;
//
// fn add(self, state: &mut State) -> WidgetHandle<Self::Widget>;
//
// fn with_id<W2>(
// self,
// f: impl FnOnce(&mut State, WidgetHandle<Self::Widget>) -> WidgetHandle<W2>,
// ) -> impl WidgetIdFn<State, W2> {
// move |state| {
// let id = self.add(state);
// f(state, id)
// }
// }
//
// fn set_root(self, state: &mut State) {
// state.get_mut().root = Some(self.add(state));
// }
//
// fn handles(self, state: &mut State) -> WidgetHandles<Self::Widget> {
// self.add(state).handles()
// }
// }
//
// pub struct WidgetTag;
// impl<State: HasUi, W: Widget> WidgetLike<State, WidgetTag> for W {
// type Widget = W;
// fn add(self, state: &mut State) -> WidgetHandle<W> {
// state.get_mut().add_widget(self)
// }
// }
//
// on(move |ctx| {
// ().add(ctx);
// });
// }
+28 -25
View File
@@ -1,16 +1,16 @@
use crate::{
ActiveData, Event, EventCtx, EventFn, EventIdCtx, EventLike, IdLike, LayerId, Widget,
WidgetEventFn, WidgetId, WidgetRef,
ActiveData, Event, EventCtx, EventFn, EventIdCtx, EventLike, HasEvents, IdLike, LayerId,
Widget, WidgetEventFn, WidgetId, WidgetRef,
util::{HashMap, HashSet, TypeMap},
};
use std::{any::TypeId, rc::Rc};
pub struct EventManager<State> {
pub struct EventManager<Rsc> {
widget_to_types: HashMap<WidgetId, HashSet<TypeId>>,
types: TypeMap<dyn EventManagerLike<State>>,
types: TypeMap<dyn EventManagerLike<Rsc>>,
}
impl<State> Default for EventManager<State> {
impl<Rsc> Default for EventManager<Rsc> {
fn default() -> Self {
Self {
widget_to_types: Default::default(),
@@ -19,8 +19,8 @@ impl<State> Default for EventManager<State> {
}
}
impl<State: 'static> EventManager<State> {
pub fn get_type<E: EventLike>(&mut self) -> &mut TypeEventManager<State, E::Event> {
impl<Rsc: HasEvents + 'static> EventManager<Rsc> {
pub fn get_type<E: EventLike>(&mut self) -> &mut TypeEventManager<Rsc, E::Event> {
self.types.type_or_default()
}
@@ -28,7 +28,7 @@ impl<State: 'static> EventManager<State> {
&mut self,
id: WidgetRef<W>,
event: E,
f: impl for<'a> WidgetEventFn<State, <E::Event as Event>::Data<'a>, W>,
f: impl for<'a> WidgetEventFn<Rsc, <E::Event as Event>::Data<'a>, W>,
) {
self.get_type::<E>().register(id, event, f);
self.widget_to_types
@@ -38,7 +38,7 @@ impl<State: 'static> EventManager<State> {
}
pub fn type_key<E: EventLike>() -> TypeId {
TypeId::of::<TypeEventManager<State, E::Event>>()
TypeId::of::<TypeEventManager<Rsc, E::Event>>()
}
}
@@ -48,7 +48,7 @@ pub trait EventsLike {
fn undraw(&mut self, active: &ActiveData);
}
impl<State: 'static> EventsLike for EventManager<State> {
impl<Rsc: HasEvents + 'static> EventsLike for EventManager<Rsc> {
fn remove(&mut self, id: WidgetId) {
for t in self.widget_to_types.get(&id).into_flat_iter() {
self.types.get_mut(t).unwrap().remove(id);
@@ -74,14 +74,14 @@ pub trait EventManagerLike<State> {
fn undraw(&mut self, data: &ActiveData);
}
type EventData<State, E> = (E, Rc<dyn for<'a> EventFn<State, <E as Event>::Data<'a>>>);
pub struct TypeEventManager<State, E: Event> {
type EventData<Rsc, E> = (E, Rc<dyn for<'a> EventFn<Rsc, <E as Event>::Data<'a>>>);
pub struct TypeEventManager<Rsc: HasEvents, E: Event> {
// TODO: reduce visiblity!!
pub active: HashMap<LayerId, HashMap<WidgetId, E::State>>,
map: HashMap<WidgetId, Vec<EventData<State, E>>>,
map: HashMap<WidgetId, Vec<EventData<Rsc, E>>>,
}
impl<State, E: Event> EventManagerLike<State> for TypeEventManager<State, E> {
impl<Rsc: HasEvents, E: Event> EventManagerLike<Rsc> for TypeEventManager<Rsc, E> {
fn remove(&mut self, id: WidgetId) {
self.map.remove(&id);
for layer in self.active.values_mut() {
@@ -102,7 +102,7 @@ impl<State, E: Event> EventManagerLike<State> for TypeEventManager<State, E> {
}
}
impl<State, E: Event> Default for TypeEventManager<State, E> {
impl<Rsc: HasEvents, E: Event> Default for TypeEventManager<Rsc, E> {
fn default() -> Self {
Self {
active: Default::default(),
@@ -111,23 +111,23 @@ impl<State, E: Event> Default for TypeEventManager<State, E> {
}
}
impl<State: 'static, E: Event> TypeEventManager<State, E> {
impl<Rsc: HasEvents + 'static, E: Event> TypeEventManager<Rsc, E> {
fn register<W: Widget + ?Sized>(
&mut self,
widget: WidgetRef<W>,
event: impl EventLike<Event = E>,
f: impl for<'a> WidgetEventFn<State, E::Data<'a>, W>,
f: impl for<'a> WidgetEventFn<Rsc, E::Data<'a>, W>,
) {
let event = event.into_event();
self.map.entry(widget.id()).or_default().push((
event,
Rc::new(move |ctx| {
Rc::new(move |ctx, rsc| {
let mut test = EventIdCtx {
widget,
state: ctx.state,
data: ctx.data,
};
f(&mut test);
f(&mut test, rsc);
}),
));
}
@@ -135,15 +135,18 @@ impl<State: 'static, E: Event> TypeEventManager<State, E> {
pub fn run_fn<'a>(
&mut self,
id: impl IdLike,
) -> impl for<'b> FnOnce(EventCtx<'_, State, E::Data<'b>>) + 'a {
) -> impl for<'b> FnOnce(EventCtx<'_, Rsc, E::Data<'b>>, &mut Rsc) + 'a {
let fs = self.map.get(&id.id()).cloned().unwrap_or_default();
move |ctx| {
move |ctx, rsc| {
for (e, f) in fs {
if e.should_run(ctx.data) {
f(&mut EventCtx {
state: ctx.state,
data: ctx.data,
})
f(
&mut EventCtx {
state: ctx.state,
data: ctx.data,
},
rsc,
)
}
}
}
+9 -6
View File
@@ -28,14 +28,17 @@ impl<E: Event> EventLike for E {
}
}
pub trait EventFn<State, Data>: Fn(&mut EventCtx<State, Data>) + 'static {}
impl<State, F: Fn(&mut EventCtx<State, Data>) + 'static, Data> EventFn<State, Data> for F {}
pub trait EventFn<Rsc: HasEvents, Data>: Fn(&mut EventCtx<Rsc, Data>, &mut Rsc) + 'static {}
impl<Rsc: HasEvents, F: Fn(&mut EventCtx<Rsc, Data>, &mut Rsc) + 'static, Data> EventFn<Rsc, Data>
for F
{
}
pub trait WidgetEventFn<State, Data, W: ?Sized>:
Fn(&mut EventIdCtx<State, Data, W>) + 'static
pub trait WidgetEventFn<Rsc: HasEvents, Data, W: ?Sized>:
Fn(&mut EventIdCtx<Rsc, Data, W>, &mut Rsc) + 'static
{
}
impl<State, F: Fn(&mut EventIdCtx<State, Data, W>) + 'static, Data, W: ?Sized>
WidgetEventFn<State, Data, W> for F
impl<Rsc: HasEvents, F: Fn(&mut EventIdCtx<Rsc, Data, W>, &mut Rsc) + 'static, Data, W: ?Sized>
WidgetEventFn<Rsc, Data, W> for F
{
}
+10 -19
View File
@@ -2,40 +2,31 @@ use crate::{
Event, EventCtx, EventLike, EventManager, HasUi, IdLike, Widget, WidgetEventFn, WidgetRef,
};
pub trait HasState {
type State: HasUi;
}
pub trait HasEvents: Sized + HasUi + 'static {
type State;
pub trait HasEvents: Sized + HasState + HasUi {
fn get(&self) -> &EventManager<Self::State>;
fn get_mut(&mut self) -> &mut EventManager<Self::State>;
fn events(&self) -> &EventManager<Self::State> {
HasEvents::get(self)
}
fn events_mut(&mut self) -> &mut EventManager<Self::State> {
HasEvents::get_mut(self)
}
fn events(&self) -> &EventManager<Self>;
fn events_mut(&mut self) -> &mut EventManager<Self>;
fn register_event<W: Widget + ?Sized, E: EventLike>(
&mut self,
id: WidgetRef<W>,
event: E,
f: impl for<'a> WidgetEventFn<Self::State, <E::Event as Event>::Data<'a>, W>,
) where
Self::State: 'static,
{
f: impl for<'a> WidgetEventFn<Self, <E::Event as Event>::Data<'a>, W>,
) {
self.events_mut().register(id, event, f);
}
}
pub trait RunEvents: HasEvents + HasState<State = Self> + 'static {
pub trait RunEvents: HasEvents + 'static {
fn run_event<E: EventLike>(
&mut self,
id: impl IdLike,
data: &mut <E::Event as Event>::Data<'_>,
state: &mut Self::State,
) {
let f = self.events_mut().get_type::<E>().run_fn(id);
f(EventCtx { state: self, data })
f(EventCtx { state, data }, self)
}
}
impl<T: HasEvents + HasState<State = Self> + 'static> RunEvents for T {}
impl<T: HasEvents + 'static> RunEvents for T {}