Skip to content

Commit

Permalink
Fix incorrect LifeCycleCtx with FocusChanged event
Browse files Browse the repository at this point in the history
We were handling this event separately, and were accidentally
reusing the parent context, instead of the child context;
this has the concrete problem that the context had
an incorrect WidgetId.

This fix feels like the lesser of two evils; we add a mechanism
for explicitly substituting a new event for the one received,
ensuring that we only call the child's lifecycle method in one place.
  • Loading branch information
cmyr committed Apr 25, 2020
1 parent 7bb8b65 commit 21d46bb
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions druid/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,10 @@ impl<T: Data, W: Widget<T>> WidgetPod<T, W> {
}

pub fn lifecycle(&mut self, ctx: &mut LifeCycleCtx, event: &LifeCycle, data: &T, env: &Env) {
// in the case of an internal routing event, if we are at our target
// we may replace the routing event with the actual event
let mut substitute_event = None;

let recurse = match event {
LifeCycle::Internal(internal) => match internal {
InternalLifeCycle::RouteWidgetAdded => {
Expand Down Expand Up @@ -641,10 +645,11 @@ impl<T: Data, W: Widget<T>> WidgetPod<T, W> {
// Only send FocusChanged in case there's actual change
if old != new {
self.state.has_focus = change;
let event = LifeCycle::FocusChanged(change);
self.inner.lifecycle(ctx, &event, data, env);
substitute_event = Some(LifeCycle::FocusChanged(change));
true
} else {
false
}
false
} else {
self.state.has_focus = false;
// Recurse when the target widgets could be our descendants.
Expand Down Expand Up @@ -694,6 +699,9 @@ impl<T: Data, W: Widget<T>> WidgetPod<T, W> {
}
};

// use the substitute event, if one exists
let event = substitute_event.as_ref().unwrap_or(event);

if recurse {
let mut child_ctx = LifeCycleCtx {
command_queue: ctx.command_queue,
Expand Down

0 comments on commit 21d46bb

Please sign in to comment.