Skip to content

Commit

Permalink
Replace event::Status in Widget::on_event with `Shell::capture_ev…
Browse files Browse the repository at this point in the history
…ent`
  • Loading branch information
hecrj committed Oct 25, 2024
1 parent 16275ca commit 019cf7b
Show file tree
Hide file tree
Showing 44 changed files with 556 additions and 705 deletions.
15 changes: 6 additions & 9 deletions core/src/element.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
use crate::event::{self, Event};
use crate::layout;
use crate::mouse;
use crate::overlay;
use crate::renderer;
use crate::widget;
use crate::widget::tree::{self, Tree};
use crate::{
Border, Clipboard, Color, Layout, Length, Rectangle, Shell, Size, Vector,
Widget,
Border, Clipboard, Color, Event, Layout, Length, Rectangle, Shell, Size,
Vector, Widget,
};

use std::borrow::Borrow;
Expand Down Expand Up @@ -319,11 +318,11 @@ where
clipboard: &mut dyn Clipboard,
shell: &mut Shell<'_, B>,
viewport: &Rectangle,
) -> event::Status {
) {
let mut local_messages = Vec::new();
let mut local_shell = Shell::new(&mut local_messages);

let status = self.widget.on_event(
self.widget.on_event(
tree,
event,
layout,
Expand All @@ -335,8 +334,6 @@ where
);

shell.merge(local_shell, &self.mapper);

status
}

fn draw(
Expand Down Expand Up @@ -457,10 +454,10 @@ where
clipboard: &mut dyn Clipboard,
shell: &mut Shell<'_, Message>,
viewport: &Rectangle,
) -> event::Status {
) {
self.element.widget.on_event(
state, event, layout, cursor, renderer, clipboard, shell, viewport,
)
);
}

fn draw(
Expand Down
6 changes: 2 additions & 4 deletions core/src/overlay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@ mod group;
pub use element::Element;
pub use group::Group;

use crate::event::{self, Event};
use crate::layout;
use crate::mouse;
use crate::renderer;
use crate::widget;
use crate::widget::Tree;
use crate::{Clipboard, Layout, Point, Rectangle, Shell, Size, Vector};
use crate::{Clipboard, Event, Layout, Point, Rectangle, Shell, Size, Vector};

/// An interactive component that can be displayed on top of other widgets.
pub trait Overlay<Message, Theme, Renderer>
Expand Down Expand Up @@ -65,8 +64,7 @@ where
_renderer: &Renderer,
_clipboard: &mut dyn Clipboard,
_shell: &mut Shell<'_, Message>,
) -> event::Status {
event::Status::Ignored
) {
}

/// Returns the current [`mouse::Interaction`] of the [`Overlay`].
Expand Down
13 changes: 5 additions & 8 deletions core/src/overlay/element.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
pub use crate::Overlay;

use crate::event::{self, Event};
use crate::layout;
use crate::mouse;
use crate::renderer;
use crate::widget;
use crate::{Clipboard, Layout, Point, Rectangle, Shell, Size};
use crate::{Clipboard, Event, Layout, Point, Rectangle, Shell, Size};

/// A generic [`Overlay`].
#[allow(missing_debug_implementations)]
Expand Down Expand Up @@ -58,9 +57,9 @@ where
renderer: &Renderer,
clipboard: &mut dyn Clipboard,
shell: &mut Shell<'_, Message>,
) -> event::Status {
) {
self.overlay
.on_event(event, layout, cursor, renderer, clipboard, shell)
.on_event(event, layout, cursor, renderer, clipboard, shell);
}

/// Returns the current [`mouse::Interaction`] of the [`Element`].
Expand Down Expand Up @@ -157,11 +156,11 @@ where
renderer: &Renderer,
clipboard: &mut dyn Clipboard,
shell: &mut Shell<'_, B>,
) -> event::Status {
) {
let mut local_messages = Vec::new();
let mut local_shell = Shell::new(&mut local_messages);

let event_status = self.content.on_event(
self.content.on_event(
event,
layout,
cursor,
Expand All @@ -171,8 +170,6 @@ where
);

shell.merge(local_shell, self.mapper);

event_status
}

fn mouse_interaction(
Expand Down
27 changes: 11 additions & 16 deletions core/src/overlay/group.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate::event;
use crate::layout;
use crate::mouse;
use crate::overlay;
Expand Down Expand Up @@ -81,21 +80,17 @@ where
renderer: &Renderer,
clipboard: &mut dyn Clipboard,
shell: &mut Shell<'_, Message>,
) -> event::Status {
self.children
.iter_mut()
.zip(layout.children())
.map(|(child, layout)| {
child.on_event(
event.clone(),
layout,
cursor,
renderer,
clipboard,
shell,
)
})
.fold(event::Status::Ignored, event::Status::merge)
) {
for (child, layout) in self.children.iter_mut().zip(layout.children()) {
child.on_event(
event.clone(),
layout,
cursor,
renderer,
clipboard,
shell,
);
}
}

fn draw(
Expand Down
23 changes: 23 additions & 0 deletions core/src/shell.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::event;
use crate::time::Instant;
use crate::window;

Expand All @@ -10,6 +11,7 @@ use crate::window;
#[derive(Debug)]
pub struct Shell<'a, Message> {
messages: &'a mut Vec<Message>,
event_status: event::Status,
redraw_request: Option<window::RedrawRequest>,
is_layout_invalid: bool,
are_widgets_invalid: bool,
Expand All @@ -20,6 +22,7 @@ impl<'a, Message> Shell<'a, Message> {
pub fn new(messages: &'a mut Vec<Message>) -> Self {
Self {
messages,
event_status: event::Status::Ignored,
redraw_request: None,
is_layout_invalid: false,
are_widgets_invalid: false,
Expand All @@ -36,6 +39,24 @@ impl<'a, Message> Shell<'a, Message> {
self.messages.push(message);
}

/// Marks the current event as captured. Prevents "event bubbling".
///
/// A widget should capture an event when no ancestor should
/// handle it.
pub fn capture_event(&mut self) {
self.event_status = event::Status::Captured;
}

/// Returns the current [`event::Status`] of the [`Shell`].
pub fn event_status(&self) -> event::Status {
self.event_status
}

/// Returns whether the current event has been captured.
pub fn is_event_captured(&self) -> bool {
self.event_status == event::Status::Captured
}

/// Requests a new frame to be drawn as soon as possible.
pub fn request_redraw(&mut self) {
self.redraw_request = Some(window::RedrawRequest::NextFrame);
Expand Down Expand Up @@ -114,5 +135,7 @@ impl<'a, Message> Shell<'a, Message> {

self.are_widgets_invalid =
self.are_widgets_invalid || other.are_widgets_invalid;

self.event_status = self.event_status.merge(other.event_status);
}
}
6 changes: 2 additions & 4 deletions core/src/widget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@ pub use operation::Operation;
pub use text::Text;
pub use tree::Tree;

use crate::event::{self, Event};
use crate::layout::{self, Layout};
use crate::mouse;
use crate::overlay;
use crate::renderer;
use crate::{Clipboard, Length, Rectangle, Shell, Size, Vector};
use crate::{Clipboard, Event, Length, Rectangle, Shell, Size, Vector};

/// A component that displays information and allows interaction.
///
Expand Down Expand Up @@ -122,8 +121,7 @@ where
_clipboard: &mut dyn Clipboard,
_shell: &mut Shell<'_, Message>,
_viewport: &Rectangle,
) -> event::Status {
event::Status::Ignored
) {
}

/// Returns the current [`mouse::Interaction`] of the [`Widget`].
Expand Down
5 changes: 1 addition & 4 deletions examples/loading_spinners/src/circular.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use iced::advanced::layout;
use iced::advanced::renderer;
use iced::advanced::widget::tree::{self, Tree};
use iced::advanced::{self, Clipboard, Layout, Shell, Widget};
use iced::event;
use iced::mouse;
use iced::time::Instant;
use iced::widget::canvas;
Expand Down Expand Up @@ -272,7 +271,7 @@ where
_clipboard: &mut dyn Clipboard,
shell: &mut Shell<'_, Message>,
_viewport: &Rectangle,
) -> event::Status {
) {
let state = tree.state.downcast_mut::<State>();

if let Event::Window(window::Event::RedrawRequested(now)) = event {
Expand All @@ -285,8 +284,6 @@ where
state.cache.clear();
shell.request_redraw();
}

event::Status::Ignored
}

fn draw(
Expand Down
5 changes: 1 addition & 4 deletions examples/loading_spinners/src/linear.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use iced::advanced::layout;
use iced::advanced::renderer::{self, Quad};
use iced::advanced::widget::tree::{self, Tree};
use iced::advanced::{self, Clipboard, Layout, Shell, Widget};
use iced::event;
use iced::mouse;
use iced::time::Instant;
use iced::window;
Expand Down Expand Up @@ -186,16 +185,14 @@ where
_clipboard: &mut dyn Clipboard,
shell: &mut Shell<'_, Message>,
_viewport: &Rectangle,
) -> event::Status {
) {
let state = tree.state.downcast_mut::<State>();

if let Event::Window(window::Event::RedrawRequested(now)) = event {
*state = state.timed_transition(self.cycle_duration, now);

shell.request_redraw();
}

event::Status::Ignored
}

fn draw(
Expand Down
53 changes: 25 additions & 28 deletions examples/toast/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,16 +169,15 @@ mod toast {
use iced::advanced::renderer;
use iced::advanced::widget::{self, Operation, Tree};
use iced::advanced::{Clipboard, Shell, Widget};
use iced::event::{self, Event};
use iced::mouse;
use iced::theme;
use iced::widget::{
button, column, container, horizontal_rule, horizontal_space, row, text,
};
use iced::window;
use iced::{
Alignment, Center, Element, Fill, Length, Point, Rectangle, Renderer,
Size, Theme, Vector,
Alignment, Center, Element, Event, Fill, Length, Point, Rectangle,
Renderer, Size, Theme, Vector,
};

pub const DEFAULT_TIMEOUT: u64 = 5;
Expand Down Expand Up @@ -369,7 +368,7 @@ mod toast {
clipboard: &mut dyn Clipboard,
shell: &mut Shell<'_, Message>,
viewport: &Rectangle,
) -> event::Status {
) {
self.content.as_widget_mut().on_event(
&mut state.children[0],
event,
Expand All @@ -379,7 +378,7 @@ mod toast {
clipboard,
shell,
viewport,
)
);
}

fn draw(
Expand Down Expand Up @@ -498,7 +497,7 @@ mod toast {
renderer: &Renderer,
clipboard: &mut dyn Clipboard,
shell: &mut Shell<'_, Message>,
) -> event::Status {
) {
if let Event::Window(window::Event::RedrawRequested(now)) = &event {
self.instants.iter_mut().enumerate().for_each(
|(index, maybe_instant)| {
Expand All @@ -520,35 +519,33 @@ mod toast {

let viewport = layout.bounds();

self.toasts
for (((child, state), layout), instant) in self
.toasts
.iter_mut()
.zip(self.state.iter_mut())
.zip(layout.children())
.zip(self.instants.iter_mut())
.map(|(((child, state), layout), instant)| {
let mut local_messages = vec![];
let mut local_shell = Shell::new(&mut local_messages);

let status = child.as_widget_mut().on_event(
state,
event.clone(),
layout,
cursor,
renderer,
clipboard,
&mut local_shell,
&viewport,
);
{
let mut local_messages = vec![];
let mut local_shell = Shell::new(&mut local_messages);

if !local_shell.is_empty() {
instant.take();
}
child.as_widget_mut().on_event(
state,
event.clone(),
layout,
cursor,
renderer,
clipboard,
&mut local_shell,
&viewport,
);

shell.merge(local_shell, std::convert::identity);
if !local_shell.is_empty() {
instant.take();
}

status
})
.fold(event::Status::Ignored, event::Status::merge)
shell.merge(local_shell, std::convert::identity);
}
}

fn draw(
Expand Down
Loading

0 comments on commit 019cf7b

Please sign in to comment.