easy_imgui_window/window/main_window/
fut.rs

1use super::*;
2use easy_imgui::future::{Action, FutureBackCallerImpl, IdleRunner};
3
4pub struct MyIdleRunner<A: Application>(pub EventLoopProxy<AppEvent<A>>);
5
6impl<A: Application> IdleRunner for MyIdleRunner<A> {
7    fn idle_run(&self, f: Action) -> Result<(), Action> {
8        self.0
9            .send_event(AppEvent::RunIdleSimple(f))
10            .map_err(|e| match e.0 {
11                AppEvent::RunIdleSimple(a) => a,
12                _ => unreachable!(),
13            })
14    }
15}
16
17type MyFutureBackCallerImpl<'a, A> = FutureBackCallerImpl<(&'a mut A, Args<'a, A>)>;
18
19// Fake static lifetimes, should be raw pointers, but the Args<> doesn't play nice with those.
20pub struct FutureBackCaller<A: Application>(MyFutureBackCallerImpl<'static, A>);
21
22struct IdType<A>(A);
23
24impl<A: Application> Default for FutureBackCaller<A> {
25    fn default() -> Self {
26        Self::new()
27    }
28}
29
30impl<A: Application> FutureBackCaller<A> {
31    pub fn new() -> Self {
32        Self(MyFutureBackCallerImpl::<A>::default())
33    }
34
35    pub fn prepare(app: &mut A, args: Args<'_, A>, f: impl FnOnce()) {
36        MyFutureBackCallerImpl::<'_, A>::prepare::<IdType<A>>((app, args), f);
37    }
38
39    pub fn run<F, R>(&self, f: F) -> Option<R>
40    where
41        F: FnOnce(&mut A, Args<'_, A>) -> R,
42    {
43        unsafe {
44            self.0.run::<IdType<A>, _, _>(move |t| {
45                let (app, args) = t;
46                f(app, args.reborrow())
47            })
48        }
49    }
50}