remove state generic from a lot of things

This commit is contained in:
2025-12-17 21:37:55 -05:00
parent 7e6369029f
commit 30bc55c78e
45 changed files with 740 additions and 856 deletions

View File

@@ -24,17 +24,57 @@ pub use sense::*;
pub type Proxy<Event> = EventLoopProxy<Event>;
pub struct UiState<State> {
pub struct DefaultUiRsc<State> {
pub ui: Ui,
pub ui_state: UiState,
pub events: EventManager<State>,
}
impl<State> DefaultUiRsc<State> {
pub fn new(window: impl Into<Arc<Window>>) -> Self {
Self {
ui: Ui::new(),
ui_state: UiState::new(window),
events: EventManager::default(),
}
}
}
impl<State: 'static> HasUi for DefaultUiRsc<State> {
fn ui_ref(&self) -> &Ui {
&self.ui
}
fn ui(&mut self) -> &mut Ui {
&mut self.ui
}
}
impl<State: 'static + HasRsc<Rsc = Self>> HasEvents for DefaultUiRsc<State> {
type State = State;
fn events(&mut self) -> &mut EventManager<State> {
&mut self.events
}
}
impl<State: 'static> HasUiState for DefaultUiRsc<State> {
fn ui_state(&mut self) -> &mut UiState {
&mut self.ui_state
}
}
pub struct UiState {
pub renderer: UiRenderer,
pub input: Input,
pub focus: Option<WidgetRef<State, TextEdit<State>>>,
pub focus: Option<WidgetRef<TextEdit>>,
pub clipboard: Clipboard,
pub window: Arc<Window>,
pub ime: usize,
pub last_click: Instant,
}
impl<State> UiState<State> {
impl UiState {
pub fn new(window: impl Into<Arc<Window>>) -> Self {
let window = window.into();
Self {
@@ -50,14 +90,23 @@ impl<State> UiState<State> {
}
pub trait HasUiState: Sized + 'static + HasUi {
fn ui_state(&mut self) -> &mut UiState<Self>;
fn ui_with_ui_state(&mut self) -> (&mut Ui<Self>, &mut UiState<Self>) {
fn ui_state(&mut self) -> &mut UiState;
fn ui_with_ui_state(&mut self) -> (&mut Ui, &mut UiState) {
// as long as you're not doing anything actually unhinged this should always work safely
(unsafe { forget_mut(self.ui()) }, self.ui_state())
}
}
pub trait DefaultAppState: Sized + 'static + HasUi + HasUiState {
impl<R: HasRsc> HasUiState for R
where
R::Rsc: HasUiState,
{
fn ui_state(&mut self) -> &mut UiState {
self.rsc_mut().ui_state()
}
}
pub trait DefaultAppState: Sized + 'static + HasRsc + HasUi + HasUiState {
type Event: 'static = ();
fn new(event_loop: &ActiveEventLoop, proxy: Proxy<Self::Event>) -> Self;
#[allow(unused_variables)]
@@ -80,6 +129,7 @@ impl<State: DefaultAppState> AppState for State {
}
fn window_event(&mut self, event: WindowEvent, event_loop: &ActiveEventLoop) {
let events = unsafe { forget_mut(self.events()) };
let ui_state = self.ui_state();
let input_changed = ui_state.input.event(&event);
let cursor_state = ui_state.cursor_state().clone();
@@ -100,7 +150,7 @@ impl<State: DefaultAppState> AppState for State {
match &event {
WindowEvent::CloseRequested => event_loop.exit(),
WindowEvent::RedrawRequested => {
ui.update();
ui.update(events);
ui_state.renderer.update(ui);
ui_state.renderer.draw();
}