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

Implement disabled for druid controls #1717

Merged
merged 67 commits into from
May 3, 2021
Merged
Show file tree
Hide file tree
Changes from 60 commits
Commits
Show all changes
67 commits
Select commit Hold shift + click to select a range
fe93a1b
- added LifeCycle::DisabledChanged and InternalLifeCycle::RouteDisab…
Mar 20, 2021
3e1874b
created tests for disable
Mar 20, 2021
d6e34b2
fixed tests
Mar 20, 2021
500954e
fixed tests
Mar 20, 2021
f606199
updated core.rs and event.rs
Mar 20, 2021
6fd0b71
fixed focus-chain bug:
Mar 22, 2021
25d19c9
fix disabled update
Mar 22, 2021
e3d8b92
update tests
Mar 22, 2021
77d4f8f
fixed code (all tests succeed)
Mar 22, 2021
4d48edd
refactored core.rs and tests/mod.rs
Mar 23, 2021
9b2c8ba
updated tests
Mar 23, 2021
d5116de
fixed focus-chain bug
Mar 23, 2021
6c79e1e
make clippy happy
Mar 23, 2021
1fe6971
Merge branch 'master' into disable-widgets-2
xarvic Mar 23, 2021
40be21f
make clippy happy #2
Mar 23, 2021
5520e5b
Apply suggestions from code review
xarvic Mar 31, 2021
232e7ee
Update druid/src/contexts.rs
xarvic Mar 31, 2021
eccd596
refactor DisabledChanged
Mar 31, 2021
9754051
refactor DisabledChanged
Mar 31, 2021
74b86b2
fixed error, revered change of focus_chain
Mar 31, 2021
c181f51
refactored tests
Mar 31, 2021
cfc544e
reordered lifecycle events
Mar 31, 2021
05d45fc
reverted changes to the focus_chain
Mar 31, 2021
b9b9aca
implemented new focus-chain using LifeCycle::BuildFocusChain
Apr 5, 2021
f091bf9
update tests
Apr 5, 2021
4766ceb
fixed problems
Apr 5, 2021
a901d1c
updated texts
Apr 5, 2021
eb3cfd7
clippy fix
Apr 5, 2021
fdd510a
fixed documentation
Apr 5, 2021
4ef9697
Update druid/src/event.rs
xarvic Apr 5, 2021
4745fe9
fixed documentation
Apr 5, 2021
4567f8f
made logic simpler
Apr 5, 2021
ba58aad
refactored post_event_processing
Apr 5, 2021
8d6b784
updated CHANGELOG.md
Apr 5, 2021
d3e73d1
fixed docs
Apr 5, 2021
7be336c
Merge branch 'master' into disable-widgets-2
xarvic Apr 5, 2021
a52437c
make clippy happy
Apr 5, 2021
2919953
Merge branch 'master' into disable-widgets-2
cmyr Apr 6, 2021
aad03e0
Merge pull request #2 from linebender/master
xarvic Apr 7, 2021
8cc5aed
implemented DisabledIf
Apr 7, 2021
1b1b661
reformat
Apr 7, 2021
c1c51fe
updated CHANGELOG.md
Apr 7, 2021
b414907
fix issue
Apr 7, 2021
9a38b81
Merge branch 'master' into disabled_if
xarvic Apr 7, 2021
2124684
updated example
Apr 8, 2021
6315f20
added License, updated documentation
Apr 8, 2021
0c4f0de
reformat
Apr 8, 2021
3a70a0a
Merge remote-tracking branch 'origin/disabled_if' into disabled_if
Apr 8, 2021
ba8ece0
Merge branch 'master' into disabled_if
xarvic Apr 8, 2021
37e47c5
reformat
Apr 8, 2021
cd79332
Merge remote-tracking branch 'origin/disabled_if' into disabled_if
Apr 8, 2021
2e991cb
implement disabled state for button
Apr 12, 2021
cddcf4e
Merge branch 'disabled_if' into disable-widgets-2
Apr 13, 2021
4f452a0
updated visuals of controls when disabled
Apr 13, 2021
cccf5e2
reformat
Apr 13, 2021
c542489
run clippy
Apr 13, 2021
4a73d4b
run cargo fmt
Apr 13, 2021
573ed24
Merge branch 'master' into disable-widgets-2
xarvic Apr 14, 2021
d15a590
Update CHANGELOG.md
xarvic Apr 14, 2021
45e2ee5
Merge branch 'master' into disable-widgets-2
xarvic Apr 17, 2021
9925a05
implemented disabled-text-color
Apr 20, 2021
7869dde
changed background
Apr 20, 2021
0ebb7b8
reformat
Apr 20, 2021
ced8b37
Merge branch 'master' into disable-widgets-2
xarvic Apr 20, 2021
cec3486
Changed label.
Apr 22, 2021
dea7c3a
added deprecation note
Apr 24, 2021
271a7df
Merge branch 'master' into disable-widgets-2
cmyr May 3, 2021
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ You can find its changes [documented below](#070---2021-01-01).
- Flex values that are less than 0.0 will default to 0.0 and warn in release. It will panic in debug mode. ([#1691] by [@arthmis])
- Lens implemented for tuples of Lenses of length 2-8, Tuple2 removed ([#1654] by [@Maan2003])
- Window size and positioning code is now in display points ([#1713] by [@jneem])
- Update look and feel of controls when disabled ([#1717] by [@xarvic])

### Deprecated

Expand Down Expand Up @@ -680,6 +681,7 @@ Last release without a changelog :(
[#1698]: https://github.com/linebender/druid/pull/1698
[#1702]: https://github.com/linebender/druid/pull/1702
[#1713]: https://github.com/linebender/druid/pull/1713
[#1717]: https://github.com/linebender/druid/pull/1717

[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
Expand Down
64 changes: 37 additions & 27 deletions druid/src/text/input_component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,38 +273,45 @@ impl<T: TextStorage + EditableText> Widget<T> for TextComponent<T> {
fn event(&mut self, ctx: &mut EventCtx, event: &Event, data: &mut T, env: &Env) {
match event {
Event::MouseDown(mouse) if self.can_write() => {
ctx.set_active(true);
// ensure data is up to date before a click
let needs_rebuild = self
.borrow()
.layout
.text()
.map(|old| !old.same(data))
.unwrap_or(true);
if needs_rebuild {
self.borrow_mut().layout.set_text(data.clone());
self.borrow_mut().layout.rebuild_if_needed(ctx.text(), env);
if !ctx.is_disabled() {
xarvic marked this conversation as resolved.
Show resolved Hide resolved
ctx.set_active(true);
// ensure data is up to date before a click
let needs_rebuild = self
.borrow()
.layout
.text()
.map(|old| !old.same(data))
.unwrap_or(true);
if needs_rebuild {
self.borrow_mut().layout.set_text(data.clone());
self.borrow_mut().layout.rebuild_if_needed(ctx.text(), env);
self.borrow_mut()
.update_pending_invalidation(ImeInvalidation::Reset);
}
self.borrow_mut()
.do_mouse_down(mouse.pos, mouse.mods, mouse.count);
self.borrow_mut()
.update_pending_invalidation(ImeInvalidation::Reset);
.update_pending_invalidation(ImeInvalidation::SelectionChanged);
ctx.request_update();
ctx.request_paint();
}
self.borrow_mut()
.do_mouse_down(mouse.pos, mouse.mods, mouse.count);
self.borrow_mut()
.update_pending_invalidation(ImeInvalidation::SelectionChanged);
ctx.request_update();
ctx.request_paint();
}
Event::MouseMove(mouse) if self.can_write() => {
ctx.set_cursor(&Cursor::IBeam);
if ctx.is_active() {
let pre_sel = self.borrow().selection();
self.borrow_mut().do_drag(mouse.pos);
if self.borrow().selection() != pre_sel {
self.borrow_mut()
.update_pending_invalidation(ImeInvalidation::SelectionChanged);
ctx.request_update();
ctx.request_paint();
if !ctx.is_disabled() {
ctx.set_cursor(&Cursor::IBeam);
if ctx.is_active() {
let pre_sel = self.borrow().selection();
self.borrow_mut().do_drag(mouse.pos);
if self.borrow().selection() != pre_sel {
self.borrow_mut()
.update_pending_invalidation(ImeInvalidation::SelectionChanged);
ctx.request_update();
ctx.request_paint();
}
}
} else {
ctx.set_disabled(false);
ctx.clear_cursor();
}
}
Event::MouseUp(_) => {
Expand Down Expand Up @@ -380,6 +387,9 @@ impl<T: TextStorage + EditableText> Widget<T> for TextComponent<T> {
}
}
}
LifeCycle::DisabledChanged(_) => {
ctx.request_paint();
}
_ => (),
}
}
Expand Down
6 changes: 6 additions & 0 deletions druid/src/theme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ pub const BACKGROUND_LIGHT: Key<Color> = Key::new("org.linebender.druid.theme.ba
pub const BACKGROUND_DARK: Key<Color> = Key::new("org.linebender.druid.theme.background_dark");
pub const FOREGROUND_LIGHT: Key<Color> = Key::new("org.linebender.druid.theme.foreground_light");
pub const FOREGROUND_DARK: Key<Color> = Key::new("org.linebender.druid.theme.foreground_dark");
pub const DISABLED_FOREGROUND_LIGHT: Key<Color> =
Key::new("org.linebender.druid.theme.disabled_foreground_light");
pub const DISABLED_FOREGROUND_DARK: Key<Color> =
Key::new("org.linebender.druid.theme.disabled_foreground_dark");
xarvic marked this conversation as resolved.
Show resolved Hide resolved
pub const BUTTON_DARK: Key<Color> = Key::new("org.linebender.druid.theme.button_dark");
pub const BUTTON_LIGHT: Key<Color> = Key::new("org.linebender.druid.theme.button_light");
pub const BUTTON_BORDER_RADIUS: Key<f64> = Key::new("org.linebender.druid.theme.button_radius");
Expand Down Expand Up @@ -116,6 +120,8 @@ pub(crate) fn add_to_env(env: Env) -> Env {
.adding(BACKGROUND_DARK, Color::rgb8(0x31, 0x31, 0x31))
.adding(FOREGROUND_LIGHT, Color::rgb8(0xf9, 0xf9, 0xf9))
.adding(FOREGROUND_DARK, Color::rgb8(0xbf, 0xbf, 0xbf))
.adding(DISABLED_FOREGROUND_LIGHT, Color::rgb8(0x49, 0x49, 0x49))
.adding(DISABLED_FOREGROUND_DARK, Color::rgb8(0x2f, 0x2f, 0x2f))
.adding(BUTTON_DARK, Color::BLACK)
.adding(BUTTON_LIGHT, Color::rgb8(0x21, 0x21, 0x21))
.adding(BUTTON_BORDER_RADIUS, 4.)
Expand Down
29 changes: 20 additions & 9 deletions druid/src/widget/button.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,24 +115,26 @@ impl<T: Data> Widget<T> for Button<T> {
fn event(&mut self, ctx: &mut EventCtx, event: &Event, _data: &mut T, _env: &Env) {
match event {
Event::MouseDown(_) => {
ctx.set_active(true);
ctx.request_paint();
trace!("Button {:?} pressed", ctx.widget_id());
if !ctx.is_disabled() {
ctx.set_active(true);
ctx.request_paint();
trace!("Button {:?} pressed", ctx.widget_id());
}
}
Event::MouseUp(_) => {
if ctx.is_active() {
ctx.set_active(false);
if ctx.is_active() && !ctx.is_disabled() {
ctx.request_paint();
trace!("Button {:?} released", ctx.widget_id());
}
ctx.set_active(false);
}
_ => (),
}
}

#[instrument(name = "Button", level = "trace", skip(self, ctx, event, data, env))]
fn lifecycle(&mut self, ctx: &mut LifeCycleCtx, event: &LifeCycle, data: &T, env: &Env) {
if let LifeCycle::HotChanged(_) = event {
if let LifeCycle::HotChanged(_) | LifeCycle::DisabledChanged(_) = event {
ctx.request_paint();
}
self.label.lifecycle(ctx, event, data, env)
Expand Down Expand Up @@ -165,7 +167,7 @@ impl<T: Data> Widget<T> for Button<T> {

#[instrument(name = "Button", level = "trace", skip(self, ctx, data, env))]
fn paint(&mut self, ctx: &mut PaintCtx, data: &T, env: &Env) {
let is_active = ctx.is_active();
let is_active = ctx.is_active() && !ctx.is_disabled();
let is_hot = ctx.is_hot();
let size = ctx.size();
let stroke_width = env.get(theme::BUTTON_BORDER_WIDTH);
Expand All @@ -175,7 +177,16 @@ impl<T: Data> Widget<T> for Button<T> {
.inset(-stroke_width / 2.0)
.to_rounded_rect(env.get(theme::BUTTON_BORDER_RADIUS));

let bg_gradient = if is_active {
let bg_gradient = if ctx.is_disabled() {
xarvic marked this conversation as resolved.
Show resolved Hide resolved
LinearGradient::new(
UnitPoint::TOP,
UnitPoint::BOTTOM,
(
env.get(theme::DISABLED_FOREGROUND_LIGHT),
env.get(theme::DISABLED_FOREGROUND_DARK),
),
)
} else if is_active {
LinearGradient::new(
UnitPoint::TOP,
UnitPoint::BOTTOM,
Expand All @@ -189,7 +200,7 @@ impl<T: Data> Widget<T> for Button<T> {
)
};

let border_color = if is_hot {
let border_color = if is_hot && !ctx.is_disabled() {
env.get(theme::BORDER_LIGHT)
} else {
env.get(theme::BORDER_DARK)
Expand Down
24 changes: 16 additions & 8 deletions druid/src/widget/checkbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,14 @@ impl Widget<bool> for Checkbox {
fn event(&mut self, ctx: &mut EventCtx, event: &Event, data: &mut bool, _env: &Env) {
match event {
Event::MouseDown(_) => {
ctx.set_active(true);
ctx.request_paint();
trace!("Checkbox {:?} pressed", ctx.widget_id());
if !ctx.is_disabled() {
ctx.set_active(true);
ctx.request_paint();
trace!("Checkbox {:?} pressed", ctx.widget_id());
}
}
Event::MouseUp(_) => {
if ctx.is_active() {
ctx.set_active(false);
if ctx.is_active() && !ctx.is_disabled() {
if ctx.is_hot() {
if *data {
*data = false;
Expand All @@ -62,6 +63,7 @@ impl Widget<bool> for Checkbox {
}
ctx.request_paint();
}
ctx.set_active(false);
}
_ => (),
}
Expand All @@ -70,7 +72,7 @@ impl Widget<bool> for Checkbox {
#[instrument(name = "CheckBox", level = "trace", skip(self, ctx, event, data, env))]
fn lifecycle(&mut self, ctx: &mut LifeCycleCtx, event: &LifeCycle, data: &bool, env: &Env) {
self.child_label.lifecycle(ctx, event, data, env);
if let LifeCycle::HotChanged(_) = event {
if let LifeCycle::HotChanged(_) | LifeCycle::DisabledChanged(_) = event {
ctx.request_paint();
}
}
Expand Down Expand Up @@ -126,7 +128,7 @@ impl Widget<bool> for Checkbox {

ctx.fill(rect, &background_gradient);

let border_color = if ctx.is_hot() {
let border_color = if ctx.is_hot() && !ctx.is_disabled() {
env.get(theme::BORDER_LIGHT)
} else {
env.get(theme::BORDER_DARK)
Expand All @@ -145,7 +147,13 @@ impl Widget<bool> for Checkbox {
.line_cap(LineCap::Round)
.line_join(LineJoin::Round);

ctx.stroke_styled(path, &env.get(theme::LABEL_COLOR), 2., &style);
let brush = if ctx.is_disabled() {
env.get(theme::DISABLED_FOREGROUND_LIGHT)
} else {
env.get(theme::LABEL_COLOR)
};

ctx.stroke_styled(path, &brush, 2., &style);
xarvic marked this conversation as resolved.
Show resolved Hide resolved
}

// Paint the text label
Expand Down
4 changes: 2 additions & 2 deletions druid/src/widget/click.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ impl<T: Data, W: Widget<T>> Controller<T, W> for Click<T> {
fn event(&mut self, child: &mut W, ctx: &mut EventCtx, event: &Event, data: &mut T, env: &Env) {
match event {
Event::MouseDown(mouse_event) => {
if mouse_event.button == MouseButton::Left {
if mouse_event.button == MouseButton::Left && !ctx.is_disabled() {
ctx.set_active(true);
ctx.request_paint();
trace!("Widget {:?} pressed", ctx.widget_id());
Expand All @@ -68,7 +68,7 @@ impl<T: Data, W: Widget<T>> Controller<T, W> for Click<T> {
Event::MouseUp(mouse_event) => {
if ctx.is_active() && mouse_event.button == MouseButton::Left {
ctx.set_active(false);
if ctx.is_hot() {
if ctx.is_hot() && !ctx.is_disabled() {
(self.action)(ctx, data, env);
}
ctx.request_paint();
Expand Down
24 changes: 16 additions & 8 deletions druid/src/widget/radio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,19 +66,21 @@ impl<T: Data + PartialEq> Widget<T> for Radio<T> {
fn event(&mut self, ctx: &mut EventCtx, event: &Event, data: &mut T, _env: &Env) {
match event {
Event::MouseDown(_) => {
ctx.set_active(true);
ctx.request_paint();
trace!("Radio button {:?} pressed", ctx.widget_id());
if !ctx.is_disabled() {
ctx.set_active(true);
ctx.request_paint();
trace!("Radio button {:?} pressed", ctx.widget_id());
}
}
Event::MouseUp(_) => {
if ctx.is_active() {
ctx.set_active(false);
if ctx.is_active() && !ctx.is_disabled() {
if ctx.is_hot() {
*data = self.variant.clone();
}
ctx.request_paint();
trace!("Radio button {:?} released", ctx.widget_id());
}
ctx.set_active(false);
}
_ => (),
}
Expand All @@ -87,7 +89,7 @@ impl<T: Data + PartialEq> Widget<T> for Radio<T> {
#[instrument(name = "Radio", level = "trace", skip(self, ctx, event, data, env))]
fn lifecycle(&mut self, ctx: &mut LifeCycleCtx, event: &LifeCycle, data: &T, env: &Env) {
self.child_label.lifecycle(ctx, event, data, env);
if let LifeCycle::HotChanged(_) = event {
if let LifeCycle::HotChanged(_) | LifeCycle::DisabledChanged(_) = event {
ctx.request_paint();
}
}
Expand Down Expand Up @@ -136,7 +138,7 @@ impl<T: Data + PartialEq> Widget<T> for Radio<T> {

ctx.fill(circle, &background_gradient);

let border_color = if ctx.is_hot() {
let border_color = if ctx.is_hot() && !ctx.is_disabled() {
env.get(theme::BORDER_LIGHT)
} else {
env.get(theme::BORDER_DARK)
Expand All @@ -148,7 +150,13 @@ impl<T: Data + PartialEq> Widget<T> for Radio<T> {
if *data == self.variant {
let inner_circle = Circle::new((size / 2., size / 2.), INNER_CIRCLE_RADIUS);

ctx.fill(inner_circle, &env.get(theme::CURSOR_COLOR));
let fill = if ctx.is_disabled() {
env.get(theme::DISABLED_FOREGROUND_LIGHT)
} else {
env.get(theme::CURSOR_COLOR)
};

ctx.fill(inner_circle, &fill);
}

// Paint the text label
Expand Down
Loading