Skip to main content

dioxus_maplibre/
events.rs

1//! Event types for map interactions
2
3use crate::types::{Bounds, LatLng, Point};
4use serde::{Deserialize, Serialize};
5
6/// Event fired when the map is clicked
7#[derive(Debug, Clone, Serialize, Deserialize)]
8pub struct MapClickEvent {
9    /// Geographic coordinates of the click
10    pub latlng: LatLng,
11    /// Screen pixel coordinates of the click
12    pub point: Point,
13}
14
15/// Event fired when the map is double-clicked
16#[derive(Debug, Clone, Serialize, Deserialize)]
17pub struct MapDblClickEvent {
18    /// Geographic coordinates of the double-click
19    pub latlng: LatLng,
20    /// Screen pixel coordinates
21    pub point: Point,
22}
23
24/// Event fired on right-click / context menu
25#[derive(Debug, Clone, Serialize, Deserialize)]
26pub struct MapContextMenuEvent {
27    /// Geographic coordinates
28    pub latlng: LatLng,
29    /// Screen pixel coordinates
30    pub point: Point,
31}
32
33/// Event fired when a marker is clicked
34#[derive(Debug, Clone, Serialize, Deserialize)]
35pub struct MarkerClickEvent {
36    /// Marker ID
37    pub marker_id: String,
38    /// Geographic coordinates of the marker
39    pub latlng: LatLng,
40}
41
42/// Event fired when hovering over a marker
43#[derive(Debug, Clone, Serialize, Deserialize)]
44pub struct MarkerHoverEvent {
45    /// Marker ID
46    pub marker_id: String,
47    /// Geographic coordinates of the marker
48    pub latlng: LatLng,
49    /// Whether the mouse is entering (true) or leaving (false)
50    pub hover: bool,
51    /// Mouse cursor X position (viewport pixels)
52    pub cursor_x: f64,
53    /// Mouse cursor Y position (viewport pixels)
54    pub cursor_y: f64,
55}
56
57/// Event fired when a draggable marker starts being dragged
58#[derive(Debug, Clone, Serialize, Deserialize)]
59pub struct MarkerDragStartEvent {
60    /// Marker ID
61    pub marker_id: String,
62    /// Geographic coordinates when drag started
63    pub latlng: LatLng,
64}
65
66/// Event fired when a draggable marker is dropped after dragging
67#[derive(Debug, Clone, Serialize, Deserialize)]
68pub struct MarkerDragEndEvent {
69    /// Marker ID
70    pub marker_id: String,
71    /// Geographic coordinates where the marker was dropped
72    pub latlng: LatLng,
73}
74
75/// Event fired when the map view changes (pan/zoom)
76#[derive(Debug, Clone, Serialize, Deserialize)]
77pub struct MapMoveEvent {
78    /// New center position
79    pub center: LatLng,
80    /// New zoom level
81    pub zoom: f64,
82    /// Current viewport bounds (sw/ne corners)
83    #[serde(default)]
84    pub bounds: Option<Bounds>,
85    /// Move phase from bridge (`move`, `moveend`, `move_load`)
86    #[serde(default)]
87    pub phase: Option<String>,
88}
89
90/// Event fired when the zoom level changes
91#[derive(Debug, Clone, Serialize, Deserialize)]
92pub struct MapZoomEvent {
93    /// New zoom level
94    pub zoom: f64,
95}
96
97/// Event fired when the bearing (rotation) changes
98#[derive(Debug, Clone, Serialize, Deserialize)]
99pub struct MapRotateEvent {
100    /// New bearing in degrees
101    pub bearing: f64,
102}
103
104/// Event fired when the pitch changes
105#[derive(Debug, Clone, Serialize, Deserialize)]
106pub struct MapPitchEvent {
107    /// New pitch in degrees
108    pub pitch: f64,
109}
110
111/// Event fired when a feature in a layer is clicked
112#[derive(Debug, Clone, Serialize, Deserialize)]
113pub struct LayerClickEvent {
114    /// Layer ID where the click occurred
115    pub layer_id: String,
116    /// GeoJSON feature ID (numeric, if present)
117    pub feature_id: Option<i64>,
118    /// Feature properties from GeoJSON
119    pub properties: serde_json::Value,
120    /// Geographic coordinates of the click
121    pub latlng: LatLng,
122}
123
124/// Event fired when hovering over a feature in a layer
125#[derive(Debug, Clone, Serialize, Deserialize)]
126pub struct LayerHoverEvent {
127    /// Layer ID where the hover occurred
128    pub layer_id: String,
129    /// GeoJSON feature ID (numeric, if present). None when mouse leaves.
130    pub feature_id: Option<i64>,
131    /// Feature properties from GeoJSON. None when mouse leaves.
132    pub properties: Option<serde_json::Value>,
133    /// Geographic coordinates
134    pub latlng: LatLng,
135    /// Cursor X position (screen coordinates)
136    pub cursor_x: f64,
137    /// Cursor Y position (screen coordinates)
138    pub cursor_y: f64,
139}
140
141/// Event fired when map initialization succeeds
142#[derive(Debug, Clone, Serialize, Deserialize)]
143pub struct MapReadyEvent;
144
145/// Event fired when bridge reports an initialization/runtime error
146#[derive(Debug, Clone, Serialize, Deserialize)]
147pub struct MapErrorEvent {
148    /// Optional message sent from the JS bridge
149    #[serde(default)]
150    pub message: Option<String>,
151}
152
153/// Internal event enum for communication from JS
154#[derive(Debug, Clone, Serialize, Deserialize)]
155#[serde(tag = "type")]
156#[allow(dead_code)]
157pub enum MapEvent {
158    #[serde(rename = "ready")]
159    Ready,
160    #[serde(rename = "error")]
161    Error(MapErrorEvent),
162    #[serde(rename = "click")]
163    Click(MapClickEvent),
164    #[serde(rename = "dblclick")]
165    DblClick(MapDblClickEvent),
166    #[serde(rename = "contextmenu")]
167    ContextMenu(MapContextMenuEvent),
168    #[serde(rename = "marker_click")]
169    MarkerClick(MarkerClickEvent),
170    #[serde(rename = "marker_hover")]
171    MarkerHover(MarkerHoverEvent),
172    #[serde(rename = "marker_dragstart")]
173    MarkerDragStart(MarkerDragStartEvent),
174    #[serde(rename = "marker_dragend")]
175    MarkerDragEnd(MarkerDragEndEvent),
176    #[serde(rename = "move")]
177    Move(MapMoveEvent),
178    #[serde(rename = "zoom")]
179    Zoom(MapZoomEvent),
180    #[serde(rename = "rotate")]
181    Rotate(MapRotateEvent),
182    #[serde(rename = "pitch")]
183    Pitch(MapPitchEvent),
184    #[serde(rename = "layer_click")]
185    LayerClick(LayerClickEvent),
186    #[serde(rename = "layer_hover")]
187    LayerHover(LayerHoverEvent),
188}