Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add widget wrapper and implementation macros. #1511

Merged
merged 1 commit into from
Jan 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 12 additions & 10 deletions druid/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,16 +207,6 @@ impl<T, W: Widget<T>> WidgetPod<T, W> {
self.state.is_hot
}

/// Return a reference to the inner widget.
pub fn widget(&self) -> &W {
&self.inner
}

/// Return a mutable reference to the inner widget.
pub fn widget_mut(&mut self) -> &mut W {
&mut self.inner
}

/// Get the identity of the widget.
pub fn id(&self) -> WidgetId {
self.state.id
Expand Down Expand Up @@ -1020,6 +1010,18 @@ impl<T, W: Widget<T> + 'static> WidgetPod<T, W> {
}
}

impl<T, W> WidgetPod<T, W> {
/// Return a reference to the inner widget.
pub fn widget(&self) -> &W {
&self.inner
}

/// Return a mutable reference to the inner widget.
pub fn widget_mut(&mut self) -> &mut W {
&mut self.inner
}
}

impl WidgetState {
pub(crate) fn new(id: WidgetId, size: Option<Size>) -> WidgetState {
WidgetState {
Expand Down
5 changes: 5 additions & 0 deletions druid/src/widget/controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
//! A widget-controlling widget.

use crate::widget::prelude::*;
use crate::widget::WidgetWrapper;

/// A trait for types that modify behaviour of a child widget.
///
Expand Down Expand Up @@ -133,3 +134,7 @@ impl<T, W: Widget<T>, C: Controller<T, W>> Widget<T> for ControllerHost<W, C> {
self.widget.id()
}
}

impl<W, C> WidgetWrapper for ControllerHost<W, C> {
widget_wrapper_body!(W, widget);
}
5 changes: 5 additions & 0 deletions druid/src/widget/env_scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
//! A widget that accepts a closure to update the environment for its child.

use crate::widget::prelude::*;
use crate::widget::WidgetWrapper;
use crate::{Data, Point, WidgetPod};

/// A widget that accepts a closure to update the environment for its child.
Expand Down Expand Up @@ -94,3 +95,7 @@ impl<T: Data, W: Widget<T>> Widget<T> for EnvScope<T, W> {
self.child.paint(ctx, data, &new_env);
}
}

impl<T, W: Widget<T>> WidgetWrapper for EnvScope<T, W> {
widget_wrapper_pod_body!(W, child);
}
6 changes: 6 additions & 0 deletions druid/src/widget/identity_wrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@

//! A widget that provides an explicit identity to a child.

use crate::kurbo::Size;
use crate::widget::prelude::*;
use crate::widget::WidgetWrapper;
use crate::Data;

/// A wrapper that adds an identity to an otherwise anonymous widget.
Expand Down Expand Up @@ -55,3 +57,7 @@ impl<T: Data, W: Widget<T>> Widget<T> for IdentityWrapper<W> {
Some(self.id)
}
}

impl<W> WidgetWrapper for IdentityWrapper<W> {
widget_wrapper_body!(W, inner);
}
5 changes: 5 additions & 0 deletions druid/src/widget/lens_wrap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use std::marker::PhantomData;

use crate::widget::prelude::*;
use crate::widget::WidgetWrapper;
use crate::{Data, Lens};

/// A wrapper for its widget subtree to have access to a part
Expand Down Expand Up @@ -114,3 +115,7 @@ where
self.inner.id()
}
}

impl<T, U, L, W> WidgetWrapper for LensWrap<T, U, L, W> {
widget_wrapper_body!(W, inner);
}
5 changes: 5 additions & 0 deletions druid/src/widget/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@

//! Common widgets.

// First as it defines macros
#[macro_use]
mod widget_wrapper;

mod align;
mod button;
mod checkbox;
Expand Down Expand Up @@ -92,6 +96,7 @@ pub use view_switcher::ViewSwitcher;
pub use widget::{Widget, WidgetId};
#[doc(hidden)]
pub use widget_ext::WidgetExt;
pub use widget_wrapper::WidgetWrapper;

/// The types required to implement a `Widget`.
///
Expand Down
5 changes: 5 additions & 0 deletions druid/src/widget/scope.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::marker::PhantomData;

use crate::widget::prelude::*;
use crate::widget::WidgetWrapper;
use crate::{Data, Lens, Point, WidgetPod};

/// A policy that controls how a [`Scope`] will interact with its surrounding
Expand Down Expand Up @@ -301,3 +302,7 @@ impl<SP: ScopePolicy, W: Widget<SP::State>> Widget<SP::In> for Scope<SP, W> {
self.with_state(data, |state, inner| inner.paint_raw(ctx, state, env));
}
}

impl<SP: ScopePolicy, W: Widget<SP::State>> WidgetWrapper for Scope<SP, W> {
widget_wrapper_pod_body!(W, inner);
}
47 changes: 47 additions & 0 deletions druid/src/widget/widget_wrapper.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/// A trait for widgets that wrap a single child to expose that child for access and mutation
pub trait WidgetWrapper {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we actually make use of the trait-ness of this trait anywhere?

My inclination would be to just have independent methods on each widget, instead of a trait (which needs to be in scope). That way we would have just Container::child and Container::child_mut methods.

An annoying consideration here is also that we are inconsistent about monomorphizing widget params; that is, some single-child containers are parameterized over their child type, and some just box unilaterally.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we don't have the trait, I need to implement them all over again in bindings in order to treat this generically, and so would any other scheme trying to do the same thing. I guess coherence sometimes forces things like that anyway, but it didn't yet.
The lack of traits in Contexts does cause real problems with duplicated code.

Also, these methods are fairly useless for anything that isn't some kind of general scheme for access.
To use them in a dynamic way (which is the only real use case, otherwise you would set it up in construction), you need to implement so much boiler plate (controllers, commands, navigation, threading through widget ids), that an import seems like the last of our worries.

I have to admit I don't agree with the 'hanging methods off of traits' objection really. Importing is just normal, and IDEs will do it for you.

/// The type of the wrapped widget.
/// Maybe we would like to constrain this to Widget<impl Data> (if existential bounds were supported).
/// Any other scheme leads to T being unconstrained in unification at some point
type Wrapped;
/// Get immutable access to the wrapped child
fn wrapped(&self) -> &Self::Wrapped;
/// Get mutable access to the wrapped child
fn wrapped_mut(&mut self) -> &mut Self::Wrapped;
}

/// A macro to help implementation of WidgetWrapper for a direct wrapper.
/// Use it in the body of the impl.
///
#[macro_export]
macro_rules! widget_wrapper_body {
($wrapped:ty, $field:ident) => {
type Wrapped = $wrapped;

fn wrapped(&self) -> &Self::Wrapped {
&self.$field
}

fn wrapped_mut(&mut self) -> &mut Self::Wrapped {
&mut self.$field
}
};
}

/// A macro to help implementation of WidgetWrapper for a wrapper of a typed pod.
/// Use it in the body of the impl.
///
#[macro_export]
macro_rules! widget_wrapper_pod_body {
($wrapped:ty, $field:ident) => {
type Wrapped = $wrapped;

fn wrapped(&self) -> &Self::Wrapped {
self.$field.widget()
}

fn wrapped_mut(&mut self) -> &mut Self::Wrapped {
self.$field.widget_mut()
}
};
}