makepad_platform/
app_main.rs

1
2use crate::event::Event;
3use crate::cx::Cx;
4
5pub trait AppMain{
6    fn handle_event(&mut self, cx: &mut Cx, event: &Event);
7}
8
9#[macro_export]
10macro_rules!app_main {
11    ( $ app: ident) => {
12        #[cfg(not(any(target_arch = "wasm32", target_os="android")))]
13        pub fn app_main() {
14            if Cx::pre_start(){
15                return
16            }
17            let app = std::rc::Rc::new(std::cell::RefCell::new(None));
18            let mut cx = std::rc::Rc::new(std::cell::RefCell::new(Cx::new(Box::new(move | cx, event | {
19                if let Event::Construct = event {
20                    *app.borrow_mut() = Some($app::new_main(cx));
21                }
22                if let Event::LiveEdit = event{
23                    app.borrow_mut().update_main(cx);
24                }
25                <dyn AppMain>::handle_event(app.borrow_mut().as_mut().unwrap(), cx, event);
26            }))));
27            live_design(&mut *cx.borrow_mut());
28            cx.borrow_mut().init_cx_os();
29            Cx::event_loop(cx);
30        }
31        
32        /*
33        #[cfg(target_os = "android")]
34        #[no_mangle]
35        pub unsafe extern "C" fn Java_dev_makepad_android_Makepad_onNewCx(_: *const std::ffi::c_void, _: *const std::ffi::c_void) -> i64 {
36            Cx::android_entry(||{
37                let app = std::rc::Rc::new(std::cell::RefCell::new(None));
38                let mut cx = Box::new(Cx::new(Box::new(move | cx, event | {
39                    if let Event::Construct = event {
40                        *app.borrow_mut() = Some($app::new_main(cx));
41                    }
42                    if let Event::LiveEdit = event{
43                        app.borrow_mut().update_main(cx);
44                    }
45                    app.borrow_mut().as_mut().unwrap().handle_event(cx, event);
46                })));
47                live_design(&mut cx);
48                cx.init_cx_os();
49                cx
50            })
51        }*/
52
53        
54        #[cfg(target_os = "android")]
55        #[no_mangle]
56        pub unsafe extern "C" fn Java_dev_makepad_android_MakepadNative_activityOnCreate(
57            _: *const std::ffi::c_void,
58            _: *const std::ffi::c_void,
59            activity: *const std::ffi::c_void,
60        ) {
61            Cx::android_entry(activity, ||{
62                let app = std::rc::Rc::new(std::cell::RefCell::new(None));
63                let mut cx = Box::new(Cx::new(Box::new(move | cx, event | {
64                    if let Event::Construct = event {
65                        *app.borrow_mut() = Some($app::new_main(cx));
66                    }
67                    if let Event::LiveEdit = event{
68                        app.borrow_mut().update_main(cx);
69                    }
70                    app.borrow_mut().as_mut().unwrap().handle_event(cx, event);
71                })));
72                live_design(&mut cx);
73                cx.init_cx_os();
74                cx
75            })
76        }
77        
78        #[cfg(target_arch = "wasm32")]
79        pub fn app_main() {}
80        
81        #[export_name = "wasm_create_app"]
82        #[cfg(target_arch = "wasm32")]
83        pub extern "C" fn create_wasm_app() -> u32 {
84            
85            let app = std::rc::Rc::new(std::cell::RefCell::new(None));
86            let mut cx = Box::new(Cx::new(Box::new(move | cx, event | {
87                if let Event::Construct = event {
88                    *app.borrow_mut() = Some($app::new_main(cx));
89                }
90                if let Event::LiveEdit = event{
91                    app.borrow_mut().update_main(cx);
92                }
93                app.borrow_mut().as_mut().unwrap().handle_event(cx, event);
94            })));
95            
96            live_design(&mut cx);
97            cx.init_cx_os();
98            Box::into_raw(cx) as u32
99        }
100
101        #[export_name = "wasm_process_msg"]
102        #[cfg(target_arch = "wasm32")]
103        pub unsafe extern "C" fn wasm_process_msg(msg_ptr: u32, cx_ptr: u32) -> u32 {
104            let cx = &mut *(cx_ptr as *mut Cx);
105            cx.process_to_wasm(msg_ptr)
106        }
107    }
108}
109