Skip to content

Commit

Permalink
Focus-chain update (#1724)
Browse files Browse the repository at this point in the history
* changed propagate_to_hidden: Lifecycle::BuildFocusChain is not send to hidden widgets anymore.

Signed-off-by: xarvic <[email protected]>

* test if the focused widget is still a functional member of the tree

Signed-off-by: xarvic <[email protected]>

* fix clippy warnings

Signed-off-by: xarvic <[email protected]>

* update CHANGELOG.md

Signed-off-by: xarvic <[email protected]>

* Apply suggestions from code review

Co-authored-by: Colin Rofls <[email protected]>

Co-authored-by: xarvic <[email protected]>
Co-authored-by: Colin Rofls <[email protected]>
  • Loading branch information
3 people authored Apr 19, 2021
1 parent 2fd7b7e commit 452328c
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ You can find its changes [documented below](#070---2021-01-01).
- GTK Shell: Prevent mangling of newline characters in clipboard ([#1695] by [@ForLoveOfCats])
- Use correct fill rule when rendering SVG paths ([#1606] by [@SecondFlight])
- Correctly capture and use stroke properties when rendering SVG paths ([#1647] by [@SecondFlight])
- focus-chain now only includes non hidden (`should_propagate_to_hidden()` on `Event` and `Lifecylce`) widgets ([#1724] by [@xarvic])
- Fixed layout of scrollbar with very small viewports ([#1715] by [@andrewhickman])

### Visual
Expand Down Expand Up @@ -682,6 +683,7 @@ Last release without a changelog :(
[#1702]: https://github.com/linebender/druid/pull/1702
[#1713]: https://github.com/linebender/druid/pull/1713
[#1715]: https://github.com/linebender/druid/pull/1715
[#1724]: https://github.com/linebender/druid/pull/1724

[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
17 changes: 17 additions & 0 deletions druid/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -912,6 +912,8 @@ impl<T: Data, W: Widget<T>> WidgetPod<T, W> {
// we may send an extra event after the actual event
let mut extra_event = None;

let had_focus = self.state.has_focus;

let recurse = match event {
LifeCycle::Internal(internal) => match internal {
InternalLifeCycle::RouteWidgetAdded => {
Expand Down Expand Up @@ -1035,6 +1037,10 @@ impl<T: Data, W: Widget<T>> WidgetPod<T, W> {
}
LifeCycle::BuildFocusChain => {
if self.state.update_focus_chain {
// Replace has_focus to check if the value changed in the meantime
let is_focused = ctx.state.focus_widget == Some(self.state.id);
self.state.has_focus = is_focused;

self.state.focus_chain.clear();
true
} else {
Expand Down Expand Up @@ -1082,6 +1088,17 @@ impl<T: Data, W: Widget<T>> WidgetPod<T, W> {
// Update focus-chain of our parent
LifeCycle::BuildFocusChain => {
self.state.update_focus_chain = false;

// had_focus is the old focus value. state.has_focus was repaced with ctx.is_focused().
// Therefore if had_focus is true but state.has_focus is false then the widget which is
// currently focused is not part of the functional tree anymore
// (Lifecycle::BuildFocusChain.should_propagate_to_hidden() is false!) and should
// resign the focus.
if had_focus && !self.state.has_focus {
self.state.request_focus = Some(FocusChange::Resign);
}
self.state.has_focus = had_focus;

if !self.state.is_disabled() {
ctx.widget_state.focus_chain.extend(&self.state.focus_chain);
}
Expand Down
9 changes: 5 additions & 4 deletions druid/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -428,10 +428,11 @@ impl LifeCycle {
pub fn should_propagate_to_hidden(&self) -> bool {
match self {
LifeCycle::Internal(internal) => internal.should_propagate_to_hidden(),
LifeCycle::WidgetAdded | LifeCycle::DisabledChanged(_) | LifeCycle::BuildFocusChain => {
true
}
LifeCycle::Size(_) | LifeCycle::HotChanged(_) | LifeCycle::FocusChanged(_) => false,
LifeCycle::WidgetAdded | LifeCycle::DisabledChanged(_) => true,
LifeCycle::Size(_)
| LifeCycle::HotChanged(_)
| LifeCycle::FocusChanged(_)
| LifeCycle::BuildFocusChain => false,
}
}
}
Expand Down

0 comments on commit 452328c

Please sign in to comment.