remove context generic

This commit is contained in:
2025-08-25 18:53:21 -04:00
parent d4b1a56467
commit e8b255c8f9
17 changed files with 167 additions and 217 deletions

View File

@@ -14,9 +14,9 @@ use std::{
sync::mpsc::{Receiver, Sender, channel},
};
pub struct Ui<Ctx> {
pub struct Ui {
base: Option<WidgetId>,
widgets: Widgets<Ctx>,
widgets: Widgets,
labels: HashMap<Id, String>,
updates: Vec<Id>,
recv: Receiver<Id>,
@@ -29,26 +29,23 @@ pub struct Ui<Ctx> {
full_redraw: bool,
pub(super) active: Active,
pub(super) sensor_map: SensorMap<Ctx>,
pub(super) sensor_map: SensorMap,
}
#[derive(Default)]
pub struct Widgets<Ctx> {
pub struct Widgets {
ids: IdTracker,
map: HashMap<Id, Box<dyn Widget<Ctx>>>,
map: HashMap<Id, Box<dyn Widget>>,
}
impl<Ctx> Ui<Ctx> {
pub fn add<W: Widget<Ctx>, 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<Ctx>, Tag>(
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()
@@ -59,11 +56,11 @@ impl<Ctx> Ui<Ctx> {
self.labels.insert(id.id.duplicate(), label);
}
pub fn add_widget<W: Widget<Ctx>>(&mut self, w: W) -> WidgetId<W> {
pub fn add_widget<W: Widget>(&mut self, w: W) -> WidgetId<W> {
self.push(w)
}
pub fn push<W: Widget<Ctx>>(&mut self, w: W) -> WidgetId<W> {
pub fn push<W: Widget>(&mut self, w: W) -> WidgetId<W> {
let id = self.id();
self.labels
.insert(id.id.duplicate(), std::any::type_name::<W>().to_string());
@@ -71,31 +68,28 @@ impl<Ctx> Ui<Ctx> {
id
}
pub fn set<W: Widget<Ctx>>(&mut self, id: &WidgetId<W>, w: W) {
pub fn set<W: Widget>(&mut self, id: &WidgetId<W>, w: W) {
self.widgets.insert(id.id.duplicate(), w);
}
pub fn set_base<Tag>(&mut self, w: impl WidgetLike<Ctx, Tag>) {
pub fn set_base<Tag>(&mut self, w: impl WidgetLike<Tag>) {
self.base = Some(w.add(self).erase_type());
self.full_redraw = true;
}
pub fn new() -> Self
where
Ctx: 'static,
{
pub fn new() -> Self {
Self::default()
}
pub fn get<W: Widget<Ctx>>(&self, id: &WidgetId<W>) -> Option<&W> {
pub fn get<W: Widget>(&self, id: &WidgetId<W>) -> Option<&W> {
self.widgets.get(id)
}
pub fn get_mut<W: Widget<Ctx>>(&mut self, id: &WidgetId<W>) -> Option<&mut W> {
pub fn get_mut<W: Widget>(&mut self, id: &WidgetId<W>) -> Option<&mut W> {
self.widgets.get_mut(id)
}
pub fn id<W: Widget<Ctx>>(&mut self) -> WidgetId<W> {
pub fn id<W: Widget>(&mut self) -> WidgetId<W> {
WidgetId::new(
self.widgets.reserve(),
TypeId::of::<W>(),
@@ -104,7 +98,7 @@ impl<Ctx> Ui<Ctx> {
)
}
pub fn id_static<W: Widget<Ctx>>(&mut self) -> StaticWidgetId<W> {
pub fn id_static<W: Widget>(&mut self) -> StaticWidgetId<W> {
let id = self.id();
id.into_static()
}
@@ -117,10 +111,7 @@ impl<Ctx> Ui<Ctx> {
self.size = size.into();
}
pub fn redraw_all(&mut self, ctx: &mut Ctx)
where
Ctx: 'static,
{
pub fn redraw_all(&mut self) {
self.active.clear();
self.primitives.clear();
// free before bc nothing should exist
@@ -128,7 +119,6 @@ impl<Ctx> Ui<Ctx> {
let mut painter = Painter::new(
&self.widgets,
&mut self.primitives,
ctx,
&self.sensor_map,
&mut self.active,
&mut self.text,
@@ -141,26 +131,19 @@ impl<Ctx> Ui<Ctx> {
self.primitives.replace();
}
pub fn update(&mut self, ctx: &mut Ctx)
where
Ctx: 'static,
{
pub fn update(&mut self) {
if self.full_redraw {
self.redraw_all(ctx);
self.redraw_all();
self.full_redraw = false;
} else if !self.updates.is_empty() {
self.redraw_updates(ctx);
self.redraw_updates();
}
}
fn redraw_updates(&mut self, ctx: &mut Ctx)
where
Ctx: 'static,
{
fn redraw_updates(&mut self) {
let mut painter = Painter::new(
&self.widgets,
&mut self.primitives,
ctx,
&self.sensor_map,
&mut self.active,
&mut self.text,
@@ -191,7 +174,7 @@ impl<Ctx> Ui<Ctx> {
}
}
impl<W: Widget<Ctx>, 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 {
@@ -199,14 +182,14 @@ impl<W: Widget<Ctx>, Ctx> Index<&WidgetId<W>> for Ui<Ctx> {
}
}
impl<W: Widget<Ctx>, 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>, 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 {
@@ -214,14 +197,14 @@ impl<W: Widget<Ctx>, Ctx> Index<StaticWidgetId<W>> for Ui<Ctx> {
}
}
impl<W: Widget<Ctx>, 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()
}
}
impl<Ctx> Widgets<Ctx> {
impl Widgets {
pub fn new() -> Self {
Self {
ids: IdTracker::default(),
@@ -229,15 +212,15 @@ impl<Ctx> Widgets<Ctx> {
}
}
pub fn get_dyn(&self, id: &Id) -> &dyn Widget<Ctx> {
pub fn get_dyn(&self, id: &Id) -> &dyn Widget {
self.map.get(id).unwrap().as_ref()
}
pub fn get_static<W: Widget<Ctx>>(&self, id: &StaticWidgetId<W>) -> Option<&W> {
pub fn get_static<W: Widget>(&self, id: &StaticWidgetId<W>) -> Option<&W> {
self.map.get(&id.id.id()).unwrap().as_any().downcast_ref()
}
pub fn get_static_mut<W: Widget<Ctx>>(&mut self, id: &StaticWidgetId<W>) -> Option<&mut W> {
pub fn get_static_mut<W: Widget>(&mut self, id: &StaticWidgetId<W>) -> Option<&mut W> {
self.map
.get_mut(&id.id.id())
.unwrap()
@@ -245,11 +228,11 @@ impl<Ctx> Widgets<Ctx> {
.downcast_mut()
}
pub fn get<W: Widget<Ctx>>(&self, id: &WidgetId<W>) -> Option<&W> {
pub fn get<W: Widget>(&self, id: &WidgetId<W>) -> Option<&W> {
self.map.get(&id.id).unwrap().as_any().downcast_ref()
}
pub fn get_mut<W: Widget<Ctx>>(&mut self, id: &WidgetId<W>) -> Option<&mut W> {
pub fn get_mut<W: Widget>(&mut self, id: &WidgetId<W>) -> Option<&mut W> {
self.map
.get_mut(&id.id)
.unwrap()
@@ -257,11 +240,11 @@ impl<Ctx> Widgets<Ctx> {
.downcast_mut()
}
pub fn insert(&mut self, id: Id, widget: impl Widget<Ctx>) {
pub fn insert(&mut self, id: Id, widget: impl Widget) {
self.map.insert(id, Box::new(widget));
}
pub fn insert_any(&mut self, id: Id, widget: Box<dyn Widget<Ctx>>) {
pub fn insert_any(&mut self, id: Id, widget: Box<dyn Widget>) {
self.map.insert(id, widget);
}
@@ -283,7 +266,7 @@ impl<Ctx> Widgets<Ctx> {
}
}
impl<Ctx> dyn Widget<Ctx> {
impl dyn Widget {
pub fn as_any(&self) -> &dyn Any {
self
}
@@ -293,7 +276,7 @@ impl<Ctx> dyn Widget<Ctx> {
}
}
impl<Ctx: 'static> Default for Ui<Ctx> {
impl Default for Ui {
fn default() -> Self {
let (send, recv) = channel();
Self {
@@ -341,7 +324,7 @@ impl Active {
instance
}
pub fn add<Ctx>(&mut self, id: &Id, instance: WidgetInstance, sensors_map: &SensorMap<Ctx>) {
pub fn add(&mut self, id: &Id, instance: WidgetInstance, sensors_map: &SensorMap) {
if sensors_map.get(id).is_some() {
self.sensors.insert(id.duplicate(), instance.region);
}