161 lines
4.0 KiB
Rust
161 lines
4.0 KiB
Rust
use crate::{
|
|
core::WidgetPtr,
|
|
layout::{Len, Painter, SizeCtx, Ui, WidgetId, WidgetIdFn},
|
|
};
|
|
|
|
use std::{any::Any, marker::PhantomData};
|
|
|
|
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;
|
|
}
|
|
|
|
impl Widget for () {
|
|
fn draw(&mut self, _: &mut Painter) {}
|
|
fn desired_width(&mut self, _: &mut SizeCtx) -> Len {
|
|
Len::ZERO
|
|
}
|
|
fn desired_height(&mut self, _: &mut SizeCtx) -> Len {
|
|
Len::ZERO
|
|
}
|
|
}
|
|
|
|
pub struct WidgetTag;
|
|
pub struct FnTag;
|
|
|
|
pub trait WidgetLike<Tag> {
|
|
type Widget: 'static;
|
|
|
|
fn add(self, ui: &mut Ui) -> WidgetId<Self::Widget>;
|
|
|
|
fn with_id<W2>(
|
|
self,
|
|
f: impl FnOnce(&mut Ui, WidgetId<Self::Widget>) -> WidgetId<W2>,
|
|
) -> impl WidgetIdFn<W2>
|
|
where
|
|
Self: Sized,
|
|
{
|
|
move |ui| {
|
|
let id = self.add(ui);
|
|
f(ui, id)
|
|
}
|
|
}
|
|
|
|
fn set_root(self, ui: &mut Ui)
|
|
where
|
|
Self: Sized,
|
|
{
|
|
ui.set_root(self);
|
|
}
|
|
|
|
fn set_ptr(self, ptr: &WidgetId<WidgetPtr>, ui: &mut Ui)
|
|
where
|
|
Self: Sized,
|
|
{
|
|
ui[ptr].inner = Some(self.add(ui).any());
|
|
}
|
|
}
|
|
|
|
/// 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 {}
|
|
|
|
impl<W: Widget, F: FnOnce(&mut Ui) -> W> WidgetLike<FnTag> for F {
|
|
type Widget = W;
|
|
fn add(self, ui: &mut Ui) -> WidgetId<W> {
|
|
self(ui).add(ui)
|
|
}
|
|
}
|
|
|
|
impl<W: Widget> WidgetLike<WidgetTag> for W {
|
|
type Widget = W;
|
|
fn add(self, ui: &mut Ui) -> WidgetId<W> {
|
|
ui.add_widget(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, Tag> {
|
|
type Ws;
|
|
fn ui(self, ui: &mut Ui) -> WidgetArr<LEN, Self::Ws>;
|
|
}
|
|
|
|
impl<const LEN: usize, Ws> WidgetArrLike<LEN, ArrTag> for WidgetArr<LEN, Ws> {
|
|
type Ws = Ws;
|
|
fn ui(self, _: &mut Ui) -> WidgetArr<LEN, Ws> {
|
|
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)*) => {
|
|
impl_widget_arr!($n;$($W)*;$(${concat($W,Tag)})*);
|
|
};
|
|
($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,)*)> {
|
|
#[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);
|
|
|
|
pub trait WidgetOption {
|
|
fn get(self, ui: &mut Ui) -> Option<WidgetId>;
|
|
}
|
|
|
|
impl WidgetOption for () {
|
|
fn get(self, _: &mut Ui) -> Option<WidgetId> {
|
|
None
|
|
}
|
|
}
|
|
|
|
impl<F: FnOnce(&mut Ui) -> Option<WidgetId>> WidgetOption for F {
|
|
fn get(self, ui: &mut Ui) -> Option<WidgetId> {
|
|
self(ui)
|
|
}
|
|
}
|