separate state from rsc
This commit is contained in:
1 parent
462c0e6416
commit
5da1e9e767
22 files changed
+373
-492
No files matched your search
+8
-106
@@ -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);
|
||||
// });
|
||||
// }
|
||||
Reference in new issue
Block a user