proper widgetid + slot vec instead of map

This commit is contained in:
2025-12-11 16:23:14 -05:00
parent 2dad409300
commit 966b6a2ac2
12 changed files with 606 additions and 152 deletions

View File

@@ -2,9 +2,11 @@ use std::{marker::Unsize, mem::MaybeUninit, ops::CoerceUnsized, sync::mpsc::Send
use crate::{
Ui, Widget,
util::{Id, RefCounter},
util::{RefCounter, SlotId},
};
pub type WidgetId = SlotId;
/// An identifier for a widget that can index a UI to get the associated widget.
/// It should always remain valid; it keeps a ref count and removes the widget from the UI if all
/// references are dropped.
@@ -14,14 +16,14 @@ use crate::{
///
/// TODO: ergonomic clones when they get put in rust-analyzer & don't cause ICEs?
pub struct WidgetHandle<W: ?Sized = dyn Widget> {
pub(super) id: Id,
pub(super) id: WidgetId,
counter: RefCounter,
send: Sender<Id>,
send: Sender<WidgetId>,
ty: *const W,
}
pub struct WidgetView<W: ?Sized = dyn Widget> {
pub(super) id: Id,
pub(super) id: WidgetId,
#[allow(unused)]
ty: *const W,
}
@@ -35,7 +37,7 @@ impl<W: Widget + ?Sized + Unsize<dyn Widget>> WidgetHandle<W> {
}
impl<W: ?Sized> WidgetHandle<W> {
pub(crate) fn new(id: Id, send: Sender<Id>) -> Self {
pub(crate) fn new(id: WidgetId, send: Sender<WidgetId>) -> Self {
Self {
id,
counter: RefCounter::new(),
@@ -49,7 +51,7 @@ impl<W: ?Sized> WidgetHandle<W> {
unsafe { std::mem::transmute(self) }
}
pub fn id(&self) -> Id {
pub fn id(&self) -> WidgetId {
self.id
}
@@ -69,7 +71,7 @@ impl<W: ?Sized> WidgetHandle<W> {
}
impl<W: ?Sized> WidgetView<W> {
pub fn id(&self) -> Id {
pub fn id(&self) -> WidgetId {
self.id
}
}
@@ -87,19 +89,19 @@ impl<W: ?Sized, F: FnOnce(&mut Ui) -> WidgetHandle<W>> WidgetIdFn<W> for F {}
pub trait IdLike {
type Widget: Widget + ?Sized + 'static;
fn id(&self) -> Id;
fn id(&self) -> WidgetId;
}
impl<W: Widget + ?Sized> IdLike for WidgetHandle<W> {
type Widget = W;
fn id(&self) -> Id {
fn id(&self) -> WidgetId {
self.id
}
}
impl<W: Widget + ?Sized> IdLike for WidgetView<W> {
type Widget = W;
fn id(&self) -> Id {
fn id(&self) -> WidgetId {
self.id
}
}