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
}

View File

@@ -1,6 +1,6 @@
use crate::{
Event, EventFn, EventLike, EventManager, IdLike, Mask, PixelRegion, PrimitiveLayers, TextData,
TextureHandle, Textures, Widget, WidgetHandle, WidgetId, WidgetLike, Widgets,
EventsLike, IdLike, Mask, PixelRegion, PrimitiveLayers, TextData, TextureHandle, Textures,
Widget, WidgetHandle, WidgetId, Widgets,
ui::draw_state::DrawState,
util::{HashMap, TrackedArena, Vec2},
};
@@ -22,10 +22,9 @@ use cache::*;
pub use painter::Painter;
pub use size::*;
pub struct Ui<State> {
pub struct Ui {
// TODO: edit visibilities
pub widgets: Widgets<State>,
pub events: EventManager<State>,
pub widgets: Widgets,
// retained painter state
pub active: HashMap<WidgetId, ActiveData>,
@@ -36,56 +35,44 @@ pub struct Ui<State> {
pub masks: TrackedArena<Mask, u32>,
pub cache: Cache,
root: Option<WidgetHandle<State>>,
pub root: Option<WidgetHandle>,
old_root: Option<WidgetId>,
recv: Receiver<WidgetId>,
send: Sender<WidgetId>,
full_redraw: bool,
resized: bool,
}
pub trait HasUi: Sized {
fn ui_ref(&self) -> &Ui<Self>;
fn ui(&mut self) -> &mut Ui<Self>;
pub trait HasUi: Sized + 'static {
fn ui_ref(&self) -> &Ui;
fn ui(&mut self) -> &mut Ui;
}
impl<State: 'static> Ui<State> {
pub fn add<W: Widget<State>, Tag>(
&mut self,
w: impl WidgetLike<State, Tag, Widget = W>,
) -> WidgetHandle<State, W> {
w.add(self)
}
impl Ui {
/// useful for debugging
pub fn set_label(&mut self, id: impl IdLike<State>, label: String) {
pub fn set_label(&mut self, id: impl IdLike, label: String) {
self.widgets.data_mut(id.id()).unwrap().label = label;
}
pub fn label(&self, id: impl IdLike<State>) -> &String {
pub fn label(&self, id: impl IdLike) -> &String {
&self.widgets.data(id.id()).unwrap().label
}
pub fn add_widget<W: Widget<State>>(&mut self, w: W) -> WidgetHandle<State, W> {
pub fn add_widget<W: Widget>(&mut self, w: W) -> WidgetHandle<W> {
WidgetHandle::new(self.widgets.add(w), self.send.clone())
}
pub fn set_root<Tag>(&mut self, w: impl WidgetLike<State, Tag>) {
self.root = Some(w.add(self));
self.full_redraw = true;
}
pub fn new() -> Self {
Self::default()
}
pub fn get<I: IdLike<State>>(&self, id: &I) -> Option<&I::Widget>
pub fn get<I: IdLike>(&self, id: &I) -> Option<&I::Widget>
where
I::Widget: Sized,
{
self.widgets.get(id)
}
pub fn get_mut<I: IdLike<State>>(&mut self, id: &I) -> Option<&mut I::Widget>
pub fn get_mut<I: IdLike>(&mut self, id: &I) -> Option<&mut I::Widget>
where
I::Widget: Sized,
{
@@ -96,49 +83,39 @@ impl<State: 'static> Ui<State> {
self.textures.add(image)
}
pub fn register_event<E: EventLike>(
&mut self,
id: impl IdLike<State>,
event: E,
f: impl for<'a> EventFn<State, <E::Event as Event>::Data<'a>>,
) {
self.events.register(id.id(), event, f);
self.widgets
.data_mut(id)
.unwrap()
.event_mgrs
.insert(EventManager::<State>::type_key::<E>());
}
pub fn resize(&mut self, size: impl Into<Vec2>) {
self.output_size = size.into();
self.resized = true;
}
pub fn update(&mut self) {
if self.full_redraw {
DrawState::new(self).redraw_all();
self.full_redraw = false;
pub fn update(&mut self, events: &mut dyn EventsLike) {
if self.root_changed() {
DrawState::new(self, events).redraw_all();
self.old_root = self.root.as_ref().map(|r| r.id());
} else if self.widgets.has_updates() {
DrawState::new(self).redraw_updates();
DrawState::new(self, events).redraw_updates();
}
if self.resized {
self.resized = false;
DrawState::new(self).redraw_all();
DrawState::new(self, events).redraw_all();
}
}
/// free any resources that don't have references anymore
fn free(&mut self) {
fn free(&mut self, events: &mut dyn EventsLike) {
for id in self.recv.try_iter() {
self.events.remove(id);
events.remove(id);
self.widgets.delete(id);
}
self.textures.free();
}
pub fn root_changed(&self) -> bool {
self.root.as_ref().map(|r| r.id()) != self.old_root
}
pub fn needs_redraw(&self) -> bool {
self.full_redraw || self.widgets.has_updates()
self.root_changed() || self.widgets.has_updates()
}
pub fn num_widgets(&self) -> usize {
@@ -161,7 +138,7 @@ impl<State: 'static> Ui<State> {
}
}
pub fn window_region(&self, id: &impl IdLike<State>) -> Option<PixelRegion> {
pub fn window_region(&self, id: &impl IdLike) -> Option<PixelRegion> {
let region = self.active.get(&id.id())?.region;
Some(region.to_px(self.output_size))
}
@@ -174,7 +151,7 @@ impl<State: 'static> Ui<State> {
}
}
impl<State: 'static, I: IdLike<State>> Index<I> for Ui<State>
impl<I: IdLike> Index<I> for Ui
where
I::Widget: Sized,
{
@@ -185,7 +162,7 @@ where
}
}
impl<State: 'static, I: IdLike<State>> IndexMut<I> for Ui<State>
impl<I: IdLike> IndexMut<I> for Ui
where
I::Widget: Sized,
{
@@ -194,12 +171,11 @@ where
}
}
impl<State: 'static> Default for Ui<State> {
impl Default for Ui {
fn default() -> Self {
let (send, recv) = channel();
Self {
widgets: Default::default(),
events: Default::default(),
active: Default::default(),
layers: Default::default(),
masks: Default::default(),
@@ -207,8 +183,8 @@ impl<State: 'static> Default for Ui<State> {
textures: Default::default(),
cache: Default::default(),
output_size: Vec2::ZERO,
root: Default::default(),
full_redraw: false,
root: None,
old_root: None,
send,
recv,
resized: false,

View File

@@ -7,8 +7,8 @@ use crate::{
};
/// makes your surfaces look pretty
pub struct Painter<'a, 'b, State> {
pub(super) state: &'a mut DrawState<'b, State>,
pub struct Painter<'a, 'b> {
pub(super) state: &'a mut DrawState<'b>,
pub(super) region: UiRegion,
pub(super) mask: MaskIdx,
@@ -19,7 +19,7 @@ pub struct Painter<'a, 'b, State> {
pub(super) id: WidgetId,
}
impl<'a, 'c, State: 'static> Painter<'a, 'c, State> {
impl<'a, 'c> Painter<'a, 'c> {
fn primitive_at<P: Primitive>(&mut self, primitive: P, region: UiRegion) {
let h = self.state.layers.write(
self.layer,
@@ -52,17 +52,17 @@ impl<'a, 'c, State: 'static> Painter<'a, 'c, State> {
}
/// Draws a widget within this widget's region.
pub fn widget<W: ?Sized>(&mut self, id: &WidgetHandle<State, W>) {
pub fn widget<W: ?Sized>(&mut self, id: &WidgetHandle<W>) {
self.widget_at(id, self.region);
}
/// Draws a widget somewhere within this one.
/// Useful for drawing child widgets in select areas.
pub fn widget_within<W: ?Sized>(&mut self, id: &WidgetHandle<State, W>, region: UiRegion) {
pub fn widget_within<W: ?Sized>(&mut self, id: &WidgetHandle<W>, region: UiRegion) {
self.widget_at(id, region.within(&self.region));
}
fn widget_at<W: ?Sized>(&mut self, id: &WidgetHandle<State, W>, region: UiRegion) {
fn widget_at<W: ?Sized>(&mut self, id: &WidgetHandle<W>, region: UiRegion) {
self.children.push(id.id());
self.state
.draw_inner(self.layer, id.id(), region, Some(self.id), self.mask, None);
@@ -95,15 +95,11 @@ impl<'a, 'c, State: 'static> Painter<'a, 'c, State> {
self.region
}
pub fn size<W: ?Sized + Widget<State>>(&mut self, id: &WidgetHandle<State, W>) -> Size {
pub fn size<W: ?Sized + Widget>(&mut self, id: &WidgetHandle<W>) -> Size {
self.size_ctx().size(id)
}
pub fn len_axis<W: ?Sized + Widget<State>>(
&mut self,
id: &WidgetHandle<State, W>,
axis: Axis,
) -> Len {
pub fn len_axis<W: ?Sized + Widget>(&mut self, id: &WidgetHandle<W>, axis: Axis) -> Len {
match axis {
Axis::X => self.size_ctx().width(id),
Axis::Y => self.size_ctx().height(id),
@@ -138,7 +134,7 @@ impl<'a, 'c, State: 'static> Painter<'a, 'c, State> {
&self.id
}
pub fn size_ctx(&mut self) -> SizeCtx<'_, State> {
pub fn size_ctx(&mut self) -> SizeCtx<'_> {
self.state.size_ctx(self.id, self.region.size())
}
}

View File

@@ -3,11 +3,11 @@ use crate::{
UiVec2, WidgetAxisFns, WidgetId, Widgets, XAxis, YAxis, ui::cache::Cache, util::Vec2,
};
pub struct SizeCtx<'a, State> {
pub struct SizeCtx<'a> {
pub text: &'a mut TextData,
pub textures: &'a mut Textures,
pub(super) source: WidgetId,
pub(super) widgets: &'a Widgets<State>,
pub(super) widgets: &'a Widgets,
pub(super) cache: &'a mut Cache,
/// TODO: should this be pub? rn used for sized
pub outer: UiVec2,
@@ -15,7 +15,7 @@ pub struct SizeCtx<'a, State> {
pub(super) id: WidgetId,
}
impl<State: 'static> SizeCtx<'_, State> {
impl SizeCtx<'_> {
pub fn id(&self) -> &WidgetId {
&self.id
}
@@ -45,22 +45,22 @@ impl<State: 'static> SizeCtx<'_, State> {
len
}
pub fn width(&mut self, id: impl IdLike<State>) -> Len {
pub fn width(&mut self, id: impl IdLike) -> Len {
self.len_inner::<XAxis>(id.id())
}
pub fn height(&mut self, id: impl IdLike<State>) -> Len {
pub fn height(&mut self, id: impl IdLike) -> Len {
self.len_inner::<YAxis>(id.id())
}
pub fn len_axis(&mut self, id: impl IdLike<State>, axis: Axis) -> Len {
pub fn len_axis(&mut self, id: impl IdLike, axis: Axis) -> Len {
match axis {
Axis::X => self.width(id),
Axis::Y => self.height(id),
}
}
pub fn size(&mut self, id: impl IdLike<State>) -> Size {
pub fn size(&mut self, id: impl IdLike) -> Size {
let id = id.id();
Size {
x: self.width(id),