1#![warn(missing_docs)]
6#![allow(unsafe_code)]
7
8use crate::api::LogicalPosition;
9use crate::input::key_codes::Key;
10use crate::platform::WindowEvent;
11
12#[unsafe(no_mangle)]
17pub extern "C" fn slint_mock_elapsed_time(time_in_ms: u64) {
18 let tick = crate::animations::CURRENT_ANIMATION_DRIVER.with(|driver| {
19 let mut tick = driver.current_tick();
20 tick += core::time::Duration::from_millis(time_in_ms);
21 driver.update_animations(tick);
22 tick
23 });
24 crate::timers::TimerList::maybe_activate_timers(tick);
25 crate::properties::ChangeTracker::run_change_handlers();
26}
27
28#[unsafe(no_mangle)]
30pub extern "C" fn slint_get_mocked_time() -> u64 {
31 crate::animations::CURRENT_ANIMATION_DRIVER.with(|driver| driver.current_tick()).as_millis()
32}
33
34#[unsafe(no_mangle)]
37pub extern "C" fn slint_send_mouse_click(
38 x: f32,
39 y: f32,
40 window_adapter: &crate::window::WindowAdapterRc,
41) {
42 let position = LogicalPosition::new(x, y);
43 let button = crate::items::PointerEventButton::Left;
44
45 window_adapter.window().dispatch_event(WindowEvent::PointerMoved { position });
46 window_adapter.window().dispatch_event(WindowEvent::PointerPressed { position, button });
47 slint_mock_elapsed_time(50);
48 window_adapter.window().dispatch_event(WindowEvent::PointerReleased { position, button });
49}
50
51#[unsafe(no_mangle)]
57pub extern "C" fn slint_send_keyboard_key_text(
58 text: &crate::SharedString,
59 pressed: bool,
60 window_adapter: &crate::window::WindowAdapterRc,
61) {
62 window_adapter.window().dispatch_event(if pressed {
63 WindowEvent::KeyPressed { text: text.clone() }
64 } else {
65 WindowEvent::KeyReleased { text: text.clone() }
66 })
67}
68
69#[unsafe(no_mangle)]
75pub extern "C" fn slint_send_keyboard_char(
76 string: &crate::SharedString,
77 pressed: bool,
78 window_adapter: &crate::window::WindowAdapterRc,
79) {
80 for ch in string.chars() {
81 slint_send_keyboard_key_text(&ch.into(), pressed, window_adapter);
82 }
83}
84
85#[unsafe(no_mangle)]
87pub extern "C" fn send_keyboard_string_sequence(
88 sequence: &crate::SharedString,
89 window_adapter: &crate::window::WindowAdapterRc,
90) {
91 for ch in sequence.chars() {
92 if ch.is_ascii_uppercase() {
93 window_adapter
94 .window()
95 .dispatch_event(WindowEvent::KeyPressed { text: Key::Shift.into() });
96 }
97
98 let text: crate::SharedString = ch.into();
99 window_adapter.window().dispatch_event(WindowEvent::KeyPressed { text: text.clone() });
100 window_adapter.window().dispatch_event(WindowEvent::KeyReleased { text });
101
102 if ch.is_ascii_uppercase() {
103 window_adapter
104 .window()
105 .dispatch_event(WindowEvent::KeyReleased { text: Key::Shift.into() });
106 }
107 }
108}
109
110#[doc(hidden)]
112pub fn debug_log_impl(args: core::fmt::Arguments) {
113 crate::context::GLOBAL_CONTEXT.with(|p| match p.get() {
114 Some(ctx) => ctx.platform().debug_log(args),
115 None => default_debug_log(args),
116 });
117}
118
119#[doc(hidden)]
120pub fn default_debug_log(_arguments: core::fmt::Arguments) {
121 cfg_if::cfg_if! {
122 if #[cfg(target_arch = "wasm32")] {
123 use wasm_bindgen::prelude::*;
124 use std::string::ToString;
125
126 #[wasm_bindgen]
127 extern "C" {
128 #[wasm_bindgen(js_namespace = console)]
129 pub fn log(s: &str);
130 }
131
132 log(&_arguments.to_string());
133 } else if #[cfg(feature = "std")] {
134 use std::io::Write;
135 let _ = writeln!(std::io::stderr(), "{_arguments}");
140 }
141 }
142}
143
144#[macro_export]
145macro_rules! debug_log {
148 ($($t:tt)*) => ($crate::tests::debug_log_impl(format_args!($($t)*)))
149}