moly_kit/utils/makepad/
ui_runner.rs1use makepad_widgets::defer_with_redraw::DeferWithRedraw;
8use makepad_widgets::{Cx, Scope, UiRunner, Widget};
9
10pub trait DeferRedraw<W>
11where
12 Self: Sized,
13{
14 fn defer_redraw(self) {}
18}
19
20impl<W: Widget + 'static> DeferRedraw<W> for UiRunner<W> {
21 fn defer_redraw(self) {
22 self.defer_with_redraw(|_, _, _| {});
23 }
24}
25
26pub trait AsyncDeferCallback<T, R>:
27 FnOnce(&mut T, &mut Cx, &mut Scope) -> R + Send + 'static
28where
29 R: Send + 'static,
30{
31}
32
33impl<T, R: Send + 'static, F: FnOnce(&mut T, &mut Cx, &mut Scope) -> R + Send + 'static>
34 AsyncDeferCallback<T, R> for F
35{
36}
37
38#[allow(unused)]
40pub trait DeferAsync<T> {
41 fn defer_async<R>(
52 self,
53 f: impl AsyncDeferCallback<T, R>,
54 ) -> impl std::future::Future<Output = Option<R>> + Send
55 where
56 R: Send + 'static,
57 Self: Sized;
58}
59
60impl<T: 'static> DeferAsync<T> for UiRunner<T> {
61 async fn defer_async<R: Send + 'static>(self, f: impl AsyncDeferCallback<T, R>) -> Option<R> {
62 let (tx, rx) = futures::channel::oneshot::channel::<R>();
63 self.defer(move |me, cx, scope| {
64 let _ = tx.send(f(me, cx, scope));
65 });
66 rx.await.ok()
67 }
68}
69
70pub trait DeferWithRedrawAsync<T: 'static> {
73 fn defer_with_redraw_async<R>(
77 self,
78 f: impl AsyncDeferCallback<T, R>,
79 ) -> impl std::future::Future<Output = Option<R>> + Send
80 where
81 R: Send + 'static,
82 Self: Sized;
83}
84
85impl<W: Widget + 'static> DeferWithRedrawAsync<W> for UiRunner<W> {
86 async fn defer_with_redraw_async<R: Send + 'static>(
87 self,
88 f: impl AsyncDeferCallback<W, R>,
89 ) -> Option<R> {
90 let (tx, rx) = futures::channel::oneshot::channel::<R>();
91 self.defer_with_redraw(move |widget, cx, scope| {
92 let _ = tx.send(f(widget, cx, scope));
93 });
94 rx.await.ok()
95 }
96}