remove context from ui (again) and create weird trait for it

This commit is contained in:
2025-09-24 17:41:25 -04:00
parent 26c248dcba
commit 719bee4b31
12 changed files with 107 additions and 92 deletions

View File

@@ -10,13 +10,11 @@ use crate::{
};
use std::{
any::{Any, TypeId},
marker::PhantomData,
ops::{Index, IndexMut},
sync::mpsc::{Receiver, Sender, channel},
};
// TODO: remove generic
pub struct Ui<Ctx> {
pub struct Ui {
root: Option<WidgetId>,
pub(super) widgets: Widgets,
updates: Vec<Id>,
@@ -31,17 +29,16 @@ pub struct Ui<Ctx> {
pub(crate) active: ActiveWidgets,
pub modules: Modules,
_pd: PhantomData<Ctx>,
}
impl<Ctx> Ui<Ctx> {
pub fn add<W: Widget, Tag>(&mut self, w: impl WidgetLike<Ctx, Tag, Widget = W>) -> WidgetId<W> {
impl Ui {
pub fn add<W: Widget, Tag>(&mut self, w: impl WidgetLike<Tag, Widget = W>) -> WidgetId<W> {
w.add(self)
}
pub fn add_static<W: Widget, Tag>(
&mut self,
w: impl WidgetLike<Ctx, Tag, Widget = W>,
w: impl WidgetLike<Tag, Widget = W>,
) -> StaticWidgetId<W> {
let id = w.add(self);
id.into_static()
@@ -70,7 +67,7 @@ impl<Ctx> Ui<Ctx> {
self.widgets.insert(id.id.duplicate(), w);
}
pub fn set_root<Tag>(&mut self, w: impl WidgetLike<Ctx, Tag>) {
pub fn set_root<Tag>(&mut self, w: impl WidgetLike<Tag>) {
self.root = Some(w.add(self).any());
self.full_redraw = true;
}
@@ -184,7 +181,7 @@ impl<Ctx> Ui<Ctx> {
}
}
impl<W: Widget, Ctx> Index<&WidgetId<W>> for Ui<Ctx> {
impl<W: Widget> Index<&WidgetId<W>> for Ui {
type Output = W;
fn index(&self, id: &WidgetId<W>) -> &Self::Output {
@@ -192,14 +189,14 @@ impl<W: Widget, Ctx> Index<&WidgetId<W>> for Ui<Ctx> {
}
}
impl<W: Widget, Ctx> IndexMut<&WidgetId<W>> for Ui<Ctx> {
impl<W: Widget> IndexMut<&WidgetId<W>> for Ui {
fn index_mut(&mut self, id: &WidgetId<W>) -> &mut Self::Output {
self.updates.push(id.id.duplicate());
self.get_mut(id).unwrap()
}
}
impl<W: Widget, Ctx> Index<StaticWidgetId<W>> for Ui<Ctx> {
impl<W: Widget> Index<StaticWidgetId<W>> for Ui {
type Output = W;
fn index(&self, id: StaticWidgetId<W>) -> &Self::Output {
@@ -207,7 +204,7 @@ impl<W: Widget, Ctx> Index<StaticWidgetId<W>> for Ui<Ctx> {
}
}
impl<W: Widget, Ctx> IndexMut<StaticWidgetId<W>> for Ui<Ctx> {
impl<W: Widget> IndexMut<StaticWidgetId<W>> for Ui {
fn index_mut(&mut self, id: StaticWidgetId<W>) -> &mut Self::Output {
self.updates.push(id.id.id());
self.widgets.get_static_mut(&id).unwrap()
@@ -224,7 +221,7 @@ impl dyn Widget {
}
}
impl<Ctx> Default for Ui<Ctx> {
impl Default for Ui {
fn default() -> Self {
let (send, recv) = channel();
Self {
@@ -240,7 +237,6 @@ impl<Ctx> Default for Ui<Ctx> {
recv,
size: Vec2::ZERO,
modules: Modules::default(),
_pd: PhantomData,
}
}
}