makepad_platform/
app_main.rs

1
2use crate::event::Event;
3use crate::cx::Cx;
4use crate::ui_runner::UiRunner;
5
6#[cfg(target_env = "ohos")]
7pub use napi_ohos;
8
9
10pub trait AppMain{
11    fn handle_event(&mut self, cx: &mut Cx, event: &Event);
12    fn ui_runner(&self) -> UiRunner<Self> where Self: Sized + 'static {
13        // This assumes there is only one `AppMain`, and that `0` is reserved for it.
14        UiRunner::new(0)
15    }
16}
17
18#[macro_export]
19macro_rules!app_main {
20    ( $ app: ident) => {
21        #[cfg(not(any(target_arch = "wasm32", target_os="android", target_env="ohos")))]
22        pub fn app_main() {
23            if Cx::pre_start(){
24                return
25            }
26                        
27            let app = std::rc::Rc::new(std::cell::RefCell::new(None));
28            let mut cx = std::rc::Rc::new(std::cell::RefCell::new(Cx::new(Box::new(move | cx, event | {
29                
30                if let Event::Startup = event {
31                    *app.borrow_mut() = $app::new_main(cx);
32                }
33                if let Event::LiveEdit = event{
34                    app.borrow_mut().update_main(cx);
35                }
36                if let Some(app) = &mut *app.borrow_mut(){
37                    <dyn AppMain>::handle_event(app, cx, event);
38                }
39                  
40            }))));
41            $app::register_main_module(&mut *cx.borrow_mut());
42            cx.borrow_mut().init_websockets(std::option_env!("MAKEPAD_STUDIO_HTTP").unwrap_or(""));
43            if std::env::args().find( | v | v == "--stdin-loop").is_some() {
44                cx.borrow_mut().in_makepad_studio = true;
45            }
46            //cx.borrow_mut().init_websockets("");
47            live_design(&mut *cx.borrow_mut());
48            cx.borrow_mut().init_cx_os();
49            Cx::event_loop(cx);
50        }
51        
52        /*
53        #[cfg(target_os = "android")]
54        #[no_mangle]
55        pub unsafe extern "C" fn Java_dev_makepad_android_Makepad_onNewCx(_: *const std::ffi::c_void, _: *const std::ffi::c_void) -> i64 {
56            Cx::android_entry(||{
57                let app = std::rc::Rc::new(std::cell::RefCell::new(None));
58                let mut cx = Box::new(Cx::new(Box::new(move | cx, event | {
59                    if let Event::Construct = event {
60                        *app.borrow_mut() = Some($app::new_main(cx));
61                    }
62                    if let Event::LiveEdit = event{
63                        app.borrow_mut().update_main(cx);
64                    }
65                    app.borrow_mut().as_mut().unwrap().handle_event(cx, event);
66                })));
67                live_design(&mut cx);
68                cx.init_cx_os();
69                cx
70            })
71        }*/
72
73        
74        #[cfg(target_os = "android")]
75        #[no_mangle]
76        pub unsafe extern "C" fn Java_dev_makepad_android_MakepadNative_activityOnCreate(
77            _: *const std::ffi::c_void,
78            _: *const std::ffi::c_void,
79            activity: *const std::ffi::c_void,
80        ) {
81            Cx::android_entry(activity, ||{
82                let app = std::rc::Rc::new(std::cell::RefCell::new(None));
83                let mut cx = Box::new(Cx::new(Box::new(move | cx, event | {
84                    if let Event::Startup = event {
85                        *app.borrow_mut() = $app::new_main(cx);
86                    }
87                    if let Event::LiveEdit = event{
88                        app.borrow_mut().update_main(cx);
89                    }
90                    if let Some(app) = &mut *app.borrow_mut(){
91                        app.handle_event(cx, event);
92                    }
93                })));
94                $app::register_main_module(&mut cx);
95                cx.init_websockets(std::option_env!("MAKEPAD_STUDIO_HTTP").unwrap_or(""));
96                live_design(&mut cx);
97                cx.init_cx_os();
98                cx
99            })
100        }
101
102        #[cfg(target_env = "ohos")]
103        #[no_mangle]
104        extern "C" fn ohos_init_app_main(exports: $crate::napi_ohos::JsObject, env: $crate::napi_ohos::Env) -> $crate::napi_ohos::Result<()> {
105            Cx::ohos_init(exports, env, || {
106                let app = std::rc::Rc::new(std::cell::RefCell::new(None));
107                let mut cx = Box::new(Cx::new(Box::new(move | cx, event | {
108                    if let Event::Startup = event {
109                        *app.borrow_mut() = $app::new_main(cx);
110                    }
111                    if let Event::LiveEdit = event{
112                        app.borrow_mut().update_main(cx);
113                    }
114                    if let Some(app) = &mut *app.borrow_mut(){
115                        app.handle_event(cx, event);
116                    }
117                })));
118                $app::register_main_module(&mut cx);
119                cx.init_websockets(std::option_env!("MAKEPAD_STUDIO_HTTP").unwrap_or(""));
120                live_design(&mut cx);
121                cx.init_cx_os();
122                cx
123            });
124            Ok(())
125        }
126        
127        #[cfg(target_arch = "wasm32")]
128        pub fn app_main() {}
129        
130        #[export_name = "wasm_create_app"]
131        #[cfg(target_arch = "wasm32")]
132        pub extern "C" fn create_wasm_app() -> u32 {
133            
134            let app = std::rc::Rc::new(std::cell::RefCell::new(None));
135            let mut cx = Box::new(Cx::new(Box::new(move | cx, event | {
136                if let Event::Startup = event {
137                    *app.borrow_mut() = $app::new_main(cx);
138                }
139                if let Event::LiveEdit = event{
140                    app.borrow_mut().update_main(cx);
141                }
142                if let Some(app) = &mut *app.borrow_mut(){
143                    app.handle_event(cx, event);
144                }
145            })));
146            $app::register_main_module(&mut cx);
147            cx.init_websockets(std::option_env!("MAKEPAD_STUDIO_HTTP").unwrap_or(""));
148            live_design(&mut cx);
149            cx.init_cx_os();
150            Box::into_raw(cx) as u32
151        }
152        
153                
154        #[export_name = "wasm_process_msg"]
155        #[cfg(target_arch = "wasm32")]
156        pub unsafe extern "C" fn wasm_process_msg(msg_ptr: u32, cx_ptr: u32) -> u32 {
157            let cx = &mut *(cx_ptr as *mut Cx);
158            cx.process_to_wasm(msg_ptr)
159        }
160        
161        #[export_name = "wasm_return_first_msg"]
162        #[cfg(target_arch = "wasm32")]
163        pub unsafe extern "C" fn wasm_return_first_msg(cx_ptr: u32) -> u32 {
164            let cx = &mut *(cx_ptr as *mut Cx); 
165            cx.os.from_wasm.take().unwrap().release_ownership()
166        }
167        
168    }
169}
170
171#[cfg(target_env = "ohos")]
172#[napi_derive_ohos::module_exports]
173fn init(exports: napi_ohos::JsObject, env: napi_ohos::Env) -> napi_ohos::Result<()> {
174    #[allow(improper_ctypes)]
175    extern "C" {
176        fn ohos_init_app_main(exports: napi_ohos::JsObject, env: napi_ohos::Env) -> napi_ohos::Result<()>;
177    }
178    unsafe { ohos_init_app_main(exports, env) }
179}