Skip to main content

dioxus_maplibre/handle/
mod.rs

1//! MapHandle - the primary API for interacting with a MapLibre map.
2
3mod controls;
4mod escape_hatch;
5mod feature_state;
6mod getters;
7mod images;
8mod layer_events;
9mod layers;
10mod markers;
11mod navigation;
12mod padding;
13mod popups;
14mod queries;
15mod sources;
16mod style;
17mod terrain_atmosphere;
18
19use crate::options::ControlPosition;
20
21/// A handle to a MapLibre map instance.
22///
23/// This is a lightweight `Clone` wrapper. Store it in a `Signal<Option<MapHandle>>`
24/// and set it in the `on_ready` callback.
25#[derive(Debug, Clone, PartialEq, Eq)]
26pub struct MapHandle {
27    map_id: String,
28}
29
30impl MapHandle {
31    /// Create a new `MapHandle` (called internally by the `Map` component).
32    #[allow(dead_code)] // Used only on wasm32 target
33    pub(crate) fn new(map_id: String) -> Self {
34        Self { map_id }
35    }
36
37    /// Get the internal map ID (useful for debugging).
38    pub fn map_id(&self) -> &str {
39        &self.map_id
40    }
41
42    /// Fire-and-forget: spawn an async eval that we don't wait for.
43    #[cfg(target_arch = "wasm32")]
44    pub(crate) fn fire_and_forget(&self, js_fn: impl FnOnce() -> String) {
45        let js = js_fn();
46        dioxus::prelude::spawn(async move {
47            let _ = document::eval(&js).await;
48        });
49    }
50
51    #[allow(clippy::unused_self)]
52    #[cfg(not(target_arch = "wasm32"))]
53    pub(crate) fn fire_and_forget(&self, _js_fn: impl FnOnce() -> String) {
54        // No-op on non-wasm targets.
55    }
56
57    /// Execute raw JS without wrapping (for escape hatch).
58    #[cfg(target_arch = "wasm32")]
59    pub(crate) fn eval_raw(&self, js: &str) {
60        let js = js.to_string();
61        dioxus::prelude::spawn(async move {
62            let _ = document::eval(&js).await;
63        });
64    }
65
66    #[allow(clippy::unused_self)]
67    #[cfg(not(target_arch = "wasm32"))]
68    pub(crate) fn eval_raw(&self, _js: &str) {}
69}
70
71#[cfg(target_arch = "wasm32")]
72use dioxus::prelude::document;
73
74pub(crate) fn control_position_str(pos: ControlPosition) -> &'static str {
75    match pos {
76        ControlPosition::TopLeft => "top-left",
77        ControlPosition::TopRight => "top-right",
78        ControlPosition::BottomLeft => "bottom-left",
79        ControlPosition::BottomRight => "bottom-right",
80    }
81}