RE ADD CONTEXT

This commit is contained in:
2025-12-15 21:50:53 -05:00
parent dc2be7f688
commit 0b8a93c5ce
39 changed files with 925 additions and 713 deletions

View File

@@ -2,16 +2,16 @@ use std::any::TypeId;
use crate::{Widget, util::HashSet};
pub struct WidgetData {
pub widget: Box<dyn Widget>,
pub struct WidgetData<State> {
pub widget: Box<dyn Widget<State>>,
pub label: String,
pub event_mgrs: HashSet<TypeId>,
/// dynamic borrow checking
pub borrowed: bool,
}
impl WidgetData {
pub fn new<W: Widget>(widget: W) -> Self {
impl<State> WidgetData<State> {
pub fn new<W: Widget<State>>(widget: W) -> Self {
let mut label = std::any::type_name::<W>().to_string();
if let (Some(first), Some(last)) = (label.find(":"), label.rfind(":")) {
label = label.split_at(first).0.to_string() + "::" + label.split_at(last + 1).1;

View File

@@ -1,4 +1,9 @@
use std::{marker::Unsize, mem::MaybeUninit, ops::CoerceUnsized, sync::mpsc::Sender};
use std::{
marker::{PhantomData, Unsize},
mem::MaybeUninit,
ops::CoerceUnsized,
sync::mpsc::Sender,
};
use crate::{
Ui, Widget,
@@ -12,43 +17,46 @@ pub type WidgetId = SlotId;
/// a signal is sent to the owning UI to clean up the resources.
///
/// TODO: ergonomic clones when they get put in rust-analyzer & don't cause ICEs?
pub struct WidgetHandle<W: ?Sized = dyn Widget> {
pub struct WidgetHandle<State, W: ?Sized = dyn Widget<State>> {
pub(super) id: WidgetId,
counter: RefCounter,
send: Sender<WidgetId>,
ty: *const W,
state: PhantomData<State>,
}
/// A weak handle to a widget.
/// Will not keep it alive, but can still be used for indexing like WidgetHandle.
pub struct WidgetRef<W: ?Sized = dyn Widget> {
pub struct WidgetRef<State, W: ?Sized = dyn Widget<State>> {
pub(super) id: WidgetId,
#[allow(unused)]
ty: *const W,
state: PhantomData<State>,
}
pub struct WidgetHandles<W: ?Sized = dyn Widget> {
pub h: WidgetHandle<W>,
pub r: WidgetRef<W>,
pub struct WidgetHandles<State, W: ?Sized = dyn Widget<State>> {
pub h: WidgetHandle<State, W>,
pub r: WidgetRef<State, W>,
}
impl<W: Widget + ?Sized + Unsize<dyn Widget>> WidgetHandle<W> {
pub fn any(self) -> WidgetHandle<dyn Widget> {
impl<State, W: Widget<State> + ?Sized + Unsize<dyn Widget<State>>> WidgetHandle<State, W> {
pub fn any(self) -> WidgetHandle<State, dyn Widget<State>> {
self
}
}
impl<W: ?Sized> WidgetHandle<W> {
impl<State, W: ?Sized> WidgetHandle<State, W> {
pub(crate) fn new(id: WidgetId, send: Sender<WidgetId>) -> Self {
Self {
id,
counter: RefCounter::new(),
send,
ty: unsafe { MaybeUninit::zeroed().assume_init() },
state: PhantomData,
}
}
pub fn as_any(&self) -> &WidgetHandle<dyn Widget> {
pub fn as_any(&self) -> &WidgetHandle<State, dyn Widget<State>> {
// SAFETY: self is repr(C) and generic only used for phantom data
unsafe { std::mem::transmute(self) }
}
@@ -61,24 +69,28 @@ impl<W: ?Sized> WidgetHandle<W> {
self.counter.refs()
}
pub fn weak(&self) -> WidgetRef<W> {
pub fn weak(&self) -> WidgetRef<State, W> {
let Self { ty, id, .. } = *self;
WidgetRef { ty, id }
WidgetRef {
ty,
id,
state: PhantomData,
}
}
pub fn handles(self) -> WidgetHandles<W> {
pub fn handles(self) -> WidgetHandles<State, W> {
let r = self.weak();
WidgetHandles { h: self, r }
}
}
impl<W: ?Sized> WidgetRef<W> {
impl<State, W: ?Sized> WidgetRef<State, W> {
pub fn id(&self) -> WidgetId {
self.id
}
}
impl<W: ?Sized> Drop for WidgetHandle<W> {
impl<State, W: ?Sized> Drop for WidgetHandle<State, W> {
fn drop(&mut self) {
if self.counter.drop() {
let _ = self.send.send(self.id);
@@ -86,52 +98,64 @@ impl<W: ?Sized> Drop for WidgetHandle<W> {
}
}
pub trait WidgetIdFn<W: ?Sized = dyn Widget>: FnOnce(&mut Ui) -> WidgetHandle<W> {}
impl<W: ?Sized, F: FnOnce(&mut Ui) -> WidgetHandle<W>> WidgetIdFn<W> for F {}
pub trait WidgetIdFn<State, W: ?Sized = dyn Widget<State>>:
FnOnce(&mut Ui<State>) -> WidgetHandle<State, W>
{
}
impl<State, W: ?Sized, F: FnOnce(&mut Ui<State>) -> WidgetHandle<State, W>> WidgetIdFn<State, W>
for F
{
}
pub trait IdLike {
type Widget: Widget + ?Sized + 'static;
pub trait IdLike<State> {
type Widget: Widget<State> + ?Sized + 'static;
fn id(&self) -> WidgetId;
}
impl<W: Widget + ?Sized> IdLike for &WidgetHandle<W> {
impl<State, W: Widget<State> + ?Sized> IdLike<State> for &WidgetHandle<State, W> {
type Widget = W;
fn id(&self) -> WidgetId {
self.id
}
}
impl<W: Widget + ?Sized> IdLike for WidgetHandle<W> {
impl<State, W: Widget<State> + ?Sized> IdLike<State> for WidgetHandle<State, W> {
type Widget = W;
fn id(&self) -> WidgetId {
self.id
}
}
impl<W: Widget + ?Sized> IdLike for WidgetRef<W> {
impl<State, W: Widget<State> + ?Sized> IdLike<State> for WidgetRef<State, W> {
type Widget = W;
fn id(&self) -> WidgetId {
self.id
}
}
impl IdLike for WidgetId {
type Widget = dyn Widget;
impl<State: 'static> IdLike<State> for WidgetId {
type Widget = dyn Widget<State>;
fn id(&self) -> WidgetId {
*self
}
}
impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<WidgetHandle<U>> for WidgetHandle<T> {}
impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<WidgetRef<U>> for WidgetRef<T> {}
impl<T: ?Sized + Unsize<U>, U: ?Sized, State> CoerceUnsized<WidgetHandle<State, U>>
for WidgetHandle<State, T>
{
}
impl<State, T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<WidgetRef<State, U>>
for WidgetRef<State, T>
{
}
impl<W: ?Sized> Clone for WidgetRef<W> {
impl<State, W: ?Sized> Clone for WidgetRef<State, W> {
fn clone(&self) -> Self {
*self
}
}
impl<W: ?Sized> Copy for WidgetRef<W> {}
impl<W: ?Sized> PartialEq for WidgetRef<W> {
impl<State, W: ?Sized> Copy for WidgetRef<State, W> {}
impl<State, W: ?Sized> PartialEq for WidgetRef<State, W> {
fn eq(&self, other: &Self) -> bool {
self.id == other.id
}

View File

@@ -1,48 +1,48 @@
use super::*;
use std::marker::Unsize;
pub trait WidgetLike<Tag>: Sized {
type Widget: Widget + ?Sized + Unsize<dyn Widget> + 'static;
pub trait WidgetLike<State: 'static, Tag>: Sized {
type Widget: Widget<State> + ?Sized + Unsize<dyn Widget<State>> + 'static;
fn add(self, ui: &mut Ui) -> WidgetHandle<Self::Widget>;
fn add(self, ui: &mut Ui<State>) -> WidgetHandle<State, Self::Widget>;
fn with_id<W2>(
self,
f: impl FnOnce(&mut Ui, WidgetHandle<Self::Widget>) -> WidgetHandle<W2>,
) -> impl WidgetIdFn<W2> {
f: impl FnOnce(&mut Ui<State>, WidgetHandle<State, Self::Widget>) -> WidgetHandle<State, W2>,
) -> impl WidgetIdFn<State, W2> {
move |ui| {
let id = self.add(ui);
f(ui, id)
}
}
fn set_root(self, ui: &mut Ui) {
fn set_root(self, ui: &mut Ui<State>) {
ui.set_root(self);
}
fn handles(self, ui: &mut Ui) -> WidgetHandles<Self::Widget> {
fn handles(self, ui: &mut Ui<State>) -> WidgetHandles<State, Self::Widget> {
self.add(ui).handles()
}
}
pub trait WidgetArrLike<const LEN: usize, Tag> {
fn ui(self, ui: &mut Ui) -> WidgetArr<LEN>;
pub trait WidgetArrLike<State, const LEN: usize, Tag> {
fn ui(self, ui: &mut Ui<State>) -> WidgetArr<State, LEN>;
}
impl<const LEN: usize> WidgetArrLike<LEN, ArrTag> for WidgetArr<LEN> {
fn ui(self, _: &mut Ui) -> WidgetArr<LEN> {
impl<State, const LEN: usize> WidgetArrLike<State, LEN, ArrTag> for WidgetArr<State, LEN> {
fn ui(self, _: &mut Ui<State>) -> WidgetArr<State, LEN> {
self
}
}
// I hate this language it's so bad why do I even use it
// variadic generics please save us
macro_rules! impl_widget_arr {
($n:expr;$($W:ident)*) => {
impl_widget_arr!($n;$($W)*;$(${concat($W,Tag)})*);
};
($n:expr;$($W:ident)*;$($Tag:ident)*) => {
impl<$($W: WidgetLike<$Tag>,$Tag,)*> WidgetArrLike<$n, ($($Tag,)*)> for ($($W,)*) {
fn ui(self, ui: &mut Ui) -> WidgetArr<$n> {
impl<State: 'static, $($W: WidgetLike<State, $Tag>,$Tag,)*> WidgetArrLike<State, $n, ($($Tag,)*)> for ($($W,)*) {
fn ui(self, ui: &mut Ui<State>) -> WidgetArr<State, $n> {
#[allow(non_snake_case)]
let ($($W,)*) = self;
WidgetArr::new(

View File

@@ -13,23 +13,23 @@ pub use like::*;
pub use tag::*;
pub use widgets::*;
pub trait Widget: Any {
fn draw(&mut self, painter: &mut Painter);
fn desired_width(&mut self, ctx: &mut SizeCtx) -> Len;
fn desired_height(&mut self, ctx: &mut SizeCtx) -> Len;
pub trait Widget<State>: Any {
fn draw(&mut self, painter: &mut Painter<State>);
fn desired_width(&mut self, ctx: &mut SizeCtx<State>) -> Len;
fn desired_height(&mut self, ctx: &mut SizeCtx<State>) -> Len;
}
impl Widget for () {
fn draw(&mut self, _: &mut Painter) {}
fn desired_width(&mut self, _: &mut SizeCtx) -> Len {
impl<State> Widget<State> for () {
fn draw(&mut self, _: &mut Painter<State>) {}
fn desired_width(&mut self, _: &mut SizeCtx<State>) -> Len {
Len::ZERO
}
fn desired_height(&mut self, _: &mut SizeCtx) -> Len {
fn desired_height(&mut self, _: &mut SizeCtx<State>) -> Len {
Len::ZERO
}
}
impl dyn Widget {
impl<State> dyn Widget<State> {
pub fn as_any(&self) -> &dyn Any {
self
}
@@ -42,31 +42,31 @@ impl dyn Widget {
/// A function that returns a widget given a UI.
/// Useful for defining trait functions on widgets that create a parent widget so that the children
/// don't need to be IDs yet
pub trait WidgetFn<W: Widget + ?Sized>: FnOnce(&mut Ui) -> W {}
impl<W: Widget + ?Sized, F: FnOnce(&mut Ui) -> W> WidgetFn<W> for F {}
pub trait WidgetFn<State, W: Widget<State> + ?Sized>: FnOnce(&mut Ui<State>) -> W {}
impl<State, W: Widget<State> + ?Sized, F: FnOnce(&mut Ui<State>) -> W> WidgetFn<State, W> for F {}
pub struct WidgetArr<const LEN: usize> {
pub arr: [WidgetHandle; LEN],
pub struct WidgetArr<State, const LEN: usize> {
pub arr: [WidgetHandle<State>; LEN],
}
impl<const LEN: usize> WidgetArr<LEN> {
pub fn new(arr: [WidgetHandle; LEN]) -> Self {
impl<State, const LEN: usize> WidgetArr<State, LEN> {
pub fn new(arr: [WidgetHandle<State>; LEN]) -> Self {
Self { arr }
}
}
pub trait WidgetOption {
fn get(self, ui: &mut Ui) -> Option<WidgetHandle>;
pub trait WidgetOption<State> {
fn get(self, ui: &mut Ui<State>) -> Option<WidgetHandle<State>>;
}
impl WidgetOption for () {
fn get(self, _: &mut Ui) -> Option<WidgetHandle> {
impl<State> WidgetOption<State> for () {
fn get(self, _: &mut Ui<State>) -> Option<WidgetHandle<State>> {
None
}
}
impl<F: FnOnce(&mut Ui) -> Option<WidgetHandle>> WidgetOption for F {
fn get(self, ui: &mut Ui) -> Option<WidgetHandle> {
impl<State, F: FnOnce(&mut Ui<State>) -> Option<WidgetHandle<State>>> WidgetOption<State> for F {
fn get(self, ui: &mut Ui<State>) -> Option<WidgetHandle<State>> {
self(ui)
}
}

View File

@@ -5,35 +5,42 @@ use super::*;
pub struct ArrTag;
pub struct WidgetTag;
impl<W: Widget> WidgetLike<WidgetTag> for W {
impl<State: 'static, W: Widget<State>> WidgetLike<State, WidgetTag> for W {
type Widget = W;
fn add(self, ui: &mut Ui) -> WidgetHandle<W> {
fn add(self, ui: &mut Ui<State>) -> WidgetHandle<State, W> {
ui.add_widget(self)
}
}
pub struct FnTag;
impl<W: Widget, F: FnOnce(&mut Ui) -> W> WidgetLike<FnTag> for F {
impl<State: 'static, W: Widget<State>, F: FnOnce(&mut Ui<State>) -> W> WidgetLike<State, FnTag>
for F
{
type Widget = W;
fn add(self, ui: &mut Ui) -> WidgetHandle<W> {
fn add(self, ui: &mut Ui<State>) -> WidgetHandle<State, W> {
self(ui).add(ui)
}
}
pub struct IdTag;
impl<W: ?Sized + Widget + Unsize<dyn Widget> + 'static> WidgetLike<IdTag> for WidgetHandle<W> {
impl<State: 'static, W: ?Sized + Widget<State> + Unsize<dyn Widget<State>> + 'static>
WidgetLike<State, IdTag> for WidgetHandle<State, W>
{
type Widget = W;
fn add(self, _: &mut Ui) -> WidgetHandle<W> {
fn add(self, _: &mut Ui<State>) -> WidgetHandle<State, W> {
self
}
}
pub struct IdFnTag;
impl<W: ?Sized + Widget + Unsize<dyn Widget> + 'static, F: FnOnce(&mut Ui) -> WidgetHandle<W>>
WidgetLike<IdFnTag> for F
impl<
State: 'static,
W: ?Sized + Widget<State> + Unsize<dyn Widget<State>> + 'static,
F: FnOnce(&mut Ui<State>) -> WidgetHandle<State, W>,
> WidgetLike<State, IdFnTag> for F
{
type Widget = W;
fn add(self, ui: &mut Ui) -> WidgetHandle<W> {
fn add(self, ui: &mut Ui<State>) -> WidgetHandle<State, W> {
self(ui)
}
}

View File

@@ -3,34 +3,44 @@ use crate::{
util::{DynBorrower, HashSet, SlotVec},
};
#[derive(Default)]
pub struct Widgets {
pub struct Widgets<State> {
pub updates: HashSet<WidgetId>,
vec: SlotVec<WidgetData>,
vec: SlotVec<WidgetData<State>>,
}
impl Widgets {
impl<State> Default for Widgets<State> {
fn default() -> Self {
Self {
updates: Default::default(),
vec: Default::default(),
}
}
}
impl<State: 'static> Widgets<State> {
pub fn has_updates(&self) -> bool {
!self.updates.is_empty()
}
pub fn get_dyn(&self, id: WidgetId) -> Option<&dyn Widget> {
pub fn get_dyn(&self, id: WidgetId) -> Option<&dyn Widget<State>> {
Some(self.vec.get(id)?.widget.as_ref())
}
pub fn get_dyn_mut(&mut self, id: WidgetId) -> Option<&mut dyn Widget> {
pub fn get_dyn_mut(&mut self, id: WidgetId) -> Option<&mut dyn Widget<State>> {
self.updates.insert(id);
Some(self.vec.get_mut(id)?.widget.as_mut())
}
/// get_dyn but dynamic borrow checking of widgets
/// lets you do recursive (tree) operations, like the painter does
pub(crate) fn get_dyn_dynamic(&self, id: WidgetId) -> WidgetWrapper<'_> {
pub(crate) fn get_dyn_dynamic(&self, id: WidgetId) -> WidgetWrapper<'_, State> {
// SAFETY: must guarantee no other mutable references to this widget exist
// done through the borrow variable
#[allow(mutable_transmutes)]
let data = unsafe {
std::mem::transmute::<&WidgetData, &mut WidgetData>(self.vec.get(id).unwrap())
std::mem::transmute::<&WidgetData<State>, &mut WidgetData<State>>(
self.vec.get(id).unwrap(),
)
};
if data.borrowed {
panic!("tried to mutably borrow the same widget twice");
@@ -38,37 +48,37 @@ impl Widgets {
WidgetWrapper::new(data.widget.as_mut(), &mut data.borrowed)
}
pub fn get<I: IdLike>(&self, id: &I) -> Option<&I::Widget>
pub fn get<I: IdLike<State>>(&self, id: &I) -> Option<&I::Widget>
where
I::Widget: Sized,
{
self.get_dyn(id.id())?.as_any().downcast_ref()
}
pub fn get_mut<I: IdLike>(&mut self, id: &I) -> Option<&mut I::Widget>
pub fn get_mut<I: IdLike<State>>(&mut self, id: &I) -> Option<&mut I::Widget>
where
I::Widget: Sized,
{
self.get_dyn_mut(id.id())?.as_any_mut().downcast_mut()
}
pub fn add<W: Widget>(&mut self, widget: W) -> WidgetId {
pub fn add<W: Widget<State>>(&mut self, widget: W) -> WidgetId {
self.vec.add(WidgetData::new(widget))
}
pub fn data(&self, id: impl IdLike) -> Option<&WidgetData> {
pub fn data(&self, id: impl IdLike<State>) -> Option<&WidgetData<State>> {
self.vec.get(id.id())
}
pub fn label(&self, id: impl IdLike) -> &String {
pub fn label(&self, id: impl IdLike<State>) -> &String {
&self.data(id.id()).unwrap().label
}
pub fn data_mut(&mut self, id: impl IdLike) -> Option<&mut WidgetData> {
pub fn data_mut(&mut self, id: impl IdLike<State>) -> Option<&mut WidgetData<State>> {
self.vec.get_mut(id.id())
}
pub fn delete(&mut self, id: impl IdLike) {
pub fn delete(&mut self, id: impl IdLike<State>) {
self.vec.free(id.id());
// not sure if there's any point in this
// self.updates.remove(&id);
@@ -80,4 +90,4 @@ impl Widgets {
}
}
pub type WidgetWrapper<'a> = DynBorrower<'a, dyn Widget>;
pub type WidgetWrapper<'a, State> = DynBorrower<'a, dyn Widget<State>>;