Skip to main content

crux_core/capabilities/
render.rs

1//! Built-in capability used to notify the Shell that a UI update is necessary.
2
3use std::future::Future;
4
5use facet::Facet;
6use serde::{Deserialize, Serialize};
7
8use crate::{Command, Request, capability::Operation, command::NotificationBuilder};
9
10/// The single operation `Render` implements.
11#[allow(clippy::unsafe_derive_deserialize)]
12#[derive(Facet, Clone, Serialize, Deserialize, Debug, PartialEq, Eq)]
13pub struct RenderOperation;
14
15impl Operation for RenderOperation {
16    type Output = ();
17}
18
19/// Signal to the shell that the UI should be redrawn.
20/// Returns a [`NotificationBuilder`].
21///
22/// ### Examples:
23/// To use in a sync context:
24/// ```
25///# use crux_core::{Command, render::{render_builder, RenderOperation}};
26///# #[crux_core::macros::effect]pub enum Effect {Render(RenderOperation)}
27///# enum Event {None}
28/// let command: Command<Effect, Event> =
29///     render_builder().into(); // or use `render_command()`
30/// ```
31/// To use in an async context:
32/// ```
33///# use crux_core::{Command, render::{render_builder, RenderOperation}};
34///# #[crux_core::macros::effect]pub enum Effect {Render(RenderOperation)}
35///# enum Event {None}
36///# let command: Command<Effect, Event> = Command::new(|ctx| async move {
37/// render_builder().into_future(ctx).await;
38///# });
39/// ```
40#[must_use]
41pub fn render_builder<Effect, Event>()
42-> NotificationBuilder<Effect, Event, impl Future<Output = ()>>
43where
44    Effect: From<Request<RenderOperation>> + Send + 'static,
45    Event: Send + 'static,
46{
47    Command::notify_shell(RenderOperation)
48}
49
50/// Signal to the shell that the UI should be redrawn.
51/// Returns a [`Command`].
52pub fn render<Effect, Event>() -> Command<Effect, Event>
53where
54    Effect: From<Request<RenderOperation>> + Send + 'static,
55    Event: Send + 'static,
56{
57    render_builder().into()
58}