strong & weak widgets
This commit is contained in:
130
core/src/widget/handle.rs
Normal file
130
core/src/widget/handle.rs
Normal file
@@ -0,0 +1,130 @@
|
||||
use std::{marker::Unsize, mem::MaybeUninit, ops::CoerceUnsized, sync::mpsc::Sender};
|
||||
|
||||
use crate::{
|
||||
Ui, Widget,
|
||||
util::{Id, RefCounter},
|
||||
};
|
||||
|
||||
/// 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.
|
||||
///
|
||||
/// W does not need to implement widget so that AnyWidget is valid;
|
||||
/// Instead, add generic bounds on methods that take an ID if they need specific data.
|
||||
///
|
||||
/// 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,
|
||||
counter: RefCounter,
|
||||
send: Sender<Id>,
|
||||
ty: *const W,
|
||||
}
|
||||
|
||||
pub struct WidgetView<W: ?Sized = dyn Widget> {
|
||||
pub(super) id: Id,
|
||||
#[allow(unused)]
|
||||
ty: *const W,
|
||||
}
|
||||
|
||||
impl<W: Widget + ?Sized + Unsize<dyn Widget>> WidgetHandle<W> {
|
||||
pub fn any(self) -> WidgetHandle<dyn Widget> {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl<W: ?Sized> WidgetHandle<W> {
|
||||
pub(crate) fn new(id: Id, send: Sender<Id>) -> Self {
|
||||
Self {
|
||||
id,
|
||||
counter: RefCounter::new(),
|
||||
send,
|
||||
ty: unsafe { MaybeUninit::zeroed().assume_init() },
|
||||
}
|
||||
}
|
||||
|
||||
pub fn as_any(&self) -> &WidgetHandle<dyn Widget> {
|
||||
// SAFETY: self is repr(C) and generic only used for phantom data
|
||||
unsafe { std::mem::transmute(self) }
|
||||
}
|
||||
|
||||
pub fn id(&self) -> Id {
|
||||
self.id
|
||||
}
|
||||
|
||||
pub fn refs(&self) -> u32 {
|
||||
self.counter.refs()
|
||||
}
|
||||
|
||||
pub fn weak(&self) -> WidgetView<W> {
|
||||
let Self { ty, id, .. } = *self;
|
||||
WidgetView { ty, id }
|
||||
}
|
||||
|
||||
pub fn handles(self) -> (Self, WidgetView<W>) {
|
||||
let weak = self.weak();
|
||||
(self, weak)
|
||||
}
|
||||
}
|
||||
|
||||
impl<W: ?Sized> WidgetView<W> {
|
||||
pub fn id(&self) -> Id {
|
||||
self.id
|
||||
}
|
||||
}
|
||||
|
||||
impl<W: ?Sized> Drop for WidgetHandle<W> {
|
||||
fn drop(&mut self) {
|
||||
if self.counter.drop() {
|
||||
let _ = self.send.send(self.id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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 IdLike {
|
||||
type Widget: Widget + ?Sized + 'static;
|
||||
fn id(&self) -> Id;
|
||||
}
|
||||
|
||||
impl<W: Widget + ?Sized> IdLike for WidgetHandle<W> {
|
||||
type Widget = W;
|
||||
fn id(&self) -> Id {
|
||||
self.id
|
||||
}
|
||||
}
|
||||
|
||||
impl<W: Widget + ?Sized> IdLike for WidgetView<W> {
|
||||
type Widget = W;
|
||||
fn id(&self) -> Id {
|
||||
self.id
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<WidgetHandle<U>> for WidgetHandle<T> {}
|
||||
impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<WidgetView<U>> for WidgetView<T> {}
|
||||
|
||||
impl<W: ?Sized> Clone for WidgetView<W> {
|
||||
fn clone(&self) -> Self {
|
||||
*self
|
||||
}
|
||||
}
|
||||
impl<W: ?Sized> Copy for WidgetView<W> {}
|
||||
impl<W: ?Sized> PartialEq for WidgetView<W> {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
self.id == other.id
|
||||
}
|
||||
}
|
||||
|
||||
impl<W> PartialEq for WidgetHandle<W> {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
self.id == other.id
|
||||
}
|
||||
}
|
||||
|
||||
impl<W> std::fmt::Debug for WidgetHandle<W> {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
self.id.fmt(f)
|
||||
}
|
||||
}
|
||||
@@ -1,152 +0,0 @@
|
||||
use std::{any::TypeId, marker::PhantomData, sync::mpsc::Sender};
|
||||
|
||||
use crate::{
|
||||
Ui, Widget,
|
||||
util::{Id, RefCounter},
|
||||
};
|
||||
|
||||
pub struct AnyWidget;
|
||||
|
||||
/// 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.
|
||||
///
|
||||
/// W does not need to implement widget so that AnyWidget is valid;
|
||||
/// Instead, add generic bounds on methods that take an ID if they need specific data.
|
||||
///
|
||||
/// TODO: ergonomic clones when they get put in rust-analyzer & don't cause ICEs?
|
||||
#[repr(C)]
|
||||
pub struct WidgetHandle<W = AnyWidget> {
|
||||
pub(super) ty: TypeId,
|
||||
pub(super) id: Id,
|
||||
counter: RefCounter,
|
||||
send: Sender<Id>,
|
||||
_pd: PhantomData<W>,
|
||||
}
|
||||
|
||||
#[repr(C)]
|
||||
pub struct WidgetView<W = AnyWidget> {
|
||||
pub(super) ty: TypeId,
|
||||
pub(super) id: Id,
|
||||
counter: RefCounter,
|
||||
send: Sender<Id>,
|
||||
_pd: PhantomData<W>,
|
||||
}
|
||||
|
||||
impl<W> PartialEq for WidgetHandle<W> {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
self.ty == other.ty && self.id == other.id
|
||||
}
|
||||
}
|
||||
|
||||
impl<W> std::fmt::Debug for WidgetHandle<W> {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
self.id.fmt(f)
|
||||
}
|
||||
}
|
||||
|
||||
impl<W> Clone for WidgetHandle<W> {
|
||||
fn clone(&self) -> Self {
|
||||
Self {
|
||||
id: self.id,
|
||||
ty: self.ty,
|
||||
counter: self.counter.clone(),
|
||||
send: self.send.clone(),
|
||||
_pd: PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<W> WidgetHandle<W> {
|
||||
pub(crate) fn new(id: Id, ty: TypeId, send: Sender<Id>) -> Self {
|
||||
Self {
|
||||
ty,
|
||||
id,
|
||||
counter: RefCounter::new(),
|
||||
send,
|
||||
_pd: PhantomData,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn any(self) -> WidgetHandle<AnyWidget> {
|
||||
self.cast_type()
|
||||
}
|
||||
|
||||
pub fn as_any(&self) -> &WidgetHandle<AnyWidget> {
|
||||
// SAFETY: self is repr(C) and generic only used for phantom data
|
||||
unsafe { std::mem::transmute(self) }
|
||||
}
|
||||
|
||||
pub fn id(&self) -> Id {
|
||||
self.id
|
||||
}
|
||||
|
||||
pub(super) fn cast_type<W2>(self) -> WidgetHandle<W2> {
|
||||
// SAFETY: self is repr(C) and generic only used for phantom data
|
||||
unsafe { std::mem::transmute(self) }
|
||||
}
|
||||
|
||||
pub fn refs(&self) -> u32 {
|
||||
self.counter.refs()
|
||||
}
|
||||
|
||||
pub fn weak(&self) -> WidgetView<W> {
|
||||
let Self {
|
||||
ty,
|
||||
id,
|
||||
ref counter,
|
||||
ref send,
|
||||
_pd,
|
||||
} = *self;
|
||||
WidgetView {
|
||||
ty,
|
||||
id,
|
||||
counter: counter.quiet_clone(),
|
||||
send: send.clone(),
|
||||
_pd,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<W> Drop for WidgetHandle<W> {
|
||||
fn drop(&mut self) {
|
||||
if self.counter.drop() {
|
||||
let _ = self.send.send(self.id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub trait WidgetIdFn<W>: FnOnce(&mut Ui) -> WidgetHandle<W> {}
|
||||
impl<W, F: FnOnce(&mut Ui) -> WidgetHandle<W>> WidgetIdFn<W> for F {}
|
||||
|
||||
pub trait WidgetRet: FnOnce(&mut Ui) -> WidgetHandle<AnyWidget> {}
|
||||
impl<F: FnOnce(&mut Ui) -> WidgetHandle<AnyWidget>> WidgetRet for F {}
|
||||
|
||||
pub trait WidgetIdLike<W> {
|
||||
fn id(self, send: &Sender<Id>) -> WidgetHandle<W>;
|
||||
}
|
||||
|
||||
impl<W> WidgetIdLike<W> for &WidgetHandle<W> {
|
||||
fn id(self, _: &Sender<Id>) -> WidgetHandle<W> {
|
||||
self.clone()
|
||||
}
|
||||
}
|
||||
|
||||
pub trait IdLike {
|
||||
type Widget: Widget + 'static;
|
||||
fn id(&self) -> Id;
|
||||
}
|
||||
|
||||
impl<W: Widget> IdLike for WidgetHandle<W> {
|
||||
type Widget = W;
|
||||
fn id(&self) -> Id {
|
||||
self.id
|
||||
}
|
||||
}
|
||||
|
||||
impl<W: Widget> IdLike for WidgetView<W> {
|
||||
type Widget = W;
|
||||
fn id(&self) -> Id {
|
||||
self.id
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,8 @@
|
||||
use super::*;
|
||||
use std::marker::Unsize;
|
||||
|
||||
pub trait WidgetLike<Tag> {
|
||||
type Widget: 'static;
|
||||
type Widget: Widget + ?Sized + Unsize<dyn Widget> + 'static;
|
||||
|
||||
fn add(self, ui: &mut Ui) -> WidgetHandle<Self::Widget>;
|
||||
|
||||
@@ -27,24 +28,15 @@ pub trait WidgetLike<Tag> {
|
||||
}
|
||||
|
||||
pub trait WidgetArrLike<const LEN: usize, Tag> {
|
||||
type Ws;
|
||||
fn ui(self, ui: &mut Ui) -> WidgetArr<LEN, Self::Ws>;
|
||||
fn ui(self, ui: &mut Ui) -> WidgetArr<LEN>;
|
||||
}
|
||||
|
||||
impl<const LEN: usize, Ws> WidgetArrLike<LEN, ArrTag> for WidgetArr<LEN, Ws> {
|
||||
type Ws = Ws;
|
||||
fn ui(self, _: &mut Ui) -> WidgetArr<LEN, Ws> {
|
||||
impl<const LEN: usize> WidgetArrLike<LEN, ArrTag> for WidgetArr<LEN> {
|
||||
fn ui(self, _: &mut Ui) -> WidgetArr<LEN> {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl<W: WidgetLike<WidgetTag>> WidgetArrLike<1, WidgetTag> for W {
|
||||
type Ws = (W::Widget,);
|
||||
fn ui(self, ui: &mut Ui) -> WidgetArr<1, (W::Widget,)> {
|
||||
WidgetArr::new([self.add(ui).any()])
|
||||
}
|
||||
}
|
||||
|
||||
// I hate this language it's so bad why do I even use it
|
||||
macro_rules! impl_widget_arr {
|
||||
($n:expr;$($W:ident)*) => {
|
||||
@@ -52,12 +44,11 @@ macro_rules! impl_widget_arr {
|
||||
};
|
||||
($n:expr;$($W:ident)*;$($Tag:ident)*) => {
|
||||
impl<$($W: WidgetLike<$Tag>,$Tag,)*> WidgetArrLike<$n, ($($Tag,)*)> for ($($W,)*) {
|
||||
type Ws = ($($W::Widget,)*);
|
||||
fn ui(self, ui: &mut Ui) -> WidgetArr<$n, ($($W::Widget,)*)> {
|
||||
fn ui(self, ui: &mut Ui) -> WidgetArr<$n> {
|
||||
#[allow(non_snake_case)]
|
||||
let ($($W,)*) = self;
|
||||
WidgetArr::new(
|
||||
[$($W.add(ui).cast_type(),)*],
|
||||
[$($W.add(ui),)*],
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
use crate::{Len, Painter, SizeCtx, Ui};
|
||||
use std::{any::Any, marker::PhantomData};
|
||||
use std::any::Any;
|
||||
|
||||
mod id;
|
||||
mod handle;
|
||||
mod like;
|
||||
mod tag;
|
||||
mod widgets;
|
||||
|
||||
pub use id::*;
|
||||
pub use handle::*;
|
||||
pub use like::*;
|
||||
pub use tag::*;
|
||||
pub use widgets::*;
|
||||
@@ -30,20 +30,16 @@ impl Widget for () {
|
||||
/// 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>: FnOnce(&mut Ui) -> W {}
|
||||
impl<W: Widget, F: FnOnce(&mut Ui) -> W> WidgetFn<W> for F {}
|
||||
pub trait WidgetFn<W: Widget + ?Sized>: FnOnce(&mut Ui) -> W {}
|
||||
impl<W: Widget + ?Sized, F: FnOnce(&mut Ui) -> W> WidgetFn<W> for F {}
|
||||
|
||||
pub struct WidgetArr<const LEN: usize, Ws> {
|
||||
pub struct WidgetArr<const LEN: usize> {
|
||||
pub arr: [WidgetHandle; LEN],
|
||||
_pd: PhantomData<Ws>,
|
||||
}
|
||||
|
||||
impl<const LEN: usize, Ws> WidgetArr<LEN, Ws> {
|
||||
impl<const LEN: usize> WidgetArr<LEN> {
|
||||
pub fn new(arr: [WidgetHandle; LEN]) -> Self {
|
||||
Self {
|
||||
arr,
|
||||
_pd: PhantomData,
|
||||
}
|
||||
Self { arr }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
use std::marker::Unsize;
|
||||
|
||||
use super::*;
|
||||
|
||||
pub struct ArrTag;
|
||||
@@ -19,7 +21,7 @@ impl<W: Widget, F: FnOnce(&mut Ui) -> W> WidgetLike<FnTag> for F {
|
||||
}
|
||||
|
||||
pub struct IdTag;
|
||||
impl<W: 'static> WidgetLike<IdTag> for WidgetHandle<W> {
|
||||
impl<W: ?Sized + Widget + Unsize<dyn Widget> + 'static> WidgetLike<IdTag> for WidgetHandle<W> {
|
||||
type Widget = W;
|
||||
fn add(self, _: &mut Ui) -> WidgetHandle<W> {
|
||||
self
|
||||
@@ -27,10 +29,11 @@ impl<W: 'static> WidgetLike<IdTag> for WidgetHandle<W> {
|
||||
}
|
||||
|
||||
pub struct IdFnTag;
|
||||
impl<W: 'static, F: FnOnce(&mut Ui) -> WidgetHandle<W>> WidgetLike<IdFnTag> for F {
|
||||
impl<W: ?Sized + Widget + Unsize<dyn Widget> + 'static, F: FnOnce(&mut Ui) -> WidgetHandle<W>>
|
||||
WidgetLike<IdFnTag> for F
|
||||
{
|
||||
type Widget = W;
|
||||
fn add(self, ui: &mut Ui) -> WidgetHandle<W> {
|
||||
self(ui)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -46,11 +46,11 @@ 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>(&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>(&mut self, id: &I) -> Option<&mut I::Widget> where I::Widget: Sized {
|
||||
self.get_dyn_mut(id.id())?.as_any_mut().downcast_mut()
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user