summaryrefslogtreecommitdiffstats
path: root/widget/src/action.rs
diff options
context:
space:
mode:
Diffstat (limited to 'widget/src/action.rs')
-rw-r--r--widget/src/action.rs89
1 files changed, 89 insertions, 0 deletions
diff --git a/widget/src/action.rs b/widget/src/action.rs
new file mode 100644
index 00000000..1dd3a787
--- /dev/null
+++ b/widget/src/action.rs
@@ -0,0 +1,89 @@
+use crate::core::event;
+use crate::core::time::Instant;
+use crate::core::window;
+
+/// A runtime action that can be performed by some widgets.
+#[derive(Debug, Clone)]
+pub struct Action<Message> {
+ message_to_publish: Option<Message>,
+ redraw_request: Option<window::RedrawRequest>,
+ event_status: event::Status,
+}
+
+impl<Message> Action<Message> {
+ fn new() -> Self {
+ Self {
+ message_to_publish: None,
+ redraw_request: None,
+ event_status: event::Status::Ignored,
+ }
+ }
+
+ /// Creates a new "capturing" [`Action`]. A capturing [`Action`]
+ /// will make other widgets consider it final and prevent further
+ /// processing.
+ ///
+ /// Prevents "event bubbling".
+ pub fn capture() -> Self {
+ Self {
+ event_status: event::Status::Captured,
+ ..Self::new()
+ }
+ }
+
+ /// Creates a new [`Action`] that publishes the given `Message` for
+ /// the application to handle.
+ ///
+ /// Publishing a `Message` always produces a redraw.
+ pub fn publish(message: Message) -> Self {
+ Self {
+ message_to_publish: Some(message),
+ ..Self::new()
+ }
+ }
+
+ /// Creates a new [`Action`] that requests a redraw to happen as
+ /// soon as possible; without publishing any `Message`.
+ pub fn request_redraw() -> Self {
+ Self {
+ redraw_request: Some(window::RedrawRequest::NextFrame),
+ ..Self::new()
+ }
+ }
+
+ /// Creates a new [`Action`] that requests a redraw to happen at
+ /// the given [`Instant`]; without publishing any `Message`.
+ ///
+ /// This can be useful to efficiently animate content, like a
+ /// blinking caret on a text input.
+ pub fn request_redraw_at(at: Instant) -> Self {
+ Self {
+ redraw_request: Some(window::RedrawRequest::At(at)),
+ ..Self::new()
+ }
+ }
+
+ /// Marks the [`Action`] as "capturing". See [`Self::capture`].
+ pub fn and_capture(mut self) -> Self {
+ self.event_status = event::Status::Captured;
+ self
+ }
+
+ /// Converts the [`Action`] into its internal parts.
+ ///
+ /// This method is meant to be used by runtimes, libraries, or internal
+ /// widget implementations.
+ pub fn into_inner(
+ self,
+ ) -> (
+ Option<Message>,
+ Option<window::RedrawRequest>,
+ event::Status,
+ ) {
+ (
+ self.message_to_publish,
+ self.redraw_request,
+ self.event_status,
+ )
+ }
+}