From 84c460a91f7d98d565f4b71a41420a3e4a8c8c31 Mon Sep 17 00:00:00 2001 From: shadow cat Date: Wed, 3 Dec 2025 22:51:33 -0500 Subject: [PATCH] app work --- TODO | 2 ++ src/bin/test/main.rs | 12 ++++++------ src/core/position/pad.rs | 31 +++++++++++++++++++++++++++++++ src/core/text/build.rs | 2 +- src/core/trait_fns.rs | 13 +++++++++++++ src/layout/id.rs | 3 +++ src/layout/widget.rs | 11 ++++++++++- 7 files changed, 66 insertions(+), 8 deletions(-) diff --git a/TODO b/TODO index 69b55c6..c4f9356 100644 --- a/TODO +++ b/TODO @@ -36,3 +36,5 @@ don't forget I'm streaming tags vecs for each widget type? + +POTENTIAL BUG: closures that store IDs will not decrement the id!!! need to not increment id if moved into closure somehow??? wait no, need to decrement ID every time an event fn is added...... only if the id is used in it..?? diff --git a/src/bin/test/main.rs b/src/bin/test/main.rs index 952bea6..99c06d9 100644 --- a/src/bin/test/main.rs +++ b/src/bin/test/main.rs @@ -142,7 +142,7 @@ impl Client { .stack() .add_static(&mut ui); - let btext = |content| text(content).size(30); + let btext = |content| wtext(content).size(30); let text_test = ( btext("this is a").align(Align::LEFT), @@ -160,21 +160,21 @@ impl Client { ) .span(Dir::RIGHT) .center(), - text("pretty cool right?").size(50), + wtext("pretty cool right?").size(50), ) .span(Dir::DOWN) .add_static(&mut ui); let texts = Span::empty(Dir::DOWN).gap(10).add_static(&mut ui); let msg_area = texts.scroll().masked().background(rect(Color::SKY)); - let add_text = text("add") + let add_text = wtext("add") .editable(false) .text_align(Align::LEFT) .size(30) .attr::(()) .id_on(Submit, move |id, client: &mut Client, _| { let content = client.ui.text(id).take(); - let text = text(content) + let text = wtext(content) .editable(false) .size(30) .text_align(Align::LEFT) @@ -226,7 +226,7 @@ impl Client { .edit_on(CursorSense::HoverEnd, move |r, _| { r.color = color; }); - (rect, text(label).size(30).text_align(Align::CENTER)).stack() + (rect, wtext(label).size(30).text_align(Align::CENTER)).stack() }; let tabs = ( @@ -242,7 +242,7 @@ impl Client { ) .span(Dir::RIGHT); - let info = text("").add(&mut ui); + let info = wtext("").add(&mut ui); let info_sect = info.clone().pad(10).align(Align::RIGHT); ((tabs.height(40), main).span(Dir::DOWN), info_sect) diff --git a/src/core/position/pad.rs b/src/core/position/pad.rs index 9bd9343..5ccc7f8 100644 --- a/src/core/position/pad.rs +++ b/src/core/position/pad.rs @@ -39,6 +39,13 @@ pub struct Padding { } impl Padding { + pub const ZERO: Self = Self { + left: 0.0, + right: 0.0, + top: 0.0, + bottom: 0.0, + }; + pub fn uniform(amt: impl UiNum) -> Self { let amt = amt.to_f32(); Self { @@ -75,6 +82,30 @@ impl Padding { } } + pub fn top(amt: impl UiNum) -> Self { + let mut s = Self::ZERO; + s.top = amt.to_f32(); + s + } + + pub fn bottom(amt: impl UiNum) -> Self { + let mut s = Self::ZERO; + s.bottom = amt.to_f32(); + s + } + + pub fn left(amt: impl UiNum) -> Self { + let mut s = Self::ZERO; + s.left = amt.to_f32(); + s + } + + pub fn right(amt: impl UiNum) -> Self { + let mut s = Self::ZERO; + s.right = amt.to_f32(); + s + } + pub fn with_top(mut self, amt: impl UiNum) -> Self { self.top = amt.to_f32(); self diff --git a/src/core/text/build.rs b/src/core/text/build.rs index a609b13..8337093 100644 --- a/src/core/text/build.rs +++ b/src/core/text/build.rs @@ -114,7 +114,7 @@ impl FnOnce<(&mut Ui,)> for TextBuilder) -> TextBuilder { +pub fn wtext(content: impl Into) -> TextBuilder { TextBuilder { content: content.into(), attrs: TextAttrs::default(), diff --git a/src/core/trait_fns.rs b/src/core/trait_fns.rs index 186f2d2..d7ae059 100644 --- a/src/core/trait_fns.rs +++ b/src/core/trait_fns.rs @@ -15,7 +15,9 @@ pub trait CoreWidget { fn scroll(self) -> impl WidgetIdFn; fn masked(self) -> impl WidgetFn; fn background(self, w: impl WidgetLike) -> impl WidgetFn; + fn foreground(self, w: impl WidgetLike) -> impl WidgetFn; fn layer_offset(self, offset: usize) -> impl WidgetFn; + fn to_any(self) -> impl WidgetRet; } impl, Tag> CoreWidget for W { @@ -120,12 +122,23 @@ impl, Tag> CoreWidget for W { } } + fn foreground(self, w: impl WidgetLike) -> impl WidgetFn { + move |ui| Stack { + children: vec![self.add(ui).any(), w.add(ui).any()], + size: StackSize::Child(0), + } + } + fn layer_offset(self, offset: usize) -> impl WidgetFn { move |ui| LayerOffset { inner: self.add(ui).any(), offset, } } + + fn to_any(self) -> impl WidgetRet { + |ui| self.add(ui).any() + } } pub trait CoreWidgetArr, Tag> { diff --git a/src/layout/id.rs b/src/layout/id.rs index e0ff91c..7cdb390 100644 --- a/src/layout/id.rs +++ b/src/layout/id.rs @@ -141,6 +141,9 @@ pub struct IdFnTag; pub trait WidgetIdFn: FnOnce(&mut Ui) -> WidgetId {} impl WidgetId> WidgetIdFn for F {} +pub trait WidgetRet: FnOnce(&mut Ui) -> WidgetId {} +impl WidgetId> WidgetRet for F {} + impl WidgetLike for WidgetId { type Widget = W; fn add(self, _: &mut Ui) -> WidgetId { diff --git a/src/layout/widget.rs b/src/layout/widget.rs index a8d5d02..5faa161 100644 --- a/src/layout/widget.rs +++ b/src/layout/widget.rs @@ -1,4 +1,7 @@ -use crate::layout::{Len, Painter, SizeCtx, StaticWidgetId, Ui, WidgetId, WidgetIdFn}; +use crate::{ + core::WidgetPtr, + layout::{Len, Painter, SizeCtx, StaticWidgetId, Ui, WidgetId, WidgetIdFn}, +}; use std::{any::Any, marker::PhantomData}; @@ -48,6 +51,12 @@ pub trait WidgetLike { { ui.set_root(self); } + fn set_ptr(self, ptr: &WidgetId, ui: &mut Ui) + where + Self: Sized, + { + ui[ptr].inner = Some(self.add(ui).any()); + } } /// A function that returns a widget given a UI.