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
#![doc = include_str!("../README.md")]
#![forbid(unsafe_code)]
#![warn(missing_docs)]

use std::fmt::{Debug, Display};

use egui::Ui;

use egui_inbox::UiInbox;
use hello_egui_utils::{asyncify, CallbackType};

type CallbackFn<T> = dyn FnOnce(T) + Send;

type ReloadFn<T, E> = dyn FnMut(Box<CallbackFn<Result<T, E>>>) + Send + Sync;

type ErrorUiFn<E> = dyn Fn(&mut Ui, &E, &mut State<'_>) + Send + Sync;

type LoadingUiFn = dyn Fn(&mut Ui) + Send + Sync;

/// Helper struct to call the reload function.
pub struct State<'a> {
    /// True if this is a reloadable suspense.
    pub reloadable: bool,
    reload_fn: &'a mut (dyn FnMut() + Send + Sync),
}

impl<'a> State<'a> {
    /// Call this to reload the data.
    pub fn reload(&mut self) {
        (self.reload_fn)();
    }
}

/// A widget that shows a spinner while data is loading and shows
/// an error message and retry button if the data failed to load.
pub struct EguiSuspense<T: Debug, E: Display + Debug = String> {
    inbox: UiInbox<Result<T, E>>,
    data: Option<Result<T, E>>,

    reload_fn: Option<Box<ReloadFn<T, E>>>,

    error_ui: Option<Box<ErrorUiFn<E>>>,
    loading_ui: Option<Box<LoadingUiFn>>,
}

impl<T: Debug, E: Display + Debug> Debug for EguiSuspense<T, E> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("EguiSuspense")
            .field("data", &self.data)
            .finish()
    }
}

impl<T: Debug + Send + Sync + 'static, E: Display + Debug + Send + Sync + 'static>
    EguiSuspense<T, E>
{
    asyncify!(
        /// Create a new suspense that can be reloaded.
        reloadable,
        callback_mut: (impl FnMut(CallbackType<Result<T, E>>, ) + Send + Sync + 'static),
        call_prefix: (Self::),
        generics: (),
        async_generics: (<F: std::future::Future<Output = Result<T, E>> + Send + 'static>),
        parameters: (),
        future: impl FnMut() -> F + Send + Sync + 'static,
        return_type: (Self),
        body: |()| {
            let mut callback_mut = callback_mut;
            let inbox = UiInbox::new();
            let inbox_clone = inbox.sender();
            callback_mut(Box::new(move |result| {
                inbox_clone.send(result).ok();
            }));
            Self {
                inbox,
                data: None,

                reload_fn: Some(Box::new(callback_mut)),
                error_ui: None,
                loading_ui: Some(Box::new(|ui| {
                    ui.spinner();
                })),
            }
        },
    );

    asyncify!(
        /// Create a new suspense that will only try to load the data once.
        single_try,
        callback_once: (impl FnOnce(CallbackType<Result<T, E>>) + Send + Sync + 'static),
        call_prefix: (Self::),
        generics: (),
        async_generics: (<F: std::future::Future<Output = Result<T, E>> + Send + Sync + 'static>),
        parameters: (),
        future: F,
        return_type: (Self),
        body: |()| {
            let inbox = UiInbox::new();
            let inbox_clone = inbox.sender();
            callback_once(Box::new(move |result| {
                inbox_clone.send(result).ok();
            }));
            Self {
                inbox,
                data: None,

                reload_fn: None,
                error_ui: None,
                loading_ui: Some(Box::new(|ui| {
                    ui.spinner();
                })),
            }
        },
    );

    /// Create a new suspense that is already loaded.
    pub fn loaded(data: T) -> Self {
        Self {
            inbox: UiInbox::new(),
            data: Some(Ok(data)),

            reload_fn: None,
            error_ui: None,
            loading_ui: None,
        }
    }

    /// Use this to customize the loading ui.
    pub fn loading_ui(mut self, f: impl Fn(&mut Ui) + 'static + Send + Sync) -> Self {
        self.loading_ui = Some(Box::new(f));
        self
    }

    /// Use this to disable the loading ui.
    /// Nothing will be shown while the data is loading.
    /// Useful when you want to show a loading indicator somewhere else, e.g. when using
    /// [egui_pull_to_refresh](https://crates.io/crates/egui_pull_to_refresh).
    pub fn no_loading_ui(mut self) -> Self {
        self.loading_ui = None;
        self
    }

    /// Use this to customize the error ui.
    /// The closure will be called with the error and a [State] struct.
    pub fn error_ui(
        mut self,
        f: impl Fn(&mut Ui, &E, &mut State<'_>) + 'static + Send + Sync,
    ) -> Self {
        self.error_ui = Some(Box::new(f));
        self
    }

    /// Show the actual ui.
    /// The content closure will be called with the data and a [State] struct.
    pub fn ui<R>(
        &mut self,
        ui: &mut Ui,
        content: impl FnOnce(&mut Ui, &mut T, &mut State) -> R,
    ) -> Option<R> {
        let mut result = None;

        if let Some(result) = self.inbox.read(ui).last() {
            self.data = Some(result);
        }

        let mut clear_data = false;
        let clear_data_ref = &mut clear_data;

        match &mut self.data {
            None => {
                if let Some(loading_ui) = &mut self.loading_ui {
                    loading_ui(ui);
                }
            }
            Some(Ok(data)) => {
                let tx = self.inbox.sender();
                result = Some(content(
                    ui,
                    data,
                    &mut State {
                        reloadable: self.reload_fn.is_some(),
                        reload_fn: &mut || {
                            if let Some(reload_fn) = &mut self.reload_fn {
                                *clear_data_ref = true;
                                let tx = tx.clone();
                                reload_fn(Box::new(move |result| {
                                    tx.send(result).ok();
                                }));
                            }
                        },
                    },
                ));
            }
            Some(Err(err)) => {
                if let Some(err_ui) = &mut self.error_ui {
                    let tx = self.inbox.sender();

                    if let Some(reload) = &mut self.reload_fn {
                        err_ui(
                            ui,
                            err,
                            &mut State {
                                reloadable: true,
                                reload_fn: &mut move || {
                                    *clear_data_ref = true;

                                    let inbox = tx.clone();
                                    reload(Box::new(move |result| {
                                        inbox.send(result).ok();
                                    }));
                                },
                            },
                        );
                    } else {
                        err_ui(
                            ui,
                            err,
                            &mut State {
                                reloadable: false,
                                reload_fn: &mut || {},
                            },
                        );
                    }
                } else {
                    ui.label("Something went wrong:");
                    ui.group(|ui| {
                        ui.label(err.to_string());
                    });
                    if let Some(retry_fn) = &mut self.reload_fn {
                        if ui.button("Retry").clicked() {
                            self.data = None;
                            let tx = self.inbox.sender();
                            retry_fn(Box::new(move |result| {
                                tx.send(result).ok();
                            }));
                        }
                    }
                }
            }
        }

        if clear_data {
            println!("Clearing data");
            self.data = None;
        }

        result
    }

    /// Reload the data.
    /// If this is a [Self::single_try], this does nothing.
    pub fn reload(&mut self) {
        if let Some(reload_fn) = &mut self.reload_fn {
            self.data = None;
            let tx = self.inbox.sender();
            reload_fn(Box::new(move |result| {
                tx.send(result).ok();
            }));
        }
    }

    /// Returns true if the data is loading.
    pub fn loading(&self) -> bool {
        self.data.is_none()
    }

    /// Returns true if the data failed to load.
    pub fn has_error(&self) -> bool {
        self.data.as_ref().map(|r| r.is_err()).unwrap_or(false)
    }

    /// Returns the data if it is loaded.
    pub fn data(&self) -> Option<&T> {
        self.data.as_ref().and_then(|r| r.as_ref().ok())
    }

    /// Returns the data if it is loaded.
    pub fn data_mut(&mut self) -> Option<&mut T> {
        self.data.as_mut().and_then(|r| r.as_mut().ok())
    }

    /// Returns the error if the data failed to load.
    pub fn error(&self) -> Option<&E> {
        self.data.as_ref().and_then(|r| r.as_ref().err())
    }
}