iced_widget/action.rs
1use crate::core::event;
2use crate::core::time::Instant;
3use crate::core::window;
4
5/// A runtime action that can be performed by some widgets.
6#[derive(Debug, Clone)]
7pub struct Action<Message> {
8 message_to_publish: Option<Message>,
9 redraw_request: window::RedrawRequest,
10 event_status: event::Status,
11}
12
13impl<Message> Action<Message> {
14 /// Creates an [`Action`] that does nothing.
15 pub fn none() -> Self {
16 Self {
17 message_to_publish: None,
18 redraw_request: window::RedrawRequest::Wait,
19 event_status: event::Status::Ignored,
20 }
21 }
22
23 /// Creates a new "capturing" [`Action`]. A capturing [`Action`]
24 /// will make other widgets consider it final and prevent further
25 /// processing.
26 ///
27 /// Prevents "event bubbling".
28 pub fn capture() -> Self {
29 Self {
30 event_status: event::Status::Captured,
31 ..Self::none()
32 }
33 }
34
35 /// Creates a new [`Action`] that publishes the given `Message` for
36 /// the application to handle.
37 ///
38 /// Publishing a `Message` always produces a redraw.
39 pub fn publish(message: Message) -> Self {
40 Self {
41 message_to_publish: Some(message),
42 ..Self::none()
43 }
44 }
45
46 /// Creates a new [`Action`] that requests a redraw to happen as
47 /// soon as possible; without publishing any `Message`.
48 pub fn request_redraw() -> Self {
49 Self {
50 redraw_request: window::RedrawRequest::NextFrame,
51 ..Self::none()
52 }
53 }
54
55 /// Creates a new [`Action`] that requests a redraw to happen at
56 /// the given [`Instant`]; without publishing any `Message`.
57 ///
58 /// This can be useful to efficiently animate content, like a
59 /// blinking caret on a text input.
60 pub fn request_redraw_at(at: Instant) -> Self {
61 Self {
62 redraw_request: window::RedrawRequest::At(at),
63 ..Self::none()
64 }
65 }
66
67 /// Marks the [`Action`] as "capturing". See [`Self::capture`].
68 pub fn and_capture(mut self) -> Self {
69 self.event_status = event::Status::Captured;
70 self
71 }
72
73 /// Requests a redraw at the given [`Instant`]. See [`Self::request_redraw_at`].
74 pub fn and_request_redraw_at(mut self, at: Instant) -> Self {
75 self.redraw_request = window::RedrawRequest::At(at);
76 self
77 }
78
79 /// Converts the [`Action`] into its internal parts.
80 ///
81 /// This method is meant to be used by runtimes, libraries, or internal
82 /// widget implementations.
83 pub fn into_inner(self) -> (Option<Message>, window::RedrawRequest, event::Status) {
84 (
85 self.message_to_publish,
86 self.redraw_request,
87 self.event_status,
88 )
89 }
90}
91
92impl<Message> Default for Action<Message> {
93 fn default() -> Self {
94 Self::none()
95 }
96}