IDC FINALLY OH MY GOD (I think like ctx + resize propagation + some other stuff)

This commit is contained in:
2025-09-11 00:59:26 -04:00
parent 709a2d0e17
commit 242c3b992e
15 changed files with 476 additions and 281 deletions

View File

@@ -1,7 +1,4 @@
use crate::layout::{
Cursor, Painter, StaticWidgetId, TextAttrs, TextBuffer, TextData, TextOffset, TextureHandle,
Textures, Ui, Vec2, WidgetId, WidgetIdFn, Widgets,
};
use crate::layout::{Painter, SizeCtx, StaticWidgetId, Ui, Vec2, WidgetId, WidgetIdFn};
use std::{any::Any, marker::PhantomData};
@@ -12,39 +9,16 @@ pub trait Widget: Any {
}
}
pub struct SizeCtx<'a> {
pub size: Vec2,
pub text: &'a mut TextData,
pub textures: &'a mut Textures,
pub(super) widgets: &'a Widgets,
}
impl SizeCtx<'_> {
pub fn size<W>(&mut self, id: &WidgetId<W>) -> Vec2 {
self.widgets.get_dyn_dynamic(&id.id).get_size(self)
}
pub fn draw_text(
&mut self,
buffer: &mut TextBuffer,
content: &str,
attrs: &TextAttrs,
cursor: &Cursor,
) -> (TextureHandle, TextOffset) {
self.text
.draw(buffer, content, attrs, cursor, self.textures)
}
}
pub struct WidgetTag;
pub struct FnTag;
pub trait WidgetLike<Tag> {
pub trait WidgetLike<Ctx, Tag> {
type Widget: 'static;
fn add(self, ui: &mut Ui) -> WidgetId<Self::Widget>;
fn add(self, ui: &mut Ui<Ctx>) -> WidgetId<Self::Widget>;
fn with_id<W2>(
self,
f: impl FnOnce(&mut Ui, WidgetId<Self::Widget>) -> WidgetId<W2>,
) -> impl WidgetIdFn<W2>
f: impl FnOnce(&mut Ui<Ctx>, WidgetId<Self::Widget>) -> WidgetId<W2>,
) -> impl WidgetIdFn<W2, Ctx>
where
Self: Sized,
{
@@ -53,7 +27,7 @@ pub trait WidgetLike<Tag> {
f(ui, id)
}
}
fn add_static(self, ui: &mut Ui) -> StaticWidgetId<Self::Widget>
fn add_static(self, ui: &mut Ui<Ctx>) -> StaticWidgetId<Self::Widget>
where
Self: Sized,
{
@@ -64,19 +38,19 @@ pub trait WidgetLike<Tag> {
/// 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, Ctx>: FnOnce(&mut Ui<Ctx>) -> W {}
impl<W: Widget, F: FnOnce(&mut Ui<Ctx>) -> W, Ctx> WidgetFn<W, Ctx> for F {}
impl<W: Widget, F: FnOnce(&mut Ui) -> W> WidgetLike<FnTag> for F {
impl<W: Widget, F: FnOnce(&mut Ui<Ctx>) -> W, Ctx> WidgetLike<Ctx, FnTag> for F {
type Widget = W;
fn add(self, ui: &mut Ui) -> WidgetId<W> {
fn add(self, ui: &mut Ui<Ctx>) -> WidgetId<W> {
self(ui).add(ui)
}
}
impl<W: Widget> WidgetLike<WidgetTag> for W {
impl<W: Widget, Ctx> WidgetLike<Ctx, WidgetTag> for W {
type Widget = W;
fn add(self, ui: &mut Ui) -> WidgetId<W> {
fn add(self, ui: &mut Ui<Ctx>) -> WidgetId<W> {
ui.add_widget(self)
}
}
@@ -96,22 +70,22 @@ impl<const LEN: usize, Ws> WidgetArr<LEN, Ws> {
}
pub struct ArrTag;
pub trait WidgetArrLike<const LEN: usize, Tags> {
pub trait WidgetArrLike<const LEN: usize, Ctx, Tags> {
type Ws;
fn ui(self, ui: &mut Ui) -> WidgetArr<LEN, Self::Ws>;
fn ui(self, ui: &mut Ui<Ctx>) -> WidgetArr<LEN, Self::Ws>;
}
impl<const LEN: usize, Ws> WidgetArrLike<LEN, ArrTag> for WidgetArr<LEN, Ws> {
impl<const LEN: usize, Ws, Ctx> WidgetArrLike<LEN, Ctx, ArrTag> for WidgetArr<LEN, Ws> {
type Ws = Ws;
fn ui(self, _: &mut Ui) -> WidgetArr<LEN, Ws> {
fn ui(self, _: &mut Ui<Ctx>) -> WidgetArr<LEN, Ws> {
self
}
}
impl<W: WidgetLike<WidgetTag>> WidgetArrLike<1, WidgetTag> for W {
impl<W: WidgetLike<Ctx, WidgetTag>, Ctx> WidgetArrLike<1, Ctx, WidgetTag> for W {
type Ws = (W::Widget,);
fn ui(self, ui: &mut Ui) -> WidgetArr<1, (W::Widget,)> {
WidgetArr::new([self.add(ui).erase_type()])
fn ui(self, ui: &mut Ui<Ctx>) -> WidgetArr<1, (W::Widget,)> {
WidgetArr::new([self.add(ui).any()])
}
}
@@ -121,9 +95,9 @@ macro_rules! impl_widget_arr {
impl_widget_arr!($n;$($W)*;$(${concat($W,Tag)})*);
};
($n:expr;$($W:ident)*;$($Tag:ident)*) => {
impl<$($W: WidgetLike<$Tag>,$Tag,)*> WidgetArrLike<$n, ($($Tag,)*)> for ($($W,)*) {
impl<Ctx, $($W: WidgetLike<Ctx, $Tag>,$Tag,)*> WidgetArrLike<$n, Ctx, ($($Tag,)*)> for ($($W,)*) {
type Ws = ($($W::Widget,)*);
fn ui(self, ui: &mut Ui) -> WidgetArr<$n, ($($W::Widget,)*)> {
fn ui(self, ui: &mut Ui<Ctx>) -> WidgetArr<$n, ($($W::Widget,)*)> {
#[allow(non_snake_case)]
let ($($W,)*) = self;
WidgetArr::new(