Skip to content

Commit

Permalink
change child type widget -> widgetchildren: widget
Browse files Browse the repository at this point in the history
  • Loading branch information
jazzfool committed Nov 9, 2019
1 parent 8d895ef commit 4844e7d
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 9 deletions.
14 changes: 9 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ impl Button {
}

impl Widget for Button {
type Aux = ();

pub fn bounds(&self) -> Rect { /* --snip-- */ }

pub fn update(&mut self, _aux: &mut ()) {
Expand Down Expand Up @@ -70,15 +72,15 @@ struct ExampleWidget {
}
```

Which expands to...
Which expands to exactly...

```rust
impl reclutch::WidgetChildren for ExampleWidget {
fn children(&self) -> Vec<&dyn Widget> {
impl reclutch::WidgetChildren<<Self as reclutch::Widget>::Aux> for ExampleWidget {
fn children(&self) -> Vec<&dyn reclutch::WidgetChildren<Self::Aux>> {
vec![&self.child]
}

fn children_mut(&mut self) -> Vec<&mut dyn Widget> {
fn children_mut(&mut self) -> Vec<&mut dyn reclutch::WidgetChildren<Self::Aux>> {
vec![&mut self.child]
}
}
Expand Down Expand Up @@ -134,11 +136,13 @@ impl Widget for VisualWidget {

The `update` method on widgets is an opportunity for widgets to update layout, animations, etc. and more importantly handle events that have been emitted since the last `update`.

Widgets have a generic type; `Aux`, which is by default simply `()` which allows for a global object to be passed around during updating. This is useful for things like updating a layout.
Widgets have an associated type; `Aux` which allows for a global object to be passed around during updating. This is useful for things like updating a layout.

Here's a simple example;

```rust
type Aux = Globals;

fn update(&mut self, aux: &mut Globals) {
if aux.layout.node_is_dirty(self.layout_node) {
self.bounds = aux.layout.get_node(self.layout_node);
Expand Down
4 changes: 2 additions & 2 deletions derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,10 @@ fn impl_widget_macro(ast: &syn::DeriveInput) -> TokenStream {

quote! {
impl #impl_generics reclutch::WidgetChildren<<Self as reclutch::Widget>::Aux> for #name #ty_generics #where_clause {
fn children(&self) -> Vec<&dyn Widget<Aux=Self::Aux>> {
fn children(&self) -> Vec<&dyn reclutch::WidgetChildren<Self::Aux>> {
vec![ #(#children),* ]
}
fn children_mut(&mut self) -> Vec<&mut dyn Widget<Aux=Self::Aux>> {
fn children_mut(&mut self) -> Vec<&mut dyn reclutch::WidgetChildren<Self::Aux>> {
vec![ #(#mut_children),* ]
}
}
Expand Down
4 changes: 2 additions & 2 deletions reclutch/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ use crate::display::{GraphicsDisplay, Rect};
///
/// Ideally, this wouldn't be implemented directly, but rather with `derive(WidgetChildren)`.
pub trait WidgetChildren<Aux>: Widget<Aux = Aux> {
fn children(&self) -> Vec<&dyn Widget<Aux = Aux>> {
fn children(&self) -> Vec<&dyn WidgetChildren<Aux>> {
Vec::new()
}

fn children_mut(&mut self) -> Vec<&mut dyn Widget<Aux = Aux>> {
fn children_mut(&mut self) -> Vec<&mut dyn WidgetChildren<Aux>> {
Vec::new()
}
}
Expand Down

0 comments on commit 4844e7d

Please sign in to comment.