remove context from ui (again) and create weird trait for it

This commit is contained in:
2025-09-24 17:41:25 -04:00
parent 26c248dcba
commit 719bee4b31
12 changed files with 107 additions and 92 deletions

View File

@@ -15,7 +15,7 @@ impl Widget for Image {
} }
} }
pub fn image<Ctx>(image: impl LoadableImage) -> impl WidgetFn<Image, Ctx> { pub fn image(image: impl LoadableImage) -> impl WidgetFn<Image> {
let image = image.get_image().expect("Failed to load image"); let image = image.get_image().expect("Failed to load image");
move |ui| Image { move |ui| Image {
handle: ui.add_texture(image), handle: ui.add_texture(image),

View File

@@ -116,10 +116,10 @@ impl TextBuilder {
} }
} }
impl<Ctx> FnOnce<(&mut Ui<Ctx>,)> for TextBuilder { impl FnOnce<(&mut Ui,)> for TextBuilder {
type Output = Text; type Output = Text;
extern "rust-call" fn call_once(self, args: (&mut Ui<Ctx>,)) -> Self::Output { extern "rust-call" fn call_once(self, args: (&mut Ui,)) -> Self::Output {
let mut buf = let mut buf =
TextBuffer::new_empty(Metrics::new(self.attrs.font_size, self.attrs.line_height)); TextBuffer::new_empty(Metrics::new(self.attrs.font_size, self.attrs.line_height));
buf.set_text( buf.set_text(

View File

@@ -255,10 +255,10 @@ impl TextInputResult {
} }
} }
impl<Ctx> FnOnce<(&mut Ui<Ctx>,)> for TextEditBuilder { impl FnOnce<(&mut Ui,)> for TextEditBuilder {
type Output = TextEdit; type Output = TextEdit;
extern "rust-call" fn call_once(self, args: (&mut Ui<Ctx>,)) -> Self::Output { extern "rust-call" fn call_once(self, args: (&mut Ui,)) -> Self::Output {
let mut text = TextEdit { let mut text = TextEdit {
buf: TextBuffer::new_empty(Metrics::new(self.attrs.font_size, self.attrs.line_height)), buf: TextBuffer::new_empty(Metrics::new(self.attrs.font_size, self.attrs.line_height)),
attrs: self.attrs, attrs: self.attrs,

View File

@@ -1,34 +1,34 @@
use super::*; use super::*;
use crate::prelude::*; use crate::prelude::*;
pub trait CoreWidget<W, Ctx, Tag> { pub trait CoreWidget<W, Tag> {
fn pad(self, padding: impl Into<Padding>) -> impl WidgetFn<Padded, Ctx>; fn pad(self, padding: impl Into<Padding>) -> impl WidgetFn<Padded>;
fn align(self, align: Align) -> impl WidgetFn<Aligned, Ctx>; fn align(self, align: Align) -> impl WidgetFn<Aligned>;
fn center(self) -> impl WidgetFn<Aligned, Ctx>; fn center(self) -> impl WidgetFn<Aligned>;
fn label(self, label: impl Into<String>) -> impl WidgetIdFn<W, Ctx>; fn label(self, label: impl Into<String>) -> impl WidgetIdFn<W>;
fn size(self, size: impl Into<Vec2>) -> impl WidgetFn<Sized, Ctx>; fn size(self, size: impl Into<Vec2>) -> impl WidgetFn<Sized>;
} }
impl<W: WidgetLike<Ctx, Tag>, Ctx, Tag> CoreWidget<W::Widget, Ctx, Tag> for W { impl<W: WidgetLike<Tag>, Tag> CoreWidget<W::Widget, Tag> for W {
fn pad(self, padding: impl Into<Padding>) -> impl WidgetFn<Padded, Ctx> { fn pad(self, padding: impl Into<Padding>) -> impl WidgetFn<Padded> {
|ui| Padded { |ui| Padded {
padding: padding.into(), padding: padding.into(),
inner: self.add(ui).any(), inner: self.add(ui).any(),
} }
} }
fn align(self, align: Align) -> impl WidgetFn<Aligned, Ctx> { fn align(self, align: Align) -> impl WidgetFn<Aligned> {
move |ui| Aligned { move |ui| Aligned {
inner: self.add(ui).any(), inner: self.add(ui).any(),
align, align,
} }
} }
fn center(self) -> impl WidgetFn<Aligned, Ctx> { fn center(self) -> impl WidgetFn<Aligned> {
self.align(Align::Center) self.align(Align::Center)
} }
fn label(self, label: impl Into<String>) -> impl WidgetIdFn<W::Widget, Ctx> { fn label(self, label: impl Into<String>) -> impl WidgetIdFn<W::Widget> {
|ui| { |ui| {
let id = self.add(ui); let id = self.add(ui);
ui.set_label(&id, label.into()); ui.set_label(&id, label.into());
@@ -36,7 +36,7 @@ impl<W: WidgetLike<Ctx, Tag>, Ctx, Tag> CoreWidget<W::Widget, Ctx, Tag> for W {
} }
} }
fn size(self, size: impl Into<Vec2>) -> impl WidgetFn<Sized, Ctx> { fn size(self, size: impl Into<Vec2>) -> impl WidgetFn<Sized> {
move |ui| Sized { move |ui| Sized {
inner: self.add(ui).any(), inner: self.add(ui).any(),
size: size.into(), size: size.into(),
@@ -44,20 +44,20 @@ impl<W: WidgetLike<Ctx, Tag>, Ctx, Tag> CoreWidget<W::Widget, Ctx, Tag> for W {
} }
} }
pub trait CoreWidgetArr<const LEN: usize, Ctx, Tag> { pub trait CoreWidgetArr<const LEN: usize, Tag> {
fn span(self, dir: Dir, lengths: impl IntoSpanLens<LEN>) -> impl WidgetFn<Span, Ctx>; fn span(self, dir: Dir, lengths: impl IntoSpanLens<LEN>) -> impl WidgetFn<Span>;
fn stack(self) -> impl WidgetFn<Stack, Ctx>; fn stack(self) -> impl WidgetFn<Stack>;
} }
impl<const LEN: usize, Wa: WidgetArrLike<LEN, Ctx, Tag>, Ctx, Tag> CoreWidgetArr<LEN, Ctx, Tag> for Wa { impl<const LEN: usize, Wa: WidgetArrLike<LEN, Tag>, Tag> CoreWidgetArr<LEN, Tag> for Wa {
fn span(self, dir: Dir, lengths: impl IntoSpanLens<LEN>) -> impl WidgetFn<Span, Ctx> { fn span(self, dir: Dir, lengths: impl IntoSpanLens<LEN>) -> impl WidgetFn<Span> {
let lengths = lengths.into_lens(); let lengths = lengths.into_lens();
move |ui| Span { move |ui| Span {
children: self.ui(ui).arr.into_iter().zip(lengths).collect(), children: self.ui(ui).arr.into_iter().zip(lengths).collect(),
dir, dir,
} }
} }
fn stack(self) -> impl WidgetFn<Stack, Ctx> { fn stack(self) -> impl WidgetFn<Stack> {
move |ui| Stack { move |ui| Stack {
children: self.ui(ui).arr.to_vec(), children: self.ui(ui).arr.to_vec(),
} }

View File

@@ -1,10 +1,10 @@
use crate::{ use crate::{
layout::{Ui, UiModule, Widget, WidgetId, WidgetIdFn, WidgetLike}, layout::{IdFnTag, Ui, UiModule, Widget, WidgetId, WidgetIdFn, WidgetLike},
util::Id, util::Id,
}; };
pub trait UiCtx { pub trait UiCtx {
fn ui(&mut self) -> &mut Ui<Self> fn ui(&mut self) -> &mut Ui
where where
Self: Sized; Self: Sized;
} }
@@ -22,13 +22,17 @@ pub trait EventFn<Ctx, Data>: FnMut(&mut Ctx, Data) + 'static {}
impl<F: FnMut(&mut Ctx, Data) + 'static, Ctx, Data> EventFn<Ctx, Data> for F {} impl<F: FnMut(&mut Ctx, Data) + 'static, Ctx, Data> EventFn<Ctx, Data> for F {}
pub trait Eventable<W, Ctx, Tag> { pub trait Eventable<W, Ctx, Tag> {
fn on<E: Event<Ctx>>(self, event: E, f: impl EventFn<Ctx, E::Data>) -> impl WidgetIdFn<W, Ctx>; fn on<E: Event<Ctx>>(
self,
event: E,
f: impl EventFn<Ctx, E::Data>,
) -> impl WidgetIdFn<W> + Eventable<W, Ctx, IdFnTag>;
fn id_on<E: Event<Ctx>>( fn id_on<E: Event<Ctx>>(
self, self,
event: E, event: E,
f: impl FnMut(&WidgetId<W>, &mut Ctx, E::Data) + 'static, f: impl FnMut(&WidgetId<W>, &mut Ctx, E::Data) + 'static,
) -> impl WidgetIdFn<W, Ctx> ) -> impl WidgetIdFn<W> + Eventable<W, Ctx, IdFnTag>
where where
W: Widget; W: Widget;
@@ -36,18 +40,29 @@ pub trait Eventable<W, Ctx, Tag> {
self, self,
event: E, event: E,
f: impl FnMut(&mut W, E::Data) + 'static, f: impl FnMut(&mut W, E::Data) + 'static,
) -> impl WidgetIdFn<W, Ctx> ) -> impl WidgetIdFn<W> + Eventable<W, Ctx, IdFnTag>
where where
W: Widget, W: Widget,
Ctx: UiCtx; Ctx: UiCtx;
} }
impl<W: WidgetLike<Ctx, Tag>, Ctx, Tag> Eventable<W::Widget, Ctx, Tag> for W { pub trait Ctxable<W, Tag> {
/// sets context which lets event functions work without needing to specify generics
fn ctx<Ctx>(self) -> impl WidgetLike<Tag> + Eventable<W, Ctx, Tag>;
}
impl<W: WidgetLike<Tag>, Tag> Ctxable<W::Widget, Tag> for W {
fn ctx<Ctx>(self) -> impl WidgetLike<Tag> + Eventable<W::Widget, Ctx, Tag> {
self
}
}
impl<W: WidgetLike<Tag>, Ctx, Tag> Eventable<W::Widget, Ctx, Tag> for W {
fn on<E: Event<Ctx>>( fn on<E: Event<Ctx>>(
self, self,
event: E, event: E,
f: impl EventFn<Ctx, E::Data>, f: impl EventFn<Ctx, E::Data>,
) -> impl WidgetIdFn<W::Widget, Ctx> { ) -> impl WidgetIdFn<W::Widget> + Eventable<W::Widget, Ctx, IdFnTag> {
move |ui| { move |ui| {
let id = self.add(ui); let id = self.add(ui);
ui.modules ui.modules
@@ -61,7 +76,7 @@ impl<W: WidgetLike<Ctx, Tag>, Ctx, Tag> Eventable<W::Widget, Ctx, Tag> for W {
self, self,
event: E, event: E,
mut f: impl FnMut(&WidgetId<W::Widget>, &mut Ctx, E::Data) + 'static, mut f: impl FnMut(&WidgetId<W::Widget>, &mut Ctx, E::Data) + 'static,
) -> impl WidgetIdFn<W::Widget, Ctx> ) -> impl WidgetIdFn<W::Widget> + Eventable<W::Widget, Ctx, IdFnTag>
where where
W::Widget: Widget, W::Widget: Widget,
{ {
@@ -75,7 +90,7 @@ impl<W: WidgetLike<Ctx, Tag>, Ctx, Tag> Eventable<W::Widget, Ctx, Tag> for W {
self, self,
event: E, event: E,
mut f: impl FnMut(&mut W::Widget, E::Data) + 'static, mut f: impl FnMut(&mut W::Widget, E::Data) + 'static,
) -> impl WidgetIdFn<W::Widget, Ctx> ) -> impl WidgetIdFn<W::Widget> + Eventable<W::Widget, Ctx, IdFnTag>
where where
W::Widget: Widget, W::Widget: Widget,
Ctx: UiCtx, Ctx: UiCtx,

View File

@@ -131,14 +131,14 @@ impl<W> Drop for WidgetId<W> {
pub struct IdTag; pub struct IdTag;
pub struct IdFnTag; pub struct IdFnTag;
pub trait WidgetIdFn<W, Ctx>: FnOnce(&mut Ui<Ctx>) -> WidgetId<W> {} pub trait WidgetIdFn<W>: FnOnce(&mut Ui) -> WidgetId<W> {}
impl<W, F: FnOnce(&mut Ui<Ctx>) -> WidgetId<W>, Ctx> WidgetIdFn<W, Ctx> for F {} impl<W, F: FnOnce(&mut Ui) -> WidgetId<W>> WidgetIdFn<W> for F {}
/// TODO: does this ever make sense to use? it allows for invalid ids /// TODO: does this ever make sense to use? it allows for invalid ids
pub trait Idable<Ctx, Tag> { pub trait Idable<Tag> {
type Widget: Widget; type Widget: Widget;
fn set(self, ui: &mut Ui<Ctx>, id: &WidgetId<Self::Widget>); fn set(self, ui: &mut Ui, id: &WidgetId<Self::Widget>);
fn id(self, id: &WidgetId<Self::Widget>) -> impl WidgetIdFn<Self::Widget, Ctx> fn id(self, id: &WidgetId<Self::Widget>) -> impl WidgetIdFn<Self::Widget>
where where
Self: Sized, Self: Sized,
{ {
@@ -148,7 +148,7 @@ pub trait Idable<Ctx, Tag> {
id id
} }
} }
fn id_static(self, id: StaticWidgetId<Self::Widget>) -> impl WidgetIdFn<Self::Widget, Ctx> fn id_static(self, id: StaticWidgetId<Self::Widget>) -> impl WidgetIdFn<Self::Widget>
where where
Self: Sized, Self: Sized,
{ {
@@ -160,33 +160,33 @@ pub trait Idable<Ctx, Tag> {
} }
} }
impl<W: Widget, Ctx> Idable<Ctx, WidgetTag> for W { impl<W: Widget> Idable<WidgetTag> for W {
type Widget = W; type Widget = W;
fn set(self, ui: &mut Ui<Ctx>, id: &WidgetId<Self::Widget>) { fn set(self, ui: &mut Ui, id: &WidgetId<Self::Widget>) {
ui.set(id, self); ui.set(id, self);
} }
} }
impl<F: FnOnce(&mut Ui<Ctx>) -> W, W: Widget, Ctx> Idable<Ctx, FnTag> for F { impl<F: FnOnce(&mut Ui) -> W, W: Widget> Idable<FnTag> for F {
type Widget = W; type Widget = W;
fn set(self, ui: &mut Ui<Ctx>, id: &WidgetId<Self::Widget>) { fn set(self, ui: &mut Ui, id: &WidgetId<Self::Widget>) {
let w = self(ui); let w = self(ui);
ui.set(id, w); ui.set(id, w);
} }
} }
impl<W: 'static, Ctx> WidgetLike<Ctx, IdTag> for WidgetId<W> { impl<W: 'static> WidgetLike<IdTag> for WidgetId<W> {
type Widget = W; type Widget = W;
fn add(self, _: &mut Ui<Ctx>) -> WidgetId<W> { fn add(self, _: &mut Ui) -> WidgetId<W> {
self self
} }
} }
impl<W: 'static, F: FnOnce(&mut Ui<Ctx>) -> WidgetId<W>, Ctx> WidgetLike<Ctx, IdFnTag> for F { impl<W: 'static, F: FnOnce(&mut Ui) -> WidgetId<W>> WidgetLike<IdFnTag> for F {
type Widget = W; type Widget = W;
fn add(self, ui: &mut Ui<Ctx>) -> WidgetId<W> { fn add(self, ui: &mut Ui) -> WidgetId<W> {
self(ui) self(ui)
} }
} }
@@ -200,9 +200,9 @@ impl<W> StaticWidgetId<W> {
} }
} }
impl<W: 'static, Ctx> WidgetLike<Ctx, IdTag> for StaticWidgetId<W> { impl<W: 'static> WidgetLike<IdTag> for StaticWidgetId<W> {
type Widget = W; type Widget = W;
fn add(self, ui: &mut Ui<Ctx>) -> WidgetId<W> { fn add(self, ui: &mut Ui) -> WidgetId<W> {
self.id(&ui.send) self.id(&ui.send)
} }
} }

View File

@@ -10,13 +10,11 @@ use crate::{
}; };
use std::{ use std::{
any::{Any, TypeId}, any::{Any, TypeId},
marker::PhantomData,
ops::{Index, IndexMut}, ops::{Index, IndexMut},
sync::mpsc::{Receiver, Sender, channel}, sync::mpsc::{Receiver, Sender, channel},
}; };
// TODO: remove generic pub struct Ui {
pub struct Ui<Ctx> {
root: Option<WidgetId>, root: Option<WidgetId>,
pub(super) widgets: Widgets, pub(super) widgets: Widgets,
updates: Vec<Id>, updates: Vec<Id>,
@@ -31,17 +29,16 @@ pub struct Ui<Ctx> {
pub(crate) active: ActiveWidgets, pub(crate) active: ActiveWidgets,
pub modules: Modules, pub modules: Modules,
_pd: PhantomData<Ctx>,
} }
impl<Ctx> Ui<Ctx> { impl Ui {
pub fn add<W: Widget, Tag>(&mut self, w: impl WidgetLike<Ctx, Tag, Widget = W>) -> WidgetId<W> { pub fn add<W: Widget, Tag>(&mut self, w: impl WidgetLike<Tag, Widget = W>) -> WidgetId<W> {
w.add(self) w.add(self)
} }
pub fn add_static<W: Widget, Tag>( pub fn add_static<W: Widget, Tag>(
&mut self, &mut self,
w: impl WidgetLike<Ctx, Tag, Widget = W>, w: impl WidgetLike<Tag, Widget = W>,
) -> StaticWidgetId<W> { ) -> StaticWidgetId<W> {
let id = w.add(self); let id = w.add(self);
id.into_static() id.into_static()
@@ -70,7 +67,7 @@ impl<Ctx> Ui<Ctx> {
self.widgets.insert(id.id.duplicate(), w); self.widgets.insert(id.id.duplicate(), w);
} }
pub fn set_root<Tag>(&mut self, w: impl WidgetLike<Ctx, Tag>) { pub fn set_root<Tag>(&mut self, w: impl WidgetLike<Tag>) {
self.root = Some(w.add(self).any()); self.root = Some(w.add(self).any());
self.full_redraw = true; self.full_redraw = true;
} }
@@ -184,7 +181,7 @@ impl<Ctx> Ui<Ctx> {
} }
} }
impl<W: Widget, Ctx> Index<&WidgetId<W>> for Ui<Ctx> { impl<W: Widget> Index<&WidgetId<W>> for Ui {
type Output = W; type Output = W;
fn index(&self, id: &WidgetId<W>) -> &Self::Output { fn index(&self, id: &WidgetId<W>) -> &Self::Output {
@@ -192,14 +189,14 @@ impl<W: Widget, Ctx> Index<&WidgetId<W>> for Ui<Ctx> {
} }
} }
impl<W: Widget, Ctx> IndexMut<&WidgetId<W>> for Ui<Ctx> { impl<W: Widget> IndexMut<&WidgetId<W>> for Ui {
fn index_mut(&mut self, id: &WidgetId<W>) -> &mut Self::Output { fn index_mut(&mut self, id: &WidgetId<W>) -> &mut Self::Output {
self.updates.push(id.id.duplicate()); self.updates.push(id.id.duplicate());
self.get_mut(id).unwrap() self.get_mut(id).unwrap()
} }
} }
impl<W: Widget, Ctx> Index<StaticWidgetId<W>> for Ui<Ctx> { impl<W: Widget> Index<StaticWidgetId<W>> for Ui {
type Output = W; type Output = W;
fn index(&self, id: StaticWidgetId<W>) -> &Self::Output { fn index(&self, id: StaticWidgetId<W>) -> &Self::Output {
@@ -207,7 +204,7 @@ impl<W: Widget, Ctx> Index<StaticWidgetId<W>> for Ui<Ctx> {
} }
} }
impl<W: Widget, Ctx> IndexMut<StaticWidgetId<W>> for Ui<Ctx> { impl<W: Widget> IndexMut<StaticWidgetId<W>> for Ui {
fn index_mut(&mut self, id: StaticWidgetId<W>) -> &mut Self::Output { fn index_mut(&mut self, id: StaticWidgetId<W>) -> &mut Self::Output {
self.updates.push(id.id.id()); self.updates.push(id.id.id());
self.widgets.get_static_mut(&id).unwrap() self.widgets.get_static_mut(&id).unwrap()
@@ -224,7 +221,7 @@ impl dyn Widget {
} }
} }
impl<Ctx> Default for Ui<Ctx> { impl Default for Ui {
fn default() -> Self { fn default() -> Self {
let (send, recv) = channel(); let (send, recv) = channel();
Self { Self {
@@ -240,7 +237,6 @@ impl<Ctx> Default for Ui<Ctx> {
recv, recv,
size: Vec2::ZERO, size: Vec2::ZERO,
modules: Modules::default(), modules: Modules::default(),
_pd: PhantomData,
} }
} }
} }

View File

@@ -12,13 +12,13 @@ pub trait Widget: Any {
pub struct WidgetTag; pub struct WidgetTag;
pub struct FnTag; pub struct FnTag;
pub trait WidgetLike<Ctx, Tag> { pub trait WidgetLike<Tag> {
type Widget: 'static; type Widget: 'static;
fn add(self, ui: &mut Ui<Ctx>) -> WidgetId<Self::Widget>; fn add(self, ui: &mut Ui) -> WidgetId<Self::Widget>;
fn with_id<W2>( fn with_id<W2>(
self, self,
f: impl FnOnce(&mut Ui<Ctx>, WidgetId<Self::Widget>) -> WidgetId<W2>, f: impl FnOnce(&mut Ui, WidgetId<Self::Widget>) -> WidgetId<W2>,
) -> impl WidgetIdFn<W2, Ctx> ) -> impl WidgetIdFn<W2>
where where
Self: Sized, Self: Sized,
{ {
@@ -27,13 +27,13 @@ pub trait WidgetLike<Ctx, Tag> {
f(ui, id) f(ui, id)
} }
} }
fn add_static(self, ui: &mut Ui<Ctx>) -> StaticWidgetId<Self::Widget> fn add_static(self, ui: &mut Ui) -> StaticWidgetId<Self::Widget>
where where
Self: Sized, Self: Sized,
{ {
self.add(ui).into_static() self.add(ui).into_static()
} }
fn set_root(self, ui: &mut Ui<Ctx>) fn set_root(self, ui: &mut Ui)
where where
Self: Sized, Self: Sized,
{ {
@@ -44,19 +44,19 @@ pub trait WidgetLike<Ctx, Tag> {
/// A function that returns a widget given a UI. /// 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 /// Useful for defining trait functions on widgets that create a parent widget so that the children
/// don't need to be IDs yet /// don't need to be IDs yet
pub trait WidgetFn<W: Widget, Ctx>: FnOnce(&mut Ui<Ctx>) -> W {} pub trait WidgetFn<W: Widget>: FnOnce(&mut Ui) -> W {}
impl<W: Widget, F: FnOnce(&mut Ui<Ctx>) -> W, Ctx> WidgetFn<W, Ctx> for F {} impl<W: Widget, F: FnOnce(&mut Ui) -> W> WidgetFn<W> for F {}
impl<W: Widget, F: FnOnce(&mut Ui<Ctx>) -> W, Ctx> WidgetLike<Ctx, FnTag> for F { impl<W: Widget, F: FnOnce(&mut Ui) -> W> WidgetLike<FnTag> for F {
type Widget = W; type Widget = W;
fn add(self, ui: &mut Ui<Ctx>) -> WidgetId<W> { fn add(self, ui: &mut Ui) -> WidgetId<W> {
self(ui).add(ui) self(ui).add(ui)
} }
} }
impl<W: Widget, Ctx> WidgetLike<Ctx, WidgetTag> for W { impl<W: Widget> WidgetLike<WidgetTag> for W {
type Widget = W; type Widget = W;
fn add(self, ui: &mut Ui<Ctx>) -> WidgetId<W> { fn add(self, ui: &mut Ui) -> WidgetId<W> {
ui.add_widget(self) ui.add_widget(self)
} }
} }
@@ -76,21 +76,21 @@ impl<const LEN: usize, Ws> WidgetArr<LEN, Ws> {
} }
pub struct ArrTag; pub struct ArrTag;
pub trait WidgetArrLike<const LEN: usize, Ctx, Tags> { pub trait WidgetArrLike<const LEN: usize, Tags> {
type Ws; type Ws;
fn ui(self, ui: &mut Ui<Ctx>) -> WidgetArr<LEN, Self::Ws>; fn ui(self, ui: &mut Ui) -> WidgetArr<LEN, Self::Ws>;
} }
impl<const LEN: usize, Ws, Ctx> WidgetArrLike<LEN, Ctx, ArrTag> for WidgetArr<LEN, Ws> { impl<const LEN: usize, Ws> WidgetArrLike<LEN, ArrTag> for WidgetArr<LEN, Ws> {
type Ws = Ws; type Ws = Ws;
fn ui(self, _: &mut Ui<Ctx>) -> WidgetArr<LEN, Ws> { fn ui(self, _: &mut Ui) -> WidgetArr<LEN, Ws> {
self self
} }
} }
impl<W: WidgetLike<Ctx, WidgetTag>, Ctx> WidgetArrLike<1, Ctx, WidgetTag> for W { impl<W: WidgetLike<WidgetTag>> WidgetArrLike<1, WidgetTag> for W {
type Ws = (W::Widget,); type Ws = (W::Widget,);
fn ui(self, ui: &mut Ui<Ctx>) -> WidgetArr<1, (W::Widget,)> { fn ui(self, ui: &mut Ui) -> WidgetArr<1, (W::Widget,)> {
WidgetArr::new([self.add(ui).any()]) WidgetArr::new([self.add(ui).any()])
} }
} }
@@ -101,9 +101,9 @@ macro_rules! impl_widget_arr {
impl_widget_arr!($n;$($W)*;$(${concat($W,Tag)})*); impl_widget_arr!($n;$($W)*;$(${concat($W,Tag)})*);
}; };
($n:expr;$($W:ident)*;$($Tag:ident)*) => { ($n:expr;$($W:ident)*;$($Tag:ident)*) => {
impl<Ctx, $($W: WidgetLike<Ctx, $Tag>,$Tag,)*> WidgetArrLike<$n, Ctx, ($($Tag,)*)> for ($($W,)*) { impl<$($W: WidgetLike<$Tag>,$Tag,)*> WidgetArrLike<$n, ($($Tag,)*)> for ($($W,)*) {
type Ws = ($($W::Widget,)*); type Ws = ($($W::Widget,)*);
fn ui(self, ui: &mut Ui<Ctx>) -> WidgetArr<$n, ($($W::Widget,)*)> { fn ui(self, ui: &mut Ui) -> WidgetArr<$n, ($($W::Widget,)*)> {
#[allow(non_snake_case)] #[allow(non_snake_case)]
let ($($W,)*) = self; let ($($W,)*) = self;
WidgetArr::new( WidgetArr::new(

View File

@@ -57,7 +57,7 @@ impl UiRenderer {
} }
} }
pub fn update<Ctx>(&mut self, device: &Device, queue: &Queue, ui: &mut Ui<Ctx>) { pub fn update(&mut self, device: &Device, queue: &Queue, ui: &mut Ui) {
self.active.clear(); self.active.clear();
for (i, ulayer) in ui.layers.iter_mut() { for (i, ulayer) in ui.layers.iter_mut() {
self.active.push(i); self.active.push(i);

View File

@@ -9,7 +9,7 @@ pub struct Input {
} }
impl Input { impl Input {
pub fn event(&mut self, event: &WindowEvent) { pub fn event(&mut self, event: &WindowEvent) -> bool {
match event { match event {
WindowEvent::CursorMoved { position, .. } => { WindowEvent::CursorMoved { position, .. } => {
self.cursor.pos = Vec2::new(position.x as f32, position.y as f32); self.cursor.pos = Vec2::new(position.x as f32, position.y as f32);
@@ -28,8 +28,9 @@ impl Input {
WindowEvent::CursorLeft { .. } => { WindowEvent::CursorLeft { .. } => {
self.cursor.exists = false; self.cursor.exists = false;
} }
_ => (), _ => return false,
} }
true
} }
pub fn end_frame(&mut self) { pub fn end_frame(&mut self) {

View File

@@ -19,7 +19,7 @@ pub fn main() {
pub struct Client { pub struct Client {
renderer: Renderer, renderer: Renderer,
input: Input, input: Input,
ui: Ui<Client>, ui: Ui,
info: WidgetId<Text>, info: WidgetId<Text>,
focus: Option<WidgetId<TextEdit>>, focus: Option<WidgetId<TextEdit>>,
} }
@@ -155,7 +155,8 @@ impl Client {
let switch_button = |color, to, label| { let switch_button = |color, to, label| {
let rect = rect(color) let rect = rect(color)
.id_on(Sense::click(), move |id, ctx: &mut Client, _| { .ctx::<Client>()
.id_on(Sense::click(), move |id, ctx, _| {
ctx.ui[main].inner.set_static(to); ctx.ui[main].inner.set_static(to);
ctx.ui[id].color = color.darker(0.3); ctx.ui[id].color = color.darker(0.3);
}) })
@@ -201,16 +202,18 @@ impl Client {
} }
pub fn event(&mut self, event: WindowEvent, event_loop: &ActiveEventLoop) { pub fn event(&mut self, event: WindowEvent, event_loop: &ActiveEventLoop) {
self.input.event(&event); let input_changed = self.input.event(&event);
let cursor_state = self.cursor_state().clone(); let cursor_state = self.cursor_state().clone();
let window_size = self.window_size();
if let Some(focus) = &self.focus if let Some(focus) = &self.focus
&& cursor_state.buttons.left.is_start() && cursor_state.buttons.left.is_start()
{ {
self.ui.text(focus).deselect(); self.ui.text(focus).deselect();
self.focus = None; self.focus = None;
} }
run_sensors(self, &cursor_state, window_size); if input_changed {
let window_size = self.window_size();
run_sensors(self, &cursor_state, window_size);
}
match event { match event {
WindowEvent::CloseRequested => event_loop.exit(), WindowEvent::CloseRequested => event_loop.exit(),
WindowEvent::RedrawRequested => { WindowEvent::RedrawRequested => {
@@ -249,7 +252,7 @@ impl Client {
} }
impl UiCtx for Client { impl UiCtx for Client {
fn ui(&mut self) -> &mut Ui<Self> { fn ui(&mut self) -> &mut Ui {
&mut self.ui &mut self.ui
} }
} }

View File

@@ -21,7 +21,7 @@ pub struct Renderer {
} }
impl Renderer { impl Renderer {
pub fn update<Ctx>(&mut self, updates: &mut Ui<Ctx>) { pub fn update(&mut self, updates: &mut Ui) {
self.ui.update(&self.device, &self.queue, updates); self.ui.update(&self.device, &self.queue, updates);
} }