assoc types

This commit is contained in:
2025-08-10 05:19:00 -04:00
parent 58d9b32077
commit b6e43c157b
4 changed files with 29 additions and 21 deletions

View File

@@ -117,12 +117,12 @@ impl<T: UINum> From<T> for Padding {
}
}
pub trait WidgetUtil<W> {
fn pad(self, padding: impl Into<Padding>) -> impl WidgetLike<Regioned>;
pub trait WidgetUtil {
fn pad(self, padding: impl Into<Padding>) -> impl WidgetLike<Widget = Regioned>;
}
impl<W: Widget, WL: WidgetLike<W>> WidgetUtil<W> for WL {
fn pad(self, padding: impl Into<Padding>) -> impl WidgetLike<Regioned> {
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(),
@@ -130,12 +130,12 @@ impl<W: Widget, WL: WidgetLike<W>> WidgetUtil<W> for WL {
}
}
pub trait WidgetArrUtil<const LEN: usize, Ws> {
fn span(self, axis: Axis, ratios: [impl UINum; LEN]) -> impl WidgetLike<Span>;
pub trait WidgetArrUtil<const LEN: usize> {
fn span(self, axis: Axis, ratios: [impl UINum; LEN]) -> impl WidgetLike<Widget = Span>;
}
impl<const LEN: usize, Ws, Wa: WidgetArrLike<LEN, Ws>> WidgetArrUtil<LEN, Ws> for Wa {
fn span(self, axis: Axis, ratios: [impl UINum; LEN]) -> impl WidgetLike<Span> {
impl<const LEN: usize, Wa: WidgetArrLike<LEN>> WidgetArrUtil<LEN> for Wa {
fn span(self, axis: Axis, ratios: [impl UINum; LEN]) -> impl WidgetLike<Widget = Span> {
WidgetFn(move |ui| Span::proportioned(axis, ratios, self.ui(ui).arr))
}
}

View File

@@ -42,7 +42,7 @@ impl UIBuilder {
WidgetId::new(id, TypeId::of::<W>())
}
pub fn finish<WL: WidgetLike<W>, W>(mut self, base: WL) -> UI {
pub fn finish<W: WidgetLike>(mut self, base: W) -> UI {
let base = base.id(&mut self).erase_type();
let mut ui = Rc::into_inner(self.ui).unwrap().into_inner();
ui.base = Some(base);

View File

@@ -54,33 +54,38 @@ impl<W> WidgetId<W> {
}
}
pub trait WidgetLike<W> {
fn id(self, ui: &mut UIBuilder) -> WidgetId<W>;
pub trait WidgetLike {
type Widget;
fn id(self, ui: &mut UIBuilder) -> WidgetId<Self::Widget>;
}
/// wouldn't be needed if negative trait bounds & disjoint impls existed
pub struct WidgetFn<F: FnOnce(&mut UIBuilder) -> W, W>(pub F);
impl<W: Widget, F: FnOnce(&mut UIBuilder) -> W> WidgetLike<W> for WidgetFn<F, W> {
impl<W: Widget, F: FnOnce(&mut UIBuilder) -> W> WidgetLike for WidgetFn<F, W> {
type Widget = W;
fn id(self, ui: &mut UIBuilder) -> WidgetId<W> {
let w = (self.0)(ui);
ui.add(w).to_id()
}
}
impl<W: Widget> WidgetLike<W> for W {
impl<W: Widget> WidgetLike for W {
type Widget = W;
fn id(self, ui: &mut UIBuilder) -> WidgetId<W> {
ui.add(self).to_id()
}
}
impl<W> WidgetLike<W> for WidgetId<W> {
impl<W> WidgetLike for WidgetId<W> {
type Widget = W;
fn id(self, _: &mut UIBuilder) -> WidgetId<W> {
self
}
}
impl<W> WidgetLike<W> for WidgetArr<1, (W,)> {
impl<W> WidgetLike for WidgetArr<1, (W,)> {
type Widget = W;
fn id(self, _: &mut UIBuilder) -> WidgetId<W> {
let [id] = self.arr;
id.cast_type()
@@ -116,11 +121,13 @@ impl<W> WidgetRef<W> {
}
}
pub trait WidgetArrLike<const LEN: usize, Ws> {
fn ui(self, ui: &mut UIBuilder) -> WidgetArr<LEN, Ws>;
pub trait WidgetArrLike<const LEN: usize> {
type Ws;
fn ui(self, ui: &mut UIBuilder) -> WidgetArr<LEN, Self::Ws>;
}
impl<const LEN: usize, Ws> WidgetArrLike<LEN, Ws> for WidgetArr<LEN, Ws> {
impl<const LEN: usize, Ws> WidgetArrLike<LEN> for WidgetArr<LEN, Ws> {
type Ws = Ws;
fn ui(self, _: &mut UIBuilder) -> WidgetArr<LEN, Ws> {
self
}
@@ -129,9 +136,10 @@ impl<const LEN: usize, Ws> WidgetArrLike<LEN, Ws> for WidgetArr<LEN, Ws> {
// I hate this language it's so bad why do I even use it
macro_rules! impl_node_arr {
($n:expr;$($T:tt)*) => {
impl<$($T,${concat($T,$T)}: WidgetLike<$T>,)*> WidgetArrLike<$n, ($($T,)*)> for ($(${concat($T,$T)},)*) {
impl<$($T: WidgetLike,)*> WidgetArrLike<$n> for ($($T,)*) {
type Ws = ($($T::Widget,)*);
#[allow(unused_variables)]
fn ui(self, ui: &mut UIBuilder) -> WidgetArr<$n, ($($T,)*)> {
fn ui(self, ui: &mut UIBuilder) -> WidgetArr<$n, ($($T::Widget,)*)> {
#[allow(non_snake_case)]
let ($($T,)*) = self;
WidgetArr::new(

View File

@@ -1,7 +1,7 @@
use std::sync::Arc;
use app::App;
use gui::{primitive::Axis, RoundedRect, UIColor, WidgetArrLike, WidgetArrUtil, WidgetUtil, UI};
use gui::{primitive::Axis, RoundedRect, UIColor, WidgetArrUtil, WidgetUtil, UI};
use render::Renderer;
use winit::{event::WindowEvent, event_loop::ActiveEventLoop, window::Window};