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 fn new() -> Self {
15 Self {
16 message_to_publish: None,
17 redraw_request: window::RedrawRequest::Wait,
18 event_status: event::Status::Ignored,
19 }
20 }
21
22 /// Creates a new "capturing" [`Action`]. A capturing [`Action`]
23 /// will make other widgets consider it final and prevent further
24 /// processing.
25 ///
26 /// Prevents "event bubbling".
27 pub fn capture() -> Self {
28 Self {
29 event_status: event::Status::Captured,
30 ..Self::new()
31 }
32 }
33
34 /// Creates a new [`Action`] that publishes the given `Message` for
35 /// the application to handle.
36 ///
37 /// Publishing a `Message` always produces a redraw.
38 pub fn publish(message: Message) -> Self {
39 Self {
40 message_to_publish: Some(message),
41 ..Self::new()
42 }
43 }
44
45 /// Creates a new [`Action`] that requests a redraw to happen as
46 /// soon as possible; without publishing any `Message`.
47 pub fn request_redraw() -> Self {
48 Self {
49 redraw_request: window::RedrawRequest::NextFrame,
50 ..Self::new()
51 }
52 }
53
54 /// Creates a new [`Action`] that requests a redraw to happen at
55 /// the given [`Instant`]; without publishing any `Message`.
56 ///
57 /// This can be useful to efficiently animate content, like a
58 /// blinking caret on a text input.
59 pub fn request_redraw_at(at: Instant) -> Self {
60 Self {
61 redraw_request: window::RedrawRequest::At(at),
62 ..Self::new()
63 }
64 }
65
66 /// Marks the [`Action`] as "capturing". See [`Self::capture`].
67 pub fn and_capture(mut self) -> Self {
68 self.event_status = event::Status::Captured;
69 self
70 }
71
72 /// Converts the [`Action`] into its internal parts.
73 ///
74 /// This method is meant to be used by runtimes, libraries, or internal
75 /// widget implementations.
76 pub fn into_inner(
77 self,
78 ) -> (Option<Message>, window::RedrawRequest, event::Status) {
79 (
80 self.message_to_publish,
81 self.redraw_request,
82 self.event_status,
83 )
84 }
85}