1use crate::animation;
2use crate::assets;
3use crate::bindings::ui;
4use crate::context_menu_manager;
5use crate::event;
6use crate::frame_signal;
7use crate::viewport;
8use crate::Application;
9use std::cell::{Cell, RefCell};
10
11#[derive(Clone, Debug, Default, PartialEq)]
12pub struct ScrollEvent {
13 pub handle: u64,
14 pub offset_x: f32,
15 pub offset_y: f32,
16 pub content_width: f32,
17 pub content_height: f32,
18 pub viewport_width: f32,
19 pub viewport_height: f32,
20}
21
22#[derive(Clone, Debug, Default, PartialEq)]
23pub struct ContextMenuRequest {
24 pub handle: u64,
25 pub x: f32,
26 pub y: f32,
27}
28
29#[derive(Clone, Debug, Default, PartialEq, Eq)]
30pub struct AssetFailure {
31 pub id: u32,
32 pub error: String,
33}
34
35#[derive(Clone, Debug, Default, PartialEq)]
36pub struct AssetReady {
37 pub id: u32,
38 pub width: f32,
39 pub height: f32,
40}
41
42thread_local! {
43 static CURRENT_ROUTE: RefCell<String> = const { RefCell::new(String::new()) };
44 static LAST_SCROLL: RefCell<Option<ScrollEvent>> = const { RefCell::new(None) };
45 static LAST_CONTEXT_MENU: RefCell<Option<ContextMenuRequest>> = const { RefCell::new(None) };
46 static LAST_FONT_LOADED: Cell<Option<u32>> = const { Cell::new(None) };
47 static LAST_SVG_LOADED: RefCell<Option<AssetReady>> = const { RefCell::new(None) };
48 static LAST_SVG_FAILED: RefCell<Option<AssetFailure>> = const { RefCell::new(None) };
49 static LAST_TEXTURE_LOADED: RefCell<Option<AssetReady>> = const { RefCell::new(None) };
50 static LAST_TEXTURE_FAILED: RefCell<Option<AssetFailure>> = const { RefCell::new(None) };
51 static PERSIST_CAPTURE_COUNT: Cell<u32> = const { Cell::new(0) };
52 static PERSIST_RESTORE_COUNT: Cell<u32> = const { Cell::new(0) };
53}
54
55fn read_utf8(ptr: *const u8, len: u32) -> String {
56 if ptr.is_null() || len == 0 {
57 return String::new();
58 }
59 let bytes = unsafe { std::slice::from_raw_parts(ptr, len as usize) };
60 String::from_utf8_lossy(bytes).into_owned()
61}
62
63pub fn current_route() -> String {
64 CURRENT_ROUTE.with(|route| route.borrow().clone())
65}
66
67pub fn last_scroll_event() -> Option<ScrollEvent> {
68 LAST_SCROLL.with(|event| event.borrow().clone())
69}
70
71pub fn last_context_menu_request() -> Option<ContextMenuRequest> {
72 LAST_CONTEXT_MENU.with(|event| event.borrow().clone())
73}
74
75pub fn is_context_menu_visible() -> bool {
76 crate::controls::ContextMenu::is_active_menu_visible()
77}
78
79pub fn last_font_loaded() -> Option<u32> {
80 LAST_FONT_LOADED.with(Cell::get)
81}
82
83pub fn last_svg_loaded() -> Option<AssetReady> {
84 LAST_SVG_LOADED.with(|event| event.borrow().clone())
85}
86
87pub fn last_svg_failed() -> Option<AssetFailure> {
88 LAST_SVG_FAILED.with(|event| event.borrow().clone())
89}
90
91pub fn last_texture_loaded() -> Option<AssetReady> {
92 LAST_TEXTURE_LOADED.with(|event| event.borrow().clone())
93}
94
95pub fn last_texture_failed() -> Option<AssetFailure> {
96 LAST_TEXTURE_FAILED.with(|event| event.borrow().clone())
97}
98
99pub fn persisted_capture_count() -> u32 {
100 PERSIST_CAPTURE_COUNT.with(Cell::get)
101}
102
103pub fn persisted_restore_count() -> u32 {
104 PERSIST_RESTORE_COUNT.with(Cell::get)
105}
106
107#[cfg_attr(not(feature = "worker-runtime"), no_mangle)]
108pub extern "C" fn __fui_on_viewport_changed(width: f32, height: f32) {
109 ui::resize_window(width, height);
110 viewport::set_viewport_size(width, height);
111}
112
113#[cfg_attr(not(feature = "worker-runtime"), no_mangle)]
114pub extern "C" fn __fui_on_frame(timestamp_ms: f64) {
115 frame_signal::set_frame_time(timestamp_ms);
116 animation::tick_animations(timestamp_ms);
117}
118
119#[cfg_attr(not(feature = "worker-runtime"), no_mangle)]
120pub unsafe extern "C" fn __fui_on_route_changed(route_ptr: *const u8, route_len: u32) {
123 let route = read_utf8(route_ptr, route_len);
124 CURRENT_ROUTE.with(|slot| slot.replace(route));
125}
126
127#[cfg_attr(not(feature = "worker-runtime"), no_mangle)]
128pub extern "C" fn __fui_on_scroll(
129 handle: u64,
130 offset_x: f32,
131 offset_y: f32,
132 content_width: f32,
133 content_height: f32,
134 viewport_width: f32,
135 viewport_height: f32,
136) {
137 LAST_SCROLL.with(|slot| {
138 slot.replace(Some(ScrollEvent {
139 handle,
140 offset_x,
141 offset_y,
142 content_width,
143 content_height,
144 viewport_width,
145 viewport_height,
146 }));
147 });
148 event::dispatch_scroll(
149 crate::node::NodeHandle::from_raw(handle),
150 offset_x,
151 offset_y,
152 content_width,
153 content_height,
154 viewport_width,
155 viewport_height,
156 );
157}
158
159#[cfg_attr(not(feature = "worker-runtime"), no_mangle)]
160pub extern "C" fn __fui_can_show_context_menu(handle: u64) -> bool {
161 context_menu_manager::can_show_for_handle(handle)
162}
163
164#[cfg_attr(not(feature = "worker-runtime"), no_mangle)]
165pub extern "C" fn __fui_on_context_menu(handle: u64, x: f32, y: f32) {
166 LAST_CONTEXT_MENU.with(|slot| slot.replace(Some(ContextMenuRequest { handle, x, y })));
167 context_menu_manager::show_for_current_selection(handle, x, y);
168}
169
170#[cfg_attr(not(feature = "worker-runtime"), no_mangle)]
171pub extern "C" fn __fui_hide_active_context_menu() {
172 context_menu_manager::hide_active_menu();
173}
174
175#[cfg_attr(not(feature = "worker-runtime"), no_mangle)]
176pub extern "C" fn __fui_on_font_loaded(font_id: u32) {
177 LAST_FONT_LOADED.with(|slot| slot.set(Some(font_id)));
178 assets::on_font_loaded(font_id);
179 crate::typography::notify_font_loaded(font_id);
180 crate::text::notify_font_loaded(font_id);
181 crate::frame_scheduler::mark_needs_commit();
182}
183
184#[cfg_attr(not(feature = "worker-runtime"), no_mangle)]
185pub extern "C" fn __fui_on_svg_loaded(svg_id: u32, width: f32, height: f32) {
186 LAST_SVG_LOADED.with(|slot| {
187 slot.replace(Some(AssetReady {
188 id: svg_id,
189 width,
190 height,
191 }))
192 });
193 assets::on_svg_loaded(svg_id, width, height);
194}
195
196#[cfg_attr(not(feature = "worker-runtime"), no_mangle)]
197pub unsafe extern "C" fn __fui_on_svg_failed(svg_id: u32, error_ptr: *const u8, error_len: u32) {
200 let error = read_utf8(error_ptr, error_len);
201 LAST_SVG_FAILED.with(|slot| {
202 slot.replace(Some(AssetFailure {
203 id: svg_id,
204 error: error.clone(),
205 }))
206 });
207 assets::on_svg_failed(svg_id, error);
208}
209
210#[cfg_attr(not(feature = "worker-runtime"), no_mangle)]
211pub extern "C" fn __fui_on_texture_loaded(texture_id: u32, width: f32, height: f32) {
212 LAST_TEXTURE_LOADED.with(|slot| {
213 slot.replace(Some(AssetReady {
214 id: texture_id,
215 width,
216 height,
217 }))
218 });
219 assets::on_texture_loaded(texture_id, width, height);
220}
221
222#[cfg_attr(not(feature = "worker-runtime"), no_mangle)]
223pub unsafe extern "C" fn __fui_on_texture_failed(
226 texture_id: u32,
227 error_ptr: *const u8,
228 error_len: u32,
229) {
230 let error = read_utf8(error_ptr, error_len);
231 LAST_TEXTURE_FAILED.with(|slot| {
232 slot.replace(Some(AssetFailure {
233 id: texture_id,
234 error: error.clone(),
235 }))
236 });
237 assets::on_texture_failed(texture_id, error);
238}
239
240#[cfg_attr(not(feature = "worker-runtime"), no_mangle)]
241pub extern "C" fn __fui_capture_persisted_ui_state() {
242 PERSIST_CAPTURE_COUNT.with(|count| count.set(count.get() + 1));
243 Application::capture_persisted_ui_state();
244}
245
246#[cfg_attr(not(feature = "worker-runtime"), no_mangle)]
247pub extern "C" fn __fui_restore_persisted_ui_state() {
248 PERSIST_RESTORE_COUNT.with(|count| count.set(count.get() + 1));
249 Application::restore_persisted_ui_state();
250}