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

@@ -1,20 +1,22 @@
use crate::{
ActiveData, Axis, Painter, SizeCtx, Ui, UiRegion, UiVec2, WidgetId,
ActiveData, Axis, EventsLike, Painter, SizeCtx, Ui, UiRegion, UiVec2, WidgetId,
render::MaskIdx,
util::{HashSet, forget_ref},
};
use std::ops::{Deref, DerefMut};
/// state maintained between widgets during painting
pub struct DrawState<'a, State> {
pub(super) ui: &'a mut Ui<State>,
pub struct DrawState<'a> {
pub(super) ui: &'a mut Ui,
pub(super) events: &'a mut dyn EventsLike,
draw_started: HashSet<WidgetId>,
}
impl<'a, State: 'static> DrawState<'a, State> {
pub fn new(ui: &'a mut Ui<State>) -> Self {
impl<'a> DrawState<'a> {
pub fn new(ui: &'a mut Ui, events: &'a mut dyn EventsLike) -> Self {
Self {
ui,
events,
draw_started: Default::default(),
}
}
@@ -23,12 +25,10 @@ impl<'a, State: 'static> DrawState<'a, State> {
while let Some(&id) = self.widgets.needs_redraw.iter().next() {
self.redraw(id);
}
self.free();
self.ui.free(self.events);
}
/// redraws a widget that's currently active (drawn)
/// can be called on something already drawn or removed,
/// will just return if so
pub fn redraw(&mut self, id: WidgetId) {
self.widgets.needs_redraw.remove(&id);
self.draw_started.remove(&id);
@@ -65,11 +65,7 @@ impl<'a, State: 'static> DrawState<'a, State> {
);
}
pub(super) fn size_ctx<'b>(
&'b mut self,
source: WidgetId,
outer: UiVec2,
) -> SizeCtx<'b, State> {
pub(super) fn size_ctx<'b>(&'b mut self, source: WidgetId, outer: UiVec2) -> SizeCtx<'b> {
SizeCtx {
source,
cache: &mut self.ui.cache,
@@ -86,10 +82,10 @@ impl<'a, State: 'static> DrawState<'a, State> {
// free all resources & cache
for (id, active) in self.ui.active.drain() {
let data = self.ui.widgets.data(id).unwrap();
self.ui.events.undraw(data, &active);
self.events.undraw(data, &active);
}
self.ui.cache.clear();
self.free();
self.ui.free(self.events);
self.layers.clear();
self.widgets.needs_redraw.clear();
@@ -107,26 +103,11 @@ impl<'a, State: 'static> DrawState<'a, State> {
mask: MaskIdx,
old_children: Option<Vec<WidgetId>>,
) {
// I have no idea if these checks work lol
// the idea is u can't redraw stuff u already drew,
// and if parent is different then there's another copy with a different parent
// but this has a very weird issue where you can't move widgets unless u remove first
// so swapping is impossible rn I think?
// there's definitely better solutions like a counter (>1 = panic) but don't care rn
// if self.draw_started.contains(&id) {
// panic!(
// "Cannot draw the same widget ({}) twice (1)",
// self.widgets.data(&id).unwrap().label
// );
// }
let mut old_children = old_children.unwrap_or_default();
if let Some(active) = self.ui.active.get_mut(&id)
&& !self.ui.widgets.needs_redraw.contains(&id)
{
// check to see if we can skip drawing first
if active.parent != parent {
panic!("Cannot draw the same widget twice (2)");
}
if active.region == region {
return;
} else if active.region.size() == region.size() {
@@ -190,7 +171,7 @@ impl<'a, State: 'static> DrawState<'a, State> {
// update modules
let data = self.ui.widgets.data(id).unwrap();
self.ui.events.draw(data, &active);
self.events.draw(data, &active);
self.active.insert(id, active);
}
@@ -222,7 +203,7 @@ impl<'a, State: 'static> DrawState<'a, State> {
self.textures.free();
if undraw {
let data = self.ui.widgets.data(id).unwrap();
self.ui.events.undraw(data, active);
self.events.undraw(data, active);
}
}
active
@@ -240,14 +221,14 @@ impl<'a, State: 'static> DrawState<'a, State> {
}
}
impl<State> Deref for DrawState<'_, State> {
type Target = Ui<State>;
impl Deref for DrawState<'_> {
type Target = Ui;
fn deref(&self) -> &Self::Target {
self.ui
}
}
impl<State> DerefMut for DrawState<'_, State> {
impl DerefMut for DrawState<'_> {
fn deref_mut(&mut self) -> &mut Self::Target {
self.ui
}