From 8cc5aed3aaed8b45d396040af9093f7561458160 Mon Sep 17 00:00:00 2001 From: xarvic Date: Wed, 7 Apr 2021 18:48:29 +0200 Subject: [PATCH 1/9] implemented DisabledIf --- druid/examples/disabled.rs | 107 +++++++++++++++++++++++++++++++++ druid/examples/web/src/lib.rs | 1 + druid/src/widget/disable_if.rs | 47 +++++++++++++++ druid/src/widget/mod.rs | 2 + druid/src/widget/widget_ext.rs | 14 ++++- 5 files changed, 170 insertions(+), 1 deletion(-) create mode 100644 druid/examples/disabled.rs create mode 100644 druid/src/widget/disable_if.rs diff --git a/druid/examples/disabled.rs b/druid/examples/disabled.rs new file mode 100644 index 0000000000..a8a04d0513 --- /dev/null +++ b/druid/examples/disabled.rs @@ -0,0 +1,107 @@ +use druid::{Widget, WidgetExt, WindowDesc, LocalizedString, AppLauncher, Data, Lens}; +use druid::widget::{Flex, TextBox, Label, Slider, Checkbox, Switch, Stepper, CrossAxisAlignment}; +use piet_common::UnitPoint; + +#[derive(Clone, Data, Lens)] +struct AppData { + option: bool, + text: String, + value: f64, + + disabled: bool, +} + +fn named_child(name: &str, widget: impl Widget + 'static) -> impl Widget { + Flex::row() + .with_child(Label::new(name)) + .with_default_spacer() + .with_child(widget) +} + +fn main_widget() -> impl Widget { + Flex::column() + .with_child(named_child( + "text:", + TextBox::new() + .lens(AppData::text) + )) + .with_default_spacer() + .with_child(named_child( + "text (disabled):", + TextBox::new() + .lens(AppData::text) + .disabled_if(|data, _|data.disabled) + )) + .with_default_spacer() + .with_child(named_child( + "text:", + TextBox::new() + .lens(AppData::text) + )) + .with_default_spacer() + .with_child(named_child( + "text (disabled):", + TextBox::new() + .lens(AppData::text) + .disabled_if(|data, _|data.disabled) + )) + .with_default_spacer() + .with_default_spacer() + .with_child(named_child( + "value (disabled):", + Slider::new() + .with_range(0.0, 10.0) + .lens(AppData::value) + .disabled_if(|data, _|data.disabled) + )) + .with_default_spacer() + .with_child(named_child( + "value (disabled):", + Stepper::new() + .with_range(0.0, 10.0) + .with_step(0.5) + .lens(AppData::value) + .disabled_if(|data, _|data.disabled) + )) + .with_default_spacer() + .with_child(named_child( + "option (disabled):", + Checkbox::new("option") + .lens(AppData::option) + .disabled_if(|data, _|data.disabled) + )) + .with_default_spacer() + .with_child(named_child( + "option (disabled):", + Switch::new() + .lens(AppData::option) + .disabled_if(|data, _|data.disabled) + )) + .with_default_spacer() + .with_default_spacer() + .with_default_spacer() + .with_child( + Checkbox::new("disabled") + .lens(AppData::disabled) + ) + .with_default_spacer() + .cross_axis_alignment(CrossAxisAlignment::End) + .align_horizontal(UnitPoint::CENTER) + +} + +pub fn main() { + let window = WindowDesc::new(main_widget()).title( + LocalizedString::new("disabled-demo-window-title") + .with_placeholder("Disabled demo"), + ); + AppLauncher::with_window(window) + .log_to_console() + .launch(AppData { + option: true, + text: "a very important text!".to_string(), + value: 2.0, + disabled: false + }) + .expect("launch failed"); +} \ No newline at end of file diff --git a/druid/examples/web/src/lib.rs b/druid/examples/web/src/lib.rs index 87c50a4d87..dadc317b1d 100644 --- a/druid/examples/web/src/lib.rs +++ b/druid/examples/web/src/lib.rs @@ -58,6 +58,7 @@ impl_example!(anim); impl_example!(calc); impl_example!(cursor); impl_example!(custom_widget); +impl_example!(disabled); impl_example!(either); impl_example!(event_viewer); impl_example!(flex); diff --git a/druid/src/widget/disable_if.rs b/druid/src/widget/disable_if.rs new file mode 100644 index 0000000000..05ad6a040c --- /dev/null +++ b/druid/src/widget/disable_if.rs @@ -0,0 +1,47 @@ +use crate::{WidgetPod, Env, Widget, Data, LifeCycle, EventCtx, PaintCtx, LifeCycleCtx, BoxConstraints, Size, LayoutCtx, Event, UpdateCtx, Point}; + +/// A widget wrapper which disables the inner widget if the provided closure return true. +pub struct DisabledIf { + inner: WidgetPod, + disabled_if: Box bool>, +} + +impl> DisabledIf { + /// Creates a new `DisabledIf` widget with the inner widget and the closure to decide if the + /// widget should be disabled. + pub fn new(widget: W, disabled_if: impl Fn(&T, &Env) -> bool + 'static) -> Self { + DisabledIf { + inner: WidgetPod::new(widget), + disabled_if: Box::new(disabled_if), + } + } +} + +impl> Widget for DisabledIf { + fn event(&mut self, ctx: &mut EventCtx, event: &Event, data: &mut T, env: &Env) { + self.inner.event(ctx, event, data, env); + } + + fn lifecycle(&mut self, ctx: &mut LifeCycleCtx, event: &LifeCycle, data: &T, env: &Env) { + if let LifeCycle::WidgetAdded = event { + ctx.set_disabled((self.disabled_if)(data, env)); + } + self.inner.lifecycle(ctx, event, data, env); + } + + fn update(&mut self, ctx: &mut UpdateCtx, _: &T, data: &T, env: &Env) { + ctx.set_disabled((self.disabled_if)(data, env)); + self.inner.update(ctx, data, env); + } + + fn layout(&mut self, ctx: &mut LayoutCtx, bc: &BoxConstraints, data: &T, env: &Env) -> Size { + let size = self.inner.layout(ctx, bc, data, env); + self.inner.set_origin(ctx, data, env, Point::ZERO); + ctx.set_baseline_offset(self.inner.baseline_offset()); + size + } + + fn paint(&mut self, ctx: &mut PaintCtx, data: &T, env: &Env) { + self.inner.paint(ctx, data, env); + } +} diff --git a/druid/src/widget/mod.rs b/druid/src/widget/mod.rs index 13c8577c8f..0a35415861 100644 --- a/druid/src/widget/mod.rs +++ b/druid/src/widget/mod.rs @@ -27,6 +27,7 @@ mod clip_box; mod common; mod container; mod controller; +mod disable_if; mod either; mod env_scope; mod flex; @@ -71,6 +72,7 @@ pub use clip_box::{ClipBox, Viewport}; pub use common::FillStrat; pub use container::Container; pub use controller::{Controller, ControllerHost}; +pub use disable_if::DisabledIf; pub use either::Either; pub use env_scope::EnvScope; pub use flex::{Axis, CrossAxisAlignment, Flex, FlexParams, MainAxisAlignment}; diff --git a/druid/src/widget/widget_ext.rs b/druid/src/widget/widget_ext.rs index 6afffad00c..7e57105447 100644 --- a/druid/src/widget/widget_ext.rs +++ b/druid/src/widget/widget_ext.rs @@ -19,7 +19,7 @@ use super::{ Added, Align, BackgroundBrush, Click, Container, Controller, ControllerHost, EnvScope, IdentityWrapper, LensWrap, Padding, Parse, SizedBox, WidgetId, }; -use crate::widget::Scroll; +use crate::widget::{Scroll, DisabledIf}; use crate::{ Color, Data, Env, EventCtx, Insets, KeyOrValue, Lens, LifeCycleCtx, UnitPoint, Widget, }; @@ -273,6 +273,18 @@ pub trait WidgetExt: Widget + Sized + 'static { fn scroll(self) -> Scroll { Scroll::new(self) } + + /// Wrap this widget in a [`DisabledIf`] widget. + /// The provided closure will determine if the widget is disabled. + /// + /// See [`is_disabled`] or [`set_disabled`] for more info about the disabled state. + /// + /// [`is_disabled`]: struct.EventCtx.html#method.is_disabled + /// [`set_disabled`]: struct.EventCtx.html#method.set_disabled + /// [`DisabledIf`]: widget/struct.DisabledIf.html + fn disabled_if(self, disabled_if: impl Fn(&T, &Env) -> bool + 'static) -> DisabledIf { + DisabledIf::new(self, disabled_if) + } } impl + 'static> WidgetExt for W {} From 1b1b661992b8d147cd00da0dc697cba4e61d03f0 Mon Sep 17 00:00:00 2001 From: xarvic Date: Wed, 7 Apr 2021 18:49:36 +0200 Subject: [PATCH 2/9] reformat --- druid/examples/disabled.rs | 41 ++++++++++++---------------------- druid/src/widget/disable_if.rs | 5 ++++- druid/src/widget/widget_ext.rs | 2 +- 3 files changed, 19 insertions(+), 29 deletions(-) diff --git a/druid/examples/disabled.rs b/druid/examples/disabled.rs index a8a04d0513..bfb0fccb43 100644 --- a/druid/examples/disabled.rs +++ b/druid/examples/disabled.rs @@ -1,5 +1,5 @@ -use druid::{Widget, WidgetExt, WindowDesc, LocalizedString, AppLauncher, Data, Lens}; -use druid::widget::{Flex, TextBox, Label, Slider, Checkbox, Switch, Stepper, CrossAxisAlignment}; +use druid::widget::{Checkbox, CrossAxisAlignment, Flex, Label, Slider, Stepper, Switch, TextBox}; +use druid::{AppLauncher, Data, Lens, LocalizedString, Widget, WidgetExt, WindowDesc}; use piet_common::UnitPoint; #[derive(Clone, Data, Lens)] @@ -20,30 +20,22 @@ fn named_child(name: &str, widget: impl Widget + 'static) -> impl Widge fn main_widget() -> impl Widget { Flex::column() - .with_child(named_child( - "text:", - TextBox::new() - .lens(AppData::text) - )) + .with_child(named_child("text:", TextBox::new().lens(AppData::text))) .with_default_spacer() .with_child(named_child( "text (disabled):", TextBox::new() .lens(AppData::text) - .disabled_if(|data, _|data.disabled) + .disabled_if(|data, _| data.disabled), )) .with_default_spacer() - .with_child(named_child( - "text:", - TextBox::new() - .lens(AppData::text) - )) + .with_child(named_child("text:", TextBox::new().lens(AppData::text))) .with_default_spacer() .with_child(named_child( "text (disabled):", TextBox::new() .lens(AppData::text) - .disabled_if(|data, _|data.disabled) + .disabled_if(|data, _| data.disabled), )) .with_default_spacer() .with_default_spacer() @@ -52,7 +44,7 @@ fn main_widget() -> impl Widget { Slider::new() .with_range(0.0, 10.0) .lens(AppData::value) - .disabled_if(|data, _|data.disabled) + .disabled_if(|data, _| data.disabled), )) .with_default_spacer() .with_child(named_child( @@ -61,39 +53,34 @@ fn main_widget() -> impl Widget { .with_range(0.0, 10.0) .with_step(0.5) .lens(AppData::value) - .disabled_if(|data, _|data.disabled) + .disabled_if(|data, _| data.disabled), )) .with_default_spacer() .with_child(named_child( "option (disabled):", Checkbox::new("option") .lens(AppData::option) - .disabled_if(|data, _|data.disabled) + .disabled_if(|data, _| data.disabled), )) .with_default_spacer() .with_child(named_child( "option (disabled):", Switch::new() .lens(AppData::option) - .disabled_if(|data, _|data.disabled) + .disabled_if(|data, _| data.disabled), )) .with_default_spacer() .with_default_spacer() .with_default_spacer() - .with_child( - Checkbox::new("disabled") - .lens(AppData::disabled) - ) + .with_child(Checkbox::new("disabled").lens(AppData::disabled)) .with_default_spacer() .cross_axis_alignment(CrossAxisAlignment::End) .align_horizontal(UnitPoint::CENTER) - } pub fn main() { let window = WindowDesc::new(main_widget()).title( - LocalizedString::new("disabled-demo-window-title") - .with_placeholder("Disabled demo"), + LocalizedString::new("disabled-demo-window-title").with_placeholder("Disabled demo"), ); AppLauncher::with_window(window) .log_to_console() @@ -101,7 +88,7 @@ pub fn main() { option: true, text: "a very important text!".to_string(), value: 2.0, - disabled: false + disabled: false, }) .expect("launch failed"); -} \ No newline at end of file +} diff --git a/druid/src/widget/disable_if.rs b/druid/src/widget/disable_if.rs index 05ad6a040c..2e153dbb4d 100644 --- a/druid/src/widget/disable_if.rs +++ b/druid/src/widget/disable_if.rs @@ -1,4 +1,7 @@ -use crate::{WidgetPod, Env, Widget, Data, LifeCycle, EventCtx, PaintCtx, LifeCycleCtx, BoxConstraints, Size, LayoutCtx, Event, UpdateCtx, Point}; +use crate::{ + BoxConstraints, Data, Env, Event, EventCtx, LayoutCtx, LifeCycle, LifeCycleCtx, PaintCtx, + Point, Size, UpdateCtx, Widget, WidgetPod, +}; /// A widget wrapper which disables the inner widget if the provided closure return true. pub struct DisabledIf { diff --git a/druid/src/widget/widget_ext.rs b/druid/src/widget/widget_ext.rs index 7e57105447..2dda75209f 100644 --- a/druid/src/widget/widget_ext.rs +++ b/druid/src/widget/widget_ext.rs @@ -19,7 +19,7 @@ use super::{ Added, Align, BackgroundBrush, Click, Container, Controller, ControllerHost, EnvScope, IdentityWrapper, LensWrap, Padding, Parse, SizedBox, WidgetId, }; -use crate::widget::{Scroll, DisabledIf}; +use crate::widget::{DisabledIf, Scroll}; use crate::{ Color, Data, Env, EventCtx, Insets, KeyOrValue, Lens, LifeCycleCtx, UnitPoint, Widget, }; From c1c51fe59ec053aefbd4554f91b91feaa7450d1b Mon Sep 17 00:00:00 2001 From: xarvic Date: Wed, 7 Apr 2021 19:01:30 +0200 Subject: [PATCH 3/9] updated CHANGELOG.md --- CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5e8b6301e7..40cd4b8daf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -33,7 +33,7 @@ You can find its changes [documented below](#070---2021-01-01). - `LifeCycle::DisabledChanged`, `InternalLifeCycle::RouteDisabledChanged` and the `set_disabled()` and `is_disabled()` context-methods to implement disabled ([#1632] by [@xarvic]) - `LifeCycle::BuildFocusChain` to update the focus-chain ([#1632] by [@xarvic]) - +- `DisabledIf` widget wrapper to disable based on the state of Data and Env ([#1702] by [@xarvic]) ### Changed - Warn on unhandled Commands ([#1533] by [@Maan2003]) @@ -672,6 +672,7 @@ Last release without a changelog :( [#1691]: https://github.com/linebender/druid/pull/1691 [#1693]: https://github.com/linebender/druid/pull/1693 [#1698]: https://github.com/linebender/druid/pull/1698 +[#1702]: https://github.com/linebender/druid/pull/1702 [Unreleased]: https://github.com/linebender/druid/compare/v0.7.0...master [0.7.0]: https://github.com/linebender/druid/compare/v0.6.0...v0.7.0 From b414907760bfe249fbedc0fa9b0de735d0ed5b1f Mon Sep 17 00:00:00 2001 From: xarvic Date: Wed, 7 Apr 2021 19:48:55 +0200 Subject: [PATCH 4/9] fix issue --- druid/examples/disabled.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/druid/examples/disabled.rs b/druid/examples/disabled.rs index bfb0fccb43..8aedef46f9 100644 --- a/druid/examples/disabled.rs +++ b/druid/examples/disabled.rs @@ -1,6 +1,5 @@ use druid::widget::{Checkbox, CrossAxisAlignment, Flex, Label, Slider, Stepper, Switch, TextBox}; -use druid::{AppLauncher, Data, Lens, LocalizedString, Widget, WidgetExt, WindowDesc}; -use piet_common::UnitPoint; +use druid::{AppLauncher, Data, Lens, LocalizedString, UnitPoint, Widget, WidgetExt, WindowDesc}; #[derive(Clone, Data, Lens)] struct AppData { From 21246845e62df209de02e650e78eb71670b2b43a Mon Sep 17 00:00:00 2001 From: xarvic Date: Thu, 8 Apr 2021 10:48:11 +0200 Subject: [PATCH 5/9] updated example Signed-off-by: xarvic --- druid/examples/disabled.rs | 40 +++++++++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/druid/examples/disabled.rs b/druid/examples/disabled.rs index 8aedef46f9..619bcdbe39 100644 --- a/druid/examples/disabled.rs +++ b/druid/examples/disabled.rs @@ -1,4 +1,23 @@ -use druid::widget::{Checkbox, CrossAxisAlignment, Flex, Label, Slider, Stepper, Switch, TextBox}; +// 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. + +//! An example showing the effect of the disabled state on focus-chain and the widgets. +//! When the disabled checkbox is clicked only the widgets not marked as disabled should +//! respond. Pressing Tab should only focus widgets not marked as disabled. If a widget +//! is focused while getting disabled it should resign the focus. + +use druid::widget::{Checkbox, CrossAxisAlignment, Flex, Label, Slider, Stepper, Switch, TextBox, Button}; use druid::{AppLauncher, Data, Lens, LocalizedString, UnitPoint, Widget, WidgetExt, WindowDesc}; #[derive(Clone, Data, Lens)] @@ -69,6 +88,25 @@ fn main_widget() -> impl Widget { .disabled_if(|data, _| data.disabled), )) .with_default_spacer() + .with_child( + Flex::row() + .with_child( + Button::new("-") + .on_click(|_, data: &mut f64, _|*data -= 1.0) + .disabled_if(|data, _|*data < 1.0) + ) + .with_default_spacer() + .with_child(Label::dynamic(|data: &f64, _|data.to_string())) + .with_default_spacer() + .with_child( + Button::new("+") + .on_click(|_, data: &mut f64, _|*data += 1.0) + .disabled_if(|data, _|*data > 9.0) + ) + .lens(AppData::value) + .disabled_if(|data: &AppData, _|data.disabled) + ) + .with_default_spacer() .with_default_spacer() .with_default_spacer() .with_child(Checkbox::new("disabled").lens(AppData::disabled)) From 6315f20f56d61290cbc1216d643590539ad6cf25 Mon Sep 17 00:00:00 2001 From: xarvic Date: Thu, 8 Apr 2021 10:49:02 +0200 Subject: [PATCH 6/9] added License, updated documentation Signed-off-by: xarvic --- druid/src/widget/disable_if.rs | 23 ++++++++++++++++++++++- druid/src/widget/widget_ext.rs | 10 +++++----- 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/druid/src/widget/disable_if.rs b/druid/src/widget/disable_if.rs index 2e153dbb4d..8576de238b 100644 --- a/druid/src/widget/disable_if.rs +++ b/druid/src/widget/disable_if.rs @@ -1,9 +1,28 @@ +// 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. + use crate::{ BoxConstraints, Data, Env, Event, EventCtx, LayoutCtx, LifeCycle, LifeCycleCtx, PaintCtx, Point, Size, UpdateCtx, Widget, WidgetPod, }; /// A widget wrapper which disables the inner widget if the provided closure return true. +/// +/// See [`is_disabled`] or [`set_disabled`] for more info about disabled state. +/// +/// [`is_disabled`]: crate::EventCtx::is_disabled +/// [`set_disabled`]: crate::EventCtx::set_disabled pub struct DisabledIf { inner: WidgetPod, disabled_if: Box bool>, @@ -11,7 +30,9 @@ pub struct DisabledIf { impl> DisabledIf { /// Creates a new `DisabledIf` widget with the inner widget and the closure to decide if the - /// widget should be disabled. + /// widget should be [`disabled`]. + /// + /// [`disabled`]: crate::EventCtx::is_disabled pub fn new(widget: W, disabled_if: impl Fn(&T, &Env) -> bool + 'static) -> Self { DisabledIf { inner: WidgetPod::new(widget), diff --git a/druid/src/widget/widget_ext.rs b/druid/src/widget/widget_ext.rs index 2dda75209f..27dc7efe3b 100644 --- a/druid/src/widget/widget_ext.rs +++ b/druid/src/widget/widget_ext.rs @@ -275,13 +275,13 @@ pub trait WidgetExt: Widget + Sized + 'static { } /// Wrap this widget in a [`DisabledIf`] widget. - /// The provided closure will determine if the widget is disabled. /// - /// See [`is_disabled`] or [`set_disabled`] for more info about the disabled state. + /// The provided closure will determine if the widget is disabled. + /// See [`is_disabled`] or [`set_disabled`] for more info about disabled state. /// - /// [`is_disabled`]: struct.EventCtx.html#method.is_disabled - /// [`set_disabled`]: struct.EventCtx.html#method.set_disabled - /// [`DisabledIf`]: widget/struct.DisabledIf.html + /// [`is_disabled`]: crate::EventCtx::is_disabled + /// [`set_disabled`]: crate::EventCtx::set_disabled + /// [`DisabledIf`]: crate::widget::DisabledIf fn disabled_if(self, disabled_if: impl Fn(&T, &Env) -> bool + 'static) -> DisabledIf { DisabledIf::new(self, disabled_if) } From 0c4f0def0bb20153c3bfce455c4f2c29ef2dfc5b Mon Sep 17 00:00:00 2001 From: xarvic Date: Thu, 8 Apr 2021 10:50:05 +0200 Subject: [PATCH 7/9] reformat Signed-off-by: xarvic --- druid/examples/disabled.rs | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/druid/examples/disabled.rs b/druid/examples/disabled.rs index 619bcdbe39..1a3c29f10e 100644 --- a/druid/examples/disabled.rs +++ b/druid/examples/disabled.rs @@ -17,8 +17,10 @@ //! respond. Pressing Tab should only focus widgets not marked as disabled. If a widget //! is focused while getting disabled it should resign the focus. -use druid::widget::{Checkbox, CrossAxisAlignment, Flex, Label, Slider, Stepper, Switch, TextBox, Button}; use druid::{AppLauncher, Data, Lens, LocalizedString, UnitPoint, Widget, WidgetExt, WindowDesc}; +use druid::widget::{ + Button, Checkbox, CrossAxisAlignment, Flex, Label, Slider, Stepper, Switch, TextBox, +}; #[derive(Clone, Data, Lens)] struct AppData { @@ -92,19 +94,19 @@ fn main_widget() -> impl Widget { Flex::row() .with_child( Button::new("-") - .on_click(|_, data: &mut f64, _|*data -= 1.0) - .disabled_if(|data, _|*data < 1.0) + .on_click(|_, data: &mut f64, _| *data -= 1.0) + .disabled_if(|data, _| *data < 1.0), ) .with_default_spacer() - .with_child(Label::dynamic(|data: &f64, _|data.to_string())) + .with_child(Label::dynamic(|data: &f64, _| data.to_string())) .with_default_spacer() .with_child( Button::new("+") - .on_click(|_, data: &mut f64, _|*data += 1.0) - .disabled_if(|data, _|*data > 9.0) + .on_click(|_, data: &mut f64, _| *data += 1.0) + .disabled_if(|data, _| *data > 9.0), ) .lens(AppData::value) - .disabled_if(|data: &AppData, _|data.disabled) + .disabled_if(|data: &AppData, _| data.disabled), ) .with_default_spacer() .with_default_spacer() From 37e47c54ef9a527f03723d4078cd3d9489bec028 Mon Sep 17 00:00:00 2001 From: xarvic Date: Thu, 8 Apr 2021 10:59:00 +0200 Subject: [PATCH 8/9] reformat Signed-off-by: xarvic --- druid/examples/disabled.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/druid/examples/disabled.rs b/druid/examples/disabled.rs index 1a3c29f10e..22cbf2dd94 100644 --- a/druid/examples/disabled.rs +++ b/druid/examples/disabled.rs @@ -17,10 +17,10 @@ //! respond. Pressing Tab should only focus widgets not marked as disabled. If a widget //! is focused while getting disabled it should resign the focus. -use druid::{AppLauncher, Data, Lens, LocalizedString, UnitPoint, Widget, WidgetExt, WindowDesc}; use druid::widget::{ Button, Checkbox, CrossAxisAlignment, Flex, Label, Slider, Stepper, Switch, TextBox, }; +use druid::{AppLauncher, Data, Lens, LocalizedString, UnitPoint, Widget, WidgetExt, WindowDesc}; #[derive(Clone, Data, Lens)] struct AppData { From 585fcdd1b2a8cba36da81740e7f3290083604dde Mon Sep 17 00:00:00 2001 From: Colin Rofls Date: Sun, 11 Apr 2021 12:23:58 -0400 Subject: [PATCH 9/9] Apply suggestions from code review --- druid/examples/disabled.rs | 2 +- druid/src/widget/disable_if.rs | 2 +- druid/src/widget/widget_ext.rs | 1 - 3 files changed, 2 insertions(+), 3 deletions(-) diff --git a/druid/examples/disabled.rs b/druid/examples/disabled.rs index 22cbf2dd94..ed22fb26cf 100644 --- a/druid/examples/disabled.rs +++ b/druid/examples/disabled.rs @@ -1,4 +1,4 @@ -// Copyright 2020 The Druid Authors. +// Copyright 2021 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. diff --git a/druid/src/widget/disable_if.rs b/druid/src/widget/disable_if.rs index 8576de238b..1ebcaf6bdd 100644 --- a/druid/src/widget/disable_if.rs +++ b/druid/src/widget/disable_if.rs @@ -1,4 +1,4 @@ -// Copyright 2020 The Druid Authors. +// Copyright 2021 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. diff --git a/druid/src/widget/widget_ext.rs b/druid/src/widget/widget_ext.rs index 27dc7efe3b..6066b47143 100644 --- a/druid/src/widget/widget_ext.rs +++ b/druid/src/widget/widget_ext.rs @@ -281,7 +281,6 @@ pub trait WidgetExt: Widget + Sized + 'static { /// /// [`is_disabled`]: crate::EventCtx::is_disabled /// [`set_disabled`]: crate::EventCtx::set_disabled - /// [`DisabledIf`]: crate::widget::DisabledIf fn disabled_if(self, disabled_if: impl Fn(&T, &Env) -> bool + 'static) -> DisabledIf { DisabledIf::new(self, disabled_if) }