-
Notifications
You must be signed in to change notification settings - Fork 569
DisabledIf widget #1702
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
Merged
Merged
DisabledIf widget #1702
Changes from 13 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
aad03e0
Merge pull request #2 from linebender/master
xarvic 8cc5aed
implemented DisabledIf
1b1b661
reformat
c1c51fe
updated CHANGELOG.md
b414907
fix issue
9a38b81
Merge branch 'master' into disabled_if
xarvic 2124684
updated example
6315f20
added License, updated documentation
0c4f0de
reformat
3a70a0a
Merge remote-tracking branch 'origin/disabled_if' into disabled_if
ba8ece0
Merge branch 'master' into disabled_if
xarvic 37e47c5
reformat
cd79332
Merge remote-tracking branch 'origin/disabled_if' into disabled_if
585fcdd
Apply suggestions from code review
cmyr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,133 @@ | ||
| // 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::{ | ||
| 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 { | ||
| option: bool, | ||
| text: String, | ||
| value: f64, | ||
|
|
||
| disabled: bool, | ||
| } | ||
|
|
||
| fn named_child(name: &str, widget: impl Widget<AppData> + 'static) -> impl Widget<AppData> { | ||
| Flex::row() | ||
| .with_child(Label::new(name)) | ||
| .with_default_spacer() | ||
| .with_child(widget) | ||
| } | ||
|
|
||
| fn main_widget() -> impl Widget<AppData> { | ||
| 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_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)) | ||
| .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"); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| // Copyright 2020 The Druid Authors. | ||
cmyr marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // | ||
| // 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::{ | ||
cmyr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| 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<T, W> { | ||
| inner: WidgetPod<T, W>, | ||
xarvic marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| disabled_if: Box<dyn Fn(&T, &Env) -> bool>, | ||
| } | ||
|
|
||
| impl<T: Data, W: Widget<T>> DisabledIf<T, W> { | ||
| /// Creates a new `DisabledIf` widget with the inner widget and the closure to decide if the | ||
| /// 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), | ||
| disabled_if: Box::new(disabled_if), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl<T: Data, W: Widget<T>> Widget<T> for DisabledIf<T, W> { | ||
| 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); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.