Skip to content

Commit

Permalink
Add "handled" state to event
Browse files Browse the repository at this point in the history
Allow an event to be handled, blocking further propagation.
  • Loading branch information
raphlinus committed Jul 10, 2019
1 parent 89edd5c commit 3505e03
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
19 changes: 17 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ pub struct LayoutCtx {}
pub struct EventCtx<'a> {
base_state: &'a mut BaseState,
had_active: bool,
is_handled: bool,
}

pub struct UpdateCtx {
Expand Down Expand Up @@ -225,7 +226,7 @@ impl<T: Data, W: Widget<T>> WidgetPod<T, W> {
data: &mut T,
env: &Env,
) -> Option<Action> {
if !event.recurse() {
if ctx.is_handled || !event.recurse() {
// This function is called by containers to propagate an event from
// containers to children. Non-recurse events will be invoked directly
// from other points in the library.
Expand All @@ -235,6 +236,7 @@ impl<T: Data, W: Widget<T>> WidgetPod<T, W> {
let mut child_ctx = EventCtx {
base_state: &mut self.state,
had_active,
is_handled: false,
};
let rect = child_ctx.base_state.layout_rect;
// Note: could also represent this as `Option<Event>`.
Expand Down Expand Up @@ -281,6 +283,7 @@ impl<T: Data, W: Widget<T>> WidgetPod<T, W> {
ctx.base_state.needs_inval |= child_ctx.base_state.needs_inval;
ctx.base_state.is_hot |= child_ctx.base_state.is_hot;
ctx.base_state.has_active |= child_ctx.base_state.has_active;
ctx.is_handled |= child_ctx.is_handled;
action
}

Expand Down Expand Up @@ -346,6 +349,7 @@ impl<T: Data> UiState<T> {
let mut ctx = EventCtx {
base_state: &mut base_state,
had_active: self.root.state.has_active,
is_handled: false,
};
let env = self.root_env();
let action = self.root.event(&event, &mut ctx, &mut self.data, &env);
Expand All @@ -357,7 +361,7 @@ impl<T: Data> UiState<T> {
self.handle.invalidate();
}
// TODO: process actions
action.is_some()
ctx.is_handled()
}

fn paint(&mut self, piet: &mut Piet) -> bool {
Expand Down Expand Up @@ -518,6 +522,17 @@ impl<'a> EventCtx<'a> {
pub fn is_active(&self) -> bool {
self.base_state.is_active
}

/// Set the event as "handled", which stops its propagation to other
/// widgets.
pub fn set_handled(&mut self) {
self.is_handled = true;
}

/// Determine whether the event has been handled by some other widget.
pub fn is_handled(&self) -> bool {
self.is_handled
}
}

impl UpdateCtx {
Expand Down
17 changes: 10 additions & 7 deletions src/widget/scroll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,18 +94,21 @@ impl<T: Data> Widget<T> for Scroll<T> {
) -> Option<Action> {
let size = ctx.base_state.size();
let viewport = Rect::from_origin_size(Point::ORIGIN, size);
if let Event::Wheel(wheel) = event {
if self.scroll(wheel.delta, size) {
ctx.invalidate();
}
}
// TODO: cancellation logic
let child_event = event.transform_scroll(self.scroll_offset, viewport);
if let Some(child_event) = child_event {
let action = if let Some(child_event) = child_event {
self.child.event(&child_event, ctx, data, env)
} else {
None
};
if !ctx.is_handled() {
if let Event::Wheel(wheel) = event {
if self.scroll(wheel.delta, size) {
ctx.invalidate();
ctx.set_handled();
}
}
}
action
}

fn update(&mut self, ctx: &mut UpdateCtx, _old_data: Option<&T>, data: &T, env: &Env) {
Expand Down

0 comments on commit 3505e03

Please sign in to comment.