From 92db1264a6646fcdfab454009d9d4cc780a7855d Mon Sep 17 00:00:00 2001 From: Shadow Cat Date: Tue, 11 Nov 2025 01:35:59 -0500 Subject: [PATCH] beginning actual app --- src/layout/id.rs | 22 +++++++++++++++++++--- src/layout/ui.rs | 15 ++++++++------- src/layout/widgets.rs | 18 +++++------------- 3 files changed, 32 insertions(+), 23 deletions(-) diff --git a/src/layout/id.rs b/src/layout/id.rs index 45fdcd5..64099ab 100644 --- a/src/layout/id.rs +++ b/src/layout/id.rs @@ -217,18 +217,34 @@ impl Clone for StaticWidgetId { impl Copy for StaticWidgetId {} -pub trait IdLike { +pub trait WidgetIdLike { fn id(self, send: &Sender) -> WidgetId; } -impl IdLike for &WidgetId { +impl WidgetIdLike for &WidgetId { fn id(self, _: &Sender) -> WidgetId { self.clone() } } -impl IdLike for StaticWidgetId { +impl WidgetIdLike for StaticWidgetId { fn id(self, send: &Sender) -> WidgetId { self.to_id(send) } } + +pub trait IdLike { + fn id(&self) -> Id; +} + +impl IdLike for WidgetId { + fn id(&self) -> Id { + self.id + } +} + +impl IdLike for StaticWidgetId { + fn id(&self) -> Id { + self.id + } +} diff --git a/src/layout/ui.rs b/src/layout/ui.rs index 25cdeb1..20dfe32 100644 --- a/src/layout/ui.rs +++ b/src/layout/ui.rs @@ -3,7 +3,8 @@ use image::DynamicImage; use crate::{ core::{TextEdit, TextEditCtx}, layout::{ - PainterCtx, PainterData, StaticWidgetId, TextureHandle, Vec2, Widget, WidgetId, WidgetLike, + IdLike, PainterCtx, PainterData, StaticWidgetId, TextureHandle, Vec2, Widget, WidgetId, + WidgetLike, }, util::Id, }; @@ -69,11 +70,11 @@ impl Ui { Self::default() } - pub fn get(&self, id: &WidgetId) -> Option<&W> { + pub fn get(&self, id: &impl IdLike) -> Option<&W> { self.data.widgets.get(id) } - pub fn get_mut(&mut self, id: &WidgetId) -> Option<&mut W> { + pub fn get_mut(&mut self, id: &impl IdLike) -> Option<&mut W> { self.data.widgets.get_mut(id) } @@ -162,8 +163,8 @@ impl Ui { self.data.active.len() } - pub fn text(&mut self, id: &WidgetId) -> TextEditCtx<'_> { - self.updates.push(id.id); + pub fn text(&mut self, id: &impl IdLike) -> TextEditCtx<'_> { + self.updates.push(id.id()); TextEditCtx { text: self.data.widgets.get_mut(id).unwrap(), font_system: &mut self.data.text.font_system, @@ -190,14 +191,14 @@ impl Index> for Ui { type Output = W; fn index(&self, id: StaticWidgetId) -> &Self::Output { - self.data.widgets.get_static(&id).unwrap() + self.data.widgets.get(&id).unwrap() } } impl IndexMut> for Ui { fn index_mut(&mut self, id: StaticWidgetId) -> &mut Self::Output { self.updates.push(id.id); - self.data.widgets.get_static_mut(&id).unwrap() + self.data.widgets.get_mut(&id).unwrap() } } diff --git a/src/layout/widgets.rs b/src/layout/widgets.rs index 930b0cc..0d01277 100644 --- a/src/layout/widgets.rs +++ b/src/layout/widgets.rs @@ -1,5 +1,5 @@ use crate::{ - layout::{StaticWidgetId, Widget, WidgetId}, + layout::{IdLike, Widget}, util::{DynBorrower, HashMap, Id, IdTracker}, }; @@ -47,20 +47,12 @@ impl Widgets { WidgetWrapper::new(data.widget.as_mut(), &mut data.borrowed) } - pub fn get_static(&self, id: &StaticWidgetId) -> Option<&W> { - self.get_dyn(id.id)?.as_any().downcast_ref() + pub fn get(&self, id: &impl IdLike) -> Option<&W> { + self.get_dyn(id.id())?.as_any().downcast_ref() } - pub fn get_static_mut(&mut self, id: &StaticWidgetId) -> Option<&mut W> { - self.get_dyn_mut(id.id)?.as_any_mut().downcast_mut() - } - - pub fn get(&self, id: &WidgetId) -> Option<&W> { - self.get_dyn(id.id)?.as_any().downcast_ref() - } - - pub fn get_mut(&mut self, id: &WidgetId) -> Option<&mut W> { - self.get_dyn_mut(id.id)?.as_any_mut().downcast_mut() + pub fn get_mut(&mut self, id: &impl IdLike) -> Option<&mut W> { + self.get_dyn_mut(id.id())?.as_any_mut().downcast_mut() } pub fn insert(&mut self, id: Id, widget: W) {