1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
#![doc = include_str!("../README.md")]
#![forbid(unsafe_code)]
#![warn(missing_docs)]

/// Broadcast channel for egui.
#[cfg(feature = "broadcast")]
pub mod broadcast;

/// Broadcast based on [type_map], useful for sending events between different parts of the application.
#[cfg(feature = "type_broadcast")]
pub mod type_broadcast;

/// Type-map based version of [UiInbox], useful for sending messages
/// to specific components from different parts of the application.
#[cfg(feature = "type_inbox")]
pub mod type_inbox;

use std::fmt::Debug;
use std::mem;
use std::sync::Arc;

use parking_lot::Mutex;

/// Trait to request a repaint.
pub trait RequestRepaintTrait {
    /// Request a repaint.
    fn request_repaint(&self);
}

impl<F> RequestRepaintTrait for F
where
    F: Fn() + Send + Sync + 'static,
{
    fn request_repaint(&self) {
        self();
    }
}

impl RequestRepaintTrait for &Box<dyn RequestRepaintTrait> {
    fn request_repaint(&self) {
        self.as_ref().request_repaint();
    }
}

enum RequestRepaintInner {
    #[cfg(feature = "egui")]
    Ctx(egui::Context),
    Box(Box<dyn RequestRepaintTrait + Send + Sync>),
}

/// Usually holds a reference to [egui::Context], but can also hold a boxed callback.
#[derive(Debug)]
pub struct RequestRepaintContext(RequestRepaintInner);

impl RequestRepaintContext {
    /// Create a new [RequestRepaintContext] from a callback function.
    pub fn from_callback<F>(f: F) -> Self
    where
        F: Fn() + Send + Sync + 'static,
    {
        Self(RequestRepaintInner::Box(Box::new(f)))
    }

    /// Create a new [RequestRepaintContext] from something that implements [RequestRepaintTrait].
    pub fn from_trait<T>(t: T) -> Self
    where
        T: RequestRepaintTrait + Send + Sync + 'static,
    {
        Self(RequestRepaintInner::Box(Box::new(t)))
    }

    /// Create a new [RequestRepaintContext] from an [egui::Context].
    #[cfg(feature = "egui")]
    pub fn from_egui_ctx(ctx: egui::Context) -> Self {
        Self(RequestRepaintInner::Ctx(ctx))
    }
}

impl RequestRepaintContext {
    fn request_repaint(&self) {
        match &self.0 {
            RequestRepaintInner::Ctx(ctx) => ctx.request_repaint(),
            RequestRepaintInner::Box(boxed) => boxed.request_repaint(),
        }
    }
}

impl Debug for RequestRepaintInner {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("RequestRepaint").finish_non_exhaustive()
    }
}

/// Trait to get a [RequestRepaintContext] from.
pub trait AsRequestRepaint {
    /// Should return a [RequestRepaintContext] that can be used to request a repaint.
    fn as_request_repaint(&self) -> RequestRepaintContext;
}

#[cfg(feature = "egui")]
mod egui_impl {
    use crate::{AsRequestRepaint, RequestRepaintContext};
    use egui::Context;

    impl AsRequestRepaint for Context {
        fn as_request_repaint(&self) -> RequestRepaintContext {
            RequestRepaintContext::from_egui_ctx(self.clone())
        }
    }

    impl AsRequestRepaint for egui::Ui {
        fn as_request_repaint(&self) -> RequestRepaintContext {
            RequestRepaintContext::from_egui_ctx(self.ctx().clone())
        }
    }
}

/// Utility to send messages to egui views from async functions, callbacks, etc. without
/// having to use interior mutability.
/// Example:
/// ```no_run
/// use eframe::egui;
/// use egui::CentralPanel;
/// use egui_inbox::UiInbox;
///
/// pub fn main() -> eframe::Result<()> {
///     let mut inbox = UiInbox::new();
///     let mut state = None;
///
///     eframe::run_simple_native(
///         "DnD Simple Example",
///         Default::default(),
///         move |ctx, _frame| {
///             CentralPanel::default().show(ctx, |ui| {
///                 inbox.replace(ui, &mut state);
///
///                 ui.label(format!("State: {:?}", state));
///                 if ui.button("Async Task").clicked() {
///                     state = Some("Waiting for async task to complete".to_string());
///                     let mut sender = inbox.sender();
///                     std::thread::spawn(move || {
///                         std::thread::sleep(std::time::Duration::from_secs(1));
///                         sender.send(Some("Hello from another thread!".to_string())).ok();
///                     });
///                 }
///             });
///         },
///     )
/// }
/// ```
pub struct UiInbox<T> {
    state: Arc<Mutex<State<T>>>,
    #[cfg(feature = "async")]
    oneshot_channels: Vec<futures_channel::oneshot::Sender<()>>,
}

impl<T> Debug for UiInbox<T> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("UiInbox").finish_non_exhaustive()
    }
}

#[derive(Debug)]
struct State<T> {
    ctx: Option<RequestRepaintContext>,
    queue: Vec<T>,
    dropped: bool,
}

impl<T> State<T> {
    fn new(ctx: Option<RequestRepaintContext>) -> Self {
        Self {
            ctx,
            queue: Vec::new(),
            dropped: false,
        }
    }
}

/// Sender for [UiInbox].
pub struct UiInboxSender<T> {
    state: Arc<Mutex<State<T>>>,
}

impl<T> Debug for UiInboxSender<T> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("UiInboxSender").finish_non_exhaustive()
    }
}

impl<T> Clone for UiInboxSender<T> {
    fn clone(&self) -> Self {
        Self {
            state: self.state.clone(),
        }
    }
}

impl<T> Default for UiInbox<T> {
    fn default() -> Self {
        Self::new()
    }
}

impl<T> Drop for UiInbox<T> {
    fn drop(&mut self) {
        #[cfg(feature = "async")]
        {
            self.oneshot_channels.drain(..).for_each(|tx| {
                tx.send(()).ok();
            });
        }

        let mut state = self.state.lock();
        state.dropped = true;
    }
}

impl<T> UiInbox<T> {
    /// Create a new inbox.
    /// The context is grabbed from the [Ui] passed to [UiInbox::read], so
    /// if you call [UiInbox::send] before [UiInbox::read], no repaint is requested.
    /// If you want to set the context on creation, use [UiInbox::new_with_ctx].
    pub fn new() -> Self {
        Self::_new(None)
    }

    /// Create a new inbox with a context.
    pub fn new_with_ctx(ctx: &impl AsRequestRepaint) -> Self {
        Self::_new(Some(ctx.as_request_repaint()))
    }

    fn _new(ctx: Option<RequestRepaintContext>) -> Self {
        let state = Arc::new(Mutex::new(State::new(ctx)));
        Self {
            state,
            #[cfg(feature = "async")]
            oneshot_channels: Vec::new(),
        }
    }

    /// Create a inbox and a sender for it.
    pub fn channel() -> (UiInboxSender<T>, Self) {
        let inbox = Self::new();
        let sender = inbox.sender();
        (sender, inbox)
    }

    /// Create a inbox with a context and a sender for it.
    pub fn channel_with_ctx(ctx: &impl AsRequestRepaint) -> (UiInboxSender<T>, Self) {
        let inbox = Self::new_with_ctx(ctx);
        let sender = inbox.sender();
        (sender, inbox)
    }

    /// Set the [Context] to use for requesting repaints.
    /// Usually this is not needed, since the [Context] is grabbed from the [Ui] passed to [UiInbox::read].
    pub fn set_ctx(&mut self, ctx: &impl AsRequestRepaint) {
        self.state.lock().ctx = Some(ctx.as_request_repaint());
    }

    /// Returns an iterator over all items sent to the inbox.
    /// The inbox is cleared after this call.
    ///
    /// The ui is only passed here so we can grab a reference to [Context].
    /// This is mostly done for convenience, so you don't have to pass a reference to [Context]
    /// to every struct that uses an inbox on creation.
    pub fn read(&self, ui: &impl AsRequestRepaint) -> impl Iterator<Item = T> {
        let mut state = self.state.lock();
        if state.ctx.is_none() {
            state.ctx = Some(ui.as_request_repaint());
        }
        mem::take(&mut state.queue).into_iter()
    }

    /// Same as [UiInbox::read], but you don't need to pass a reference to [Ui].
    /// If you use this, make sure you set the [Context] with [UiInbox::set_ctx] or
    /// [UiInbox::new_with_ctx] manually.
    pub fn read_without_ctx(&self) -> impl Iterator<Item = T> {
        let mut state = self.state.lock();
        mem::take(&mut state.queue).into_iter()
    }

    /// Replaces the value of `target` with the last item sent to the inbox.
    /// Any other updates are discarded.
    /// If no item was sent to the inbox, `target` is not updated.
    /// Returns `true` if `target` was updated.
    ///
    /// The ui is only passed here so we can grab a reference to [Context].
    /// This is mostly done for convenience, so you don't have to pass a reference to [Context]
    /// to every struct that uses an inbox on creation.
    pub fn replace(&self, ui: &impl AsRequestRepaint, target: &mut T) -> bool {
        let mut state = self.state.lock();
        if state.ctx.is_none() {
            state.ctx = Some(ui.as_request_repaint());
        }

        let item = mem::take(&mut state.queue).pop();
        if let Some(item) = item {
            *target = item;
            true
        } else {
            false
        }
    }

    /// Replaces the value of the options with [Some<T>] if there is an item in the inbox.
    /// Otherwise, similar to [UiInbox::replace].
    pub fn replace_option(&self, ui: &impl AsRequestRepaint, target: &mut Option<T>) {
        let mut state = self.state.lock();
        if state.ctx.is_none() {
            state.ctx = Some(ui.as_request_repaint());
        }

        let item = mem::take(&mut state.queue).pop();
        if let Some(item) = item {
            *target = Some(item);
        }
    }

    /// Same as [UiInbox::replace], but you don't need to pass a reference to [Ui].
    /// If you use this, make sure you set the [Context] with [UiInbox::set_ctx] or
    /// [UiInbox::new_with_ctx] manually.
    pub fn replace_without_ctx(&self, target: &mut T) -> bool {
        let mut state = self.state.lock();
        let item = mem::take(&mut state.queue).pop();
        if let Some(item) = item {
            *target = item;
            true
        } else {
            false
        }
    }

    /// Returns a sender for this inbox.
    pub fn sender(&self) -> UiInboxSender<T> {
        UiInboxSender {
            state: self.state.clone(),
        }
    }
}

#[cfg(feature = "async")]
mod async_impl {
    use std::pin::{pin, Pin};
    use std::task::{Context, Poll};

    use futures::{select, FutureExt, Sink, SinkExt, StreamExt};

    use hello_egui_utils::{spawn, MaybeSend};

    use crate::{SendError, UiInbox, UiInboxSender};

    impl<T> UiInbox<T> {
        /// Spawns a future that will automatically be cancelled when the inbox is dropped.
        /// Make sure your future is safe to cancel (It may stop at any await point).
        ///
        /// If you want to spawn a future that should definitely run to completion, use [UiInbox::spawn_detached] instead.
        pub fn spawn<F>(&mut self, f: impl FnOnce(UiInboxSender<T>) -> F)
        where
            F: std::future::Future<Output = ()> + MaybeSend + 'static,
        {
            let (tx, mut rx) = futures_channel::oneshot::channel();
            self.oneshot_channels.push(tx);

            let sender = self.sender();
            let future = f(sender).fuse();

            spawn(async move {
                let mut future = pin!(future);

                select! {
                    _ = future => {},
                    _ = rx => {},
                }
            });
        }

        /// Spawns a future that will **not** be cancelled when the inbox is dropped.
        pub fn spawn_detached<F>(&mut self, f: impl FnOnce(UiInboxSender<T>) -> F)
        where
            F: std::future::Future<Output = ()> + Send + 'static,
        {
            let sender = self.sender();
            let future = f(sender);
            spawn(future);
        }
    }

    impl<T> UiInboxSender<T> {
        /// Send each item of a stream to the inbox, as they come in.
        pub async fn send_stream(
            &mut self,
            stream: impl futures::Stream<Item = T> + Send + 'static,
        ) -> Result<(), SendError<T>> {
            let stream = stream.map(|i| Ok(i));
            let mut stream = pin!(stream);
            self.send_all(&mut stream).await
        }
    }

    impl<T> Sink<T> for UiInboxSender<T> {
        type Error = SendError<T>;

        fn poll_ready(
            self: Pin<&mut Self>,
            _cx: &mut Context<'_>,
        ) -> Poll<Result<(), Self::Error>> {
            Poll::Ready(Ok(()))
        }

        fn start_send(self: Pin<&mut Self>, item: T) -> Result<(), Self::Error> {
            UiInboxSender::send(&self, item)
        }

        fn poll_flush(
            self: Pin<&mut Self>,
            _cx: &mut Context<'_>,
        ) -> Poll<Result<(), Self::Error>> {
            Poll::Ready(Ok(()))
        }

        fn poll_close(
            self: Pin<&mut Self>,
            _cx: &mut Context<'_>,
        ) -> Poll<Result<(), Self::Error>> {
            Poll::Ready(Ok(()))
        }
    }
}

impl<T> UiInboxSender<T> {
    /// Send an item to the inbox.
    /// Calling this will request a repaint from egui.
    /// If this is called before a call to `UiInbox::read` was done, no repaint is requested
    /// (Since we didn't have a chance to get a reference to [Context] yet).
    ///
    /// This returns an error if the inbox was dropped.
    pub fn send(&self, item: T) -> Result<(), SendError<T>> {
        let mut state = self.state.lock();
        if state.dropped {
            Err(SendError(item))
        } else {
            state.queue.push(item);
            if let Some(ctx) = &state.ctx {
                ctx.request_repaint();
            }
            Ok(())
        }
    }
}

/// Error returned when sending a message to the inbox fails.
/// This can happen if the inbox was dropped.
/// The message is returned in the error, so it can be recovered.
pub struct SendError<T>(pub T);

impl<T: Debug> Debug for SendError<T> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("SendError").field("item", &self.0).finish()
    }
}