euv_ui/component/layout/hook/
impl.rs1use super::*;
2
3impl UseEuvLayout {
7 pub fn use_resize() -> Signal<bool> {
18 let mobile_signal: Signal<bool> = App::use_signal(Router::is_mobile);
19 let timer_signal: Signal<Option<i32>> = App::use_signal(|| None);
20 let debounce_closure: Closure<dyn FnMut()> = Closure::wrap(Box::new(move || {
21 let mobile: bool = Router::is_mobile();
22 mobile_signal.set(mobile);
23 }));
24 let debounce_callback: Function = debounce_closure
25 .as_ref()
26 .unchecked_ref::<Function>()
27 .clone();
28 debounce_closure.forget();
29 let Some(timeout_window) = window() else {
30 return mobile_signal;
31 };
32 App::use_window_event("resize", move || {
33 let old_timer: Option<i32> = timer_signal.get();
34 if let Some(timer_id) = old_timer {
35 timeout_window.clear_timeout_with_handle(timer_id);
36 }
37 let new_timer: i32 = timeout_window
38 .set_timeout_with_callback_and_timeout_and_arguments_0(
39 &debounce_callback,
40 RESIZE_DEBOUNCE_MILLIS,
41 )
42 .unwrap_or_default();
43 timer_signal.set(Some(new_timer));
44 });
45 mobile_signal
46 }
47
48 pub fn use_drawer_toggle(drawer_open: Signal<bool>) -> Option<Rc<dyn Fn(Event)>> {
64 Some(Rc::new(move |_: Event| {
65 let is_open: bool = drawer_open.get();
66 if is_open {
67 Router::overlay_stack_close();
68 }
69 drawer_open.set(!is_open);
70 }))
71 }
72
73 pub fn use_safe_area_fix() {
99 Self::cache_safe_area_insets();
100 Self::init_immersive_safe_area();
101 App::use_window_event("fullscreenchange", || {
102 let Some(window_value) = window() else {
103 return;
104 };
105 let Some(document_value) = window_value.document() else {
106 return;
107 };
108 let is_fullscreen: bool = document_value.fullscreen_element().is_some();
109 if is_fullscreen {
110 NATIVE_FULLSCREEN_ACTIVE.with(|flag: &Cell<bool>| flag.set(true));
111 Router::overlay_push_state();
112 } else {
113 let was_active: bool =
114 NATIVE_FULLSCREEN_ACTIVE.with(|flag: &Cell<bool>| flag.get());
115 if was_active {
116 NATIVE_FULLSCREEN_ACTIVE.with(|flag: &Cell<bool>| flag.set(false));
117 let exit_by_popstate: bool =
118 NATIVE_FULLSCREEN_EXIT_BY_POPSTATE.with(|flag: &Cell<bool>| flag.get());
119 if exit_by_popstate {
120 NATIVE_FULLSCREEN_EXIT_BY_POPSTATE
121 .with(|flag: &Cell<bool>| flag.set(false));
122 } else {
123 Router::overlay_back(None);
124 }
125 }
126 Self::apply_cached_insets();
127 }
128 });
129 App::use_window_event("webkitfullscreenchange", || {
130 Self::apply_cached_insets();
131 });
132 App::use_window_event("resize", || {
133 Self::apply_cached_insets();
134 });
135 Router::register_popstate_guard(Rc::new(|| {
136 if !NATIVE_FULLSCREEN_ACTIVE.with(|flag: &Cell<bool>| flag.get()) {
137 return false;
138 }
139 NATIVE_FULLSCREEN_EXIT_BY_POPSTATE.with(|flag: &Cell<bool>| flag.set(true));
140 let Some(window_value) = window() else {
141 return false;
142 };
143 let Some(document_value) = window_value.document() else {
144 return false;
145 };
146 document_value.exit_fullscreen();
147 true
148 }));
149 }
150
151 fn init_immersive_safe_area() {
168 if !Self::is_immersive_declared() {
169 return;
170 }
171 let top_value: String =
172 SAFE_AREA_INSET_TOP.with(|cell: &RefCell<String>| cell.borrow().clone());
173 if top_value.is_empty() {
174 return;
175 }
176 let Some(window_value) = window() else {
177 return;
178 };
179 let Some(document_value) = window_value.document() else {
180 return;
181 };
182 let Some(root) = document_value.document_element() else {
183 return;
184 };
185 let root_element: HtmlElement = root.unchecked_into();
186 let _: Result<(), JsValue> = root_element
187 .style()
188 .set_property("--euv-mobile-safe-top", &top_value);
189 }
190
191 fn is_immersive_declared() -> bool {
199 let Some(window_value) = window() else {
200 return false;
201 };
202 let global_flag: bool =
203 js_sys::Reflect::get(&window_value, &JsValue::from_str("__EUV_IMMERSIVE__"))
204 .ok()
205 .and_then(|value: JsValue| value.as_bool())
206 .unwrap_or(false);
207 if global_flag {
208 return true;
209 }
210 window_value
211 .document()
212 .and_then(|document_value: Document| {
213 document_value
214 .query_selector(r#"meta[name="euv-immersive"]"#)
215 .ok()
216 .flatten()
217 })
218 .and_then(|meta: Element| meta.get_attribute("content"))
219 .map(|content: String| content == "true")
220 .unwrap_or(false)
221 }
222
223 fn cache_safe_area_insets() {
235 let top_cached: String =
236 SAFE_AREA_INSET_TOP.with(|cell: &RefCell<String>| cell.borrow().clone());
237 if !top_cached.is_empty() {
238 return;
239 }
240 let Some(win) = window() else {
241 return;
242 };
243 let Some(document_value) = win.document() else {
244 return;
245 };
246 let Some(body) = document_value.body() else {
247 return;
248 };
249 let Ok(created_element) = document_value.create_element("div") else {
250 return;
251 };
252 let sentinel: HtmlElement = created_element.unchecked_into();
253 let _: Result<(), JsValue> = sentinel.style().set_property("position", "absolute");
254 let _: Result<(), JsValue> = sentinel.style().set_property("visibility", "hidden");
255 let _: Result<(), JsValue> = sentinel.style().set_property("pointer-events", "none");
256 let _: Result<(), JsValue> = sentinel
257 .style()
258 .set_property("padding-top", "env(safe-area-inset-top, 0px)");
259 let _: Result<(), JsValue> = sentinel
260 .style()
261 .set_property("padding-right", "env(safe-area-inset-right, 0px)");
262 let _: Result<(), JsValue> = sentinel
263 .style()
264 .set_property("padding-bottom", "env(safe-area-inset-bottom, 0px)");
265 let _: Result<(), JsValue> = sentinel
266 .style()
267 .set_property("padding-left", "env(safe-area-inset-left, 0px)");
268 let _: Result<Node, JsValue> = body.append_child(&sentinel);
269 let Some(computed) = win.get_computed_style(&sentinel).ok().flatten() else {
270 let _: Result<Node, JsValue> = body.remove_child(&sentinel);
271 return;
272 };
273 let top_value: String = computed
274 .get_property_value("padding-top")
275 .unwrap_or_default();
276 let right_value: String = computed
277 .get_property_value("padding-right")
278 .unwrap_or_default();
279 let bottom_value: String = computed
280 .get_property_value("padding-bottom")
281 .unwrap_or_default();
282 let left_value: String = computed
283 .get_property_value("padding-left")
284 .unwrap_or_default();
285 let _: Result<Node, JsValue> = body.remove_child(&sentinel);
286 if top_value.is_empty() || top_value == "0px" {
287 return;
288 }
289 SAFE_AREA_INSET_TOP.with(|cell: &RefCell<String>| *cell.borrow_mut() = top_value);
290 SAFE_AREA_INSET_RIGHT.with(|cell: &RefCell<String>| *cell.borrow_mut() = right_value);
291 SAFE_AREA_INSET_BOTTOM.with(|cell: &RefCell<String>| *cell.borrow_mut() = bottom_value);
292 SAFE_AREA_INSET_LEFT.with(|cell: &RefCell<String>| *cell.borrow_mut() = left_value);
293 }
294
295 pub fn apply_cached_insets() {
311 let top_value: String =
312 SAFE_AREA_INSET_TOP.with(|cell: &RefCell<String>| cell.borrow().clone());
313 if top_value.is_empty() {
314 return;
315 }
316 let right_value: String =
317 SAFE_AREA_INSET_RIGHT.with(|cell: &RefCell<String>| cell.borrow().clone());
318 let bottom_value: String =
319 SAFE_AREA_INSET_BOTTOM.with(|cell: &RefCell<String>| cell.borrow().clone());
320 let left_value: String =
321 SAFE_AREA_INSET_LEFT.with(|cell: &RefCell<String>| cell.borrow().clone());
322 let Some(window_value) = window() else {
323 return;
324 };
325 let Some(document_value) = window_value.document() else {
326 return;
327 };
328 let apply_to = |element: &HtmlElement| {
329 let _: Result<(), JsValue> = element
330 .style()
331 .set_property("--safe-area-inset-top", &top_value);
332 let _: Result<(), JsValue> = element
333 .style()
334 .set_property("--safe-area-inset-right", &right_value);
335 let _: Result<(), JsValue> = element
336 .style()
337 .set_property("--safe-area-inset-bottom", &bottom_value);
338 let _: Result<(), JsValue> = element
339 .style()
340 .set_property("--safe-area-inset-left", &left_value);
341 };
342 if let Some(app_root) = document_value
343 .query_selector(".c_mobile_app_root")
344 .ok()
345 .flatten()
346 .map(|element: Element| element.unchecked_into::<HtmlElement>())
347 .or_else(|| {
348 document_value
349 .query_selector(".c_app_root")
350 .ok()
351 .flatten()
352 .map(|element: Element| element.unchecked_into::<HtmlElement>())
353 })
354 {
355 apply_to(&app_root);
356 }
357 if let Some(canvas_fullscreen) = document_value
358 .query_selector(".c_canvas_container_fullscreen")
359 .ok()
360 .flatten()
361 .map(|element: Element| element.unchecked_into::<HtmlElement>())
362 {
363 apply_to(&canvas_fullscreen);
364 }
365 if let Some(game_fullscreen) = document_value
366 .query_selector(".c_game_container_fullscreen")
367 .ok()
368 .flatten()
369 .map(|element: Element| element.unchecked_into::<HtmlElement>())
370 {
371 apply_to(&game_fullscreen);
372 }
373 }
374}