From a29cf32961df34f09b5da818f01b63d6a1ccc1a5 Mon Sep 17 00:00:00 2001 From: lazypassion <25536767+lazypassion@users.noreply.github.com> Date: Thu, 24 Dec 2020 14:06:09 -0500 Subject: [PATCH 1/8] adding added controller widget --- druid/src/widget/added.rs | 61 ++++++++++++++++++++++++++++++++++ druid/src/widget/mod.rs | 2 ++ druid/src/widget/widget_ext.rs | 19 +++++++++-- 3 files changed, 80 insertions(+), 2 deletions(-) create mode 100644 druid/src/widget/added.rs diff --git a/druid/src/widget/added.rs b/druid/src/widget/added.rs new file mode 100644 index 0000000000..19621a42ee --- /dev/null +++ b/druid/src/widget/added.rs @@ -0,0 +1,61 @@ +// Copyright 2020 The Druid Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +//! A [`Controller`] widget that responds to [`LifeCycle::WidgetAdded`] event. +//! +//! [`Controller`]: struct.Controller.html +//! [`LifeCycle::WidgetAdded`]: enum.LifeCycle.html#variant.WidgetAdded + +use crate::widget::Controller; +use crate::{Data, Env, LifeCycleCtx, Widget}; +/// This [`Controller`] widget responds to [`LifeCycle::WidgetAdded`] event +/// with the provided closure. Pass this and a child widget to [`ControllerHost`] +/// to respond to the event when the child widget is added to the widget tree. +/// This is also available, for convenience, as an `on_added` method +/// via [`WidgetExt`]. +/// +/// [`Controller`]: struct.Controller.html +/// [`ControllerHost`]: struct.ControllerHost.html +/// [`WidgetExt`]: ../trait.WidgetExt.html +/// [`LifeCycle::WidgetAdded`]: enum.LifeCycle.html#variant.WidgetAdded +pub struct Added { + /// A closure that will be invoked when the child widget is added + /// to the widget tree + action: Box, +} + +impl Added { + /// Create a new [`Controller`] widget to respond to widget added to tree event. + pub fn new(action: impl Fn(&mut LifeCycleCtx, &T, &Env) + 'static) -> Self { + Self { + action: Box::new(action), + } + } +} + +impl> Controller for Added { + fn lifecycle( + &mut self, + child: &mut W, + ctx: &mut LifeCycleCtx, + event: &crate::LifeCycle, + data: &T, + env: &Env, + ) { + if let crate::LifeCycle::WidgetAdded = event { + (self.action)(ctx, data, env); + } + child.lifecycle(ctx, event, data, env) + } +} diff --git a/druid/src/widget/mod.rs b/druid/src/widget/mod.rs index fb148a35af..1fa86c5f12 100644 --- a/druid/src/widget/mod.rs +++ b/druid/src/widget/mod.rs @@ -14,6 +14,7 @@ //! Common widgets. +mod added; mod align; mod button; mod checkbox; @@ -55,6 +56,7 @@ mod widget; mod widget_ext; pub use self::image::Image; +pub use added::Added; pub use align::Align; pub use button::Button; pub use checkbox::Checkbox; diff --git a/druid/src/widget/widget_ext.rs b/druid/src/widget/widget_ext.rs index 129af463bd..66f08b4474 100644 --- a/druid/src/widget/widget_ext.rs +++ b/druid/src/widget/widget_ext.rs @@ -16,10 +16,12 @@ use super::invalidation::DebugInvalidation; use super::{ - Align, BackgroundBrush, Click, Container, Controller, ControllerHost, EnvScope, + Added, Align, BackgroundBrush, Click, Container, Controller, ControllerHost, EnvScope, IdentityWrapper, LensWrap, Padding, Parse, SizedBox, WidgetId, }; -use crate::{Color, Data, Env, EventCtx, Insets, KeyOrValue, Lens, UnitPoint, Widget}; +use crate::{ + Color, Data, Env, EventCtx, Insets, KeyOrValue, Lens, LifeCycleCtx, UnitPoint, Widget, +}; /// A trait that provides extra methods for combining `Widget`s. pub trait WidgetExt: Widget + Sized + 'static { @@ -159,6 +161,19 @@ pub trait WidgetExt: Widget + Sized + 'static { ControllerHost::new(self, controller) } + /// Control the [`LifeCycle::WidgetAdded`] event of this widget with an [`Added`] widget. + /// The function provided will be called when the widget is added to the + /// widget tree. + /// + /// [`Added`]: widget/struct.Added.html + /// [`LifeCycle::WidgetAdded`]: enum.LifeCycle.html#variant.WidgetAdded + fn on_added( + self, + f: impl Fn(&mut LifeCycleCtx, &T, &Env) + 'static, + ) -> ControllerHost> { + ControllerHost::new(self, Added::new(f)) + } + /// Control the events of this widget with a [`Click`] widget. The closure /// provided will be called when the widget is clicked with the left mouse /// button. From 7b2d235f1a0649d34b684b16a0a33496d9a05a4a Mon Sep 17 00:00:00 2001 From: lazypassion <25536767+lazypassion@users.noreply.github.com> Date: Thu, 31 Dec 2020 12:00:13 -0500 Subject: [PATCH 2/8] updating docs to use linking by name for intra doc links --- druid/src/widget/added.rs | 12 ++++++------ druid/src/widget/widget_ext.rs | 4 ++-- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/druid/src/widget/added.rs b/druid/src/widget/added.rs index 19621a42ee..693fadb3c4 100644 --- a/druid/src/widget/added.rs +++ b/druid/src/widget/added.rs @@ -14,8 +14,8 @@ //! A [`Controller`] widget that responds to [`LifeCycle::WidgetAdded`] event. //! -//! [`Controller`]: struct.Controller.html -//! [`LifeCycle::WidgetAdded`]: enum.LifeCycle.html#variant.WidgetAdded +//! [`Controller`]: crate::widget::Controller +//! [`LifeCycle::WidgetAdded`]: crate::LifeCycle::WidgetAdded use crate::widget::Controller; use crate::{Data, Env, LifeCycleCtx, Widget}; @@ -25,10 +25,10 @@ use crate::{Data, Env, LifeCycleCtx, Widget}; /// This is also available, for convenience, as an `on_added` method /// via [`WidgetExt`]. /// -/// [`Controller`]: struct.Controller.html -/// [`ControllerHost`]: struct.ControllerHost.html -/// [`WidgetExt`]: ../trait.WidgetExt.html -/// [`LifeCycle::WidgetAdded`]: enum.LifeCycle.html#variant.WidgetAdded +/// [`Controller`]: crate::widget::Controller +/// [`ControllerHost`]: crate::widget::ControllerHost +/// [`WidgetExt`]: crate::widget::WidgetExt +/// [`LifeCycle::WidgetAdded`]: crate::LifeCycle::WidgetAdded pub struct Added { /// A closure that will be invoked when the child widget is added /// to the widget tree diff --git a/druid/src/widget/widget_ext.rs b/druid/src/widget/widget_ext.rs index 66f08b4474..ae1abf0ae6 100644 --- a/druid/src/widget/widget_ext.rs +++ b/druid/src/widget/widget_ext.rs @@ -165,8 +165,8 @@ pub trait WidgetExt: Widget + Sized + 'static { /// The function provided will be called when the widget is added to the /// widget tree. /// - /// [`Added`]: widget/struct.Added.html - /// [`LifeCycle::WidgetAdded`]: enum.LifeCycle.html#variant.WidgetAdded + /// [`Added`]: crate::widget::Added + /// [`LifeCycle::WidgetAdded`]: crate::LifeCycle::WidgetAdded fn on_added( self, f: impl Fn(&mut LifeCycleCtx, &T, &Env) + 'static, From 0aaa07da1febc2c39b4aa8f8b9fb4bb1e0dfa86f Mon Sep 17 00:00:00 2001 From: lazypassion <25536767+lazypassion@users.noreply.github.com> Date: Thu, 24 Dec 2020 14:06:09 -0500 Subject: [PATCH 3/8] adding added controller widget --- druid/src/widget/added.rs | 61 ++++++++++++++++++++++++++++++++++ druid/src/widget/mod.rs | 2 ++ druid/src/widget/widget_ext.rs | 19 +++++++++-- 3 files changed, 80 insertions(+), 2 deletions(-) create mode 100644 druid/src/widget/added.rs diff --git a/druid/src/widget/added.rs b/druid/src/widget/added.rs new file mode 100644 index 0000000000..19621a42ee --- /dev/null +++ b/druid/src/widget/added.rs @@ -0,0 +1,61 @@ +// Copyright 2020 The Druid Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +//! A [`Controller`] widget that responds to [`LifeCycle::WidgetAdded`] event. +//! +//! [`Controller`]: struct.Controller.html +//! [`LifeCycle::WidgetAdded`]: enum.LifeCycle.html#variant.WidgetAdded + +use crate::widget::Controller; +use crate::{Data, Env, LifeCycleCtx, Widget}; +/// This [`Controller`] widget responds to [`LifeCycle::WidgetAdded`] event +/// with the provided closure. Pass this and a child widget to [`ControllerHost`] +/// to respond to the event when the child widget is added to the widget tree. +/// This is also available, for convenience, as an `on_added` method +/// via [`WidgetExt`]. +/// +/// [`Controller`]: struct.Controller.html +/// [`ControllerHost`]: struct.ControllerHost.html +/// [`WidgetExt`]: ../trait.WidgetExt.html +/// [`LifeCycle::WidgetAdded`]: enum.LifeCycle.html#variant.WidgetAdded +pub struct Added { + /// A closure that will be invoked when the child widget is added + /// to the widget tree + action: Box, +} + +impl Added { + /// Create a new [`Controller`] widget to respond to widget added to tree event. + pub fn new(action: impl Fn(&mut LifeCycleCtx, &T, &Env) + 'static) -> Self { + Self { + action: Box::new(action), + } + } +} + +impl> Controller for Added { + fn lifecycle( + &mut self, + child: &mut W, + ctx: &mut LifeCycleCtx, + event: &crate::LifeCycle, + data: &T, + env: &Env, + ) { + if let crate::LifeCycle::WidgetAdded = event { + (self.action)(ctx, data, env); + } + child.lifecycle(ctx, event, data, env) + } +} diff --git a/druid/src/widget/mod.rs b/druid/src/widget/mod.rs index d5a96fb720..c2287fc2ac 100644 --- a/druid/src/widget/mod.rs +++ b/druid/src/widget/mod.rs @@ -14,6 +14,7 @@ //! Common widgets. +mod added; mod align; mod button; mod checkbox; @@ -55,6 +56,7 @@ mod widget; mod widget_ext; pub use self::image::Image; +pub use added::Added; pub use align::Align; pub use button::Button; pub use checkbox::Checkbox; diff --git a/druid/src/widget/widget_ext.rs b/druid/src/widget/widget_ext.rs index ccf05b3a05..547d57f67b 100644 --- a/druid/src/widget/widget_ext.rs +++ b/druid/src/widget/widget_ext.rs @@ -16,10 +16,12 @@ use super::invalidation::DebugInvalidation; use super::{ - Align, BackgroundBrush, Click, Container, Controller, ControllerHost, EnvScope, + Added, Align, BackgroundBrush, Click, Container, Controller, ControllerHost, EnvScope, IdentityWrapper, LensWrap, Padding, Parse, SizedBox, WidgetId, }; -use crate::{Color, Data, Env, EventCtx, Insets, KeyOrValue, Lens, UnitPoint, Widget}; +use crate::{ + Color, Data, Env, EventCtx, Insets, KeyOrValue, Lens, LifeCycleCtx, UnitPoint, Widget, +}; /// A trait that provides extra methods for combining `Widget`s. pub trait WidgetExt: Widget + Sized + 'static { @@ -159,6 +161,19 @@ pub trait WidgetExt: Widget + Sized + 'static { ControllerHost::new(self, controller) } + /// Control the [`LifeCycle::WidgetAdded`] event of this widget with an [`Added`] widget. + /// The function provided will be called when the widget is added to the + /// widget tree. + /// + /// [`Added`]: widget/struct.Added.html + /// [`LifeCycle::WidgetAdded`]: enum.LifeCycle.html#variant.WidgetAdded + fn on_added( + self, + f: impl Fn(&mut LifeCycleCtx, &T, &Env) + 'static, + ) -> ControllerHost> { + ControllerHost::new(self, Added::new(f)) + } + /// Control the events of this widget with a [`Click`] widget. The closure /// provided will be called when the widget is clicked with the left mouse /// button. From 15716fb07e52c1e0ae5376b5b80bc427e98cb043 Mon Sep 17 00:00:00 2001 From: lazypassion <25536767+lazypassion@users.noreply.github.com> Date: Thu, 31 Dec 2020 12:00:13 -0500 Subject: [PATCH 4/8] updating docs to use linking by name for intra doc links --- druid/src/widget/added.rs | 12 ++++++------ druid/src/widget/widget_ext.rs | 4 ++-- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/druid/src/widget/added.rs b/druid/src/widget/added.rs index 19621a42ee..693fadb3c4 100644 --- a/druid/src/widget/added.rs +++ b/druid/src/widget/added.rs @@ -14,8 +14,8 @@ //! A [`Controller`] widget that responds to [`LifeCycle::WidgetAdded`] event. //! -//! [`Controller`]: struct.Controller.html -//! [`LifeCycle::WidgetAdded`]: enum.LifeCycle.html#variant.WidgetAdded +//! [`Controller`]: crate::widget::Controller +//! [`LifeCycle::WidgetAdded`]: crate::LifeCycle::WidgetAdded use crate::widget::Controller; use crate::{Data, Env, LifeCycleCtx, Widget}; @@ -25,10 +25,10 @@ use crate::{Data, Env, LifeCycleCtx, Widget}; /// This is also available, for convenience, as an `on_added` method /// via [`WidgetExt`]. /// -/// [`Controller`]: struct.Controller.html -/// [`ControllerHost`]: struct.ControllerHost.html -/// [`WidgetExt`]: ../trait.WidgetExt.html -/// [`LifeCycle::WidgetAdded`]: enum.LifeCycle.html#variant.WidgetAdded +/// [`Controller`]: crate::widget::Controller +/// [`ControllerHost`]: crate::widget::ControllerHost +/// [`WidgetExt`]: crate::widget::WidgetExt +/// [`LifeCycle::WidgetAdded`]: crate::LifeCycle::WidgetAdded pub struct Added { /// A closure that will be invoked when the child widget is added /// to the widget tree diff --git a/druid/src/widget/widget_ext.rs b/druid/src/widget/widget_ext.rs index 547d57f67b..b5a584702c 100644 --- a/druid/src/widget/widget_ext.rs +++ b/druid/src/widget/widget_ext.rs @@ -165,8 +165,8 @@ pub trait WidgetExt: Widget + Sized + 'static { /// The function provided will be called when the widget is added to the /// widget tree. /// - /// [`Added`]: widget/struct.Added.html - /// [`LifeCycle::WidgetAdded`]: enum.LifeCycle.html#variant.WidgetAdded + /// [`Added`]: crate::widget::Added + /// [`LifeCycle::WidgetAdded`]: crate::LifeCycle::WidgetAdded fn on_added( self, f: impl Fn(&mut LifeCycleCtx, &T, &Env) + 'static, From aba089a26c83b2ad4b2e4e011212d72765fa28e0 Mon Sep 17 00:00:00 2001 From: arthmis Date: Wed, 6 Jan 2021 11:37:50 -0500 Subject: [PATCH 5/8] making docs more concise Co-authored-by: Colin Rofls --- druid/src/widget/widget_ext.rs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/druid/src/widget/widget_ext.rs b/druid/src/widget/widget_ext.rs index b5a584702c..85c7e63271 100644 --- a/druid/src/widget/widget_ext.rs +++ b/druid/src/widget/widget_ext.rs @@ -161,11 +161,13 @@ pub trait WidgetExt: Widget + Sized + 'static { ControllerHost::new(self, controller) } - /// Control the [`LifeCycle::WidgetAdded`] event of this widget with an [`Added`] widget. - /// The function provided will be called when the widget is added to the - /// widget tree. + /// Provide a closure that will be called when this widget is added to the widget tree. + /// + /// You can use this to perform any initial setup. + /// + /// This is equivalent to handling the [`LifeCycle::WidgetAdded`] event in a + /// custom [`Controller`]. /// - /// [`Added`]: crate::widget::Added /// [`LifeCycle::WidgetAdded`]: crate::LifeCycle::WidgetAdded fn on_added( self, From d5045154f485ad5731da3b05688742fab83e035e Mon Sep 17 00:00:00 2001 From: arthmis Date: Wed, 6 Jan 2021 11:38:31 -0500 Subject: [PATCH 6/8] updating some formatting on comment Co-authored-by: Colin Rofls --- druid/src/widget/added.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/druid/src/widget/added.rs b/druid/src/widget/added.rs index 693fadb3c4..370c40e2f2 100644 --- a/druid/src/widget/added.rs +++ b/druid/src/widget/added.rs @@ -19,6 +19,7 @@ use crate::widget::Controller; use crate::{Data, Env, LifeCycleCtx, Widget}; + /// This [`Controller`] widget responds to [`LifeCycle::WidgetAdded`] event /// with the provided closure. Pass this and a child widget to [`ControllerHost`] /// to respond to the event when the child widget is added to the widget tree. From 13b17b3673439a5acc242e96b0a19846ad17e955 Mon Sep 17 00:00:00 2001 From: lazypassion <25536767+lazypassion@users.noreply.github.com> Date: Wed, 6 Jan 2021 12:52:21 -0500 Subject: [PATCH 7/8] Added controller will now take a mut reference to child --- druid/src/widget/added.rs | 12 ++++++------ druid/src/widget/widget_ext.rs | 6 +++--- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/druid/src/widget/added.rs b/druid/src/widget/added.rs index 370c40e2f2..32f81aba78 100644 --- a/druid/src/widget/added.rs +++ b/druid/src/widget/added.rs @@ -30,22 +30,22 @@ use crate::{Data, Env, LifeCycleCtx, Widget}; /// [`ControllerHost`]: crate::widget::ControllerHost /// [`WidgetExt`]: crate::widget::WidgetExt /// [`LifeCycle::WidgetAdded`]: crate::LifeCycle::WidgetAdded -pub struct Added { +pub struct Added { /// A closure that will be invoked when the child widget is added /// to the widget tree - action: Box, + action: Box, } -impl Added { +impl> Added { /// Create a new [`Controller`] widget to respond to widget added to tree event. - pub fn new(action: impl Fn(&mut LifeCycleCtx, &T, &Env) + 'static) -> Self { + pub fn new(action: impl Fn(&mut W, &mut LifeCycleCtx, &T, &Env) + 'static) -> Self { Self { action: Box::new(action), } } } -impl> Controller for Added { +impl> Controller for Added { fn lifecycle( &mut self, child: &mut W, @@ -55,7 +55,7 @@ impl> Controller for Added { env: &Env, ) { if let crate::LifeCycle::WidgetAdded = event { - (self.action)(ctx, data, env); + (self.action)(child, ctx, data, env); } child.lifecycle(ctx, event, data, env) } diff --git a/druid/src/widget/widget_ext.rs b/druid/src/widget/widget_ext.rs index 85c7e63271..e30020b99d 100644 --- a/druid/src/widget/widget_ext.rs +++ b/druid/src/widget/widget_ext.rs @@ -169,10 +169,10 @@ pub trait WidgetExt: Widget + Sized + 'static { /// custom [`Controller`]. /// /// [`LifeCycle::WidgetAdded`]: crate::LifeCycle::WidgetAdded - fn on_added( + fn on_added>( self, - f: impl Fn(&mut LifeCycleCtx, &T, &Env) + 'static, - ) -> ControllerHost> { + f: impl Fn(&mut W, &mut LifeCycleCtx, &T, &Env) + 'static, + ) -> ControllerHost> { ControllerHost::new(self, Added::new(f)) } From 075b2b58ad171faa7274905d7e298f0b0561fd17 Mon Sep 17 00:00:00 2001 From: lazypassion <25536767+lazypassion@users.noreply.github.com> Date: Wed, 6 Jan 2021 13:47:17 -0500 Subject: [PATCH 8/8] updating on added function signature --- druid/src/widget/widget_ext.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/druid/src/widget/widget_ext.rs b/druid/src/widget/widget_ext.rs index e30020b99d..822a85aa6d 100644 --- a/druid/src/widget/widget_ext.rs +++ b/druid/src/widget/widget_ext.rs @@ -169,10 +169,10 @@ pub trait WidgetExt: Widget + Sized + 'static { /// custom [`Controller`]. /// /// [`LifeCycle::WidgetAdded`]: crate::LifeCycle::WidgetAdded - fn on_added>( + fn on_added( self, - f: impl Fn(&mut W, &mut LifeCycleCtx, &T, &Env) + 'static, - ) -> ControllerHost> { + f: impl Fn(&mut Self, &mut LifeCycleCtx, &T, &Env) + 'static, + ) -> ControllerHost> { ControllerHost::new(self, Added::new(f)) }