Compare commits
3 Commits
tuple_atte
...
tuple_expe
| Author | SHA1 | Date | |
|---|---|---|---|
| a6b36fdcca | |||
| 577d62b1da | |||
| b6e43c157b |
@@ -2,7 +2,8 @@ use std::ops::Range;
|
||||
|
||||
use crate::{
|
||||
primitive::{Axis, Painter, RoundedRectData, UIRegion},
|
||||
UIColor, Widget, WidgetArrLike, WidgetFn, WidgetId, WidgetLike,
|
||||
ToId, UIColor, Widget, WidgetArrLike, WidgetFn, WidgetId, WidgetIdLikeTuple, WidgetLike,
|
||||
WidgetLikeTuple,
|
||||
};
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
@@ -49,20 +50,18 @@ impl Widget for Span {
|
||||
}
|
||||
|
||||
impl Span {
|
||||
pub fn proportioned(
|
||||
pub fn proportioned<const LEN: usize>(
|
||||
axis: Axis,
|
||||
elements: impl IntoIterator<Item = (WidgetId, impl UINum)>,
|
||||
ratios: [impl UINum; LEN],
|
||||
elements: [WidgetId; LEN],
|
||||
) -> Self {
|
||||
// TODO: update
|
||||
let elements = elements
|
||||
.into_iter()
|
||||
.map(|(w, r)| (w, r.to_f32()))
|
||||
.collect::<Vec<_>>();
|
||||
let total: f32 = elements.iter().map(|(_, r)| r).sum();
|
||||
let ratios = ratios.map(|r| r.to_f32());
|
||||
let total: f32 = ratios.iter().sum();
|
||||
let mut start = 0.0;
|
||||
Self {
|
||||
elements: elements
|
||||
.into_iter()
|
||||
.zip(ratios)
|
||||
.map(|(e, r)| {
|
||||
let end = start + r / total;
|
||||
let res = (start..end, e);
|
||||
@@ -119,26 +118,29 @@ impl<T: UINum> From<T> for Padding {
|
||||
}
|
||||
}
|
||||
|
||||
pub trait WidgetUtil<W> {
|
||||
pub trait WidgetUtil {
|
||||
fn pad(self, padding: impl Into<Padding>) -> impl WidgetLike<Widget = Regioned>;
|
||||
}
|
||||
|
||||
impl<W: WidgetLike> WidgetUtil<W::Widget> for W {
|
||||
impl<W: WidgetLike> WidgetUtil for W {
|
||||
fn pad(self, padding: impl Into<Padding>) -> impl WidgetLike<Widget = Regioned> {
|
||||
WidgetFn(|ui| Regioned {
|
||||
region: padding.into().region(),
|
||||
inner: self.id(ui).erase_type(),
|
||||
inner: self.add(ui).erase_type(),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub trait WidgetArrUtil<Wa: WidgetArrLike> {
|
||||
fn span(self, axis: Axis, ratios: [impl UINum; Wa::LEN]) -> impl WidgetLike<Widget = Span>;
|
||||
pub trait WidgetArrUtil<const LEN: usize> {
|
||||
fn span(self, axis: Axis, ratios: [impl UINum; LEN]) -> impl WidgetLike<Widget = Span>;
|
||||
}
|
||||
|
||||
impl<Wa: WidgetArrLike> WidgetArrUtil<Wa> for Wa {
|
||||
fn span(self, axis: Axis, ratios: [impl UINum; Wa::LEN]) -> impl WidgetLike<Widget = Span> {
|
||||
WidgetFn(move |ui| Span::proportioned(axis, self.ui(ui).ids().into_iter().zip(ratios)))
|
||||
impl<const LEN: usize, Wa: WidgetArrLike<LEN>> WidgetArrUtil<LEN> for Wa
|
||||
where
|
||||
<Wa::Ws as WidgetLikeTuple<LEN>>::Wrap<ToId>: WidgetIdLikeTuple<LEN>,
|
||||
{
|
||||
fn span(self, axis: Axis, ratios: [impl UINum; LEN]) -> impl WidgetLike<Widget = Span> {
|
||||
WidgetFn(move |ui| Span::proportioned(axis, ratios, self.ui(ui).erase_types()))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -32,7 +32,7 @@ impl From<UI> for UIBuilder {
|
||||
|
||||
impl UIBuilder {
|
||||
pub fn add<W: Widget>(&mut self, w: W) -> WidgetRef<W> {
|
||||
WidgetRef::new(self.clone(), (self.push(w), ()))
|
||||
WidgetRef::new(self.clone(), (self.push(w),))
|
||||
}
|
||||
|
||||
pub fn push<W: Widget>(&mut self, w: W) -> WidgetId<W> {
|
||||
@@ -42,8 +42,8 @@ impl UIBuilder {
|
||||
WidgetId::new(id, TypeId::of::<W>())
|
||||
}
|
||||
|
||||
pub fn finish<WL: WidgetLike>(mut self, base: WL) -> UI {
|
||||
let base = base.id(&mut self).erase_type();
|
||||
pub fn finish<W: WidgetLike>(mut self, base: W) -> UI {
|
||||
let base = base.add(&mut self).erase_type();
|
||||
let mut ui = Rc::into_inner(self.ui).unwrap().into_inner();
|
||||
ui.base = Some(base);
|
||||
ui
|
||||
|
||||
@@ -5,7 +5,7 @@ use std::{
|
||||
|
||||
use crate::{
|
||||
primitive::Painter,
|
||||
util::{IntoTupleList, TupleList, ID},
|
||||
util::{impl_tuple, ID},
|
||||
UIBuilder,
|
||||
};
|
||||
|
||||
@@ -27,7 +27,7 @@ pub struct WidgetId<W = ()> {
|
||||
}
|
||||
|
||||
// TODO: temp
|
||||
impl<W> Clone for WidgetId<W> {
|
||||
impl<W: Widget> Clone for WidgetId<W> {
|
||||
fn clone(&self) -> Self {
|
||||
Self {
|
||||
ty: self.ty,
|
||||
@@ -59,8 +59,8 @@ impl<W> WidgetId<W> {
|
||||
}
|
||||
|
||||
pub trait WidgetLike {
|
||||
type Widget;
|
||||
fn id(self, ui: &mut UIBuilder) -> WidgetId<Self::Widget>;
|
||||
type Widget: Widget;
|
||||
fn add(self, ui: &mut UIBuilder) -> WidgetId<Self::Widget>;
|
||||
}
|
||||
|
||||
/// wouldn't be needed if negative trait bounds & disjoint impls existed
|
||||
@@ -68,7 +68,7 @@ pub struct WidgetFn<F: FnOnce(&mut UIBuilder) -> W, W>(pub F);
|
||||
|
||||
impl<W: Widget, F: FnOnce(&mut UIBuilder) -> W> WidgetLike for WidgetFn<F, W> {
|
||||
type Widget = W;
|
||||
fn id(self, ui: &mut UIBuilder) -> WidgetId<W> {
|
||||
fn add(self, ui: &mut UIBuilder) -> WidgetId<W> {
|
||||
let w = (self.0)(ui);
|
||||
ui.add(w).to_id()
|
||||
}
|
||||
@@ -76,111 +76,103 @@ impl<W: Widget, F: FnOnce(&mut UIBuilder) -> W> WidgetLike for WidgetFn<F, W> {
|
||||
|
||||
impl<W: Widget> WidgetLike for W {
|
||||
type Widget = W;
|
||||
fn id(self, ui: &mut UIBuilder) -> WidgetId<W> {
|
||||
fn add(self, ui: &mut UIBuilder) -> WidgetId<W> {
|
||||
ui.add(self).to_id()
|
||||
}
|
||||
}
|
||||
|
||||
impl<W> WidgetLike for WidgetId<W> {
|
||||
impl<W: Widget> WidgetLike for WidgetId<W> {
|
||||
type Widget = W;
|
||||
fn id(self, _: &mut UIBuilder) -> WidgetId<W> {
|
||||
fn add(self, _: &mut UIBuilder) -> WidgetId<W> {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
pub struct WidgetArr<Ws: WidgetList> {
|
||||
pub ui: UIBuilder,
|
||||
pub arr: Ws::Ids,
|
||||
impl<W: Widget> WidgetLike for WidgetArr<1, (W,)> {
|
||||
type Widget = W;
|
||||
fn add(self, _: &mut UIBuilder) -> WidgetId<W> {
|
||||
self.arr.0
|
||||
}
|
||||
}
|
||||
|
||||
impl<Ws: WidgetList> WidgetArr<Ws> {
|
||||
pub fn new(ui: UIBuilder, arr: Ws::Ids) -> Self {
|
||||
pub struct WidgetArr<const LEN: usize, Ws: WidgetLikeTuple<LEN>> {
|
||||
pub ui: UIBuilder,
|
||||
pub arr: Ws::Wrap<ToId>,
|
||||
}
|
||||
|
||||
impl<const LEN: usize, Ws: WidgetLikeTuple<LEN>> WidgetArr<LEN, Ws>
|
||||
where
|
||||
Ws::Wrap<ToId>: WidgetIdLikeTuple<LEN>,
|
||||
{
|
||||
pub fn new(ui: UIBuilder, arr: Ws::Wrap<ToId>) -> Self {
|
||||
Self { ui, arr }
|
||||
}
|
||||
pub fn ids(self) -> Vec<WidgetId> {
|
||||
self.arr.all()
|
||||
pub fn erase_types(self) -> [WidgetId; LEN] {
|
||||
self.arr.map::<EraseId>(&mut ())
|
||||
}
|
||||
}
|
||||
|
||||
pub type WidgetRef<W> = WidgetArr<(W, ())>;
|
||||
pub type WidgetRef<W> = WidgetArr<1, (W,)>;
|
||||
|
||||
impl<W> WidgetRef<W> {
|
||||
pub fn handle(&self) -> WidgetId<W> {
|
||||
self.arr.head().clone()
|
||||
impl<W: WidgetLike> WidgetRef<W> {
|
||||
pub fn handle(&self) -> WidgetId<W::Widget> {
|
||||
self.arr.0.clone()
|
||||
}
|
||||
pub fn to_id(self) -> WidgetId<W> {
|
||||
self.arr.into_head()
|
||||
pub fn to_id(self) -> WidgetId<W::Widget> {
|
||||
self.arr.0
|
||||
}
|
||||
}
|
||||
|
||||
impl<W> WidgetLike for WidgetRef<W> {
|
||||
type Widget = W;
|
||||
fn id(self, _: &mut UIBuilder) -> WidgetId<W> {
|
||||
self.arr.into_head()
|
||||
}
|
||||
pub trait WidgetArrLike<const LEN: usize> {
|
||||
type Ws: WidgetLikeTuple<LEN>;
|
||||
fn ui(self, ui: &mut UIBuilder) -> WidgetArr<LEN, Self::Ws>;
|
||||
}
|
||||
|
||||
pub trait WidgetArrLike {
|
||||
const LEN: usize;
|
||||
type Ws: WidgetList;
|
||||
fn ui(self, ui: &mut UIBuilder) -> WidgetArr<Self::Ws>;
|
||||
}
|
||||
|
||||
impl<Ws: WidgetList> WidgetArrLike for WidgetArr<Ws> {
|
||||
const LEN: usize = Ws::LEN;
|
||||
impl<const LEN: usize, Ws: WidgetLikeTuple<LEN>> WidgetArrLike<LEN> for WidgetArr<LEN, Ws> {
|
||||
type Ws = Ws;
|
||||
fn ui(self, _: &mut UIBuilder) -> WidgetArr<Ws> {
|
||||
fn ui(self, _: &mut UIBuilder) -> WidgetArr<LEN, Ws> {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl<W: WidgetLike> WidgetArrLike for (W, ())
|
||||
where
|
||||
Self: TupleList,
|
||||
{
|
||||
const LEN: usize = <Self as TupleList>::LEN;
|
||||
type Ws = (W::Widget, ());
|
||||
fn ui(self, ui: &mut UIBuilder) -> WidgetArr<(W::Widget, ())> {
|
||||
WidgetArr::new(ui.clone(), (self.0.id(ui), ()))
|
||||
impl_tuple!(Widget);
|
||||
impl_tuple!(WidgetLike);
|
||||
impl_tuple!(WidgetIdLike);
|
||||
|
||||
pub trait WidgetIdLike {
|
||||
fn erase_type(self) -> WidgetId;
|
||||
}
|
||||
}
|
||||
impl<W: WidgetLike, T: WidgetArrLike> WidgetArrLike for (W, T)
|
||||
where
|
||||
Self: TupleList,
|
||||
{
|
||||
const LEN: usize = <Self as TupleList>::LEN;
|
||||
type Ws = (W::Widget, T::Ws);
|
||||
fn ui(self, ui: &mut UIBuilder) -> WidgetArr<(W::Widget, T::Ws)> {
|
||||
WidgetArr::new(ui.clone(), (self.0.id(ui), self.1.ui(ui).arr))
|
||||
|
||||
impl<W> WidgetIdLike for WidgetId<W> {
|
||||
fn erase_type(self) -> WidgetId {
|
||||
self.erase_type()
|
||||
}
|
||||
}
|
||||
|
||||
pub trait WidgetList: TupleList {
|
||||
type Ids: IdList;
|
||||
pub struct ToId;
|
||||
impl WidgetLikeWrapper for ToId {
|
||||
type Wrap<T: WidgetLike> = WidgetId<T::Widget>;
|
||||
type Ctx = UIBuilder;
|
||||
fn wrap<T: WidgetLike>(t: T, ctx: &mut Self::Ctx) -> Self::Wrap<T> {
|
||||
t.add(ctx)
|
||||
}
|
||||
impl<H> WidgetList for (H, ()) {
|
||||
type Ids = (WidgetId<H>, ());
|
||||
}
|
||||
impl<H, T: WidgetList> WidgetList for (H, T) {
|
||||
type Ids = (WidgetId<H>, T::Ids);
|
||||
}
|
||||
|
||||
pub trait IdList: TupleList {
|
||||
fn iter(self) -> impl Iterator<Item = WidgetId>;
|
||||
fn all(self) -> Vec<WidgetId>
|
||||
struct EraseId;
|
||||
impl WidgetIdLikeMapper for EraseId {
|
||||
type Map = WidgetId<()>;
|
||||
type Ctx = ();
|
||||
fn map<Id: WidgetIdLike>(t: Id, _: &mut Self::Ctx) -> Self::Map {
|
||||
t.erase_type()
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: WidgetLikeTuple<LEN>, const LEN: usize> WidgetArrLike<LEN> for T
|
||||
where
|
||||
Self: Sized,
|
||||
T::Wrap<ToId>: WidgetIdLikeTuple<LEN>,
|
||||
{
|
||||
self.iter().collect::<Vec<_>>()
|
||||
}
|
||||
}
|
||||
impl<H> IdList for (WidgetId<H>, ()) {
|
||||
fn iter(self) -> impl Iterator<Item = WidgetId> {
|
||||
core::iter::once(self.0.erase_type())
|
||||
}
|
||||
}
|
||||
impl<H, T: IdList> IdList for (WidgetId<H>, T) {
|
||||
fn iter(self) -> impl Iterator<Item = WidgetId> {
|
||||
core::iter::once(self.0.erase_type()).chain(self.1.iter())
|
||||
type Ws = T;
|
||||
fn ui(self, ui: &mut UIBuilder) -> WidgetArr<LEN, T> {
|
||||
WidgetArr::new(ui.clone(), self.wrap::<ToId>(ui))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,11 +3,10 @@
|
||||
#![feature(const_trait_impl)]
|
||||
#![feature(const_from)]
|
||||
#![feature(trait_alias)]
|
||||
#![feature(generic_const_exprs)]
|
||||
|
||||
mod layout;
|
||||
mod render;
|
||||
pub mod util;
|
||||
mod util;
|
||||
mod base;
|
||||
|
||||
pub use layout::*;
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
use gui::util::IntoTupleList;
|
||||
use std::sync::Arc;
|
||||
|
||||
use app::App;
|
||||
@@ -39,19 +38,15 @@ impl Client {
|
||||
rect.color(UIColor::ORANGE),
|
||||
rect.color(UIColor::LIME).pad(10.0),
|
||||
)
|
||||
.into_list()
|
||||
.span(Axis::Y, [1, 1].into()),
|
||||
.span(Axis::Y, [1, 1]),
|
||||
rect.color(UIColor::YELLOW),
|
||||
)
|
||||
.into_list()
|
||||
.span(Axis::X, [2, 2, 1])
|
||||
.pad(10),
|
||||
)
|
||||
.into_list()
|
||||
.span(Axis::X, [1, 3]),
|
||||
rect.color(UIColor::GREEN),
|
||||
)
|
||||
.into_list()
|
||||
.span(Axis::Y, [3, 1])
|
||||
.pad(10),
|
||||
);
|
||||
|
||||
@@ -27,6 +27,7 @@ impl IDTracker {
|
||||
id
|
||||
}
|
||||
|
||||
// TODO: use this
|
||||
#[allow(dead_code)]
|
||||
pub fn free(&mut self, id: ID) {
|
||||
self.free.push(id);
|
||||
|
||||
@@ -1,70 +1,54 @@
|
||||
pub trait TupleList {
|
||||
const LEN: usize;
|
||||
type Head;
|
||||
type Tail;
|
||||
fn into_head(self) -> Self::Head;
|
||||
fn head(&self) -> &Self::Head;
|
||||
fn split(self) -> (Self::Head, Self::Tail);
|
||||
}
|
||||
impl TupleList for () {
|
||||
const LEN: usize = 0;
|
||||
type Head = ();
|
||||
type Tail = ();
|
||||
fn head(&self) -> &Self::Head {
|
||||
&()
|
||||
}
|
||||
fn into_head(self) -> Self::Head {}
|
||||
fn split(self) -> (Self::Head, Self::Tail) {
|
||||
((), ())
|
||||
}
|
||||
}
|
||||
impl<T, Rest: TupleList> TupleList for (T, Rest) {
|
||||
const LEN: usize = Rest::LEN + 1;
|
||||
type Head = T;
|
||||
type Tail = Rest;
|
||||
fn head(&self) -> &Self::Head {
|
||||
&self.0
|
||||
}
|
||||
fn into_head(self) -> Self::Head {
|
||||
self.0
|
||||
}
|
||||
fn split(self) -> (Self::Head, Self::Tail) {
|
||||
(self.0, self.1)
|
||||
}
|
||||
}
|
||||
|
||||
pub trait IntoTupleList {
|
||||
type List;
|
||||
fn into_list(self) -> Self::List;
|
||||
}
|
||||
|
||||
impl IntoTupleList for () {
|
||||
type List = ();
|
||||
fn into_list(self) -> Self::List {}
|
||||
}
|
||||
|
||||
macro_rules! impl_tuple {
|
||||
($H:tt $($T:tt)*) => {
|
||||
impl<$H, $($T,)*> IntoTupleList for ($H, $($T,)*) {
|
||||
type List = ($H, <($($T,)*) as IntoTupleList>::List);
|
||||
fn into_list(self) -> Self::List {
|
||||
($Tuple:ident $Bound:ident $Wrapper:ident $Mapper:ident $n:expr;$TL:tt $($T:tt)*) => {
|
||||
#[allow(non_snake_case)]
|
||||
let ($H, $($T,)*) = self;
|
||||
($H, ($($T,)*).into_list())
|
||||
impl<$($T: $Bound,)* $TL: $Bound> $Tuple<$n> for ($($T,)* $TL,) {
|
||||
type Wrap<W: $Wrapper> = ($(W::Wrap<$T>,)* W::Wrap<$TL>,);
|
||||
type Map<M: $Mapper> = [M::Map; $n];
|
||||
fn wrap<W: $Wrapper>(self, ctx: &mut W::Ctx) -> ($(W::Wrap<$T>,)* W::Wrap<$TL>,) {
|
||||
let ($($T,)* $TL,) = self;
|
||||
($(W::wrap($T, ctx),)* W::wrap($TL, ctx),)
|
||||
}
|
||||
fn map<M: $Mapper>(self, ctx: &mut M::Ctx) -> [M::Map; $n] {
|
||||
let ($($T,)* $TL,) = self;
|
||||
[$(M::map($T, ctx),)* M::map($TL, ctx)]
|
||||
}
|
||||
}
|
||||
};
|
||||
($Tuple:ident, $Bound:ident, $Wrapper:ident, $Mapper:ident) => {
|
||||
pub trait $Wrapper {
|
||||
type Wrap<T: $Bound>;
|
||||
type Ctx;
|
||||
fn wrap<T: $Bound>(t: T, ctx: &mut Self::Ctx) -> Self::Wrap<T>;
|
||||
}
|
||||
|
||||
impl_tuple!(A);
|
||||
impl_tuple!(A B);
|
||||
impl_tuple!(A B C);
|
||||
impl_tuple!(A B C D);
|
||||
impl_tuple!(A B C D E);
|
||||
impl_tuple!(A B C D E F);
|
||||
impl_tuple!(A B C D E F G);
|
||||
impl_tuple!(A B C D E F G H);
|
||||
impl_tuple!(A B C D E F G H I);
|
||||
impl_tuple!(A B C D E F G H I J);
|
||||
impl_tuple!(A B C D E F G H I J K);
|
||||
impl_tuple!(A B C D E F G H I J K L);
|
||||
pub trait $Mapper {
|
||||
type Map;
|
||||
type Ctx;
|
||||
fn map<T: $Bound>(t: T, ctx: &mut Self::Ctx) -> Self::Map;
|
||||
}
|
||||
|
||||
pub trait $Tuple<const LEN: usize> {
|
||||
type Wrap<W: $Wrapper>;
|
||||
type Map<M: $Mapper>;
|
||||
fn map<M: $Mapper>(self, ctx: &mut M::Ctx) -> [M::Map; LEN];
|
||||
fn wrap<W: $Wrapper>(self, ctx: &mut W::Ctx) -> Self::Wrap<W>;
|
||||
}
|
||||
|
||||
impl_tuple!($Tuple $Bound $Wrapper $Mapper 1;A);
|
||||
impl_tuple!($Tuple $Bound $Wrapper $Mapper 2;A B);
|
||||
impl_tuple!($Tuple $Bound $Wrapper $Mapper 3;A B C);
|
||||
impl_tuple!($Tuple $Bound $Wrapper $Mapper 4;A B C D);
|
||||
impl_tuple!($Tuple $Bound $Wrapper $Mapper 5;A B C D E);
|
||||
impl_tuple!($Tuple $Bound $Wrapper $Mapper 6;A B C D E F);
|
||||
// impl_tuple!($Tuple $Bound $Wrapper $Mapper 7;A B C D E F G);
|
||||
// impl_tuple!($Tuple $Bound $Wrapper $Mapper 8;A B C D E F G H);
|
||||
// impl_tuple!($Tuple $Bound $Wrapper $Mapper 9;A B C D E F G H I);
|
||||
// impl_tuple!($Tuple $Bound $Wrapper $Mapper 10;A B C D E F G H I J);
|
||||
// impl_tuple!($Tuple $Bound $Wrapper $Mapper 11;A B C D E F G H I J K);
|
||||
// impl_tuple!($Tuple $Bound $Wrapper $Mapper 12;A B C D E F G H I J K L);
|
||||
};
|
||||
($Bound:ident) => {
|
||||
impl_tuple!(${concat($Bound, Tuple)}, $Bound, ${concat($Bound, Wrapper)}, ${concat($Bound, Mapper)});
|
||||
}
|
||||
}
|
||||
pub(crate) use impl_tuple;
|
||||
|
||||
Reference in New Issue
Block a user