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

use egui::{Align, Label, Layout, Ui, Vec2, WidgetText};

pub use concat_idents::concat_idents;

/// Returns the size of the text in the current ui (based on the max width of the ui)
pub fn measure_text(ui: &mut Ui, text: impl Into<WidgetText>) -> Vec2 {
    // There might be a more elegant way but this is enough for now
    let res = Label::new(text).layout_in_ui(&mut ui.child_ui(
        ui.available_rect_before_wrap(),
        Layout::left_to_right(Align::Center),
    ));

    // There seem to be rounding errors in egui's text rendering
    // so we add a little bit of padding
    res.2.rect.size() + Vec2::new(0.1, 0.0)
}

/// Returns the approximate current scroll delta of the ui
pub fn current_scroll_delta(ui: &Ui) -> Vec2 {
    -ui.min_rect().min.to_vec2()
}

/// Spawns a tokio task
#[cfg(all(feature = "tokio", not(target_arch = "wasm32")))]
pub fn spawn(future: impl std::future::Future<Output = ()> + Send + 'static) {
    tokio::task::spawn(future);
}

/// Spawns a wasm_bindgen_futures task
#[cfg(all(feature = "async", target_arch = "wasm32"))]
pub fn spawn(future: impl std::future::Future<Output = ()> + 'static) {
    wasm_bindgen_futures::spawn_local(future);
}

#[cfg(all(feature = "async", not(feature = "tokio"), not(target_arch = "wasm32")))]
compile_error!("You need to enable the `tokio` feature to use this crate on native. If you need a different async runtime, please open an issue (should be easy to add).");

/// Matches the self reference of the callback fn
#[macro_export]
macro_rules! call_self_fn {
    ($path:path, &mut $self:expr, ($($arg:expr,)*)) => {
        $path($self, $($arg,)*)
    };
    ($path:path, mut $self:expr, ($($arg:expr,)*)) => {
        $path($self, $($arg,)*)
    };
    ($path:path, &$self:expr, ($($arg:expr,)*)) => {
        $path($self, $($arg,)*)
    };
    ($path:path, $self:expr, ($($arg:expr,)*)) => {
        $path($self, $($arg,)*)
    };
    ($path:path, ($($arg:expr,)*)) => {
        $path($($arg,)*)
    };
}

/// This macro generates the async fn
#[macro_export]
macro_rules! async_fn_def {
    (
        $(#[$docs:meta])*
        // Block that contains the async logic
        $body:block,
        // The name of the async fn
        $name:ident,
        // The path to the callback fn (e.g. Self::callback)
        $callback_fn_path:path,
        // The parameters of the async fn. The semicolon in front of the mutt is a hack to circumvent ambiguity
        ($($(;$mutt:ident)? $arg:ident: $typ:ty,)*)
        // The parameters of the call to the callback fn
        ($($call_args:ident,)*)
        // The generics of the async fn
        ($($gen:tt)*),
        // Return type
        ($($return_type:tt)*),
        // Self reference
        ($($callback_body_self:tt)*),
    ) => {
        // We use concat_idents to generate the name for the async fn
        $crate::concat_idents!(fn_name = $name, _async {
            $(#[$docs])*
            #[doc = concat!("This is the async version of `", stringify!($name), "`")]
            #[allow(unused_mut)]
            pub fn fn_name$($gen)*(
                $($callback_body_self)*
                $($($mutt)? $arg: $typ,)*
            ) -> $($return_type)* {
                let callback = $body;

                // Construct the call to the callback fn
                $crate::call_self_fn!{
                    $callback_fn_path,
                    $($callback_body_self)*
                    ($($call_args,)*
                    callback,)
                }
            }
        });
    };
}

/// This macro generates the callback fn
#[macro_export]
macro_rules! fn_def {
    (
        $(#[$docs:meta])*
        // Block that contains the callback function body
        $body:block,
        // The name of the callback fn
        $name:ident,
        // The parameters of the callback fn
        $($arg:ident: $typ:ty,)*
        // The generics of the callback fn
        ($($gen:tt)*),
        // The return type
        ($($return_type:tt)*),
        // The self declaration
        ($($callback_body_self:tt)*),
    ) => {
        $(#[$docs])*
        pub fn $name $($gen)*(
            $($callback_body_self)*
            $($arg: $typ,)*
        ) -> $($return_type)* {
            $body
        }
    };
}

/// This macro generates the async fn and the callback fn
#[macro_export]
macro_rules! fnify {
    (
        $(#[$docs:meta])*
        $name:ident,
        body: $body:block,
        parameters: ($($arg:ident: $typ:ty,)*),
        async_body: $async_body:block,
        async_parameters: ($(;$async_mutt:ident)? $($async_arg:ident: $async_typ:ty,)*),
        call_args: ($($call_args:ident,)*),
        generics: ($($gen:tt)*),
        async_generics: ($($async_gen:tt)*),
        return_type: ($($return_type:tt)*),
        call_prefix: ($($call_prefix:tt)*),
        callback_body_self: ($($callback_body_self:tt)*),
    ) => {
        $crate::fn_def!(
            $(#[$docs])*
            $body,
            $name,
            $($arg: $typ,)*
            ($($gen)*),
            ($($return_type)*),
            ($($callback_body_self)*),
        );

        #[cfg(feature = "async")]
        $crate::async_fn_def!(
            $(#[$docs])*
            $async_body,
            $name,
            $($call_prefix)*$name,
            ($(;$async_mutt)? $($async_arg: $async_typ,)*)
            ($($call_args,)*)
            ($($async_gen)*),
            ($($return_type)*),
            ($($callback_body_self)*),
        );
    };
}

/// This macro generates a callback based and a async version of the function
#[macro_export]
macro_rules! asyncify {
    (
        $(#[$docs:meta])*
        $name:ident,
        $callback_name:ident: (impl FnMut($callback_type:ty, $($closure_arg_name:ident: $closure_arg:ty,)*) $($bounds:tt)*),
        call_prefix: ($($call_prefix:tt)*),
        generics: ($($gen:tt)*),
        async_generics: ($($async_gen:tt)*),
        parameters: ($($arg:ident: $typ:ty,)*),
        future: $future:ty,
        return_type: ($($return_type:tt)*),
        body: |($($callback_body_self:tt)*)| $body:block,
    ) => {
        $crate::fnify!{
            $(#[$docs])*
            $name,
            body: $body,
            parameters: ($($arg: $typ,)* $callback_name: impl FnMut($($closure_arg,)* $callback_type) $($bounds)*,),
            async_body: {
                Box::new(move |$($closure_arg_name: $closure_arg,)* callback: $callback_type| {
                    let fut = future_fn($($closure_arg_name,)*);
                    $crate::spawn(async move {
                        let res = fut.await;
                        callback(res);
                    })
                })
            },
            async_parameters: ($($arg: $typ,)* ;mut future_fn: $future,),
            call_args: ($($arg,)*),
            generics: ($($gen)*),
            async_generics: ($($async_gen)*),
            return_type: ($($return_type)*),
            call_prefix: ($($call_prefix)*),
            callback_body_self: ($($callback_body_self)*),
        }
    };
    (
        $(#[$docs:meta])*
        $name:ident,
        $callback_name:ident: (impl FnOnce($callback_type:ty) $($bounds:tt)*),
        call_prefix: ($($call_prefix:tt)*),
        generics: ($($gen:tt)*),
        async_generics: ($($async_gen:tt)*),
        parameters: ($($arg:ident: $typ:ty,)*),
        future: $future:ty,
        return_type: ($($return_type:tt)*),
        body: |($($callback_body_self:tt)*)| $body:block,
    ) => {
        $crate::fnify!{
            $(#[$docs])*
            $name,
            body: $body,
            parameters: ($($arg: $typ,)* $callback_name: impl FnOnce($callback_type) $($bounds)*,),
            async_body: {
                Box::new(move |callback: $callback_type| {
                    let fut = future;
                    $crate::spawn(async move {
                        let res = fut.await;
                        callback(res);
                    })
                })
            },
            async_parameters: ($($arg: $typ,)* ;mut future: $future,),
            call_args: ($($arg,)*),
            generics: ($($gen)*),
            async_generics: ($($async_gen)*),
            return_type: ($($return_type)*),
            call_prefix: ($($call_prefix)*),
            callback_body_self: ($($callback_body_self)*),
        }
    };
}

/// Type of the callback function
pub type CallbackType<T> = Box<dyn FnOnce(T) + Send>;

#[cfg(not(target_arch = "wasm32"))]
mod sync {
    pub use Send as MaybeSend;
}
#[cfg(target_arch = "wasm32")]
mod unsync {
    pub trait MaybeSend {}

    impl<T> MaybeSend for T where T: ?Sized {}
}

#[cfg(not(target_arch = "wasm32"))]
pub use sync::*;

#[cfg(target_arch = "wasm32")]
pub use unsync::*;