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

@@ -4,11 +4,11 @@ use winit::dpi::{LogicalPosition, LogicalSize};
pub struct Selector;
impl<State: HasUiState, W: Widget<State> + 'static> WidgetAttr<State, W> for Selector {
type Input = WidgetRef<State, TextEdit<State>>;
impl<State: HasUiState + HasEvents, W: Widget + 'static> WidgetAttr<State, W> for Selector {
type Input = WidgetRef<TextEdit>;
fn run(ui: &mut Ui<State>, container: WidgetRef<State, W>, id: Self::Input) {
ui.register_event(container, CursorSense::click_or_drag(), move |ctx| {
fn run(state: &mut State, container: WidgetRef<W>, id: Self::Input) {
state.register_event(container, CursorSense::click_or_drag(), move |ctx| {
let region = ctx.state.ui().window_region(&id).unwrap();
let id_pos = region.top_left;
let container_pos = ctx.state.ui().window_region(&container).unwrap().top_left;
@@ -21,11 +21,11 @@ impl<State: HasUiState, W: Widget<State> + 'static> WidgetAttr<State, W> for Sel
pub struct Selectable;
impl<State: HasUiState> WidgetAttr<State, TextEdit<State>> for Selectable {
impl<State: HasUiState + HasEvents> WidgetAttr<State, TextEdit> for Selectable {
type Input = ();
fn run(ui: &mut Ui<State>, id: WidgetRef<State, TextEdit<State>>, _: Self::Input) {
ui.register_event(id, CursorSense::click_or_drag(), move |ctx| {
fn run(state: &mut State, id: WidgetRef<TextEdit>, _: Self::Input) {
state.register_event(id, CursorSense::click_or_drag(), move |ctx| {
select(
ctx.state,
id,
@@ -37,9 +37,9 @@ impl<State: HasUiState> WidgetAttr<State, TextEdit<State>> for Selectable {
}
}
fn select<State: 'static + HasUiState>(
state: &mut State,
id: WidgetRef<State, TextEdit<State>>,
fn select(
state: &mut impl HasUiState,
id: WidgetRef<TextEdit>,
pos: Vec2,
size: Vec2,
dragging: bool,

View File

@@ -66,7 +66,7 @@ impl Input {
}
}
impl<State> UiState<State> {
impl UiState {
pub fn window_size(&self) -> Vec2 {
let size = self.renderer.window().inner_size();
(size.width, size.height).into()

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();
}

View File

@@ -18,7 +18,7 @@ pub struct UiRenderer {
}
impl UiRenderer {
pub fn update<State>(&mut self, ui: &mut Ui<State>) {
pub fn update(&mut self, ui: &mut Ui) {
self.ui.update(&self.device, &self.queue, ui);
}

View File

@@ -142,11 +142,10 @@ pub trait SensorUi<State> {
fn run_sensors(&mut self, cursor: &CursorState, window_size: Vec2);
}
impl<State: 'static + HasUi> SensorUi<State> for State {
impl<State: 'static + HasRsc> SensorUi<State> for State {
fn run_sensors(&mut self, cursor: &CursorState, window_size: Vec2) {
let layers = std::mem::take(&mut self.ui().layers);
let mut active =
std::mem::take(&mut self.ui().events.get_type::<CursorSense>().active);
let mut active = std::mem::take(&mut self.events().get_type::<CursorSense>().active);
for layer in layers.indices().rev() {
let mut sensed = false;
for (id, sensor) in active.get_mut(&layer).into_iter().flatten() {
@@ -175,7 +174,7 @@ impl<State: 'static + HasUi> SensorUi<State> for State {
break;
}
}
self.ui().events.get_type::<CursorSense>().active = active;
self.events().get_type::<CursorSense>().active = active;
self.ui().layers = layers;
}
}