223 lines
5.6 KiB
Rust
223 lines
5.6 KiB
Rust
use std::{
|
|
any::{Any, TypeId},
|
|
marker::PhantomData,
|
|
};
|
|
|
|
use crate::{Painter, Ui, util::Id};
|
|
|
|
pub trait Widget<Ctx>: Any {
|
|
fn draw(&self, painter: &mut Painter<Ctx>);
|
|
}
|
|
|
|
pub struct AnyWidget;
|
|
|
|
/// An identifier for a widget.
|
|
/// Can index a UI to get the associated widget.
|
|
///
|
|
/// 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.
|
|
#[repr(C)]
|
|
#[derive(Eq, Hash, PartialEq)]
|
|
pub struct WidgetId<W = AnyWidget> {
|
|
pub(super) ty: TypeId,
|
|
pub(super) id: Id,
|
|
_pd: PhantomData<W>,
|
|
}
|
|
|
|
impl<W> std::fmt::Debug for WidgetId<W> {
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
self.id.fmt(f)
|
|
}
|
|
}
|
|
|
|
// TODO: temp
|
|
impl<W> Clone for WidgetId<W> {
|
|
fn clone(&self) -> Self {
|
|
Self::new(self.id.duplicate(), self.ty)
|
|
}
|
|
}
|
|
|
|
impl<W> WidgetId<W> {
|
|
pub(super) fn new(id: Id, ty: TypeId) -> Self {
|
|
Self {
|
|
ty,
|
|
id,
|
|
_pd: PhantomData,
|
|
}
|
|
}
|
|
|
|
pub fn erase_type(self) -> WidgetId<AnyWidget> {
|
|
self.cast_type()
|
|
}
|
|
|
|
pub fn as_any(&self) -> &WidgetId<AnyWidget> {
|
|
// safety: self is repr(C) and generic only used for phantom data
|
|
unsafe { std::mem::transmute(self) }
|
|
}
|
|
|
|
fn cast_type<W2>(self) -> WidgetId<W2> {
|
|
WidgetId {
|
|
ty: self.ty,
|
|
id: self.id,
|
|
_pd: PhantomData,
|
|
}
|
|
}
|
|
}
|
|
|
|
pub struct WidgetTag;
|
|
pub struct FnTag;
|
|
pub struct IdTag;
|
|
|
|
pub trait WidgetLike<Ctx, Tag> {
|
|
type Widget: 'static;
|
|
fn add(self, ui: &mut Ui<Ctx>) -> WidgetId<Self::Widget>;
|
|
fn with_id<W2>(
|
|
self,
|
|
f: impl FnOnce(&mut Ui<Ctx>, WidgetId<Self::Widget>) -> WidgetId<W2>,
|
|
) -> impl WidgetIdFn<W2, Ctx>
|
|
where
|
|
Self: Sized,
|
|
{
|
|
move |ui| {
|
|
let id = self.add(ui);
|
|
f(ui, id)
|
|
}
|
|
}
|
|
}
|
|
|
|
/// 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<Ctx>, Ctx> = FnOnce(&mut Ui<Ctx>) -> W;
|
|
pub trait WidgetIdFn<W, Ctx> = FnOnce(&mut Ui<Ctx>) -> WidgetId<W>;
|
|
|
|
pub trait Idable<Ctx, Tag> {
|
|
type Widget: Widget<Ctx>;
|
|
fn set(self, ui: &mut Ui<Ctx>, id: &WidgetId<Self::Widget>);
|
|
fn id<'a>(
|
|
self,
|
|
id: &WidgetId<Self::Widget>,
|
|
) -> impl WidgetIdFn<Self::Widget, Ctx> + use<'a, Self, Ctx, Tag>
|
|
where
|
|
Self: Sized,
|
|
{
|
|
let id = id.clone();
|
|
move |ui| {
|
|
self.set(ui, &id);
|
|
id
|
|
}
|
|
}
|
|
}
|
|
|
|
impl<W: Widget<Ctx>, Ctx> Idable<Ctx, WidgetTag> for W {
|
|
type Widget = W;
|
|
|
|
fn set(self, ui: &mut Ui<Ctx>, id: &WidgetId<Self::Widget>) {
|
|
ui.set(id, self);
|
|
}
|
|
}
|
|
|
|
impl<F: FnOnce(&mut Ui<Ctx>) -> W, W: Widget<Ctx>, Ctx> Idable<Ctx, FnTag> for F {
|
|
type Widget = W;
|
|
|
|
fn set(self, ui: &mut Ui<Ctx>, id: &WidgetId<Self::Widget>) {
|
|
let w = self(ui);
|
|
ui.set(id, w);
|
|
}
|
|
}
|
|
|
|
impl<W: Widget<Ctx>, Ctx, F: FnOnce(&mut Ui<Ctx>) -> W> WidgetLike<Ctx, FnTag> for F {
|
|
type Widget = W;
|
|
fn add(self, ui: &mut Ui<Ctx>) -> WidgetId<W> {
|
|
let w = self(ui);
|
|
ui.add(w)
|
|
}
|
|
}
|
|
|
|
impl<W: 'static, F: FnOnce(&mut Ui<Ctx>) -> WidgetId<W>, Ctx> WidgetLike<Ctx, IdTag> for F {
|
|
type Widget = W;
|
|
fn add(self, ui: &mut Ui<Ctx>) -> WidgetId<W> {
|
|
self(ui)
|
|
}
|
|
}
|
|
|
|
impl<W: Widget<Ctx>, Ctx> WidgetLike<Ctx, WidgetTag> for W {
|
|
type Widget = W;
|
|
fn add(self, ui: &mut Ui<Ctx>) -> WidgetId<W> {
|
|
ui.add_widget(self)
|
|
}
|
|
}
|
|
|
|
impl<W: 'static, Ctx> WidgetLike<Ctx, FnTag> for WidgetId<W> {
|
|
type Widget = W;
|
|
fn add(self, _: &mut Ui<Ctx>) -> WidgetId<W> {
|
|
self
|
|
}
|
|
}
|
|
|
|
pub struct WidgetArr<const LEN: usize, Ws> {
|
|
pub arr: [WidgetId; LEN],
|
|
_pd: PhantomData<Ws>,
|
|
}
|
|
|
|
impl<const LEN: usize, Ws> WidgetArr<LEN, Ws> {
|
|
pub fn new(arr: [WidgetId; LEN]) -> Self {
|
|
Self {
|
|
arr,
|
|
_pd: PhantomData,
|
|
}
|
|
}
|
|
}
|
|
|
|
pub struct ArrTag;
|
|
pub trait WidgetArrLike<const LEN: usize, Ctx, Tags> {
|
|
type Ws;
|
|
fn ui(self, ui: &mut Ui<Ctx>) -> WidgetArr<LEN, Self::Ws>;
|
|
}
|
|
|
|
impl<const LEN: usize, Ws, Ctx> WidgetArrLike<LEN, Ctx, ArrTag> for WidgetArr<LEN, Ws> {
|
|
type Ws = Ws;
|
|
fn ui(self, _: &mut Ui<Ctx>) -> WidgetArr<LEN, Ws> {
|
|
self
|
|
}
|
|
}
|
|
|
|
impl<W: WidgetLike<Ctx, WidgetTag>, Ctx> WidgetArrLike<1, Ctx, WidgetTag> for W {
|
|
type Ws = (W::Widget,);
|
|
fn ui(self, ui: &mut Ui<Ctx>) -> WidgetArr<1, (W::Widget,)> {
|
|
WidgetArr::new([self.add(ui).erase_type()])
|
|
}
|
|
}
|
|
|
|
// I hate this language it's so bad why do I even use it
|
|
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<Ctx, $Tag>,$Tag,)* Ctx> WidgetArrLike<$n, Ctx, ($($Tag,)*)> for ($($W,)*) {
|
|
type Ws = ($($W::Widget,)*);
|
|
fn ui(self, ui: &mut Ui<Ctx>) -> WidgetArr<$n, ($($W::Widget,)*)> {
|
|
#[allow(non_snake_case)]
|
|
let ($($W,)*) = self;
|
|
WidgetArr::new(
|
|
[$($W.add(ui).cast_type(),)*],
|
|
)
|
|
}
|
|
}
|
|
};
|
|
}
|
|
|
|
impl_widget_arr!(1;A);
|
|
impl_widget_arr!(2;A B);
|
|
impl_widget_arr!(3;A B C);
|
|
impl_widget_arr!(4;A B C D);
|
|
impl_widget_arr!(5;A B C D E);
|
|
impl_widget_arr!(6;A B C D E F);
|
|
impl_widget_arr!(7;A B C D E F G);
|
|
impl_widget_arr!(8;A B C D E F G H);
|
|
impl_widget_arr!(9;A B C D E F G H I);
|
|
impl_widget_arr!(10;A B C D E F G H I J);
|
|
impl_widget_arr!(11;A B C D E F G H I J K);
|
|
impl_widget_arr!(12;A B C D E F G H I J K L);
|