1 | // Copyright © SixtyFPS GmbH <info@slint.dev> |
2 | // SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-1.1 OR LicenseRef-Slint-commercial |
3 | |
4 | mod interpreter; |
5 | pub use interpreter::*; |
6 | |
7 | mod types; |
8 | pub use types::*; |
9 | |
10 | use napi::{Env, JsFunction}; |
11 | |
12 | #[macro_use ] |
13 | extern crate napi_derive; |
14 | |
15 | #[napi ] |
16 | pub fn mock_elapsed_time(ms: f64) { |
17 | i_slint_core::tests::slint_mock_elapsed_time(time_in_ms:ms as _); |
18 | } |
19 | |
20 | #[napi ] |
21 | pub fn get_mocked_time() -> f64 { |
22 | i_slint_core::tests::slint_get_mocked_time() as f64 |
23 | } |
24 | |
25 | #[napi ] |
26 | pub enum ProcessEventsResult { |
27 | Continue, |
28 | Exited, |
29 | } |
30 | |
31 | #[napi ] |
32 | pub fn process_events() -> napi::Result<ProcessEventsResult> { |
33 | i_slint_backend_selector::with_platform(|b| { |
34 | b.process_events(std::time::Duration::ZERO, i_slint_core::InternalToken) |
35 | }) |
36 | .map_err(|e| napi::Error::from_reason(e.to_string())) |
37 | .and_then(|result: ControlFlow<()>| { |
38 | Ok(match result { |
39 | core::ops::ControlFlow::Continue(()) => ProcessEventsResult::Continue, |
40 | core::ops::ControlFlow::Break(()) => ProcessEventsResult::Exited, |
41 | }) |
42 | }) |
43 | } |
44 | |
45 | #[napi ] |
46 | pub fn invoke_from_event_loop(env: Env, callback: JsFunction) -> napi::Result<napi::JsUndefined> { |
47 | i_slint_backend_selector::with_platform(|_b| { |
48 | // Nothing to do, just make sure a backend was created |
49 | Ok(()) |
50 | }) |
51 | .map_err(|e: PlatformError| napi::Error::from_reason(e.to_string()))?; |
52 | |
53 | let function_ref: RefCountedReference = RefCountedReference::new(&env, value:callback)?; |
54 | let function_ref: SendWrapper = send_wrapper::SendWrapper::new(data:function_ref); |
55 | i_slint_core::api::invoke_from_event_loop(move || { |
56 | let function_ref = function_ref.take(); |
57 | let callback: JsFunction = function_ref.get().unwrap(); |
58 | callback.call_without_args(None).ok(); |
59 | }) |
60 | .map_err(|e| napi::Error::from_reason(e.to_string())) |
61 | .and_then(|_| env.get_undefined()) |
62 | } |
63 | |
64 | #[napi ] |
65 | pub fn set_quit_on_last_window_closed( |
66 | env: Env, |
67 | quit_on_last_window_closed: bool, |
68 | ) -> napi::Result<napi::JsUndefined> { |
69 | if !quit_on_last_window_closed { |
70 | i_slint_backend_selector::with_platform(|b| { |
71 | #[allow (deprecated)] |
72 | b.set_event_loop_quit_on_last_window_closed(false); |
73 | Ok(()) |
74 | }) |
75 | .map_err(|e: PlatformError| napi::Error::from_reason(e.to_string()))?; |
76 | } |
77 | env.get_undefined() |
78 | } |
79 | |