1
//! AzulMaps map widget. The P3 goal-app's central primitive.
2
//!
3
//! Architecture (per the user's design in MOBILE_SESSION_LOG and the
4
//! follow-up clarification):
5
//!
6
//! - **Widget, not a NodeType.** `MapWidget` builds a regular `<div>` that owns a `MapTileCache`
7
//!   `RefAny` dataset. The cache holds decoded SVG bytes per `MapTileId`; the dataset is the unit
8
//!   of persistence across relayout.
9
//! - **Tile cache survives relayout** via a `DatasetMergeCallback`. Every relayout creates a fresh
10
//!   `MapTileCache` skeleton; the merge callback transfers all `Ready` / `Pending` entries from the
11
//!   old dataset into the new one, so in-flight fetches and already-decoded SVGs aren't dropped.
12
//! - **VirtualView drives lazy rendering.** The widget's body is a `VirtualView` callback that:
13
//!     1. Computes which tile XYZs are visible from the current viewport + viewport size.
14
//!     2. For each visible tile not yet in the cache, marks it `Pending` and (eventually) enqueues
15
//!        an HTTP fetch.
16
//!     3. Returns a `Dom` whose children are one `<div>` per visible tile, GPU-translated into
17
//!        screen space via `transform: translate(x, y) scale(z)`. Each tile div's inner content is
18
//!        the cached SVG DOM, or an empty placeholder while the fetch is in flight.
19
//! - **MVT + MapCSS → SVG → DOM.** The decode pipeline (MVT protobuf bytes + a MapCSS stylesheet →
20
//!   an `<svg>` tree → the framework's existing svg-to-dom path) lands in a follow-up tick. This
21
//!   tick provides the widget shell + the dataset / merge-callback / virtual- view wiring; tiles
22
//!   render as empty placeholders.
23
//! - **Geolocation dot composes on top.** Users stack a normal child `Dom` (with a
24
//!   `NodeType::GeolocationProbe` deeper in the subtree) on top of the map widget - the widget
25
//!   doesn't bake in any geolocation feature itself.
26
//!
27
//! Compile gate: no new HTTP / MVT / proj4 dependencies in this tick.
28
//! Those land alongside the actual decode pipeline.
29

            
30
use alloc::collections::btree_map::BTreeMap;
31

            
32
use azul_core::{
33
    callbacks::{VirtualViewCallback, VirtualViewCallbackInfo, VirtualViewReturn},
34
    dom::{DatasetMergeCallbackType, Dom, OptionDom},
35
    refany::{OptionRefAny, RefAny},
36
};
37
use azul_css::impl_option_inner; // for impl_widget_callback!'s impl_option!
38
use azul_css::{
39
    dynamic_selector::{CssPropertyWithConditionsVec, OptionCssPropertyWithConditionsVec},
40
    AzString,
41
};
42

            
43
// ────────── POD types (api.json + codegen surface) ─────────────────────
44

            
45
/// Identity of one tile in a tiled-map XYZ scheme. Matches Leaflet /
46
/// `OpenLayers` / Mapbox conventions (Web Mercator, origin top-left).
47
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
48
#[repr(C)]
49
pub struct MapTileId {
50
    /// Zoom level. `0` = whole world in one tile, `~14` = street level
51
    /// for vector tiles, `~19` for raster.
52
    pub z: u8,
53
    /// Tile column at this zoom.
54
    pub x: u32,
55
    /// Tile row at this zoom.
56
    pub y: u32,
57
}
58

            
59
/// Configuration of one map tile layer - usually the base raster /
60
/// vector layer. Additional layers (heatmaps, custom `GeoJSON`) compose
61
/// as further `MapWidget` instances stacked atop.
62
#[derive(Debug, Clone, PartialEq, Eq)]
63
#[repr(C)]
64
pub struct MapTileLayer {
65
    /// `{z}` / `{x}` / `{y}` placeholders are substituted at fetch
66
    /// time. Matches Leaflet's `tileLayer(url_template)`.
67
    pub url_template: AzString,
68
    /// Attribution string the user MUST display (`ODbL` "© OpenStreetMap
69
    /// contributors" or similar). Most providers require it.
70
    pub attribution: AzString,
71
    /// MapCSS-style stylesheet driving per-layer fill / stroke /
72
    /// stroke-width. Empty = use the built-in default palette. Each
73
    /// rule is `selector { fill: …; stroke: …; stroke-width: …; }`
74
    /// where the selector's trailing token is matched against the MVT
75
    /// layer name (e.g. `water { fill: #9ecae1; }`, `.buildings { … }`).
76
    /// Parsed by `azul_dll::desktop::extra::map`'s tile decoder.
77
    pub style_css: AzString,
78
    /// The look: a built-in preset, `System` (follows the window's light /
79
    /// dark theme), or `Custom` (your `style_css`). A non-empty `style_css`
80
    /// always wins over a preset. See [`MapTheme`].
81
    pub theme: MapTheme,
82
    /// Minimum integer zoom this layer supports.
83
    pub min_zoom: u8,
84
    /// Maximum integer zoom this layer supports.
85
    pub max_zoom: u8,
86
}
87

            
88
/// A map THEME: which map the user is looking at — the cartography. Presets
89
/// are vendored `MapCSS` palettes (`widgets::map_themes`, see its header for
90
/// provenance and licences — the `OpenFreeMap` designs are CC BY 4.0, credit
91
/// is shown through [`MapTheme::credit`] / the layer's attribution); `Custom`
92
/// uses `MapTileLayer::style_css` (the built-in palette when that is empty).
93
///
94
/// Every theme has a LIGHT and a DARK rendering, and which one shows is not
95
/// the theme's decision but the cascade's — the same
96
/// `@media (prefers-color-scheme: dark)` answer every other node follows. So
97
/// `Apple` is ONE theme, not a light and a dark one: a window that turns dark
98
/// re-decodes the visible tiles with the dark palette on its own. The two
99
/// halves are [`MapTheme::stylesheet`] under a light and a dark window theme.
100
#[repr(C)]
101
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
102
pub enum MapTheme {
103
    /// The platform's familiar cartography: Apple-like on macOS / iOS,
104
    /// Google-like on Android, Positron elsewhere.
105
    #[default]
106
    System,
107
    /// CARTO Positron via `OpenFreeMap` (desaturated); CARTO Dark Matter in
108
    /// dark mode.
109
    Positron,
110
    /// OSM Bright via `OpenFreeMap` (colourful); Dark Matter in dark mode.
111
    Bright,
112
    /// OSM Liberty via `OpenFreeMap` (blue sea); Dark Matter in dark mode.
113
    Liberty,
114
    /// A Google-Maps-like look; Google Maps' published "Night mode" palette
115
    /// in dark mode.
116
    Google,
117
    /// An Apple-Maps-like look, light and dark.
118
    Apple,
119
    /// The caller's own `MapTileLayer::style_css`.
120
    Custom,
121
}
122

            
123
/// The LIGHT/DARK half of a map look — the axis the CASCADE owns.
124
///
125
/// This is deliberately not a `MapTheme` variant. Every cartography style has
126
/// both a light and a dark rendering, and which one to show is the same
127
/// question `@media (prefers-color-scheme: dark)` already answers for every
128
/// other node in the tree. The widget must not answer it a second time, with
129
/// its own platform rules, against its own copy of the theme.
130
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
131
pub enum MapColorScheme {
132
    #[default]
133
    Light,
134
    Dark,
135
}
136

            
137
impl MapColorScheme {
138
    /// The scheme the CASCADE is resolving against — `SystemStyle::theme` is
139
    /// literally what `DynamicSelectorContext::from_system_style` turns into
140
    /// the `ThemeCondition` that `prefers-color-scheme` matches on
141
    /// (`css/src/dynamic_selector.rs`). Reading it here means the map and the
142
    /// stylesheet cannot disagree about what "dark" means.
143
    #[must_use]
144
11
    pub const fn from_system_theme(theme: azul_css::system::Theme) -> Self {
145
11
        match theme {
146
            azul_css::system::Theme::Dark => Self::Dark,
147
11
            azul_css::system::Theme::Light => Self::Light,
148
        }
149
11
    }
150

            
151
    /// The scheme of a WINDOW theme — what a `MapTheme::System` layer
152
    /// follows at render time (`VirtualViewCallbackInfo::window_theme`).
153
    #[must_use]
154
97
    pub const fn from_window_theme(theme: azul_core::window::WindowTheme) -> Self {
155
97
        match theme {
156
8
            azul_core::window::WindowTheme::DarkMode => Self::Dark,
157
89
            azul_core::window::WindowTheme::LightMode => Self::Light,
158
        }
159
97
    }
160
}
161

            
162
/// One concrete `MapCSS` sheet: a [`MapTheme`] taken to a [`MapColorScheme`]
163
/// — THE join of the two axes, and the only thing a tile is ever decoded with
164
/// or keyed by. Not public API (`MapTheme` is): the widget's cache and worker
165
/// speak in looks, the app speaks in themes.
166
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
167
pub enum MapLook {
168
    /// CARTO Positron (light).
169
    Positron,
170
    /// OSM Bright (light).
171
    Bright,
172
    /// OSM Liberty (light).
173
    Liberty,
174
    /// CARTO Dark Matter — the dark half of every `OpenFreeMap` theme.
175
    DarkMatter,
176
    /// Google-Maps-like, light.
177
    GoogleLight,
178
    /// Google Maps' published "Night mode" palette.
179
    GoogleNight,
180
    /// Apple-Maps-like, light.
181
    AppleLight,
182
    /// Apple-Maps-like, dark.
183
    AppleDark,
184
    /// The caller's own `MapTileLayer::style_css`, in either scheme.
185
    Custom,
186
}
187

            
188
impl MapTheme {
189
    /// The platform's familiar cartography. A FAMILY choice, not a light/dark
190
    /// one — picking Apple-like maps on Apple platforms is a product decision
191
    /// that has nothing to do with the colour scheme, which is why this `cfg!`
192
    /// is legitimate where a light/dark `cfg!` would not be.
193
    #[must_use]
194
3104
    pub const fn platform_family() -> Self {
195
3104
        if cfg!(any(target_os = "macos", target_os = "ios")) {
196
            Self::Apple
197
3104
        } else if cfg!(target_os = "android") {
198
            Self::Google
199
        } else {
200
3104
            Self::Positron
201
        }
202
3104
    }
203

            
204
    /// This theme rendered for `scheme`: the only place a (theme, scheme) pair
205
    /// becomes one concrete sheet. Bright and Liberty are light-only designs;
206
    /// their dark half is Dark Matter, the same pairing `MapLibre`'s demo
207
    /// styles use.
208
    #[must_use]
209
6266
    pub const fn look(self, scheme: MapColorScheme) -> MapLook {
210
6266
        let dark = matches!(scheme, MapColorScheme::Dark);
211
3119
        match self {
212
3102
            Self::System => Self::platform_family().look(scheme),
213
21
            Self::Positron | Self::Bright | Self::Liberty if dark => MapLook::DarkMatter,
214
3106
            Self::Positron => MapLook::Positron,
215
3
            Self::Bright => MapLook::Bright,
216
3
            Self::Liberty => MapLook::Liberty,
217
            Self::Google => {
218
10
                if dark {
219
5
                    MapLook::GoogleNight
220
                } else {
221
5
                    MapLook::GoogleLight
222
                }
223
            }
224
            Self::Apple => {
225
19
                if dark {
226
10
                    MapLook::AppleDark
227
                } else {
228
9
                    MapLook::AppleLight
229
                }
230
            }
231
2
            Self::Custom => MapLook::Custom,
232
        }
233
6266
    }
234

            
235
    /// The `MapCSS` sheet this theme renders with under `window_theme`; empty
236
    /// for `Custom` (the layer's `style_css` is the sheet then).
237
    #[must_use]
238
11
    pub fn stylesheet(self, window_theme: azul_core::window::WindowTheme) -> AzString {
239
11
        AzString::from(
240
11
            self.look(MapColorScheme::from_window_theme(window_theme))
241
11
                .sheet(),
242
        )
243
11
    }
244

            
245
    /// The credit line the design rendered under `window_theme` asks for (CC
246
    /// BY 4.0 for the `OpenFreeMap` designs, Apache-2.0 for Google's night
247
    /// sample); empty for the authored looks and `Custom`. Both halves are
248
    /// appended to the layer's attribution by [`MapTileLayer::with_theme`].
249
    #[must_use]
250
2
    pub fn credit(self, window_theme: azul_core::window::WindowTheme) -> AzString {
251
2
        AzString::from(
252
2
            self.look(MapColorScheme::from_window_theme(window_theme))
253
2
                .credit_str(),
254
        )
255
2
    }
256
}
257

            
258
impl MapLook {
259
    /// The `MapCSS` sheet; empty for `Custom`.
260
    #[must_use]
261
11
    pub fn stylesheet(self) -> AzString {
262
11
        AzString::from(self.sheet())
263
11
    }
264

            
265
    /// [`Self::stylesheet`] as the static slice (internal, no allocation).
266
    #[must_use]
267
68
    pub(crate) const fn sheet(self) -> &'static str {
268
        use super::map_themes as t;
269
68
        match self {
270
26
            Self::Positron => t::POSITRON,
271
4
            Self::Bright => t::BRIGHT,
272
4
            Self::Liberty => t::LIBERTY,
273
17
            Self::DarkMatter => t::DARK,
274
4
            Self::GoogleLight => t::GOOGLE_LIGHT,
275
4
            Self::GoogleNight => t::GOOGLE_NIGHT,
276
4
            Self::AppleLight => t::APPLE_LIGHT,
277
4
            Self::AppleDark => t::APPLE_DARK,
278
1
            Self::Custom => "",
279
        }
280
68
    }
281

            
282
    /// Is this the dark half?
283
    #[must_use]
284
22
    pub const fn is_dark(self) -> bool {
285
22
        matches!(self, Self::DarkMatter | Self::GoogleNight | Self::AppleDark)
286
22
    }
287

            
288
    /// The credit line a design's licence asks for (internal, no allocation).
289
    #[must_use]
290
16
    pub(crate) const fn credit_str(self) -> &'static str {
291
16
        match self {
292
6
            Self::Positron => "Style: Positron © CARTO (CC BY 4.0) via OpenFreeMap",
293
6
            Self::DarkMatter => "Style: Dark Matter © CARTO (CC BY 4.0) via OpenFreeMap",
294
            Self::Bright => "Style: OSM Bright © OpenMapTiles (CC BY 4.0) via OpenFreeMap",
295
            Self::Liberty => "Style: OSM Liberty (CC BY 4.0) via OpenFreeMap",
296
            Self::GoogleNight => "Style: Google Maps Platform night-mode sample (Apache-2.0)",
297
4
            Self::GoogleLight | Self::AppleLight | Self::AppleDark | Self::Custom => "",
298
        }
299
16
    }
300
}
301

            
302
impl MapTileLayer {
303
    /// Pick a theme. Appends the licence credit of BOTH its halves (where a
304
    /// design has one) to the attribution, so an app that shows `attribution`
305
    /// complies with CC BY whichever way the window is themed.
306
    #[must_use]
307
4
    pub fn with_theme(mut self, theme: MapTheme) -> Self {
308
4
        self.theme = theme;
309
8
        for look in [
310
4
            theme.look(MapColorScheme::Light),
311
4
            theme.look(MapColorScheme::Dark),
312
        ] {
313
8
            let credit = look.credit_str();
314
8
            if !credit.is_empty() && !self.attribution.as_str().contains(credit) {
315
4
                let mut s = self.attribution.as_str().to_string();
316
4
                if !s.is_empty() {
317
4
                    s.push_str(" · ");
318
4
                }
319
4
                s.push_str(credit);
320
4
                self.attribution = AzString::from(s);
321
4
            }
322
        }
323
4
        self
324
4
    }
325

            
326
    /// The `MapCSS` the tiles are decoded with under `window_theme`: a
327
    /// non-empty `style_css` always wins; else the theme's sheet for that
328
    /// half; else the built-in palette (empty).
329
    #[must_use]
330
3
    pub fn effective_style_css(&self, window_theme: azul_core::window::WindowTheme) -> AzString {
331
3
        self.effective_style_css_for(
332
3
            self.theme
333
3
                .look(MapColorScheme::from_window_theme(window_theme)),
334
        )
335
3
    }
336

            
337
    /// [`Self::effective_style_css`] for one concrete look — what the cache
338
    /// and the worker deal in.
339
    #[must_use]
340
24
    pub(crate) fn effective_style_css_for(&self, look: MapLook) -> AzString {
341
24
        if !self.style_css.as_str().is_empty() {
342
1
            return self.style_css.clone();
343
23
        }
344
23
        AzString::from(look.sheet())
345
24
    }
346
}
347

            
348
impl Default for MapTileLayer {
349
130
    fn default() -> Self {
350
130
        Self {
351
130
            // OpenFreeMap's public planet vector tiles (full-detail OSM, z0–14, no
352
130
            // API key). The tile path is VERSIONED by planet-build date — the
353
130
            // unversioned `/planet/{z}/{x}/{y}.pbf` returns empty tiles. The version
354
130
            // below is the current build from the TileJSON at
355
130
            // `https://tiles.openfreemap.org/planet` (`tiles[0]`); when OpenFreeMap
356
130
            // rebuilds the planet this goes stale, so the proper long-term path is to
357
130
            // resolve it on the background thread by fetching that TileJSON first (a
358
130
            // follow-up to the Leaflet-style layer work). Raster relief is also
359
130
            // available at `…/natural_earth/ne2sr/{z}/{x}/{y}.png` (z0–6).
360
130
            url_template: AzString::from(
361
130
                "https://tiles.openfreemap.org/planet/20260531_080002_pt/{z}/{x}/{y}.pbf",
362
130
            ),
363
130
            min_zoom: 0,
364
130
            max_zoom: 14,
365
130
            attribution: AzString::from(
366
130
                "© OpenFreeMap © OpenMapTiles · Data © OpenStreetMap contributors",
367
130
            ),
368
130
            style_css: AzString::from(""),
369
130
            theme: MapTheme::System,
370
130
        }
371
130
    }
372
}
373

            
374
/// Centre + zoom + camera state. The Leaflet shape
375
/// (`map.setView([lat, lon], zoom)`) plus the `MapLibre` camera: `bearing_deg`
376
/// rotates the map (clockwise, degrees), `pitch_deg` tilts it away from the
377
/// viewer (0 = straight down, up to [`MAX_PITCH_DEG`]). Both render as a
378
/// CSS `perspective() rotateX() rotate()` transform on the tile canvas —
379
/// `MapWidget::with_pitch` / `with_bearing`, right-drag, or a rotate
380
/// gesture drive them. Panning and tapping work in the un-tilted plane.
381
#[derive(Debug, Clone, Copy, PartialEq)]
382
#[repr(C)]
383
pub struct MapViewport {
384
    pub centre_lat_deg: f64,
385
    pub centre_lon_deg: f64,
386
    pub zoom: f32,
387
    pub bearing_deg: f32,
388
    pub pitch_deg: f32,
389
}
390

            
391
impl Default for MapViewport {
392
205
    fn default() -> Self {
393
        // A neutral "whole world, slightly zoomed in" default. Apps
394
        // care will replace this immediately.
395
205
        Self {
396
205
            centre_lat_deg: 0.0,
397
205
            centre_lon_deg: 0.0,
398
205
            zoom: 2.0,
399
205
            bearing_deg: 0.0,
400
205
            pitch_deg: 0.0,
401
205
        }
402
205
    }
403
}
404

            
405
/// A geographic coordinate in degrees. Returned by
406
/// [`MapWidget::latlon_at_px`] and (P3) the map's `on_pin_tap` hook.
407
#[derive(Debug, Clone, Copy, PartialEq)]
408
#[repr(C)]
409
pub struct MapLatLon {
410
    pub lat_deg: f64,
411
    pub lon_deg: f64,
412
}
413

            
414
// ────────── MapWidget builder ──────────────────────────────────────────
415

            
416
// NOTE: `MapWidget` mirrors the api.json struct field-for-field so the
417
// codegen FFI transmute stays sound. Callback fields (e.g.
418
// `on_viewport_changed`) ARE allowed: codegen keeps `AzMapWidget` in sync
419
// (the Button / Camera pattern). The Rust-only tile-fetch worker stays in
420
// the FFI-opaque `MapTileCache` dataset (installed when the map mounts).
421
#[derive(Debug, Clone, PartialEq)]
422
#[repr(C)]
423
pub struct MapWidget {
424
    pub layer: MapTileLayer,
425
    pub viewport: MapViewport,
426
    pub container_style: OptionCssPropertyWithConditionsVec,
427
    /// Optional hook fired when the user pans / zooms (effects / persist
428
    /// the viewport). FFI-exposed; re-set on each fresh build.
429
    pub on_viewport_changed: OptionMapViewportChanged,
430
    /// Optional hook fired when the user taps the map, with the tapped
431
    /// lat/lon. FFI-exposed; re-set on each fresh build.
432
    pub on_pin_tap: OptionMapPinTap,
433
    /// Optional hook fired when the map is mounted, returning the [`MapSetup`]
434
    /// its tile fetches run on (a shared connection pool, a thread pool, an
435
    /// in-flight limit). FFI-exposed; re-set on each fresh build.
436
    pub on_mount: OptionMapMount,
437
}
438

            
439
/// The runtime-installed tile fetcher every map picks up when it mounts.
440
/// Registered once at startup by the dll (the worker lives there, with the
441
/// MVT / Mercator dependencies).
442
static MAP_TILE_FETCHER: azul_core::sync::OnceLock<crate::thread::ThreadCallback> =
443
    azul_core::sync::OnceLock::new();
444

            
445
/// Install the framework-owned tile fetcher. The first registration wins;
446
/// returns `false` when one was already installed.
447
1
pub fn register_map_tile_fetcher(cb: crate::thread::ThreadCallback) -> bool {
448
1
    MAP_TILE_FETCHER.set(cb).is_ok()
449
1
}
450

            
451
/// Whether a tile fetcher has been installed (i.e. whether a mounted map
452
/// loads tiles or renders placeholders).
453
#[must_use]
454
1
pub fn has_map_tile_fetcher() -> bool {
455
1
    MAP_TILE_FETCHER.get().is_some()
456
1
}
457

            
458
impl MapWidget {
459
    #[must_use]
460
42
    pub fn create(layer: MapTileLayer) -> Self {
461
42
        Self {
462
42
            layer,
463
42
            viewport: MapViewport::default(),
464
42
            container_style: OptionCssPropertyWithConditionsVec::None,
465
42
            on_viewport_changed: OptionMapViewportChanged::None,
466
42
            on_pin_tap: OptionMapPinTap::None,
467
42
            on_mount: OptionMapMount::None,
468
42
        }
469
42
    }
470

            
471
    #[must_use]
472
17
    pub const fn with_viewport(mut self, viewport: MapViewport) -> Self {
473
17
        self.viewport = viewport;
474
17
        self
475
17
    }
476

            
477
    /// Pick a look for the tile layer (see [`MapTheme`]); `System` follows
478
    /// the window's light / dark theme.
479
    #[must_use]
480
    pub fn with_theme(mut self, theme: MapTheme) -> Self {
481
        self.layer = self.layer.with_theme(theme);
482
        self
483
    }
484

            
485
    /// Tilt the camera: 0 looks straight down, [`MAX_PITCH_DEG`] is the
486
    /// steepest 3D view (clamped). Rendered as a perspective transform on
487
    /// the tile canvas; right-drag vertically changes it at runtime.
488
    #[must_use]
489
1
    pub fn with_pitch(mut self, pitch_deg: f32) -> Self {
490
1
        self.viewport.pitch_deg = clamp_pitch(pitch_deg);
491
1
        self
492
1
    }
493

            
494
    /// Rotate the map (clockwise degrees, normalised to `-180..180`).
495
    /// Right-drag horizontally or a rotate gesture changes it at runtime.
496
    #[must_use]
497
1
    pub const fn with_bearing(mut self, bearing_deg: f32) -> Self {
498
1
        self.viewport.bearing_deg = normalize_bearing(bearing_deg);
499
1
        self
500
1
    }
501

            
502
    #[must_use]
503
12
    pub fn with_container_style(mut self, css: CssPropertyWithConditionsVec) -> Self {
504
12
        self.container_style = OptionCssPropertyWithConditionsVec::Some(css);
505
12
        self
506
12
    }
507

            
508
    /// Set a hook fired when the user pans / zooms the map. The map owns its
509
    /// own pan/pinch state; this lets your app observe or persist the
510
    /// resulting `MapViewport`.
511
3
    pub fn set_on_viewport_changed<C: Into<MapViewportChangedCallback>>(
512
3
        &mut self,
513
3
        data: RefAny,
514
3
        callback: C,
515
3
    ) {
516
3
        self.on_viewport_changed = Some(MapViewportChanged {
517
3
            refany: data,
518
3
            callback: callback.into(),
519
3
        })
520
3
        .into();
521
3
    }
522

            
523
    /// Builder form of [`set_on_viewport_changed`](Self::set_on_viewport_changed).
524
    #[must_use]
525
2
    pub fn with_on_viewport_changed<C: Into<MapViewportChangedCallback>>(
526
2
        mut self,
527
2
        data: RefAny,
528
2
        callback: C,
529
2
    ) -> Self {
530
2
        self.set_on_viewport_changed(data, callback);
531
2
        self
532
2
    }
533

            
534
    /// Set a hook fired when the user taps the map (a press + release at ~the
535
    /// same point, no drag), with the tapped lat/lon.
536
3
    pub fn set_on_pin_tap<C: Into<MapPinTapCallback>>(&mut self, data: RefAny, callback: C) {
537
3
        self.on_pin_tap = Some(MapPinTap {
538
3
            refany: data,
539
3
            callback: callback.into(),
540
3
        })
541
3
        .into();
542
3
    }
543

            
544
    /// Builder form of [`set_on_pin_tap`](Self::set_on_pin_tap).
545
    #[must_use]
546
2
    pub fn with_on_pin_tap<C: Into<MapPinTapCallback>>(
547
2
        mut self,
548
2
        data: RefAny,
549
2
        callback: C,
550
2
    ) -> Self {
551
2
        self.set_on_pin_tap(data, callback);
552
2
        self
553
2
    }
554

            
555
    /// Set a hook fired when the map is mounted. It receives the map's current
556
    /// [`MapSetup`] and returns the one to use: this is where an app hands the
557
    /// map a shared `HttpClient` or `ThreadPool`. Without a hook every tile
558
    /// opens its own connection on its own thread.
559
    ///
560
    /// Runs again if the map is mounted again, e.g. after moving to another
561
    /// parent, so return the setup unchanged when there is nothing to change.
562
2
    pub fn set_on_mount<C: Into<MapMountCallback>>(&mut self, data: RefAny, callback: C) {
563
2
        self.on_mount = Some(MapMount {
564
2
            refany: data,
565
2
            callback: callback.into(),
566
2
        })
567
2
        .into();
568
2
    }
569

            
570
    /// Builder form of [`set_on_mount`](Self::set_on_mount).
571
    #[must_use]
572
2
    pub fn with_on_mount<C: Into<MapMountCallback>>(mut self, data: RefAny, callback: C) -> Self {
573
2
        self.set_on_mount(data, callback);
574
2
        self
575
2
    }
576

            
577
    /// Project a screen pixel `px` (relative to the map node's top-left, in a
578
    /// node of size `container`) to a lat/lon on the map at `viewport`. Small-
579
    /// angle Mercator (accurate at city zooms). Inverse of
580
    /// [`px_at_latlon`](Self::px_at_latlon). Exposed so apps don't reimplement
581
    /// the projection (e.g. to drop a pin where the user tapped).
582
    #[allow(clippy::suboptimal_flops)] // mul_add not guaranteed faster/available without target +fma; keep explicit a*b+c
583
    #[must_use]
584
32
    pub fn latlon_at_px(
585
32
        viewport: MapViewport,
586
32
        px: azul_core::geom::LogicalPosition,
587
32
        container: azul_core::geom::LogicalSize,
588
32
    ) -> MapLatLon {
589
32
        let world = 256.0_f64 * 2.0_f64.powf(f64::from(viewport.zoom));
590
32
        let dx = f64::from(px.x - container.width * 0.5);
591
32
        let dy = f64::from(px.y - container.height * 0.5);
592
32
        let lon = (viewport.centre_lon_deg + dx * 360.0 / world).clamp(-180.0, 180.0);
593
32
        let cos_lat = viewport.centre_lat_deg.to_radians().cos();
594
32
        let lat = (viewport.centre_lat_deg - dy * 360.0 / world * cos_lat).clamp(-85.0, 85.0);
595
32
        MapLatLon {
596
32
            lat_deg: lat,
597
32
            lon_deg: lon,
598
32
        }
599
32
    }
600

            
601
    /// Inverse of [`latlon_at_px`](Self::latlon_at_px): where `coord` lands in
602
    /// container pixels at `viewport`.
603
    #[allow(clippy::suboptimal_flops)] // mul_add not guaranteed faster/available without target +fma; keep explicit a*b+c
604
    #[allow(clippy::cast_possible_truncation)] // bounded layout/render numeric cast
605
    #[must_use]
606
12
    pub fn px_at_latlon(
607
12
        viewport: MapViewport,
608
12
        coord: MapLatLon,
609
12
        container: azul_core::geom::LogicalSize,
610
12
    ) -> azul_core::geom::LogicalPosition {
611
12
        let world = 256.0_f64 * 2.0_f64.powf(f64::from(viewport.zoom));
612
12
        let cos_lat = viewport.centre_lat_deg.to_radians().cos();
613
12
        let px = f64::from(container.width) * 0.5
614
12
            + (coord.lon_deg - viewport.centre_lon_deg) * world / 360.0;
615
12
        let py = f64::from(container.height) * 0.5
616
12
            - (coord.lat_deg - viewport.centre_lat_deg) * world / (360.0 * cos_lat);
617
12
        azul_core::geom::LogicalPosition::new(px as f32, py as f32)
618
12
    }
619

            
620
    /// Construct the rendered `Dom`. The returned `Dom` is a single
621
    /// `<div>` with:
622
    /// - A `MapTileCache` `RefAny` dataset (initialised from this widget's `viewport` + `layer`).
623
    /// - A `DatasetMergeCallback` so the cache survives relayout.
624
    /// - A `VirtualView` child that re-renders the visible-tile grid on bounds change.
625
    /// - Mouse-down / mouse-move / mouse-up callbacks that pan the viewport while a drag is active
626
    ///   (the widget owns the pan state via `MapTileCache::drag_anchor`, so user code doesn't have
627
    ///   to wire anything).
628
    /// - Pinch callbacks that zoom in / out.
629
    ///
630
    /// Tiles are fetched by the framework's built-in worker, which the map
631
    /// installs when it mounts (see [`register_map_tile_fetcher`]; the dll
632
    /// registers it, with the MVT / Mercator dependencies this crate does not
633
    /// carry). Without a registered worker the grid renders placeholders.
634
    /// Pools and limits for those fetches come from the `on_mount` hook, never
635
    /// from here: building the `Dom` only describes the UI.
636
    #[must_use]
637
104
    pub fn dom(self) -> Dom {
638
        use azul_core::dom::{ComponentEventFilter, EventFilter, HoverEventFilter};
639

            
640
104
        let mut cache = MapTileCache::new(self.layer.clone(), self.viewport);
641
104
        cache.on_mount = self.on_mount;
642
104
        cache.on_viewport_changed = self.on_viewport_changed;
643
104
        cache.on_pin_tap = self.on_pin_tap;
644
104
        let dataset = RefAny::new(cache);
645
104
        let virtual_view_data = dataset.clone();
646

            
647
104
        let root = Dom::create_div()
648
            // Fill the container (the Leaflet contract) via absolute inset:0 rather
649
            // than height:100%. A percentage height only resolves against a parent
650
            // with a DEFINITE height; the usual map container is a `flex-grow` item
651
            // whose height is not definite for percentage children, so height:100%
652
            // there resolves to INFINITY → the VirtualView gets infinite bounds and
653
            // positions every tile at y=∞ (off-screen → blank map). Absolute inset:0
654
            // instead sizes against the container's final, finite content box. The
655
            // container MUST be a positioned box (the demo's `position: relative`);
656
            // a non-empty `container_style` (via `with_container_style`) overrides.
657
104
            .with_css("position: absolute; top: 0; left: 0; right: 0; bottom: 0; overflow: hidden;")
658
            // The widget as a whole; the tile grid inside it is the part a
659
            // screen reader describes (see map_widget_render).
660
104
            .with_accessibility_info(azul_core::a11y::AccessibilityInfo::named(
661
                "Map",
662
104
                azul_core::a11y::AccessibilityRole::Grouping,
663
            ))
664
104
            .with_dataset(OptionRefAny::Some(dataset.clone()))
665
104
            .with_merge_callback(azul_core::dom::DatasetMergeCallback::from_ptr(merge_map_tile_cache))
666
            // AfterMount fires once when the widget first appears (and
667
            // again after a DOM-structure change re-mounts it). It's the
668
            // earliest point with a `CallbackInfo`, so we kick the
669
            // initial tile fetches here — without it the first frame's
670
            // tiles would stay `Pending` until the user panned/tapped.
671
104
            .with_callback(
672
104
                EventFilter::Component(ComponentEventFilter::AfterMount),
673
104
                dataset.clone(),
674
104
                crate::callbacks::Callback::from_ptr(map_on_after_mount),
675
            )
676
104
            .with_callback(
677
104
                EventFilter::Hover(HoverEventFilter::MouseDown),
678
104
                dataset.clone(),
679
104
                crate::callbacks::Callback::from_ptr(map_on_pointer_down),
680
            )
681
104
            .with_callback(
682
104
                EventFilter::Hover(HoverEventFilter::MouseMove),
683
104
                dataset.clone(),
684
104
                crate::callbacks::Callback::from_ptr(map_on_pointer_move),
685
            )
686
104
            .with_callback(
687
104
                EventFilter::Hover(HoverEventFilter::MouseUp),
688
104
                dataset.clone(),
689
104
                crate::callbacks::Callback::from_ptr(map_on_pointer_up),
690
            )
691
104
            .with_callback(
692
104
                EventFilter::Hover(HoverEventFilter::MouseLeave),
693
104
                dataset.clone(),
694
104
                crate::callbacks::Callback::from_ptr(map_on_pointer_up),
695
            )
696
104
            .with_callback(
697
104
                EventFilter::Hover(HoverEventFilter::TouchStart),
698
104
                dataset.clone(),
699
104
                crate::callbacks::Callback::from_ptr(map_on_pointer_down),
700
            )
701
104
            .with_callback(
702
104
                EventFilter::Hover(HoverEventFilter::TouchMove),
703
104
                dataset.clone(),
704
104
                crate::callbacks::Callback::from_ptr(map_on_pointer_move),
705
            )
706
104
            .with_callback(
707
104
                EventFilter::Hover(HoverEventFilter::TouchEnd),
708
104
                dataset.clone(),
709
104
                crate::callbacks::Callback::from_ptr(map_on_pointer_up),
710
            )
711
104
            .with_callback(
712
104
                EventFilter::Hover(HoverEventFilter::TouchCancel),
713
104
                dataset.clone(),
714
104
                crate::callbacks::Callback::from_ptr(map_on_pointer_up),
715
            )
716
            // Native gesture events (UIPinchGestureRecognizer on iOS,
717
            // ScaleGestureDetector on Android, NSMagnificationGestureRecognizer
718
            // on macOS) — fire through the same map_on_pointer_move handler
719
            // which reads `info.get_pinch()` and applies the zoom delta.
720
104
            .with_callback(
721
104
                EventFilter::Hover(HoverEventFilter::PinchIn),
722
104
                dataset.clone(),
723
104
                crate::callbacks::Callback::from_ptr(map_on_pointer_move),
724
            )
725
104
            .with_callback(
726
104
                EventFilter::Hover(HoverEventFilter::PinchOut),
727
104
                dataset,
728
104
                crate::callbacks::Callback::from_ptr(map_on_pointer_move),
729
            )
730
104
            .with_child(
731
104
                Dom::create_virtual_view(
732
104
                    virtual_view_data,
733
104
                    azul_core::callbacks::VirtualViewCallback::create(map_widget_render),
734
                )
735
                // Fill the widget div with a PERCENTAGE box (not absolute). The
736
                // outer div above is absolutely sized, so its height IS definite —
737
                // height:100% here resolves against it (441px), giving the
738
                // VirtualView a finite box. (Absolute-against-absolute collapses to
739
                // 0 in the solver; percentage-against-a-definite-parent does not.)
740
104
                .with_css("width: 100%; height: 100%; overflow: hidden;"),
741
            );
742

            
743
        // A caller-supplied container style replaces the default fill above
744
        // (`with_css_props` replaces the inline style) — the caller then owns sizing.
745
104
        match self.container_style.into_option() {
746
            // No opinion: keep the default fill declared above.
747
103
            None => root,
748
            // A caller-supplied style REPLACES it (`with_css_props` replaces the
749
            // inline style), so an empty vec is a real answer here — it means
750
            // "no properties", and the caller takes over sizing.
751
1
            Some(css) => root.with_css_props(css),
752
        }
753
104
    }
754
}
755

            
756
// ────────── Tile cache (dataset RefAny payload) ───────────────────────
757

            
758
/// What a cached tile IS: these coordinates, styled for that look.
759
///
760
/// The look is part of the KEY, not a stamp the cache checks and invalidates
761
/// on. Two looks of one tile are two entries that coexist, so switching look
762
/// (a light/dark flip, an app changing cartography) RE-KEYS the lookup and the
763
/// other variant stays cached — instantly available if the user flips back, and
764
/// usable as a fallback while the new one styles.
765
///
766
/// The old cache keyed on `MapTileId` alone and kept a single `decoded_theme`
767
/// beside it. Any disagreement between the two threw decoded tiles away and
768
/// re-fetched them, so a look that was not yet settled on the first frame cost
769
/// a second network round-trip for every visible tile.
770
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
771
pub struct TileStyleKey {
772
    pub tile: MapTileId,
773
    pub look: MapLook,
774
}
775

            
776
// Ordered by hand: the ordering only has to be total and stable so the
777
// `BTreeMap` iterates deterministically for the debug log and the e2e
778
// snapshots, and a fieldless enum's discriminant is exactly that.
779
impl PartialOrd for TileStyleKey {
780
    fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
781
        Some(self.cmp(other))
782
    }
783
}
784
impl Ord for TileStyleKey {
785
71435
    fn cmp(&self, other: &Self) -> core::cmp::Ordering {
786
71435
        self.tile
787
71435
            .cmp(&other.tile)
788
71435
            .then_with(|| (self.look as u8).cmp(&(other.look as u8)))
789
71435
    }
790
}
791

            
792
#[derive(Debug)]
793
pub struct MapTileCache {
794
    pub layer: MapTileLayer,
795
    pub viewport: MapViewport,
796
    /// `Ready(svg)` once the tile has been fetched + styled for THIS key's
797
    /// look; `Pending` while queued, `Fetching` while a worker thread is
798
    /// in flight; absent otherwise. `BTreeMap` for deterministic
799
    /// iteration so the debug log + e2e snapshots are stable.
800
    pub tiles: BTreeMap<TileStyleKey, TileEntry>,
801
    /// The tile's raw MVT payload, keyed by coordinates ALONE because the bytes
802
    /// do not depend on the look — the same features are styled differently.
803
    ///
804
    /// This is what makes a look change free of network traffic: a restyle is
805
    /// handed these bytes and performs no HTTP request at all. A tile is
806
    /// therefore fetched at most once per session no matter how often the
807
    /// cascade's light/dark answer changes.
808
    pub tile_bytes: BTreeMap<MapTileId, azul_css::U8Vec>,
809
    /// Worker thread entry point that fetches + decodes one tile.
810
    /// Installed on mount from [`register_map_tile_fetcher`] (the MVT
811
    /// decoder lives in `azul-dll`, which `azul-layout` can't depend
812
    /// on). `None` means "no fetch wired": tiles stay `Pending` and
813
    /// the placeholder grid renders. Lives on the persistent cache the
814
    /// merge callback keeps. Held as the `ThreadCallback` wrapper (not the
815
    /// raw fn pointer) so it round-trips through the FFI codegen.
816
    pub fetch_callback: Option<crate::thread::ThreadCallback>,
817
    /// Pixel coordinates of the cursor at the last mouse-down /
818
    /// touch-down on the widget. `Some` while a drag is in flight,
819
    /// `None` between drags. The framework consults this on every
820
    /// mouse-move to derive the pixel delta, which then converts to a
821
    /// lat/lon delta via the Web Mercator inverse.
822
    pub drag_anchor: Option<azul_core::geom::LogicalPosition>,
823
    /// Pinch reference distance (pixels) - the two-finger separation
824
    /// the last time a pinch event was observed for this widget.
825
    /// `Some` while a pinch is in flight, `None` between gestures.
826
    /// On each subsequent pinch update we compute
827
    /// `dz = log2(current_distance / pinch_anchor)` and add it to
828
    /// `viewport.zoom`, then reset the anchor to the current
829
    /// distance - so the gesture stays continuous across many frames.
830
    pub pinch_anchor: Option<f32>,
831
    /// The user's `on_viewport_changed` hook, copied here from the builder
832
    /// so the pan / pinch callbacks can fire it. Carried across relayout.
833
    pub on_viewport_changed: OptionMapViewportChanged,
834
    /// Pixel position of the last pointer-down (the original press point, not
835
    /// overwritten by pan moves). Used to tell a tap from a drag in pointer-up.
836
    pub press_origin: Option<azul_core::geom::LogicalPosition>,
837
    /// Pixel position of the last right-button press while a camera drag
838
    /// (tilt / rotate) is in flight; `None` between drags.
839
    pub tilt_anchor: Option<azul_core::geom::LogicalPosition>,
840
    /// A MEMO of the look the last render resolved — nothing more.
841
    ///
842
    /// The render decides the look from the style available at render time and
843
    /// writes it here purely so the spawn/sweep callbacks (which have no render
844
    /// info) know which key to fill. It is deliberately NOT a validity stamp:
845
    /// changing it invalidates nothing and discards nothing, because the look
846
    /// is part of every tile's key.
847
    pub active_look: MapLook,
848
    /// The light/dark answer THE CASCADE gave, recorded by whichever callback
849
    /// last had a `CallbackInfo` (mount, the 250 ms sweep, any pointer event).
850
    ///
851
    /// `SystemStyle::theme` is the exact input `prefers-color-scheme` matches
852
    /// on, so the map and the app's stylesheet always agree. It is `None` until
853
    /// the first such callback runs — the very first `VirtualView` render happens
854
    /// before mount, and rather than invent an answer there it renders the
855
    /// layer's own look and lets the sweep correct it. Getting that first guess
856
    /// wrong is now free: it re-keys, it does not invalidate, and no tile is
857
    /// fetched twice.
858
    pub cascade_scheme: Option<MapColorScheme>,
859
    /// The user's `on_pin_tap` hook, copied from the builder so pointer-up can
860
    /// fire it. Carried across relayout.
861
    pub on_pin_tap: OptionMapPinTap,
862
    /// The user's `on_mount` hook, copied from the builder so mount can fire
863
    /// it. Carried across relayout.
864
    pub on_mount: OptionMapMount,
865
    /// What tile fetches run on, as the `on_mount` hook last returned it.
866
    ///
867
    /// Written on mount, never by a rebuild: the merge callback keeps the old
868
    /// cache, so a shared pool stays attached through every relayout and is
869
    /// released when the map leaves the tree and this cache drops.
870
    pub setup: MapSetup,
871
    /// The tiles the view needs, as the last render or zoom gesture computed
872
    /// them. `None` until the first one. Queued tiles outside it are forgotten
873
    /// by the next spawn pass (see [`MapTileCache::drop_unwanted_pending`]).
874
    pub wanted: Option<WantedTiles>,
875
}
876

            
877
impl MapTileCache {
878
    #[must_use]
879
170
    pub const fn new(layer: MapTileLayer, viewport: MapViewport) -> Self {
880
170
        let active_look = layer.theme.look(MapColorScheme::Light);
881
170
        Self {
882
170
            layer,
883
170
            viewport,
884
170
            tiles: BTreeMap::new(),
885
170
            tile_bytes: BTreeMap::new(),
886
170
            fetch_callback: None,
887
170
            drag_anchor: None,
888
170
            pinch_anchor: None,
889
170
            press_origin: None,
890
170
            tilt_anchor: None,
891
170
            active_look,
892
170
            cascade_scheme: None,
893
170
            on_viewport_changed: OptionMapViewportChanged::None,
894
170
            on_pin_tap: OptionMapPinTap::None,
895
170
            on_mount: OptionMapMount::None,
896
170
            setup: MapSetup::new(),
897
170
            wanted: None,
898
170
        }
899
170
    }
900

            
901
    /// Queue `wanted`'s tiles for `look` and remember the range as what the
902
    /// view needs now.
903
83
    pub fn want_tiles(&mut self, wanted: WantedTiles, look: MapLook) {
904
1547
        for tile in wanted.tiles() {
905
1547
            self.tiles
906
1547
                .entry(TileStyleKey { tile, look })
907
1547
                .or_insert(TileEntry::Pending);
908
1547
        }
909
83
        self.wanted = Some(wanted);
910
83
    }
911

            
912
    /// Forget queued tiles the view no longer needs.
913
    ///
914
    /// A `Pending` tile is only a request: no worker is running for it, so
915
    /// dropping it costs nothing. Kept, it would be downloaded after the user has
916
    /// already panned or zoomed past it — every tile that was ever on screen
917
    /// would be. Tiles already `Fetching` are left alone; their worker is running
918
    /// and writes back into this entry.
919
4
    pub fn drop_unwanted_pending(&mut self) {
920
4
        let Some(wanted) = self.wanted else {
921
3
            return;
922
        };
923
1
        self.tiles
924
20
            .retain(|key, entry| !matches!(entry, TileEntry::Pending) || wanted.contains(key.tile));
925
4
    }
926

            
927
    /// The look to render right now: the layer's choice of cartography, taken
928
    /// to the cascade's light/dark. An explicitly pinned preset wins.
929
    #[must_use]
930
2921
    pub const fn current_look(&self) -> MapLook {
931
2921
        match self.cascade_scheme {
932
123
            Some(scheme) => self.layer.theme.look(scheme),
933
            // Nothing has told us what the cascade says yet (pre-mount). Use the
934
            // light half; the sweep re-keys within 250 ms, for free.
935
2798
            None => self.layer.theme.look(MapColorScheme::Light),
936
        }
937
2921
    }
938

            
939
    /// Record the look the renderer resolved. Returns `true` when it moved.
940
    ///
941
    /// This USED to be `adopt_theme`, and it used to push every decoded tile
942
    /// back to `Pending` so the worker would re-fetch and re-decode it. That
943
    /// was the conflation: a look is not a reason to throw away geometry. The
944
    /// look now selects a key, so a change here costs nothing — the tiles for
945
    /// the new look are styled from bytes already in `tile_bytes`, and the
946
    /// tiles for the old look stay cached.
947
87
    pub fn set_active_look(&mut self, look: MapLook) -> bool {
948
87
        let changed = self.active_look != look;
949
87
        self.active_look = look;
950
87
        changed
951
87
    }
952

            
953
    /// The key `tile` occupies at the look the cache is currently showing.
954
    #[must_use]
955
2837
    pub const fn key_at_current_look(&self, tile: MapTileId) -> TileStyleKey {
956
2837
        TileStyleKey {
957
2837
            tile,
958
2837
            look: self.current_look(),
959
2837
        }
960
2837
    }
961

            
962
    /// Insert / replace `tile`'s entry at the current look.
963
2799
    pub fn insert_tile(&mut self, tile: MapTileId, entry: TileEntry) {
964
2799
        let k = self.key_at_current_look(tile);
965
2799
        self.tiles.insert(k, entry);
966
2799
    }
967

            
968
    /// `tile`'s entry at the current look, if the cache holds one.
969
    #[must_use]
970
36
    pub fn tile_entry(&self, tile: MapTileId) -> Option<&TileEntry> {
971
36
        self.tiles.get(&self.key_at_current_look(tile))
972
36
    }
973

            
974
    /// Worker-thread → main-thread write path. Set the decoded SVG for
975
    /// a tile AT THE LOOK IT WAS STYLED FOR (called from
976
    /// `map_tile_writeback`). Stamps `Ready`.
977
11
    pub fn mark_tile_ready(&mut self, key: TileStyleKey, svg: AzString) {
978
11
        self.tiles.insert(key, TileEntry::Ready { svg });
979
11
    }
980

            
981
    /// Mark a tile's fetch as failed so the grid doesn't re-spawn it
982
    /// every frame.
983
2
    pub fn mark_tile_failed(&mut self, key: TileStyleKey, error: AzString) {
984
2
        self.tiles.insert(key, TileEntry::Failed { error });
985
2
    }
986

            
987
    /// The best SVG the cache can show for `tile` right now, preferring
988
    /// `want` — the depth in "fix the fallback in depth".
989
    ///
990
    /// An exact hit is used as-is. Otherwise ANY other look of the same tile is
991
    /// returned: real cartography in the wrong palette beats a grey placeholder,
992
    /// and it is already in memory. This is what removes the flash of empty grid
993
    /// when the colour scheme flips — the previous look keeps painting until the
994
    /// restyle lands, and the restyle needs no network.
995
    #[must_use]
996
1104
    pub fn best_available_svg(
997
1104
        &self,
998
1104
        tile: MapTileId,
999
1104
        want: MapLook,
1104
    ) -> Option<(MapLook, AzString)> {
1104
        if let Some(TileEntry::Ready { svg }) = self.tiles.get(&TileStyleKey { tile, look: want }) {
12
            return Some((want, svg.clone()));
1092
        }
1092
        self.tiles.iter().find_map(|(k, e)| match e {
15
            TileEntry::Ready { svg } if k.tile == tile => Some((k.look, svg.clone())),
20707
            _ => None,
20708
        })
1104
    }
    /// Bound the tile cache by evicting tiles far from the current viewport.
    ///
    /// Without this, `tiles` grows without limit - panning across the world or
    /// zooming in and out keeps every tile ever fetched (each decoded SVG is
    /// tens-to-hundreds of KB), so a long session leaks memory. Called after a
    /// viewport change once the new view's tiles are queued.
    ///
    /// Eviction is viewport-distance based (the right policy for spatial data,
    /// stronger than plain LRU): each tile is scored by zoom mismatch + squared
    /// distance from the viewport centre (projected into the current zoom's tile
    /// space), and the farthest are dropped first. IN-FLIGHT tiles
    /// (`Pending`/`Fetching`) are never evicted (their worker would write into a
    /// gone entry), and on-screen tiles score near-zero so they survive.
14
    pub fn prune_distant_tiles(&mut self) {
        const MAX_CACHED_TILES: usize = 192;
14
        if self.tiles.len() <= MAX_CACHED_TILES {
4
            return;
10
        }
        // Higher score = farther from what the user sees = evict sooner.
        //
        // An entry for a look we are NOT showing is the first thing to go: it is
        // a stand-in for a flip that has already happened, not what the user is
        // looking at. Without this penalty the budget below is a budget on
        // (tile, look) PAIRS, so while two looks are live the number of distinct
        // tiles the cache can hold would silently halve.
        const OTHER_LOOK_PENALTY: f64 = 1.0e9;
10
        let active = self.active_look;
2448
        let score = |k: &TileStyleKey| {
2448
            tile_viewport_score(&self.viewport, &self.layer, k.tile)
2448
                + if k.look == active {
2448
                    0.0
                } else {
                    OTHER_LOOK_PENALTY
                }
2448
        };
10
        let mut evictable: Vec<(f64, TileStyleKey)> = self
10
            .tiles
10
            .iter()
2705
            .filter(|(_, e)| !matches!(e, TileEntry::Pending | TileEntry::Fetching))
2448
            .map(|(k, _)| (score(k), *k))
10
            .collect();
        // Farthest first.
19550
        evictable.sort_by(|a, b| b.0.partial_cmp(&a.0).unwrap_or(core::cmp::Ordering::Equal));
10
        let mut to_remove = self.tiles.len().saturating_sub(MAX_CACHED_TILES);
731
        for (_, k) in evictable {
730
            if to_remove == 0 {
9
                break;
721
            }
721
            self.tiles.remove(&k);
721
            to_remove -= 1;
        }
        // The byte cache is bounded by the same policy, but SEPARATELY: it is
        // keyed by coordinates, so one entry backs every look of a tile. Keep
        // bytes for any tile that still has a styled entry (a restyle must not
        // have to re-fetch), and drop the rest farthest-first.
10
        let live: alloc::collections::BTreeSet<MapTileId> =
10
            self.tiles.keys().map(|k| k.tile).collect();
10
        if self.tile_bytes.len() > MAX_CACHED_TILES {
            let mut byte_evictable: Vec<(f64, MapTileId)> = self
                .tile_bytes
                .keys()
                .filter(|t| !live.contains(t))
                .map(|t| (tile_viewport_score(&self.viewport, &self.layer, *t), *t))
                .collect();
            byte_evictable
                .sort_by(|a, b| b.0.partial_cmp(&a.0).unwrap_or(core::cmp::Ordering::Equal));
            let mut drop_n = self.tile_bytes.len().saturating_sub(MAX_CACHED_TILES);
            for (_, t) in byte_evictable {
                if drop_n == 0 {
                    break;
                }
                self.tile_bytes.remove(&t);
                drop_n -= 1;
            }
10
        }
14
    }
    /// Every `Pending` tile, NEAREST TO THE VIEWPORT CENTRE FIRST — the
    /// order fetches are spawned in (`spawn_pending_tile_fetches` takes the
    /// head of this list each sweep).
    ///
    /// The sweep used to walk the `BTreeMap` in key order, `(z, x, y)`
    /// ascending: the first column spawned was the INVISIBLE west margin,
    /// and a lower-zoom leftover was fetched before anything at the current
    /// zoom — tiles visibly loaded from the left edge inwards. Ordering by
    /// [`tile_viewport_score`] puts the tile under the user's eyes first and
    /// the margin and stale zooms last, with nothing cancelled or lost.
    #[must_use]
5
    pub fn pending_tiles_nearest_first(&self) -> Vec<TileStyleKey> {
5
        let mut pending: Vec<(f64, TileStyleKey)> = self
5
            .tiles
5
            .iter()
65
            .filter(|(_, e)| matches!(e, TileEntry::Pending))
46
            .map(|(k, _)| (tile_viewport_score(&self.viewport, &self.layer, k.tile), *k))
5
            .collect();
        // Nearest first; the `(z, x, y)` key order breaks exact ties so the
        // result is deterministic.
176
        pending.sort_by(|a, b| {
176
            a.0.partial_cmp(&b.0)
176
                .unwrap_or(core::cmp::Ordering::Equal)
176
                .then_with(|| a.1.cmp(&b.1))
176
        });
5
        pending.into_iter().map(|(_, k)| k).collect()
5
    }
}
/// How far a tile is from what the viewport shows (lower = nearer).
///
/// Zoom mismatch first (10 000 per level), then the squared distance of the
/// tile's centre from the viewport centre, both measured in the CURRENT
/// zoom's tile space so tiles of different zooms compare. One function for
/// both the fetch order (nearest first) and the cache eviction (farthest
/// first), so the two can never disagree about what "near the user" means.
#[allow(clippy::suboptimal_flops)] // mul_add not guaranteed faster/available without target +fma; keep explicit a*b+c
#[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)] // bounded layout/render numeric
                                                                   // cast
2494
fn tile_viewport_score(viewport: &MapViewport, layer: &MapTileLayer, id: MapTileId) -> f64 {
2494
    let z = (viewport.zoom.floor() as i32)
2494
        .clamp(i32::from(layer.min_zoom), i32::from(layer.max_zoom)) as u8;
2494
    let tile_count = 1u32 << u32::from(z);
2494
    let cx = lon_to_tile_x(viewport.centre_lon_deg, f64::from(tile_count));
2494
    let cy = lat_to_tile_y(viewport.centre_lat_deg, f64::from(tile_count));
2494
    let zt_count = 1u32 << u32::from(id.z);
    // Project the tile's centre into the CURRENT zoom's tile space so
    // distances across zoom levels are comparable.
2494
    let scale = f64::from(tile_count) / f64::from(zt_count);
2494
    let tx = (f64::from(id.x) + 0.5) * scale;
2494
    let ty = (f64::from(id.y) + 0.5) * scale;
2494
    let dz = f64::from((i32::from(id.z) - i32::from(z)).abs());
2494
    let dx = tx - cx;
2494
    let dy = ty - cy;
2494
    dz * 10_000.0 + dx * dx + dy * dy
2494
}
#[derive(Debug, Clone)]
pub enum TileEntry {
    /// Needed by the viewport, fetch not yet spawned.
    Pending,
    /// A worker thread is fetching / decoding this tile right now.
    /// Distinct from `Pending` so the spawn pass doesn't double-fire.
    Fetching,
    /// Tile decoded into an SVG document. Held as the raw SVG
    /// string for now; the `VirtualView` callback will feed it
    /// through the framework's svg-to-dom pipeline on the next
    /// re-render.
    Ready { svg: AzString },
    /// Fetch failed. Held so the framework doesn't immediately
    /// re-try the same URL - caller can choose to clear failed
    /// entries on retry.
    Failed { error: AzString },
}
/// Worker-thread input: which tile to fetch, the resolved URL, and the
/// `MapCSS` stylesheet to apply when converting features to SVG. Boxed
/// into the `Thread::create` init `RefAny`.
#[derive(Debug, Clone)]
pub struct TileFetchInit {
    pub tile: MapTileId,
    pub url: AzString,
    /// The `MapCSS` to decode with: `MapTileLayer::effective_style_css` for
    /// the look being styled (empty = the built-in palette).
    pub style_css: AzString,
    /// The look `style_css` belongs to — echoed back in `TileReadyMsg` so the
    /// result is filed under the key it was actually styled for.
    pub look: MapLook,
    /// The tile's MVT payload when the cache already holds it.
    ///
    /// Non-empty turns this job into a pure RESTYLE: the worker decodes and
    /// styles these bytes and issues NO network request. This is what makes a
    /// colour-scheme flip cost zero fetches — the geometry is already here, only
    /// the palette changed.
    pub bytes: azul_css::U8Vec,
    /// The connection pool to download through, from the map's [`MapSetup`].
    /// `None` opens a connection for this tile alone.
    pub client: OptionHttpClient,
}
/// Worker-thread output, sent back via `ThreadWriteBackMsg`. The
/// `map_tile_writeback` callback downcasts to this and stamps the
/// cache.
#[derive(Debug, Clone)]
pub struct TileReadyMsg {
    pub tile: MapTileId,
    /// Decoded SVG document for the tile, or empty on failure (with
    /// `error` set).
    pub svg: AzString,
    /// Empty on success; an error message on failure.
    pub error: AzString,
    /// The look the SVG was decoded for (from `TileFetchInit::look`).
    pub look: MapLook,
    /// The MVT payload the worker downloaded, handed back so the main thread
    /// can cache it and never fetch this tile again. Empty when the worker was
    /// given bytes to begin with (a restyle) — there is nothing new to store.
    pub bytes: azul_css::U8Vec,
}
// ────────── Merge callback — cache survives relayout ─────────────────
/// Copy every entry from the previous frame's cache into the new
/// frame's cache. The next layout pass thus sees the same in-flight /
/// decoded set without re-fetching anything.
9
extern "C" fn merge_map_tile_cache(mut new_data: RefAny, mut old_data: RefAny) -> RefAny {
    // SHARE the previous cache across the relayout — do NOT copy its tiles into
    // the freshly-built one. The tile-fetch worker threads each hold a clone of
    // THIS very `RefAny` (handed to them at spawn time); returning it keeps their
    // writebacks landing in the same cache the VirtualView reads. The reconcile
    // pass re-points the VirtualView node's `refany` at this returned dataset
    // (core::diff::transfer_states), so the pure content callback reads it too.
    //
    // The old behaviour returned a fresh `new_data` with the old tiles *copied*
    // in. That orphaned the workers' clone after the first relayout: every tile
    // arriving later was written into the old, no-longer-rendered cache, so the
    // map stayed blank. Returning the persistent (old) cache fixes it at the root
    // — workers, dataset and VirtualView all reference one underlying allocation.
    //
    // The freshly-built `new_data` carries the layout-callback-controlled
    // CONFIG: the hooks and — critically — the viewport/layer the app passed
    // to `with_viewport()` / `create()` for THIS build. Adopt those into the persistent cache: app callbacks (zoom
    // buttons, Recentre, Locate) mutate app state and return RefreshDom, and
    // the merge previously discarded that new viewport ("viewport intact"),
    // so external viewport changes never took effect — only the widget's
    // internal drag/wheel (which mutate the persistent cache directly)
    // worked. Widget-internal changes stay consistent because every build's
    // `with_viewport()` receives the app state, which the on_viewport_changed
    // hook keeps in sync with internal pans/zooms.
    {
9
        let new_g = new_data.downcast_ref::<MapTileCache>();
9
        let old_guard = old_data.downcast_mut::<MapTileCache>();
9
        if let (Some(new_g), Some(mut old_g)) = (new_g, old_guard) {
6
            old_g.viewport = new_g.viewport;
6
            // Adopt the app's layer verbatim. There is nothing to invalidate: if
6
            // the app switched cartography, the next render simply looks up a
6
            // different key, and the tiles for the previous look stay cached (so
6
            // switching back is instant, and they serve as the fallback until
6
            // the new look styles). This block used to force `decoded_theme` to
6
            // a sentinel so `adopt_theme` would push every decoded tile back to
6
            // `Pending` — a rebuild that re-declared the SAME layer therefore
6
            // threw away and re-fetched the whole viewport.
6
            old_g.layer = new_g.layer.clone();
6
            old_g.on_viewport_changed = new_g.on_viewport_changed.clone();
6
            // The hook is adopted like the others, but `setup` and the fetch
6
            // worker are NOT: those were installed on mount and belong to the
6
            // widget instance, which is exactly what surviving the rebuild means.
6
            old_g.on_mount = new_g.on_mount.clone();
6
        }
    }
9
    old_data
9
}
// ────────── Pan + zoom callbacks ─────────────────────────────────────
use azul_core::{
    callbacks::{TimerCallbackReturn, Update},
    task::{Duration, SystemTimeDiff, TerminateTimer, TimerId},
};
use crate::{
    callbacks::CallbackInfo,
    http::{HttpClient, OptionHttpClient},
    thread::{OptionThreadPool, ThreadPool},
    timer::{Timer, TimerCallback, TimerCallbackInfo},
};
// --- User hook: on_viewport_changed (backreference DI, FFI-exposed) ---
/// User hook fired when the user pans or zooms the map.
///
/// Lets app code observe or persist the widget-driven `MapViewport` (which
/// otherwise lives only in the opaque `MapTileCache`).
pub type MapViewportChangedCallbackType =
    extern "C" fn(RefAny, CallbackInfo, MapViewport) -> Update;
impl_widget_callback!(
    MapViewportChanged,
    OptionMapViewportChanged,
    MapViewportChangedCallback,
    MapViewportChangedCallbackType
);
azul_core::impl_managed_callback! {
    wrapper:        MapViewportChangedCallback,
    info_ty:        CallbackInfo,
    return_ty:      Update,
    default_ret:    Update::DoNothing,
    invoker_static: MAP_VIEWPORT_CHANGED_INVOKER,
    invoker_ty:     AzMapViewportChangedCallbackInvoker,
    thunk_fn:       az_map_viewport_changed_callback_thunk,
    setter_fn:      AzApp_setMapViewportChangedCallbackInvoker,
    from_handle_fn: AzMapViewportChangedCallback_createFromHostHandle,
    extra_args:     [ viewport: MapViewport ],
}
/// Invoke a map widget's optional `on_viewport_changed` hook with the new
/// viewport, returning the user's `Update` (`DoNothing` if no hook is set).
///
/// `#[must_use]`: the crate denies `unused_must_use`, so a handler that
/// calls the hook and throws its answer away no longer compiles. Every
/// pointer / wheel handler used to do exactly that and return `DoNothing`
/// itself, so an app's `RefreshDom` from the hook never happened — the
/// demo's zoom/centre readout froze during drags and wheel zooms.
#[must_use]
8
fn invoke_viewport_changed(
8
    hook: &OptionMapViewportChanged,
8
    info: &CallbackInfo,
8
    viewport: MapViewport,
8
) -> Update {
8
    match hook {
4
        OptionMapViewportChanged::Some(h) => (h.callback.cb)(h.refany.clone(), *info, viewport),
4
        OptionMapViewportChanged::None => Update::DoNothing,
    }
8
}
// --- User hook: on_pin_tap (backreference DI, FFI-exposed) ---
/// User hook fired when the user taps the map (a press + release at ~the same
/// point, no pan/pinch).
///
/// Receives the tapped [`MapLatLon`] (projected via
/// [`MapWidget::latlon_at_px`]) so apps can drop a pin without wiring their own
/// tap handling + projection.
pub type MapPinTapCallbackType = extern "C" fn(RefAny, CallbackInfo, MapLatLon) -> Update;
impl_widget_callback!(
    MapPinTap,
    OptionMapPinTap,
    MapPinTapCallback,
    MapPinTapCallbackType
);
azul_core::impl_managed_callback! {
    wrapper:        MapPinTapCallback,
    info_ty:        CallbackInfo,
    return_ty:      Update,
    default_ret:    Update::DoNothing,
    invoker_static: MAP_PIN_TAP_INVOKER,
    invoker_ty:     AzMapPinTapCallbackInvoker,
    thunk_fn:       az_map_pin_tap_callback_thunk,
    setter_fn:      AzApp_setMapPinTapCallbackInvoker,
    from_handle_fn: AzMapPinTapCallback_createFromHostHandle,
    extra_args:     [ coord: MapLatLon ],
}
/// Invoke a map widget's optional `on_pin_tap` hook with the tapped coordinate.
/// `#[must_use]` for the same reason as [`invoke_viewport_changed`].
#[must_use]
4
fn invoke_pin_tap(hook: &OptionMapPinTap, info: &CallbackInfo, coord: MapLatLon) -> Update {
4
    match hook {
2
        OptionMapPinTap::Some(h) => (h.callback.cb)(h.refany.clone(), *info, coord),
2
        OptionMapPinTap::None => Update::DoNothing,
    }
4
}
// --- User hook: on_mount (backreference DI, FFI-exposed) ---
/// What a map's tile fetches run on. Handed to the `on_mount` hook, which
/// returns the setup the map uses from then on.
///
/// The default shares nothing: each tile opens its own connection on its own
/// thread, which is also what a map without an `on_mount` hook does.
#[derive(Debug, Clone, PartialEq)]
#[repr(C)]
pub struct MapSetup {
    /// Connection pool for tile downloads. `None`: one connection per tile.
    pub http_client: OptionHttpClient,
    /// Workers that run tile downloads. `None`: one thread per tile.
    pub thread_pool: OptionThreadPool,
    /// Most tiles downloading at once. 0 = no limit (at most 16 are started
    /// per pass).
    pub max_in_flight: u32,
}
impl Default for MapSetup {
    fn default() -> Self {
        Self::new()
    }
}
impl MapSetup {
    /// A setup that shares nothing and limits nothing
    #[must_use]
173
    pub const fn new() -> Self {
173
        Self {
173
            http_client: OptionHttpClient::None,
173
            thread_pool: OptionThreadPool::None,
173
            max_in_flight: 0,
173
        }
173
    }
    /// Download tiles through a shared connection pool
    #[must_use]
    pub fn with_http_client(mut self, client: HttpClient) -> Self {
        self.http_client = OptionHttpClient::Some(client);
        self
    }
    /// Run tile downloads on a shared thread pool
    #[must_use]
1
    pub fn with_thread_pool(mut self, pool: ThreadPool) -> Self {
1
        self.thread_pool = OptionThreadPool::Some(pool);
1
        self
1
    }
    /// Limit how many tiles download at once (0 = no limit)
    #[must_use]
3
    pub const fn with_max_in_flight(mut self, max_in_flight: u32) -> Self {
3
        self.max_in_flight = max_in_flight;
3
        self
3
    }
}
/// User hook fired when the map is mounted: receives the map's current
/// [`MapSetup`] and returns the one its tile fetches should use.
pub type MapMountCallbackType = extern "C" fn(RefAny, CallbackInfo, MapSetup) -> MapSetup;
impl_widget_callback!(
    MapMount,
    OptionMapMount,
    MapMountCallback,
    MapMountCallbackType
);
azul_core::impl_managed_callback! {
    wrapper:        MapMountCallback,
    info_ty:        CallbackInfo,
    return_ty:      MapSetup,
    default_ret:    MapSetup::new(),
    invoker_static: MAP_MOUNT_INVOKER,
    invoker_ty:     AzMapMountCallbackInvoker,
    thunk_fn:       az_map_mount_callback_thunk,
    setter_fn:      AzApp_setMapMountCallbackInvoker,
    from_handle_fn: AzMapMountCallback_createFromHostHandle,
    extra_args:     [ setup: MapSetup ],
}
/// Everything a map needs before its first fetch, done once it is in the tree
/// rather than while `layout()` describes it: the built-in tile worker, then
/// whatever the app's `on_mount` hook wants the fetches to run on.
4
fn mount_map(data: &mut RefAny, info: &CallbackInfo) {
3
    let (hook, setup) = {
4
        let Some(mut cache) = data.downcast_mut::<MapTileCache>() else {
1
            return;
        };
3
        if cache.fetch_callback.is_none() {
3
            cache.fetch_callback = MAP_TILE_FETCHER.get().cloned();
3
        }
3
        (cache.on_mount.clone(), cache.setup.clone())
    };
3
    let OptionMapMount::Some(hook) = hook else {
2
        return;
    };
    // The cache is released while the hook runs: it is app code and may reach
    // back into this map.
1
    let setup = (hook.callback.cb)(hook.refany, *info, setup);
1
    if let Some(mut cache) = data.downcast_mut::<MapTileCache>() {
1
        cache.setup = setup;
1
    }
4
}
/// The tail every viewport-changing handler shares: fire the user's
/// `on_viewport_changed` hook and RETURN ITS `Update`.
///
/// A hook that asks for `RefreshDom` gets it — the merge callback keeps the
/// tile cache across the rebuild and the full layout path re-invokes the
/// `VirtualView`. A hook that answers `DoNothing` (or no hook at all) gets
/// the cheap in-place re-render instead, so the new viewport's tiles compute
/// without a DOM rebuild (see `map_tile_writeback` for why that path avoids
/// `RefreshDom`). Doing both on a `RefreshDom` would render the view twice.
6
fn finish_viewport_change(
6
    hook: &OptionMapViewportChanged,
6
    info: &mut CallbackInfo,
6
    viewport: MapViewport,
6
) -> Update {
6
    let user = invoke_viewport_changed(hook, info, viewport);
6
    if user == Update::DoNothing {
5
        info.trigger_all_virtual_view_rerender();
5
    }
6
    user
6
}
/// Pointer down → record the drag anchor. The widget knows nothing
/// about the user's overall state `RefAny` - only its own dataset -
/// so the anchor lives in `MapTileCache::drag_anchor`.
3
extern "C" fn map_on_pointer_down(mut data: RefAny, info: CallbackInfo) -> Update {
    #[cfg(feature = "std")]
3
    if std::env::var("AZ_MAP_DEBUG").is_ok() {
        eprintln!("[map] pointer_down fired");
3
    }
3
    let pos = match info.get_cursor_relative_to_node().into_option() {
2
        Some(p) => azul_core::geom::LogicalPosition::new(p.x, p.y),
1
        None => return Update::DoNothing,
    };
2
    if let Some(mut cache) = data.downcast_mut::<MapTileCache>() {
1
        cache.drag_anchor = Some(pos);
1
        cache.press_origin = Some(pos);
1
    }
2
    Update::DoNothing
3
}
/// Pointer move during an active drag → translate the pixel delta
/// into a lat/lon delta via the Web Mercator inverse and update
/// `viewport.centre_lat_deg / centre_lon_deg`. Updates the anchor so
/// the next move computes a fresh delta.
///
/// If a pinch gesture is in flight (two fingers on the widget), the
/// pan branch is skipped and the move event drives zoom instead -
/// `dz = log2(current_distance / pinch_anchor)`. The next move resets
/// the anchor to the current distance so the gesture stays
/// continuous across many frames.
#[allow(clippy::similar_names)] // domain-standard coordinate/geometry/short-lived names
5
extern "C" fn map_on_pointer_move(mut data: RefAny, mut info: CallbackInfo) -> Update {
    #[cfg(feature = "std")]
5
    if std::env::var("AZ_MAP_DEBUG").is_ok() {
        let dragging = data
            .downcast_ref::<MapTileCache>()
            .is_some_and(|c| c.drag_anchor.is_some());
        eprintln!("[map] pointer_move fired (dragging={dragging})");
5
    }
    // Active pinch wins over single-finger pan.
5
    if let Some(pinch) = info.get_pinch().into_option() {
        let Some(mut cache) = data.downcast_mut::<MapTileCache>() else {
            return Update::DoNothing;
        };
        let anchor = *cache.pinch_anchor.get_or_insert(pinch.current_distance);
        if anchor > 1.0 && pinch.current_distance > 1.0 {
            let dz = (pinch.current_distance / anchor).log2();
            let min = f32::from(cache.layer.min_zoom);
            let max = f32::from(cache.layer.max_zoom);
            cache.viewport.zoom = (cache.viewport.zoom + dz).clamp(min, max);
        }
        cache.pinch_anchor = Some(pinch.current_distance);
        // Pinch is exclusive with pan — clear the drag anchor so the
        // pinch end doesn't accidentally drop into a pan.
        cache.drag_anchor = None;
        let hook = cache.on_viewport_changed.clone();
        let vp = cache.viewport;
        drop(cache);
        return finish_viewport_change(&hook, &mut info, vp);
5
    }
5
    let pos = match info.get_cursor_relative_to_node().into_option() {
5
        Some(p) => azul_core::geom::LogicalPosition::new(p.x, p.y),
        None => return Update::DoNothing,
    };
5
    let Some(mut cache_guard) = data.downcast_mut::<MapTileCache>() else {
        return Update::DoNothing;
    };
    // A camera drag (right button held) tilts / rotates instead of panning.
5
    if let Some(tilt_anchor) = cache_guard.tilt_anchor {
        let (ddx, ddy) = (pos.x - tilt_anchor.x, pos.y - tilt_anchor.y);
        if ddx.abs() < 0.5 && ddy.abs() < 0.5 {
            return Update::DoNothing;
        }
        let (bearing, pitch) = camera_drag(
            cache_guard.viewport.bearing_deg,
            cache_guard.viewport.pitch_deg,
            ddx,
            ddy,
        );
        cache_guard.viewport.bearing_deg = bearing;
        cache_guard.viewport.pitch_deg = pitch;
        cache_guard.tilt_anchor = Some(pos);
        let hook = cache_guard.on_viewport_changed.clone();
        let vp = cache_guard.viewport;
        drop(cache_guard);
        return finish_viewport_change(&hook, &mut info, vp);
5
    }
5
    let Some(anchor) = cache_guard.drag_anchor else {
1
        return Update::DoNothing; // no active drag
    };
4
    let dx_px = f64::from(pos.x - anchor.x);
4
    let dy_px = f64::from(pos.y - anchor.y);
4
    if dx_px.abs() < 0.5 && dy_px.abs() < 0.5 {
1
        return Update::DoNothing;
3
    }
3
    let (new_lon, new_lat) = pan_viewport(
3
        cache_guard.viewport.centre_lat_deg,
3
        cache_guard.viewport.centre_lon_deg,
3
        f64::from(cache_guard.viewport.zoom),
3
        dx_px,
3
        dy_px,
3
    );
3
    cache_guard.viewport.centre_lon_deg = new_lon;
3
    cache_guard.viewport.centre_lat_deg = new_lat;
3
    cache_guard.drag_anchor = Some(pos);
3
    let hook = cache_guard.on_viewport_changed.clone();
3
    let vp = cache_guard.viewport;
3
    drop(cache_guard);
3
    finish_viewport_change(&hook, &mut info, vp)
5
}
/// A right-button drag moves the camera: horizontal pixels turn the
/// bearing (0.5 deg/px, clockwise when dragging right), vertical pixels
/// change the pitch (dragging UP tilts the view — `MapLibre`'s direction),
/// clamped to `0..=MAX_PITCH_DEG`. Pure, so the convention is pinned by a
/// test and shared by every backend.
#[must_use]
4
pub fn camera_drag(bearing_deg: f32, pitch_deg: f32, dx_px: f32, dy_px: f32) -> (f32, f32) {
    const DEG_PER_PX: f32 = 0.5;
4
    (
4
        normalize_bearing(bearing_deg + dx_px * DEG_PER_PX),
4
        clamp_pitch(pitch_deg - dy_px * DEG_PER_PX),
4
    )
4
}
/// Right button down on the canvas: start a camera drag (tilt / rotate).
extern "C" fn map_on_tilt_down(mut data: RefAny, info: CallbackInfo) -> Update {
    let pos = match info.get_cursor_relative_to_node().into_option() {
        Some(p) => azul_core::geom::LogicalPosition::new(p.x, p.y),
        None => return Update::DoNothing,
    };
    if let Some(mut cache) = data.downcast_mut::<MapTileCache>() {
        cache.tilt_anchor = Some(pos);
        // a camera drag is not a pan and not a tap
        cache.drag_anchor = None;
        cache.press_origin = None;
    }
    Update::DoNothing
}
/// Right button up: end the camera drag.
extern "C" fn map_on_tilt_up(mut data: RefAny, _info: CallbackInfo) -> Update {
    if let Some(mut cache) = data.downcast_mut::<MapTileCache>() {
        cache.tilt_anchor = None;
    }
    Update::DoNothing
}
/// A two-finger rotate gesture turns the bearing by the detected angle.
extern "C" fn map_on_rotate_gesture(mut data: RefAny, mut info: CallbackInfo) -> Update {
    let Some(rotation) = info.get_rotation().into_option() else {
        return Update::DoNothing;
    };
    let Some(mut cache_guard) = data.downcast_mut::<MapTileCache>() else {
        return Update::DoNothing;
    };
    let delta = rotation.angle_radians.to_degrees();
    if !delta.is_finite() || delta.abs() < 0.01 {
        return Update::DoNothing;
    }
    cache_guard.viewport.bearing_deg = normalize_bearing(cache_guard.viewport.bearing_deg + delta);
    let hook = cache_guard.on_viewport_changed.clone();
    let vp = cache_guard.viewport;
    drop(cache_guard);
    finish_viewport_change(&hook, &mut info, vp)
}
/// Pointer up / pointer leave → end the drag *and* the pinch. Either
/// can be in flight (and pinch supersedes pan in the move handler);
/// clear both anchors on release.
#[allow(clippy::suboptimal_flops)] // mul_add not guaranteed faster/available without target +fma;
                                   // keep explicit a*b+c
5
extern "C" fn map_on_pointer_up(mut data: RefAny, mut info: CallbackInfo) -> Update {
    // Cursor + container size for tap projection (read before borrowing data).
5
    let up_pos = info
5
        .get_cursor_relative_to_node()
5
        .into_option()
5
        .map(|p| azul_core::geom::LogicalPosition::new(p.x, p.y));
5
    let container = info
5
        .get_hit_node_rect()
5
        .map_or(azul_core::geom::LogicalSize::new(0.0, 0.0), |r| r.size);
5
    let (press, viewport, hook) = data.downcast_mut::<MapTileCache>().map_or_else(
1
        || (None, MapViewport::default(), OptionMapPinTap::None),
4
        |mut cache| {
4
            let out = (cache.press_origin, cache.viewport, cache.on_pin_tap.clone());
4
            cache.drag_anchor = None;
4
            cache.pinch_anchor = None;
4
            cache.press_origin = None;
            // a pointer leaving the canvas ends a camera drag too
4
            cache.tilt_anchor = None;
4
            out
4
        },
    );
    // A press + release at ~the same point (no pan/pinch) is a tap: project it
    // to lat/lon and fire the user's on_pin_tap hook — and carry its answer
    // out, so a hook that drops a pin with `RefreshDom` sees the pin now, not
    // after the next unrelated rebuild.
5
    let mut user = Update::DoNothing;
5
    if let (Some(origin), Some(up)) = (press, up_pos) {
3
        let dx = f64::from(up.x - origin.x);
3
        let dy = f64::from(up.y - origin.y);
3
        if dx * dx + dy * dy < 36.0 {
2
            let coord = MapWidget::latlon_at_px(viewport, up, container);
2
            user = invoke_pin_tap(&hook, &info, coord);
2
        }
2
    }
    // After a pan / pinch settles, kick off fetches for any tiles the new
    // viewport needs. (Only a `CallbackInfo`-bearing callback can spawn them.)
5
    spawn_pending_tile_fetches(&mut data, &mut info);
    // Re-render in place so Fetching/Ready states show as tiles arrive. The
    // worker writebacks will trigger further re-renders themselves. A hook
    // that asked for a rebuild gets the re-render from the full path instead.
5
    if user == Update::DoNothing {
4
        info.trigger_all_virtual_view_rerender();
4
    }
5
    user
5
}
/// Mouse-wheel / trackpad scroll over the map = ZOOM (Leaflet / Google-Maps
/// convention), not content scroll. The map's `VirtualView` has no scroll overflow,
/// so the framework's queued wheel deltas would otherwise be wasted - drain them
/// and apply as a zoom step, then queue + spawn the tiles the new zoom needs and
/// re-render in place.
2
extern "C" fn map_on_scroll(mut data: RefAny, mut info: CallbackInfo) -> Update {
    // Wheel delta that triggered this Scroll callback (sign = direction). The map
    // is not a scroll container, so this comes from the per-pass wheel delta, not
    // the scroll-physics input queue (which only feeds scrollable nodes).
2
    let dy: f32 = {
2
        let hn = info.get_hit_node();
2
        hn.node.into_crate_internal().map_or(0.0, |nid| {
            info.get_scroll_delta(hn.dom, nid).map_or(0.0, |d| d.y)
        })
    };
    #[cfg(feature = "std")]
2
    if std::env::var("AZ_MAP_DEBUG").is_ok() {
        eprintln!("[map] scroll fired dy={dy}");
2
    }
2
    if dy == 0.0 {
2
        return Update::DoNothing;
    }
    // The grid's on-screen rect is the widget size (needed to recompute the tiles
    // the new zoom needs).
    let bounds = info
        .get_hit_node_rect()
        .map_or(azul_core::geom::LogicalSize::new(0.0, 0.0), |r| r.size);
    let (vp, hook) = {
        let Some(mut cache) = data.downcast_mut::<MapTileCache>() else {
            return Update::DoNothing;
        };
        let min = f32::from(cache.layer.min_zoom);
        let max = f32::from(cache.layer.max_zoom);
        // Wheel-up (dy > 0) zooms IN, wheel-down zooms OUT (Leaflet /
        // Google-Maps). Proportional to the delta and bounded per event —
        // see `wheel_zoom_step` for why `signum() * 0.5` was a runaway.
        let dz = wheel_zoom_step(dy);
        cache.viewport.zoom = (cache.viewport.zoom + dz).clamp(min, max);
        let vp = cache.viewport;
        let layer = cache.layer.clone();
        let look = cache.current_look();
        cache.want_tiles(map_wanted_tiles(&vp, bounds, &layer), look);
        (vp, cache.on_viewport_changed.clone())
    };
    spawn_pending_tile_fetches(&mut data, &mut info);
    finish_viewport_change(&hook, &mut info, vp)
2
}
/// Wheel pixels per zoom level: one mouse notch (3 lines × 20 px on every
/// desktop backend) is half a level, the feel the map always had.
const WHEEL_PX_PER_ZOOM_LEVEL: f32 = 120.0;
/// No single wheel event moves more than this, whatever its delta.
const MAX_ZOOM_STEP_PER_WHEEL_EVENT: f32 = 0.5;
/// Wheel / trackpad delta (px, sign = direction) → zoom-level step.
///
/// A trackpad reports one two-finger flick as dozens of small precise
/// deltas plus a momentum tail — 20-40 events. The old `dy.signum() * 0.5`
/// charged every one of them a full half-level, so one flick ran from zoom
/// 2 to the layer's cap (where "+" then did nothing: the first symptom the
/// user reported). Proportional to the delta and bounded per event, a 60 px
/// notch is still 0.5, a 300 px flick is 2.5 levels, and a momentum tail of
/// 2-px events barely moves.
47
fn wheel_zoom_step(dy_px: f32) -> f32 {
47
    if !dy_px.is_finite() {
1
        return 0.0;
46
    }
46
    (dy_px / WHEEL_PX_PER_ZOOM_LEVEL).clamp(
46
        -MAX_ZOOM_STEP_PER_WHEEL_EVENT,
        MAX_ZOOM_STEP_PER_WHEEL_EVENT,
    )
47
}
87
fn wrap_lon(lon: f64) -> f64 {
    // `rem_euclid` (not `%`) so even large negative deltas normalise:
    // `%` follows the dividend's sign and would leak values < -180.
87
    (lon + 180.0).rem_euclid(360.0) - 180.0
87
}
// ────────── Web-Mercator (WGS-84 ↔ XYZ tile space) ───────────────────
//
// `tile_count` is `2^zoom`. Tile-space x grows east (0 at lon -180,
// `tile_count` at lon +180); y grows south (0 at the north edge
// ~85.05°, `tile_count` at the south edge). These four functions are
// exact inverses of each other and are the single source of truth for
// the widget's projection — `map_widget_render` forward-projects the
// viewport centre through them; tap-to-pin will inverse-project taps.
/// The steepest tilt the camera allows (`MapLibre`'s default maximum).
pub const MAX_PITCH_DEG: f32 = 60.0;
/// `pitch_deg` clamped to `0..=MAX_PITCH_DEG` (NaN -> 0).
#[must_use]
#[allow(clippy::missing_const_for_fn)] // `f32::clamp` is not const on the CI toolchain
245
pub fn clamp_pitch(pitch_deg: f32) -> f32 {
245
    if pitch_deg.is_finite() {
244
        pitch_deg.clamp(0.0, MAX_PITCH_DEG)
    } else {
1
        0.0
    }
245
}
/// `bearing_deg` wrapped into `-180..180` (NaN -> 0).
#[must_use]
246
pub const fn normalize_bearing(bearing_deg: f32) -> f32 {
246
    if !bearing_deg.is_finite() {
        return 0.0;
246
    }
246
    let mut b = bearing_deg % 360.0;
246
    if b >= 180.0 {
2
        b -= 360.0;
244
    } else if b < -180.0 {
1
        b += 360.0;
243
    }
246
    b
246
}
/// The CSS transform that tilts / rotates the tile canvas for a viewport,
/// `None` for the flat view (no transform, no layer promotion, no cost).
/// `perspective()` is the camera distance — 1.5x the larger viewport side,
/// a natural "standing above the map" look; `rotateX` leans the top edge
/// away (`MapLibre`'s pitch), `rotate` applies the bearing; all about the
/// canvas centre.
#[must_use]
84
pub fn camera_transform_css(
84
    viewport: &MapViewport,
84
    width_px: f32,
84
    height_px: f32,
84
) -> Option<String> {
84
    let pitch = clamp_pitch(viewport.pitch_deg);
84
    let bearing = normalize_bearing(viewport.bearing_deg);
84
    if pitch.abs() < 0.01 && bearing.abs() < 0.01 {
83
        return None;
1
    }
1
    let distance = (width_px.max(height_px) * 1.5).max(100.0);
1
    Some(format!(
1
        "transform: perspective({distance:.0}px) rotateX({pitch:.2}deg) rotate({bearing:.2}deg); \
1
         transform-origin: 50% 50%;"
1
    ))
84
}
/// How much MORE of the flat plane a tilted / rotated camera can see than
/// the straight-down one, as `(width, height)` multipliers for the tile
/// range: a rotation's axis-aligned bounding box (`w|cos| + h|sin|`), and
/// a pitch that shows the far ground at the top (up to 2x the rows at
/// [`MAX_PITCH_DEG`]). `(1, 1)` for the flat view.
#[must_use]
155
pub fn camera_overscan(viewport: &MapViewport, width_px: f32, height_px: f32) -> (f32, f32) {
155
    let pitch = clamp_pitch(viewport.pitch_deg);
155
    let bearing = normalize_bearing(viewport.bearing_deg);
155
    if pitch.abs() < 0.01 && bearing.abs() < 0.01 {
152
        return (1.0, 1.0);
3
    }
3
    let (w, h) = (width_px.max(1.0), height_px.max(1.0));
3
    let (s, c) = bearing.to_radians().sin_cos();
3
    let rot_w = (w * c.abs() + h * s.abs()) / w;
3
    let rot_h = (w * s.abs() + h * c.abs()) / h;
3
    let tilt = 1.0 + pitch / MAX_PITCH_DEG;
3
    (rot_w * (1.0 + pitch / (2.0 * MAX_PITCH_DEG)), rot_h * tilt)
155
}
/// Longitude (deg) → fractional tile-x at the given `tile_count`.
2719
fn lon_to_tile_x(lon_deg: f64, tile_count: f64) -> f64 {
2719
    (lon_deg + 180.0) / 360.0 * tile_count
2719
}
/// Latitude (deg) → fractional tile-y at the given `tile_count`.
2710
fn lat_to_tile_y(lat_deg: f64, tile_count: f64) -> f64 {
2710
    let lat_rad = lat_deg.to_radians();
2710
    let mercator = (1.0 - (lat_rad.tan() + 1.0 / lat_rad.cos()).ln() / core::f64::consts::PI) / 2.0;
2710
    mercator * tile_count
2710
}
/// Fractional tile-x → longitude (deg). Inverse of [`lon_to_tile_x`].
/// Verified against the forward direction in the tests below; the
/// upcoming tap-to-pin handler reuses it to turn a tap into a lat/lon.
#[allow(dead_code)]
#[allow(clippy::suboptimal_flops)] // mul_add not guaranteed faster/available without target +fma;
                                   // keep explicit a*b+c
56
fn tile_x_to_lon(x: f64, tile_count: f64) -> f64 {
56
    x / tile_count * 360.0 - 180.0
56
}
/// Fractional tile-y → latitude (deg). Inverse of [`lat_to_tile_y`].
#[allow(dead_code)]
57
fn tile_y_to_lat(y: f64, tile_count: f64) -> f64 {
57
    let n = core::f64::consts::PI * (1.0 - 2.0 * y / tile_count);
57
    n.sinh().atan().to_degrees()
57
}
/// Apply a drag of `(dx_px, dy_px)` screen pixels to a viewport centre,
/// returning the new `(centre_lon_deg, centre_lat_deg)`. Dragging right
/// (+dx) pans the map content right, i.e. recentres on a *lower* longitude
/// (hence the minus). Latitude uses the small-angle Mercator approximation
/// (`d_lat ≈ dy·cos(lat)·360/world`), accurate to a few metres at city
/// zooms; the exact inverse only matters for very long drags near the
/// poles. Longitude wraps to [-180, 180); latitude clamps to the
/// Web-Mercator ±85.05° limit. The shared, unit-tested core of
/// `map_on_pointer_move`.
#[allow(clippy::suboptimal_flops)] // mul_add not guaranteed faster/available without target +fma; keep explicit a*b+c
#[allow(clippy::similar_names)] // domain-standard coordinate/geometry/short-lived names
47
fn pan_viewport(
47
    centre_lat_deg: f64,
47
    centre_lon_deg: f64,
47
    zoom: f64,
47
    dx_px: f64,
47
    dy_px: f64,
47
) -> (f64, f64) {
    // World pixels at the current fractional zoom (256 px / tile).
47
    let world_px = 256.0 * (2.0_f64).powf(zoom);
47
    let d_lon = -dx_px * 360.0 / world_px;
47
    let d_lat = dy_px * 360.0 / world_px * centre_lat_deg.to_radians().cos();
47
    let new_lon = wrap_lon(centre_lon_deg + d_lon);
47
    let new_lat = (centre_lat_deg + d_lat).clamp(-85.0, 85.0);
47
    (new_lon, new_lat)
47
}
/// Parse a standalone `<svg>…</svg>` string into a `Dom` subtree via
/// the framework's existing XML→DOM path.
///
/// The SVG is wrapped in a
/// minimal `<html><body>` envelope because `str_to_dom_unstyled`
/// expects a document root; the wrapper divs are zero-impact in
/// layout. Returns `None` if the `xml` feature is off or parsing
/// fails - the caller then falls back to the placeholder glyph.
// Render the decoded tile SVG to a COLOUR image node, reusing the framework's
// `render_svg_group` rasteriser (the one that renders the tiger), which honours
// the SVG `fill`/`stroke` attrs that `features_to_svg` emits. The DOM SVG path
// (`str_to_dom_unstyled` → `SvgNodeData::Path`) only produces a clip mask, so it
// cannot paint the feature colours — hence the tiles rendered grey.
#[cfg(all(feature = "xml", feature = "cpurender"))]
#[must_use]
36
pub fn svg_string_to_dom(svg: &str) -> Option<Dom> {
    // Rasterise at TWICE the tile's logical size. The image node below is laid
    // out at 100% of a 256px tile, so on a 2x display the tile occupies 512
    // device pixels — rasterising at 256 and letting the blit stretch it made
    // every map visibly soft. 2x is pixel-exact there, and on a 1x display the
    // blit AREA-AVERAGES on the way down, so the extra samples come back as
    // antialiasing rather than waste. (The honest fix is to rasterise at the
    // window's real DPI, but this function is on the tile-decode path and has
    // no window to ask; 2x covers every display azul currently ships on.)
    const TILE_RASTER_PX: u32 = 512;
16
    let img =
36
        crate::cpurender::render_svg_to_imageref(svg.as_bytes(), TILE_RASTER_PX, TILE_RASTER_PX)
36
            .ok()?;
16
    Some(
16
        Dom::create_image(img)
16
            .with_css("position: absolute; left: 0; top: 0; width: 100%; height: 100%;")
16
            // One piece of the map graphic, which the tile grid names: decorative,
16
            // so a screen reader does not read out every tile.
16
            .with_accessibility_info(azul_core::a11y::AccessibilityInfo {
16
                role: azul_core::a11y::AccessibilityRole::Nothing,
16
                ..Default::default()
16
            }),
16
    )
36
}
#[cfg(all(feature = "xml", not(feature = "cpurender")))]
pub fn svg_string_to_dom(svg: &str) -> Option<Dom> {
    use azul_core::xml::{str_to_dom_unstyled, ComponentMap};
    let wrapped = alloc::format!("<html><body>{}</body></html>", svg);
    let nodes = crate::xml::parse_xml_string(&wrapped).ok()?;
    let component_map = ComponentMap::default();
    str_to_dom_unstyled(nodes.as_ref(), &component_map).ok()
}
#[cfg(not(feature = "xml"))]
fn svg_string_to_dom(_svg: &str) -> Option<Dom> {
    // A tile/page SVG arrived and this build cannot turn it into a DOM — the
    // caller sees a permanent None, indistinguishable from a bad SVG.
    static ANNOUNCE: std::sync::Once = std::sync::Once::new();
    ANNOUNCE.call_once(|| {
        eprintln!(
            "[azul][svg] svg_string_to_dom called, but this build has no `xml` feature — \
             SVG-to-DOM always returns None. Rebuild azul-layout with the `xml` feature"
        );
    });
    None
}
/// Fires once when the widget first mounts. Kicks the initial tile
/// fetches so the map populates without waiting for a user gesture.
/// (The `VirtualView` marks the viewport's tiles `Pending` during the
/// layout pass that precedes mount-event dispatch; this handler then
/// spawns the workers for them.) Returns `RefreshDom` so the
/// `Fetching` state shows immediately.
2
extern "C" fn map_on_after_mount(mut data: RefAny, mut info: CallbackInfo) -> Update {
    #[cfg(feature = "std")]
2
    if std::env::var("AZ_MAP_DEBUG").is_ok() {
        eprintln!("[map] after_mount fired");
2
    }
2
    mount_map(&mut data, &info);
2
    spawn_pending_tile_fetches(&mut data, &mut info);
    // Install a low-frequency sweep timer. Pointer/scroll/after_mount spawn
    // fetches directly, but a viewport change that originates from a *rebuild*
    // (an app's zoom/recentre button → with_viewport) marks new tiles `Pending`
    // in the VirtualView render, which has no `add_thread` — so without this
    // sweep the map would sit grey after a button-zoom until the next
    // drag/wheel. The timer's cache clone tracks the persistent dataset
    // `transfer_states` keeps across rebuilds, so it stays unified.
2
    let sweep = Timer::create(
2
        data.clone(),
2
        TimerCallback::create(map_fetch_sweep_tick),
2
        info.get_system_time_fn(),
    )
2
    .with_interval(Duration::System(SystemTimeDiff::from_millis(250)));
2
    info.add_timer(TimerId::unique(), sweep);
    // Re-render the VirtualView IN PLACE (not RefreshDom). RefreshDom would
    // rebuild the DOM, allocate a fresh MapTileCache, and orphan the clone of
    // the cache we just handed the worker threads — their tiles would then write
    // to a cache nobody renders. The dataset is shared via the construction-time
    // RefAny::clone(), so re-invoking in place lets the workers' writes land in
    // the same cache the VirtualView reads.
2
    info.trigger_all_virtual_view_rerender();
2
    Update::DoNothing
2
}
/// Scan the cache for `Pending` tiles and spawn one framework `Thread`
/// per tile (capped per call so a big viewport jump doesn't spawn
/// hundreds at once). Each thread gets:
/// - init `RefAny` = `TileFetchInit { tile, url }`
/// - writeback `RefAny` = a clone of the cache dataset, so `map_tile_writeback` mutates the same
///   cache the `VirtualView` reads.
///
/// Tiles transition `Pending → Fetching` here so they aren't
/// re-spawned next frame. No-op when the cache has no `fetch_callback`.
11
fn spawn_pending_tile_fetches(data: &mut RefAny, info: &mut CallbackInfo) {
    use azul_core::task::ThreadId;
    use crate::thread::Thread;
    // THE CASCADE'S ANSWER, taken here because this is the earliest point the
    // widget has a `CallbackInfo` (mount, the 250 ms sweep, every pointer
    // event all funnel through here). `SystemStyle::theme` is the same value
    // `DynamicSelectorContext::from_system_style` feeds to
    // `prefers-color-scheme`, so the tiles and the app's stylesheet resolve
    // light/dark from one source instead of two.
11
    let scheme = MapColorScheme::from_system_theme(info.get_system_style().theme);
    // Collect the work first (URL build + state flip) under one borrow,
    // then spawn outside it so we don't hold the cache lock across
    // `info.add_thread`.
11
    let mut to_spawn: Vec<TileFetchInit> = Vec::new();
    {
11
        let Some(mut cache) = data.downcast_mut::<MapTileCache>() else {
            // The dataset handed to this callback is not a MapTileCache. Every
            // tile stays Pending forever and the map shows placeholders — the
            // exact symptom of "panning works, tiles never paint".
            #[cfg(feature = "std")]
3
            if std::env::var("AZ_MAP_DEBUG").is_ok() {
                std::eprintln!("[map] spawn_pending: ABORT — dataset is not a MapTileCache");
3
            }
3
            return;
        };
8
        if cache.fetch_callback.is_none() {
            // No worker wired. Either the build has no `map-tiles` feature, or
            // the callback was lost when the cache RefAny was rebuilt (the
            // merge callback is what preserves it across relayout).
            #[cfg(feature = "std")]
6
            if std::env::var("AZ_MAP_DEBUG").is_ok() {
                std::eprintln!(
                    "[map] spawn_pending: ABORT — no fetch_callback on the cache ({} tiles held)",
                    cache.tiles.len()
                );
6
            }
6
            return; // no worker wired — leave tiles Pending (placeholder grid)
2
        }
2
        cache.cascade_scheme = Some(scheme);
2
        let look = cache.current_look();
2
        let look_changed = cache.set_active_look(look);
        #[cfg(feature = "std")]
2
        if std::env::var("AZ_MAP_DEBUG").is_ok() {
            std::eprintln!(
                "[map] spawn_pending: system theme={:?} scheme={:?} look={:?} \
                 look_changed={look_changed} ready={} pending={}",
                info.get_system_style().theme,
                scheme,
                look,
                cache
                    .tiles
                    .values()
                    .filter(|e| matches!(e, TileEntry::Ready { .. }))
                    .count(),
                cache
                    .tiles
                    .values()
                    .filter(|e| matches!(e, TileEntry::Pending))
                    .count(),
            );
2
        }
        // A tile that is merely QUEUED for a look we have since left is RE-KEYED
        // to the look we now want — not dropped, and never fetched for the look
        // nobody is going to look at. `Pending` means "wanted, nothing done
        // yet"; what is wanted has not changed, only which sheet to style it
        // with, so moving the key preserves the request and costs no request.
        //
        // Dropping them instead strands the FIRST render's queue on every cold
        // start: that render runs before any callback exists, so it keys at the
        // layer's own look — `System`, which is never equal to the look `System`
        // resolves to — and the whole viewport would have to be re-queued by the
        // next render before anything could be fetched.
        //
        // `Fetching` / `Ready` / `Failed` are never touched here: those hold real
        // work, and each is already filed under the look it belongs to.
2
        let stale: Vec<TileStyleKey> = cache
2
            .tiles
2
            .iter()
40
            .filter(|(k, e)| matches!(e, TileEntry::Pending) && k.look != look)
2
            .map(|(k, _)| *k)
2
            .collect();
2
        for k in stale {
            cache.tiles.remove(&k);
            cache
                .tiles
                .entry(TileStyleKey { tile: k.tile, look })
                .or_insert(TileEntry::Pending);
        }
2
        cache.drop_unwanted_pending();
2
        let template = cache.layer.url_template.as_str().to_string();
2
        let budget = spawn_budget(&cache);
        // Centre-out: the tiles under the user's eyes first, the off-screen
        // margin and other-zoom leftovers last (see `pending_tiles_nearest_first`).
2
        let pending: Vec<TileStyleKey> = cache
2
            .pending_tiles_nearest_first()
2
            .into_iter()
2
            .take(budget)
2
            .collect();
22
        for key in pending {
20
            let url = build_tile_url(&template, key.tile);
            // Each job is styled for the look ITS KEY asks for, not for one
            // cache-wide "current" look. A sweep can therefore legitimately
            // carry jobs for two different looks at once (the new one being
            // filled in, an old one still queued) and neither invalidates the
            // other.
20
            let style_css = cache.layer.effective_style_css_for(key.look);
            // Bytes already in hand => restyle, no network. This is the whole
            // point of the split cache: geometry is fetched once per tile, ever.
20
            let bytes = cache
20
                .tile_bytes
20
                .get(&key.tile)
20
                .cloned()
20
                .unwrap_or_else(|| azul_css::U8Vec::from_vec(Vec::new()));
20
            cache.tiles.insert(key, TileEntry::Fetching);
20
            to_spawn.push(TileFetchInit {
20
                tile: key.tile,
20
                url: AzString::from(url),
20
                style_css,
20
                look: key.look,
20
                bytes,
20
                client: cache.setup.http_client.clone(),
20
            });
        }
        // Now that the current view's tiles are queued (Fetching, so eviction
        // protects them), bound the cache by dropping tiles far from the
        // viewport — otherwise panning/zooming grows it without limit.
2
        cache.prune_distant_tiles();
    }
2
    let (cb, pool) = {
2
        let Some(cache) = data.downcast_ref::<MapTileCache>() else {
            #[cfg(feature = "std")]
            if std::env::var("AZ_MAP_DEBUG").is_ok() {
                std::eprintln!("[map] spawn_pending: ABORT — dataset vanished before spawn");
            }
            return;
        };
2
        let Some(cb) = cache.fetch_callback.as_ref() else {
            #[cfg(feature = "std")]
            if std::env::var("AZ_MAP_DEBUG").is_ok() {
                std::eprintln!("[map] spawn_pending: ABORT — fetch_callback gone at spawn");
            }
            return;
        };
2
        (cb.clone(), cache.setup.thread_pool.clone())
    };
    #[cfg(feature = "std")]
2
    let spawn_count = to_spawn.len();
    // Distinguish "nothing to do" from "never got here" — a quiet log and an
    // aborted one look identical otherwise.
    #[cfg(feature = "std")]
2
    if spawn_count == 0 && std::env::var("AZ_MAP_DEBUG").is_ok() {
        std::eprintln!("[map] spawn_pending: 0 tiles were Pending (nothing to spawn)");
2
    }
22
    for init in to_spawn {
20
        let init_data = RefAny::new(init);
20
        let writeback_data = data.clone(); // same cache dataset
20
        let thread = match &pool {
            OptionThreadPool::Some(pool) => {
                pool.create_thread(init_data, writeback_data, cb.clone())
            }
20
            OptionThreadPool::None => Thread::create(init_data, writeback_data, cb.clone()),
        };
20
        info.add_thread(ThreadId::unique(), thread);
    }
    #[cfg(feature = "std")]
2
    if std::env::var("AZ_MAP_DEBUG").is_ok() {
        eprintln!("[map] spawn_pending: {spawn_count} thread(s) spawned");
2
    }
11
}
/// How many new fetches one pass may start: the room left under the setup's
/// `max_in_flight`, or [`MAX_SPAWN_PER_CALL`] when there is no limit.
5
fn spawn_budget(cache: &MapTileCache) -> usize {
5
    match cache.setup.max_in_flight {
3
        0 => MAX_SPAWN_PER_CALL,
2
        cap => {
2
            let in_flight = cache
2
                .tiles
2
                .values()
21
                .filter(|e| matches!(e, TileEntry::Fetching))
2
                .count();
2
            (cap as usize).saturating_sub(in_flight)
        }
    }
5
}
/// Fetches one pass starts when the map has no `max_in_flight` — bounds the
/// burst on a big viewport jump, but not how many are running.
const MAX_SPAWN_PER_CALL: usize = 16;
/// Low-frequency timer that spawns fetches for any `Pending` tiles the
/// `VirtualView` marked since the last spawn - the path that the
/// `pointer/scroll/after_mount` handlers can't cover (a rebuild-driven viewport
/// change marks tiles `Pending` in the `VirtualView` render, which has no
/// `add_thread`). Installed once in `map_on_after_mount`. The `data` clone
/// tracks the persistent dataset, so writebacks land in the rendered cache.
/// Cheap no-op when nothing is `Pending`; never `RefreshDom`s (that would
/// orphan the cache the workers write to - tile writebacks drive re-render).
extern "C" fn map_fetch_sweep_tick(
    mut data: RefAny,
    mut info: TimerCallbackInfo,
) -> TimerCallbackReturn {
    spawn_pending_tile_fetches(&mut data, &mut info.callback_info);
    TimerCallbackReturn {
        should_update: Update::DoNothing,
        should_terminate: TerminateTimer::Continue,
    }
}
/// `{z}/{x}/{y}` substitution. Mirrors `azul_dll`'s `build_tile_url`
/// (the widget can't reach the dll, so it's duplicated here - trivial).
32
fn build_tile_url(template: &str, tile: MapTileId) -> String {
    use alloc::string::ToString;
32
    template
32
        .replace("{z}", &tile.z.to_string())
32
        .replace("{x}", &tile.x.to_string())
32
        .replace("{y}", &tile.y.to_string())
32
}
#[must_use]
/// Worker-thread → main-thread writeback.
///
/// `cache_dataset` is the
/// `writeback_data` handed to `Thread::create` (the same
/// `MapTileCache` the widget reads); `incoming` is the `TileReadyMsg`
/// the worker sent. Stamps the tile `Ready` (or `Failed`) and asks for
/// a relayout so the `VirtualView` renders the new content.
6
pub extern "C" fn map_tile_writeback(
6
    mut cache_dataset: RefAny,
6
    mut incoming: RefAny,
6
    mut info: CallbackInfo,
6
) -> Update {
    // The worker sent something that is not a TileReadyMsg: the tile arrived
    // and is dropped on the floor here.
6
    let Some(m) = incoming.downcast_ref::<TileReadyMsg>() else {
        #[cfg(feature = "std")]
1
        if std::env::var("AZ_MAP_DEBUG").is_ok() {
            std::eprintln!("[map] writeback: DROPPED — payload is not a TileReadyMsg");
1
        }
1
        return Update::DoNothing;
    };
5
    let msg = (
5
        m.tile,
5
        m.svg.clone(),
5
        m.error.clone(),
5
        m.look,
5
        m.bytes.clone(),
5
    );
5
    drop(m);
    {
5
        let Some(mut cache) = cache_dataset.downcast_mut::<MapTileCache>() else {
            // The tile came back but the dataset it targets is no longer a
            // MapTileCache — typically a rebuild replaced it. The fetch
            // succeeded and the pixels are still discarded.
            #[cfg(feature = "std")]
1
            if std::env::var("AZ_MAP_DEBUG").is_ok() {
                std::eprintln!(
                    "[map] writeback: DROPPED tile=({},{},{}) — target dataset is not a \
                     MapTileCache",
                    msg.0.z,
                    msg.0.x,
                    msg.0.y
                );
1
            }
1
            return Update::DoNothing;
        };
        // Cache the geometry FIRST and unconditionally. Whatever happens to the
        // styled result, these bytes mean this tile never has to be downloaded
        // again — including for a look nobody has asked for yet.
4
        let fetched_bytes = msg.4.as_ref().len();
4
        if fetched_bytes != 0 {
1
            cache.tile_bytes.insert(msg.0, msg.4);
3
        }
        // File the result under the look it was STYLED FOR. There is no
        // "arrived for the wrong theme" case any more: a result for a look the
        // widget has moved on from is still correct data for that look, so it is
        // kept, not thrown away and re-fetched. If the user flips back it paints
        // instantly; until then it serves as the fallback for its own tile.
4
        let key = TileStyleKey {
4
            tile: msg.0,
4
            look: msg.3,
4
        };
4
        let ok = msg.2.as_str().is_empty();
4
        let svg_len = msg.1.as_str().len();
4
        if ok {
3
            cache.mark_tile_ready(key, msg.1);
3
        } else {
1
            cache.mark_tile_failed(key, msg.2.clone());
1
        }
        // Logged AFTER the decision, reporting the decision. The old line
        // printed `ok=true` from "the error string is empty" BEFORE the theme
        // check that then discarded the tile — so a tile about to be thrown
        // away and re-fetched logged as a success. A log line that lies is
        // worse than no log line.
        #[cfg(feature = "std")]
4
        if std::env::var("AZ_MAP_DEBUG").is_ok() {
            eprintln!(
                "[map] writeback tile=({},{},{}) theme={:?} stored={} svg_len={} bytes_cached={} \
                 err={:?}",
                msg.0.z,
                msg.0.x,
                msg.0.y,
                msg.3,
                if ok { "Ready" } else { "Failed" },
                svg_len,
                fetched_bytes,
                msg.2.as_str()
            );
4
        }
    } // drop the cache borrow before touching `info`
    // Re-render the VirtualView(s) IN PLACE so the pure content callback re-reads
    // the shared cache we just mutated. NOT `RefreshDom`: a DOM rebuild would
    // allocate a fresh `MapTileCache` and orphan THIS worker's clone of it (the
    // VirtualView's `refany`, the node dataset and the worker's writeback handle
    // are all clones of one `RefAny` — same underlying data — only while the DOM
    // is not rebuilt). Re-invoking in place keeps that share intact, so this tile
    // and every later one reach the rendered view.
4
    info.trigger_all_virtual_view_rerender();
4
    Update::DoNothing
6
}
/// Inclusive `(x_min, x_max, y_min, y_max)` tile range for a `width_px x
/// height_px` viewport centred at tile-space `(centre_x, centre_y)`, at
/// fractional `zoom_scale` and integer `tile_count` (2^z). The pure core of
/// `map_widget_render`'s grid loop — what decides which tiles get fetched.
///
/// The range is the tiles that actually INTERSECT the viewport, plus ONE tile of
/// padding per axis (USER RULING 2026-09-14: "even with one tile padding over the
/// 4x3 visible tiles it's 5x4 = 20 tiles, not 42"). The padding goes on the side
/// whose viewport edge is nearest the next tile boundary — the tile a small pan
/// reveals first.
///
/// This used to add a whole tile on EVERY side (`+ 1.0` to each half-extent) and
/// then took `ceil` of an inclusive maximum, which names the tile that starts
/// beyond the edge. Together that made an 874x523 map request 42 tiles (7x6) for
/// 12 visible. Every request is a real download: ask for what is shown.
///
/// x is not clamped (the map wraps horizontally; callers take the column mod
/// `tile_count`); y is clamped to `0..=tile_count-1`, and padding that would fall
/// past a pole goes to the other side instead of being lost.
#[allow(clippy::cast_possible_truncation, clippy::cast_possible_wrap)] // bounded layout/render
                                                                       // numeric cast
170
fn visible_tile_range(
170
    centre_x: f32,
170
    centre_y: f32,
170
    width_px: f32,
170
    height_px: f32,
170
    zoom_scale: f32,
170
    tile_count: u32,
170
) -> (i32, i32, i32, i32) {
170
    let tile_px = 256.0 * zoom_scale;
170
    let half_w = (width_px / tile_px).abs() * 0.5;
170
    let half_h = (height_px / tile_px).abs() * 0.5;
170
    let max_idx = tile_count as i32 - 1;
170
    let (x_min, x_max) = padded_axis(centre_x, half_w, None);
170
    let (y_min, y_max) = padded_axis(centre_y, half_h, Some(max_idx));
170
    (x_min, x_max, y_min, y_max)
170
}
/// One axis of [`visible_tile_range`]: the tiles intersecting
/// `[centre - half, centre + half]`, plus one tile on the nearer side.
/// `clamp_max` bounds a clamped axis (y) to `0..=clamp_max`.
#[allow(clippy::cast_possible_truncation)] // saturating float->int casts, by design
340
fn padded_axis(centre: f32, half: f32, clamp_max: Option<i32>) -> (i32, i32) {
340
    let lo_edge = centre - half;
340
    let hi_edge = centre + half;
    // A non-finite extent (zoom scale 0 → infinite tiles per pixel) spans
    // everything, and a NaN centre collapses to one cell; the saturating casts
    // already say both. Neither is a real view, so neither gets padding.
340
    if !half.is_finite() || !centre.is_finite() {
14
        let (lo, hi) = (lo_edge.floor() as i32, hi_edge.ceil() as i32);
14
        return match clamp_max {
7
            Some(m) => (lo.max(0), hi.min(m)),
7
            None => (lo, hi),
        };
326
    }
    // Tile n covers [n, n + 1). The first tile the view touches is floor(lo);
    // the last is the tile whose START is below hi, i.e. ceil(hi) - 1 — and at
    // least the first, for a zero-width view.
326
    let mut lo = lo_edge.floor() as i32;
326
    let mut hi = (hi_edge.ceil() as i32).saturating_sub(1).max(lo);
326
    if let Some(m) = clamp_max {
        // A degenerate tile count (0, or u32::MAX wrapping to -1) leaves no valid
        // row: return an EMPTY span (lo > hi) so the caller's loop never runs.
        // `clamp` would panic on a max below its min.
163
        if m < 0 {
2
            return (0, m);
161
        }
161
        lo = lo.clamp(0, m);
161
        hi = hi.clamp(lo, m);
163
    }
    // One tile of padding, toward the edge nearest the next tile boundary.
324
    let lo_gap = lo_edge - lo_edge.floor(); // how close the tile before `lo` is
324
    let hi_gap = hi_edge.ceil() - hi_edge; // how close the tile after `hi` is
324
    let lo_room = clamp_max.is_none_or(|_| lo > 0);
324
    let hi_room = clamp_max.is_none_or(|m| hi < m);
324
    if (lo_gap <= hi_gap && lo_room) || !hi_room {
299
        if lo_room {
258
            lo = lo.saturating_sub(1);
258
        }
25
    } else {
25
        hi = hi.saturating_add(1);
25
    }
324
    (lo, hi)
340
}
/// Wrap a (possibly negative or over-range) tile column into the valid
/// `0..tile_count` band - the horizontal world-wrap. `rem_euclid` (not `%`)
/// so columns west of the antimeridian map to the east side: at `tile_count`
/// = 4, column `-1` → `3`, column `4` → `0`.
#[allow(clippy::cast_possible_wrap)] // bounded layout/render numeric cast
6408
fn wrap_tile_x(x: i32, tile_count: u32) -> u32 {
6408
    x.rem_euclid(tile_count.max(1) as i32) as u32
6408
}
/// `f(view)` - the tile ids a `viewport` needs to fill a `bounds`-sized widget.
/// Shared by the `VirtualView` render and the pan/zoom handlers so a handler can
/// mark + spawn the NEW viewport's tiles immediately, rather than waiting for the
/// next render pass to discover them. Mirrors `map_widget_render`'s grid math.
#[allow(clippy::suboptimal_flops)] // mul_add not guaranteed faster/available without target +fma; keep explicit a*b+c
#[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)] // bounded layout/render numeric
                                                                   // cast
70
fn map_visible_tiles(
70
    viewport: &MapViewport,
70
    bounds: azul_core::geom::LogicalSize,
70
    layer: &MapTileLayer,
70
) -> Vec<MapTileId> {
70
    map_wanted_tiles(viewport, bounds, layer).tiles().collect()
70
}
/// The tile range a view of `bounds` at `viewport` needs: what the render
/// draws, including the tilt/rotation overscan and the one tile of padding.
#[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)] // bounded layout/render numeric cast
70
fn map_wanted_tiles(
70
    viewport: &MapViewport,
70
    bounds: azul_core::geom::LogicalSize,
70
    layer: &MapTileLayer,
70
) -> WantedTiles {
70
    let z_int = (viewport.zoom.floor() as i32)
70
        .clamp(i32::from(layer.min_zoom), i32::from(layer.max_zoom)) as u8;
70
    let tile_count = 1u32 << u32::from(z_int);
70
    let frac_zoom = viewport.zoom - f32::from(z_int);
70
    let zoom_scale = 2.0_f32.powf(frac_zoom);
70
    let centre_x = lon_to_tile_x(viewport.centre_lon_deg, f64::from(tile_count)) as f32;
70
    let centre_y = lat_to_tile_y(viewport.centre_lat_deg, f64::from(tile_count)) as f32;
70
    let (over_w, over_h) = camera_overscan(viewport, bounds.width, bounds.height);
70
    let (x_min, x_max, y_min, y_max) = visible_tile_range(
70
        centre_x,
70
        centre_y,
70
        bounds.width * over_w,
70
        bounds.height * over_h,
70
        zoom_scale,
70
        tile_count,
70
    );
70
    WantedTiles {
70
        z: z_int,
70
        x_min,
70
        x_max,
70
        y_min,
70
        y_max,
70
    }
70
}
/// A rectangle of tiles at one zoom level, as [`visible_tile_range`] returns
/// it: `x` is not yet wrapped around the antimeridian, `y` is clamped.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct WantedTiles {
    pub z: u8,
    pub x_min: i32,
    pub x_max: i32,
    pub y_min: i32,
    pub y_max: i32,
}
impl WantedTiles {
    /// Every tile in the range, `x` wrapped into the world.
    #[allow(clippy::cast_sign_loss)] // y is clamped to 0.. by visible_tile_range
154
    pub fn tiles(self) -> impl Iterator<Item = MapTileId> {
154
        let tile_count = 1u32 << u32::from(self.z.min(31));
1024
        (self.x_min..=self.x_max).flat_map(move |x| {
1024
            (self.y_min..=self.y_max).map(move |y| MapTileId {
4617
                z: self.z,
4617
                x: wrap_tile_x(x, tile_count),
4617
                y: y as u32,
4617
            })
1024
        })
154
    }
    /// Whether `tile` is one of [`tiles`](Self::tiles).
    #[must_use]
89
    pub fn contains(self, tile: MapTileId) -> bool {
89
        if tile.z != self.z {
1
            return false;
88
        }
88
        let y = i64::from(tile.y);
88
        if y < i64::from(self.y_min) || y > i64::from(self.y_max) {
54
            return false;
34
        }
34
        if self.x_max < self.x_min {
            return false;
34
        }
34
        let tile_count = 1_i64 << u32::from(self.z.min(31));
34
        let span = i64::from(self.x_max) - i64::from(self.x_min);
34
        if span + 1 >= tile_count {
2
            return true; // the range wraps the whole world
32
        }
32
        (i64::from(tile.x) - i64::from(self.x_min)).rem_euclid(tile_count) <= span
89
    }
}
/// What the map shows, for the accessibility value of the tile grid:
/// "48.21 N, 16.37 E, zoom 6.0".
81
fn map_viewport_description(viewport: &MapViewport) -> String {
81
    let lat = viewport.centre_lat_deg;
81
    let lon = viewport.centre_lon_deg;
81
    alloc::format!(
81
        "{:.2} {}, {:.2} {}, zoom {:.1}",
81
        lat.abs(),
81
        if lat < 0.0 { "S" } else { "N" },
81
        lon.abs(),
81
        if lon < 0.0 { "W" } else { "E" },
        viewport.zoom
    )
81
}
// ────────── VirtualView callback — visible-tile rendering ─────────────
#[allow(clippy::suboptimal_flops)] // mul_add not guaranteed faster/available without target +fma; keep explicit a*b+c
#[allow(
    clippy::cast_possible_truncation,
    clippy::cast_precision_loss,
    clippy::cast_sign_loss
)] // bounded layout/render numeric cast
#[allow(clippy::too_many_lines)] // large but cohesive: single-purpose layout/render/parse routine
                                 // (one branch per case)
90
extern "C" fn map_widget_render(data: RefAny, info: VirtualViewCallbackInfo) -> VirtualViewReturn {
    enum TileDisplay {
        Glyph(&'static str),
        Svg(AzString),
    }
90
    let mut data = data;
90
    let bounds = info.get_bounds();
90
    let bounds_logical = bounds.get_logical_size();
90
    let width_px = bounds_logical.width;
90
    let height_px = bounds_logical.height;
    // Defensive: if the widget was placed in a container that gives it no definite
    // size, the bounds come through as 0 or non-finite. Computing a tile grid then
    // positions tiles at NaN/∞ (off-screen → blank) and can allocate unboundedly, so
    // render nothing until the layout settles to a finite box.
90
    if !width_px.is_finite() || !height_px.is_finite() || width_px <= 0.0 || height_px <= 0.0 {
8
        if std::env::var("AZ_MAP_DEBUG").is_ok() {
            eprintln!("[map] non-finite bounds {width_px}x{height_px} — skipping render");
8
        }
8
        return VirtualViewReturn {
8
            dom: OptionDom::None,
8
            materialized: azul_core::geom::LogicalRect::new(
8
                azul_core::geom::LogicalPosition::zero(),
8
                bounds_logical,
8
            ),
8
            virtual_rect: azul_core::geom::LogicalRect::new(
8
                azul_core::geom::LogicalPosition::zero(),
8
                bounds_logical,
8
            ),
8
        };
82
    }
82
    let (layer, viewport, look) = match data.downcast_mut::<MapTileCache>() {
81
        Some(mut c) => {
            // THE WINDOW OWNS LIGHT/DARK. The look is the layer's cartography
            // taken to the window's scheme, resolved HERE, before the tile
            // lookup: the scheme used to be recorded only by the fetch timer
            // (`spawn_pending_tile_fetches`), so the first render after a
            // theme switch still read the old look's keys — every tile
            // Ready, nothing Pending, nothing to re-decode — and the map
            // stayed light on a dark window until some later fetch. Resolving
            // the scheme where the keys are read is what makes a switch
            // re-key the tiles (a miss under the new look inserts Pending, the
            // timer decodes it under the new sheet).
81
            c.cascade_scheme = Some(MapColorScheme::from_window_theme(info.window_theme));
81
            let look = c.current_look();
81
            c.set_active_look(look);
81
            (c.layer.clone(), c.viewport, look)
        }
        None => {
1
            return VirtualViewReturn {
1
                dom: OptionDom::None,
1
                materialized: azul_core::geom::LogicalRect::new(
1
                    azul_core::geom::LogicalPosition::zero(),
1
                    bounds_logical,
1
                ),
1
                virtual_rect: azul_core::geom::LogicalRect::new(
1
                    azul_core::geom::LogicalPosition::zero(),
1
                    bounds_logical,
1
                ),
1
            };
        }
    };
    // Round the requested fractional zoom down to the nearest integer
    // tile zoom the layer supports.
81
    let z_int = (viewport.zoom.floor() as i32)
81
        .clamp(i32::from(layer.min_zoom), i32::from(layer.max_zoom)) as u8;
81
    let tile_count = 1u32 << u32::from(z_int);
81
    let frac_zoom = viewport.zoom - f32::from(z_int);
81
    let zoom_scale = 2.0_f32.powf(frac_zoom);
    // Convert WGS-84 → Web-Mercator-XYZ tile-space via the shared
    // projection helpers (the single source of truth, unit-tested below).
81
    let centre_x = lon_to_tile_x(viewport.centre_lon_deg, f64::from(tile_count)) as f32;
81
    let centre_y = lat_to_tile_y(viewport.centre_lat_deg, f64::from(tile_count)) as f32;
    // 256 is the Mercator tile pixel size at integer zoom; tile_px is also
    // used below to position each tile div.
81
    let tile_px = 256.0 * zoom_scale;
    // A tilted / rotated camera sees more of the flat plane than the
    // straight-down one: fetch the tiles the transform will reveal.
81
    let (over_w, over_h) = camera_overscan(&viewport, width_px, height_px);
81
    let (x_min, x_max, y_min, y_max) = visible_tile_range(
81
        centre_x,
81
        centre_y,
81
        width_px * over_w,
81
        height_px * over_h,
81
        zoom_scale,
81
        tile_count,
81
    );
    // Opt-in render trace (`AZ_MAP_DEBUG=1`): the VirtualView callback fires only
    // when the framework finds this node with real bounds — so seeing this line at
    // all confirms invocation, and the values reveal a zero / infinite / off-screen
    // grid (the usual causes of a blank map).
81
    if std::env::var("AZ_MAP_DEBUG").is_ok() {
        eprintln!(
            "[map] render bounds={:.0}x{:.0} z={} centre_tile=({:.2},{:.2}) tiles x{}..{} y{}..{} \
             = {}",
            width_px,
            height_px,
            z_int,
            centre_x,
            centre_y,
            x_min,
            x_max,
            y_min,
            y_max,
            (x_max - x_min + 1).max(0) * (y_max - y_min + 1).max(0)
        );
81
    }
    // Queue any missing tiles as `Pending` and record this range as what the
    // view needs; the next spawn pass starts the fetches and forgets queued
    // tiles outside it.
81
    if let Some(mut cache) = data.downcast_mut::<MapTileCache>() {
81
        cache.want_tiles(
81
            WantedTiles {
81
                z: z_int,
81
                x_min,
81
                x_max,
81
                y_min,
81
                y_max,
81
            },
81
            look,
81
        );
81
    }
    // Snapshot what to DISPLAY per visible tile, under a short borrow, then
    // drop it before building DOM.
    //
    // Display is a lookup, not the cache state: `best_available_svg` prefers
    // the current look and otherwise takes any look already decoded for that
    // tile. So while a scheme change re-styles, the previous palette keeps
    // painting real cartography instead of the grid falling back to grey
    // placeholders. Only a tile with NO decoded look at all shows a glyph
    // (`…` Pending / `⟳` Fetching / `✗` Failed), which keeps the fetch path
    // observable.
81
    let states: BTreeMap<MapTileId, TileDisplay> =
81
        data.downcast_ref::<MapTileCache>()
81
            .map_or_else(BTreeMap::new, |c| {
81
                let mut out = BTreeMap::new();
1102
                for k in c.tiles.keys() {
1102
                    if out.contains_key(&k.tile) {
                        continue;
1102
                    }
1102
                    let disp = if let Some((_, svg)) = c.best_available_svg(k.tile, look) {
11
                        TileDisplay::Svg(svg)
                    } else {
1091
                        match c.tiles.get(&TileStyleKey { tile: k.tile, look }) {
2
                            Some(TileEntry::Fetching) => TileDisplay::Glyph("⟳"),
2
                            Some(TileEntry::Failed { .. }) => TileDisplay::Glyph("✗"),
1087
                            _ => TileDisplay::Glyph("…"),
                        }
                    };
1102
                    out.insert(k.tile, disp);
                }
81
                out
81
            });
    // Build the visible-tile grid. Each tile div is GPU-translated
    // into its screen position; the (CSS-driven) `transform` keeps
    // pan / zoom O(1) — no relayout per frame.
    // THE CAMERA: pitch / bearing are one CSS transform on the tile canvas
    // (`camera_transform_css`). The flat view carries no transform at all,
    // so nothing changes for the default map; a tilt promotes the canvas to
    // a reference frame the compositor projects (CPU: the projective blit;
    // GPU: WebRender's 3D transforms).
81
    let grid_css = match camera_transform_css(&viewport, width_px, height_px) {
        Some(camera) => format!(
            "position: absolute; left: 0; top: 0; width: 100%; height: 100%; overflow: hidden; \
             {camera}"
        ),
81
        None => "position: absolute; left: 0; top: 0; width: 100%; height: 100%; overflow: hidden;"
81
            .to_string(),
    };
    // The grid is what the user pans and zooms, so it is the node that is named:
    // one graphic, "Map", whose value says where it is looking. The tiles inside
    // are decorative parts of that graphic (see svg_string_to_dom).
81
    let mut grid = Dom::create_div()
81
        .with_css(grid_css.as_str())
81
        .with_accessibility_info(
81
            azul_core::a11y::AccessibilityInfo::named(
                "Map",
81
                azul_core::a11y::AccessibilityRole::Graphic,
            )
81
            .with_value(map_viewport_description(&viewport)),
        );
    // Pan / zoom handlers live HERE, on the VirtualView content — NOT on the
    // outer widget div. The VirtualView renders as a separate DomId painted on
    // top of the outer div, so pointer events hit-test to these tiles and never
    // bubble to the outer div's handlers (which is why mouse-drag panning did
    // nothing). `data` is the shared cache the handlers mutate; the in-place
    // re-render they trigger re-reads it.
    {
        use azul_core::dom::{EventFilter, HoverEventFilter};
        use crate::callbacks::{Callback, CallbackType};
81
        grid = grid
81
            .with_callback(
81
                EventFilter::Hover(HoverEventFilter::MouseDown),
81
                data.clone(),
81
                Callback::from_ptr(map_on_pointer_down),
            )
81
            .with_callback(
81
                EventFilter::Hover(HoverEventFilter::MouseMove),
81
                data.clone(),
81
                Callback::from_ptr(map_on_pointer_move),
            )
81
            .with_callback(
81
                EventFilter::Hover(HoverEventFilter::MouseUp),
81
                data.clone(),
81
                Callback::from_ptr(map_on_pointer_up),
            )
81
            .with_callback(
81
                EventFilter::Hover(HoverEventFilter::MouseLeave),
81
                data.clone(),
81
                Callback::from_ptr(map_on_pointer_up),
            )
81
            .with_callback(
81
                EventFilter::Hover(HoverEventFilter::Scroll),
81
                data.clone(),
81
                Callback::from_ptr(map_on_scroll),
            )
            // Touch + pinch were registered on the OUTER div only, which the
            // same shadowing above made unreachable: a trackpad pinch
            // (PinchIn/PinchOut target the hovered node = a tile in THIS dom)
            // could never reach a handler. Same handlers, same data.
81
            .with_callback(
81
                EventFilter::Hover(HoverEventFilter::TouchStart),
81
                data.clone(),
81
                Callback::from_ptr(map_on_pointer_down),
            )
81
            .with_callback(
81
                EventFilter::Hover(HoverEventFilter::TouchMove),
81
                data.clone(),
81
                Callback::from_ptr(map_on_pointer_move),
            )
81
            .with_callback(
81
                EventFilter::Hover(HoverEventFilter::TouchEnd),
81
                data.clone(),
81
                Callback::from_ptr(map_on_pointer_up),
            )
81
            .with_callback(
81
                EventFilter::Hover(HoverEventFilter::TouchCancel),
81
                data.clone(),
81
                Callback::from_ptr(map_on_pointer_up),
            )
81
            .with_callback(
81
                EventFilter::Hover(HoverEventFilter::PinchIn),
81
                data.clone(),
81
                Callback::from_ptr(map_on_pointer_move),
            )
81
            .with_callback(
81
                EventFilter::Hover(HoverEventFilter::PinchOut),
81
                data.clone(),
81
                Callback::from_ptr(map_on_pointer_move),
            )
            // THE CAMERA: right-drag tilts (vertical) and rotates
            // (horizontal) — MapLibre's convention; a two-finger rotate
            // gesture turns the bearing.
81
            .with_callback(
81
                EventFilter::Hover(HoverEventFilter::RightMouseDown),
81
                data.clone(),
81
                Callback::from_ptr(map_on_tilt_down),
            )
81
            .with_callback(
81
                EventFilter::Hover(HoverEventFilter::RightMouseUp),
81
                data.clone(),
81
                Callback::from_ptr(map_on_tilt_up),
            )
81
            .with_callback(
81
                EventFilter::Hover(HoverEventFilter::RotateClockwise),
81
                data.clone(),
81
                Callback::from_ptr(map_on_rotate_gesture),
            )
81
            .with_callback(
81
                EventFilter::Hover(HoverEventFilter::RotateCounterClockwise),
81
                data.clone(),
81
                Callback::from_ptr(map_on_rotate_gesture),
            );
    }
428
    for x in x_min..=x_max {
1527
        for y in y_min..=y_max {
            // Tile id wraps horizontally (the column past ±180° shows the far
            // side of the world); the *screen* position uses the raw un-wrapped
            // column so the wrapped tile lands seamlessly in the gutter.
1527
            let id = MapTileId {
1527
                z: z_int,
1527
                x: wrap_tile_x(x, tile_count),
1527
                y: y as u32,
1527
            };
            // Derive each tile's on-screen box from the ROUNDED origins of THIS
            // tile and the NEXT one along each axis, so neighbours always share an
            // exact edge — no gaps, no overlaps — at fractional zoom too. A fixed
            // `tile_px.round()` size drifts out of step with the per-tile rounded
            // origin the moment `tile_px` isn't a whole number (any non-integer
            // zoom, e.g. a scroll-wheel notch), scattering the tiles into a
            // disconnected grid. At integer zoom `tile_px` is exactly 256, so each
            // span is exactly 256 and this is identical to the previous behaviour.
6108
            let proj = |coord: f32, centre: f32, span_px: f32| {
6108
                ((coord - centre) * tile_px + span_px * 0.5).round() as i32
6108
            };
1527
            let screen_x = proj(x as f32, centre_x, width_px);
1527
            let screen_y = proj(y as f32, centre_y, height_px);
            // `saturating_sub`: `proj` ends in `as i32`, which SATURATES a
            // non-finite or out-of-range float to i32::MIN / i32::MAX. A viewport
            // whose zoom is `f32::INFINITY` therefore puts the two projected
            // origins at opposite ends of i32, and a plain `-` overflows — an
            // abort in an overflow-checked build (these run inside an `extern "C"`
            // render callback, so the panic does not unwind) and a wrapped,
            // nonsensical tile size in release.
1527
            let size_w = proj(x as f32 + 1.0, centre_x, width_px)
1527
                .saturating_sub(screen_x)
1527
                .max(1);
1527
            let size_h = proj(y as f32 + 1.0, centre_y, height_px)
1527
                .saturating_sub(screen_y)
1527
                .max(1);
            // Placeholder (still-loading) tiles show the loading grid — a grey
            // background + 1px border — so fetch state is visible. A LOADED tile
            // drops that chrome entirely: the decoded SVG covers the tile, and
            // keeping the per-tile border would draw a grey seam-grid over the
            // whole map (user-reported "small grey borders around the tiles").
1527
            let is_ready = matches!(states.get(&id), Some(TileDisplay::Svg(_)));
1527
            let chrome = if is_ready {
11
                ""
            } else {
1516
                "background: #e7e9ec; border: 1px solid #d0d4d9;"
            };
1527
            let style = alloc::format!(
1527
                "position: absolute; left: {screen_x}px; top: {screen_y}px; width: {size_w}px; \
1527
                 height: {size_h}px; {chrome}"
            );
1527
            let mut tile_div = Dom::create_div().with_css(style.as_str());
            // `Ready` tiles render their decoded SVG as a child DOM
            // tree (parsed via the framework's existing XML→DOM path);
            // everything else shows a state glyph + tile id so the grid
            // math + fetch state stay observable.
1527
            match states.get(&id) {
11
                Some(TileDisplay::Svg(svg)) => match svg_string_to_dom(svg.as_str()) {
                    Some(svg_dom) => {
                        tile_div = tile_div.with_child(svg_dom);
                    }
11
                    None => {
11
                        tile_div = tile_div.with_child(
11
                            crate::widgets::widget_p_with_text(alloc::format!(
11
                                "✓? z{z_int}/{x}/{y}"
11
                            ))
11
                            .with_css(
11
                                "position: absolute; left: 4px; top: 4px; font-size: 11px; color: \
11
                                 #888;",
11
                            ),
11
                        );
11
                    }
                },
1516
                other => {
1516
                    let state_tag = match other {
1516
                        Some(TileDisplay::Glyph(g)) => *g,
                        _ => "",
                    };
1516
                    tile_div = tile_div.with_child(
1516
                        crate::widgets::widget_p_with_text(alloc::format!(
1516
                            "{state_tag} z{z_int}/{x}/{y}"
                        ))
1516
                        .with_css(
1516
                            "position: absolute; left: 4px; top: 4px; font-size: 11px; color: \
1516
                             #888;",
                        ),
                    );
                }
            }
1527
            grid = grid.with_child(tile_div);
        }
    }
81
    VirtualViewReturn {
81
        dom: OptionDom::Some(grid),
81
        materialized: azul_core::geom::LogicalRect::new(
81
            azul_core::geom::LogicalPosition::zero(),
81
            bounds_logical,
81
        ),
81
        virtual_rect: azul_core::geom::LogicalRect::new(
81
            azul_core::geom::LogicalPosition::zero(),
81
            bounds_logical,
81
        ),
81
    }
90
}
#[cfg(test)]
mod camera_tests {
    use super::*;
7
    fn vp(pitch: f32, bearing: f32) -> MapViewport {
7
        MapViewport {
7
            pitch_deg: pitch,
7
            bearing_deg: bearing,
7
            ..MapViewport::default()
7
        }
7
    }
    #[test]
1
    fn the_flat_view_has_no_transform_and_no_overscan() {
1
        assert_eq!(camera_transform_css(&vp(0.0, 0.0), 800.0, 600.0), None);
1
        assert_eq!(camera_overscan(&vp(0.0, 0.0), 800.0, 600.0), (1.0, 1.0));
1
        assert_eq!(
1
            camera_transform_css(&vp(0.001, -0.001), 800.0, 600.0),
            None,
            "sub-0.01deg is flat"
        );
1
    }
    #[test]
1
    fn pitch_and_bearing_become_one_perspective_transform_about_the_centre() {
1
        let css = camera_transform_css(&vp(45.0, 30.0), 800.0, 600.0).expect("a tilt transforms");
1
        assert!(css.contains("perspective(1200px)"), "{css}");
1
        assert!(css.contains("rotateX(45.00deg)"), "{css}");
1
        assert!(css.contains("rotate(30.00deg)"), "{css}");
1
        assert!(css.contains("transform-origin: 50% 50%"), "{css}");
        // the widget API clamps and normalises
1
        let w = MapWidget::create(MapTileLayer::default())
1
            .with_pitch(95.0)
1
            .with_bearing(370.0);
1
        assert_eq!(w.viewport.pitch_deg, MAX_PITCH_DEG);
1
        assert!((w.viewport.bearing_deg - 10.0).abs() < 1e-4);
1
        assert_eq!(clamp_pitch(f32::NAN), 0.0);
1
        assert_eq!(normalize_bearing(-190.0), 170.0);
1
        assert_eq!(normalize_bearing(180.0), -180.0);
1
    }
    #[test]
1
    fn a_tilted_camera_fetches_more_rows_and_a_rotated_one_a_bounding_box() {
1
        let (w, h) = camera_overscan(&vp(60.0, 0.0), 800.0, 600.0);
1
        assert!((h - 2.0).abs() < 1e-4, "max pitch doubles the rows: {h}");
1
        assert!(w > 1.0 && w < 2.0, "{w}");
1
        let (w, h) = camera_overscan(&vp(0.0, 90.0), 800.0, 600.0);
1
        assert!(
1
            (w - 600.0 / 800.0).abs() < 1e-4 && (h - 800.0 / 600.0).abs() < 1e-4,
            "a 90deg turn swaps the sides: {w} {h}"
        );
1
        let (w, h) = camera_overscan(&vp(0.0, 45.0), 800.0, 800.0);
1
        assert!(
1
            (w - core::f32::consts::SQRT_2).abs() < 1e-3
1
                && (h - core::f32::consts::SQRT_2).abs() < 1e-3
        );
1
    }
    #[test]
1
    fn a_right_drag_tilts_up_and_turns_clockwise_within_the_limits() {
1
        let (b, p) = camera_drag(0.0, 0.0, 40.0, -20.0);
1
        assert!((b - 20.0).abs() < 1e-4, "40 px right = +20deg bearing: {b}");
1
        assert!((p - 10.0).abs() < 1e-4, "20 px up = +10deg pitch: {p}");
1
        let (_, p) = camera_drag(0.0, 55.0, 0.0, -100.0);
1
        assert_eq!(p, MAX_PITCH_DEG, "pitch is clamped");
1
        let (_, p) = camera_drag(0.0, 5.0, 0.0, 100.0);
1
        assert_eq!(p, 0.0, "…at both ends");
1
        let (b, _) = camera_drag(170.0, 0.0, 40.0, 0.0);
1
        assert!((b + 170.0).abs() < 1e-4, "bearing wraps: {b}");
1
    }
}
#[cfg(test)]
mod theme_tests {
    use azul_core::window::WindowTheme;
    use super::*;
    #[test]
1
    fn a_theme_is_one_cartography_with_a_light_and_a_dark_half() {
        // `System` is the platform family, taken to whichever half the window is.
1
        let light = MapTheme::System.look(MapColorScheme::Light);
1
        let dark = MapTheme::System.look(MapColorScheme::Dark);
1
        assert!(!light.is_dark() && dark.is_dark(), "{light:?} / {dark:?}");
1
        assert!(!light.sheet().is_empty() && !dark.sheet().is_empty());
1
        assert_eq!(
            light,
1
            MapTheme::platform_family().look(MapColorScheme::Light)
        );
        // Every theme has both halves, and the half says which it is.
5
        for theme in [
1
            MapTheme::Positron,
1
            MapTheme::Bright,
1
            MapTheme::Liberty,
1
            MapTheme::Google,
1
            MapTheme::Apple,
        ] {
5
            let l = theme.look(MapColorScheme::Light);
5
            let d = theme.look(MapColorScheme::Dark);
5
            assert!(!l.is_dark() && d.is_dark(), "{theme:?}: {l:?} / {d:?}");
5
            assert!(
5
                !l.sheet().is_empty() && !d.sheet().is_empty(),
                "{theme:?} must have a sheet for both halves"
            );
5
            assert_eq!(theme.stylesheet(WindowTheme::LightMode).as_str(), l.sheet());
5
            assert_eq!(theme.stylesheet(WindowTheme::DarkMode).as_str(), d.sheet());
        }
        // Apple is ONE theme: the dark window picks its dark palette, no
        // second variant needed.
1
        assert_eq!(
1
            MapTheme::Apple.look(MapColorScheme::Light),
            MapLook::AppleLight
        );
1
        assert_eq!(
1
            MapTheme::Apple.look(MapColorScheme::Dark),
            MapLook::AppleDark
        );
        // The light-only OpenFreeMap designs share Dark Matter as their dark half.
3
        for theme in [MapTheme::Positron, MapTheme::Bright, MapTheme::Liberty] {
3
            assert_eq!(theme.look(MapColorScheme::Dark), MapLook::DarkMatter);
        }
1
        assert!(MapTheme::Custom
1
            .stylesheet(WindowTheme::LightMode)
1
            .as_str()
1
            .is_empty());
1
        assert_eq!(
1
            MapLook::DarkMatter.stylesheet().as_str(),
1
            MapLook::DarkMatter.sheet()
        );
1
    }
    #[test]
1
    fn a_custom_sheet_wins_over_a_preset_and_with_theme_credits_the_design() {
1
        let layer = MapTileLayer::default().with_theme(MapTheme::Positron);
1
        assert_eq!(
1
            layer.effective_style_css(WindowTheme::LightMode).as_str(),
            super::super::map_themes::POSITRON
        );
1
        assert_eq!(
1
            layer.effective_style_css(WindowTheme::DarkMode).as_str(),
            super::super::map_themes::DARK,
            "the dark window gets the theme's dark half"
        );
1
        assert!(
1
            layer.attribution.as_str().contains("Positron ©"),
            "the CC BY design credit must reach the attribution: {}",
            layer.attribution.as_str()
        );
1
        assert!(
1
            layer.attribution.as_str().contains("Dark Matter ©"),
            "…and so must the dark half's: {}",
            layer.attribution.as_str()
        );
        // with_theme twice does not duplicate the credit
1
        let twice = layer.clone().with_theme(MapTheme::Positron);
1
        assert_eq!(twice.attribution.as_str().matches("Positron ©").count(), 1);
1
        let mut custom = MapTileLayer::default().with_theme(MapTheme::Apple);
1
        custom.style_css = AzString::from("water { fill: #123456; }");
1
        assert_eq!(
1
            custom.effective_style_css(WindowTheme::DarkMode).as_str(),
            "water { fill: #123456; }"
        );
        // authored looks carry no third-party credit
1
        assert!(
1
            MapLook::AppleLight.credit_str().is_empty()
1
                && MapLook::GoogleLight.credit_str().is_empty()
        );
1
        assert_eq!(
1
            MapTheme::Positron.credit(WindowTheme::LightMode).as_str(),
1
            MapLook::Positron.credit_str()
        );
1
        assert_eq!(
1
            MapTheme::Positron.credit(WindowTheme::DarkMode).as_str(),
1
            MapLook::DarkMatter.credit_str()
        );
        // System credits BOTH halves' designs where they have one
1
        let sys = MapTileLayer::default().with_theme(MapTheme::System);
1
        let l = MapTheme::System.look(MapColorScheme::Light).credit_str();
1
        let d = MapTheme::System.look(MapColorScheme::Dark).credit_str();
1
        assert!(l.is_empty() || sys.attribution.as_str().contains(l));
1
        assert!(d.is_empty() || sys.attribution.as_str().contains(d));
1
    }
    #[test]
1
    fn changing_the_look_re_keys_the_cache_and_keeps_the_decoded_tile() {
1
        let mut cache = MapTileCache::new(MapTileLayer::default(), MapViewport::default());
1
        let id = MapTileId { z: 1, x: 0, y: 0 };
1
        let look_a = cache.key_at_current_look(id);
1
        cache.mark_tile_ready(look_a, AzString::from("<svg/>"));
1
        assert!(
1
            !cache.set_active_look(cache.active_look),
            "same look: nothing to do"
        );
1
        assert!(matches!(cache.tiles[&look_a], TileEntry::Ready { .. }));
1
        assert!(cache.set_active_look(MapLook::DarkMatter));
        // A look change RE-KEYS the lookup; it must never invalidate geometry
        // that is already decoded. Look A's tile stays Ready and instantly
        // available if the user flips back.
1
        assert!(
1
            matches!(cache.tiles[&look_a], TileEntry::Ready { .. }),
            "a look change must not discard a decoded tile"
        );
1
        assert_eq!(cache.active_look, MapLook::DarkMatter);
1
        assert_eq!(
1
            cache
1
                .layer
1
                .effective_style_css_for(cache.active_look)
1
                .as_str(),
            super::super::map_themes::DARK
        );
1
    }
}
#[cfg(test)]
mod tests {
    use super::*;
54
    fn approx(a: f64, b: f64, eps: f64) {
54
        assert!((a - b).abs() < eps, "expected {a} ≈ {b} (within {eps})");
54
    }
    #[test]
1
    fn wrap_lon_keeps_in_range() {
1
        approx(wrap_lon(0.0), 0.0, 1e-9);
1
        approx(wrap_lon(179.0), 179.0, 1e-9);
1
        approx(wrap_lon(-179.0), -179.0, 1e-9);
        // Past the antimeridian wraps to the other side.
1
        approx(wrap_lon(181.0), -179.0, 1e-9);
1
        approx(wrap_lon(-181.0), 179.0, 1e-9);
        // 540° ≡ 180° ≡ -180° — the antimeridian normalises to -180.
1
        approx(wrap_lon(540.0), -180.0, 1e-9);
        // Anything fed in must come out within [-180, 180].
5
        for raw in [-1234.5, -360.0, 360.0, 999.9] {
4
            let w = wrap_lon(raw);
4
            assert!((-180.0..=180.0).contains(&w), "{raw} → {w} out of range");
        }
1
    }
    #[test]
1
    fn build_tile_url_substitutes_zxy() {
1
        let tile = MapTileId {
1
            z: 11,
1
            x: 327,
1
            y: 791,
1
        };
1
        assert_eq!(
1
            build_tile_url("https://t.example/{z}/{x}/{y}.pbf", tile),
            "https://t.example/11/327/791.pbf"
        );
        // Repeated and out-of-order placeholders both resolve.
1
        assert_eq!(
1
            build_tile_url("{y}-{x}-{z}-{z}", MapTileId { z: 3, x: 4, y: 5 }),
            "5-4-3-3"
        );
1
    }
    #[test]
1
    fn lon_tile_endpoints() {
        // At zoom 0 the world is one tile: -180° → 0, +180° → 1.
1
        approx(lon_to_tile_x(-180.0, 1.0), 0.0, 1e-9);
1
        approx(lon_to_tile_x(180.0, 1.0), 1.0, 1e-9);
1
        approx(lon_to_tile_x(0.0, 1.0), 0.5, 1e-9);
        // Greenwich at zoom 1 (2 tiles wide) sits on the seam.
1
        approx(lon_to_tile_x(0.0, 2.0), 1.0, 1e-9);
1
    }
    #[test]
1
    fn lat_tile_equator_and_symmetry() {
        // Equator maps to the vertical centre of the map.
1
        approx(lat_to_tile_y(0.0, 1.0), 0.5, 1e-9);
        // North is above (smaller y) and is mirror-symmetric to south.
1
        let north = lat_to_tile_y(45.0, 1.0);
1
        let south = lat_to_tile_y(-45.0, 1.0);
1
        assert!(north < 0.5 && south > 0.5);
1
        approx(north + south, 1.0, 1e-9);
1
    }
    #[test]
    #[allow(clippy::cast_precision_loss)] // bounded layout/render numeric cast
1
    fn projection_round_trips() {
        // Forward then inverse must return the original coordinate, for
        // a handful of real-world points across several zooms.
1
        let points = [
1
            (37.7749, -122.4194), // San Francisco
1
            (51.5074, -0.1278),   // London
1
            (-33.8688, 151.2093), // Sydney
1
            (0.0, 0.0),           // null island
1
        ];
5
        for z in [0u32, 5, 11, 18] {
4
            let tc = (1u64 << z) as f64;
20
            for (lat, lon) in points {
16
                let x = lon_to_tile_x(lon, tc);
16
                let y = lat_to_tile_y(lat, tc);
16
                approx(tile_x_to_lon(x, tc), lon, 1e-6);
16
                approx(tile_y_to_lat(y, tc), lat, 1e-6);
16
            }
        }
1
    }
    #[test]
1
    fn pan_up_reveals_north_in_both_hemispheres() {
        // The drag convention the demo's arrow buttons must follow: dragging
        // the CONTENT down (+dy) recentres on a HIGHER latitude (reveals the
        // north); dragging it up reveals the south — in both hemispheres.
        // The demo's "↑" once added a tile-space dy (y grows south) straight
        // to latitude and panned south.
4
        for lat in [37.7749, -33.8688, 0.0] {
3
            let (_, down) = pan_viewport(lat, 0.0, 4.0, 0.0, 128.0);
3
            let (_, up) = pan_viewport(lat, 0.0, 4.0, 0.0, -128.0);
3
            assert!(
3
                down > lat,
                "content dragged down must reveal the north: {lat} → {down}"
            );
3
            assert!(
3
                up < lat,
                "content dragged up must reveal the south: {lat} → {up}"
            );
        }
1
    }
    #[test]
1
    fn wheel_zoom_step_is_proportional_and_bounded() {
        // A mouse notch (3 lines × 20 px) keeps the half-level it always had.
1
        approx(f64::from(wheel_zoom_step(60.0)), 0.5, 1e-6);
1
        approx(f64::from(wheel_zoom_step(-60.0)), -0.5, 1e-6);
        // A precise trackpad delta is charged for what it is.
1
        approx(f64::from(wheel_zoom_step(6.0)), 0.05, 1e-6);
        // No event moves more than half a level, however violent.
1
        approx(f64::from(wheel_zoom_step(10_000.0)), 0.5, 1e-6);
1
        approx(f64::from(wheel_zoom_step(-10_000.0)), -0.5, 1e-6);
        // Nothing in, nothing out.
1
        assert_eq!(wheel_zoom_step(0.0), 0.0);
1
        assert_eq!(wheel_zoom_step(f32::NAN), 0.0);
1
    }
    #[test]
1
    fn a_trackpad_flick_no_longer_runs_to_the_zoom_cap() {
        // One two-finger flick: ~40 events of a few px each plus momentum.
40
        let events: Vec<f32> = (0..40).map(|i| if i < 20 { 8.0 } else { 2.0 }).collect();
40
        let total: f32 = events.iter().map(|d| wheel_zoom_step(*d)).sum();
        // The old signum() * 0.5 charged 40 × 0.5 = 20 levels — past any cap.
1
        assert!(
1
            total < 3.0,
            "a flick must stay within a couple of levels, got {total}"
        );
1
        assert!(
1
            total > 0.5,
            "a flick must still zoom noticeably, got {total}"
        );
1
    }
    #[test]
1
    fn pending_tiles_are_fetched_centre_out() {
        // Viewport centred on the middle of tile (2, 2) at zoom 2 (4×4 world):
        // x = 2.5 → lon 45°, y = 2.5 → its latitude via the inverse projection.
1
        let layer = MapTileLayer::default();
1
        let viewport = MapViewport {
1
            centre_lat_deg: tile_y_to_lat(2.5, 4.0),
1
            centre_lon_deg: tile_x_to_lon(2.5, 4.0),
1
            zoom: 2.0,
1
            ..MapViewport::default()
1
        };
1
        let mut cache = MapTileCache::new(layer, viewport);
5
        for x in 0..4 {
20
            for y in 0..4 {
16
                cache.insert_tile(MapTileId { z: 2, x, y }, TileEntry::Pending);
16
            }
        }
        // A leftover from the previous zoom, which the (z, x, y) key order
        // used to fetch FIRST.
1
        cache.insert_tile(MapTileId { z: 1, x: 0, y: 0 }, TileEntry::Pending);
        // Tiles already in flight / ready are not re-queued.
1
        cache.insert_tile(MapTileId { z: 2, x: 9, y: 9 }, TileEntry::Fetching);
        // The queue is keyed by (tile, look); this test is about the ORDER the
        // coordinates come out in, so drop the (single, uniform) look here.
1
        let order: Vec<MapTileId> = cache
1
            .pending_tiles_nearest_first()
1
            .into_iter()
1
            .map(|k| k.tile)
1
            .collect();
1
        assert_eq!(order.len(), 17, "{order:?}");
1
        assert_eq!(
1
            order[0],
            MapTileId { z: 2, x: 2, y: 2 },
            "the tile under the centre first"
        );
1
        let ring: std::collections::BTreeSet<MapTileId> = order[..9].iter().copied().collect();
4
        for x in 1..=3 {
12
            for y in 1..=3 {
9
                assert!(
9
                    ring.contains(&MapTileId { z: 2, x, y }),
                    "3×3 ring before the edge: {order:?}"
                );
            }
        }
1
        assert_eq!(
1
            *order.last().unwrap(),
            MapTileId { z: 1, x: 0, y: 0 },
            "another zoom's leftover last"
        );
1
        assert!(
1
            !order.contains(&MapTileId { z: 2, x: 9, y: 9 }),
            "in-flight tiles are not pending"
        );
1
    }
    #[test]
1
    fn pan_zero_drag_is_identity() {
        // No movement → centre unchanged (lon/lat already in range).
1
        let (lon, lat) = pan_viewport(37.0, -122.0, 11.0, 0.0, 0.0);
1
        approx(lon, -122.0, 1e-9);
1
        approx(lat, 37.0, 1e-9);
1
    }
    #[test]
1
    fn pan_right_decreases_longitude() {
        // Dragging content right (+dx) recentres on a lower longitude.
1
        let (lon, _) = pan_viewport(0.0, 0.0, 0.0, 100.0, 0.0);
1
        assert!(lon < 0.0, "drag right should lower longitude, got {lon}");
        // Dragging left (-dx) is the mirror.
1
        let (lon_left, _) = pan_viewport(0.0, 0.0, 0.0, -100.0, 0.0);
1
        approx(lon_left, -lon, 1e-9);
1
    }
    #[test]
1
    fn pan_step_scales_inversely_with_zoom() {
        // Each extra zoom level doubles the world size, so the same pixel
        // drag should move the centre half as far in degrees.
1
        let (lon_z0, _) = pan_viewport(0.0, 0.0, 0.0, 50.0, 0.0);
1
        let (lon_z1, _) = pan_viewport(0.0, 0.0, 1.0, 50.0, 0.0);
1
        approx(lon_z1, lon_z0 / 2.0, 1e-9);
1
    }
    #[test]
1
    fn pan_clamps_latitude_to_mercator_limit() {
        // A huge vertical drag can't push the centre past ±85°.
1
        let (_, lat_north) = pan_viewport(84.0, 0.0, 0.0, 0.0, 1.0e6);
1
        assert!((-85.0..=85.0).contains(&lat_north));
1
        let (_, lat_south) = pan_viewport(-84.0, 0.0, 0.0, 0.0, -1.0e6);
1
        assert!((-85.0..=85.0).contains(&lat_south));
1
    }
    #[test]
1
    fn pan_wraps_longitude_across_antimeridian() {
        // Starting near +180 and panning further east wraps into negatives
        // rather than producing an out-of-range longitude.
1
        let (lon, _) = pan_viewport(0.0, 179.0, 0.0, -100.0, 0.0);
1
        assert!((-180.0..180.0).contains(&lon), "lon {lon} out of range");
1
    }
5
    fn viewport_at(zoom: f32) -> MapViewport {
5
        MapViewport {
5
            centre_lat_deg: 0.0,
5
            centre_lon_deg: 0.0,
5
            zoom,
5
            bearing_deg: 0.0,
5
            pitch_deg: 0.0,
5
        }
5
    }
    #[test]
1
    fn merge_shares_old_cache_so_worker_writebacks_survive_relayout() {
        // THE regression behind the blank map: the merge must SHARE the previous
        // cache (the very `RefAny` the fetch-worker threads cloned at spawn), not
        // copy its tiles into a freshly-built one. With a copy, a tile that writes
        // back AFTER a relayout lands in the orphaned old cache and never renders.
        // Here we prove a post-merge writeback through a retained handle is
        // visible in the merged cache — i.e. they are one shared allocation.
1
        let tile = MapTileId { z: 5, x: 1, y: 2 };
1
        let old_cache = MapTileCache::new(MapTileLayer::default(), viewport_at(5.0));
1
        let old_ref = RefAny::new(old_cache);
        // A worker thread keeps THIS clone and writes into it after the relayout.
1
        let mut worker_handle = old_ref.clone();
        // dom() rebuilds a fresh, empty cache (default viewport) each relayout.
1
        let new_cache = MapTileCache::new(MapTileLayer::default(), viewport_at(9.0));
1
        let mut merged = merge_map_tile_cache(RefAny::new(new_cache), old_ref);
        // Worker finishes a fetch AFTER the merge and stamps the tile Ready on its
        // retained handle...
1
        worker_handle
1
            .downcast_mut::<MapTileCache>()
1
            .unwrap()
1
            .insert_tile(
1
                tile,
1
                TileEntry::Ready {
1
                    svg: AzString::from("<svg/>"),
1
                },
1
            );
        // ...and it IS visible through the merged cache (shared storage). With the
        // old copy-merge this assertion failed — the tile was stranded.
1
        let g = merged.downcast_ref::<MapTileCache>().unwrap();
1
        assert!(
1
            g.tile_entry(tile).is_some(),
            "a worker writeback after relayout must reach the rendered cache"
        );
1
    }
    #[test]
1
    fn merge_adopts_build_viewport_but_keeps_tiles() {
        // CONTRACT (changed 2026-06-10): `with_viewport()` is authoritative on
        // every rebuild. App callbacks (zoom buttons / Recentre / Locate)
        // mutate app state and RefreshDom; the old merge kept the persistent
        // cache's viewport "intact", silently discarding those changes — the
        // demo's +/− buttons fired but did nothing. Widget-internal drags stay
        // consistent because the on_viewport_changed hook mirrors them into
        // app state, which the next build passes back via with_viewport().
        // Tiles and the fetch worker stay with the persistent cache: workers
        // hold clones of that very RefAny, so writebacks keep landing in it.
1
        let mut old_cache = MapTileCache::new(MapTileLayer::default(), viewport_at(5.0));
1
        old_cache.viewport.zoom = 7.0; // internal state from previous frames
1
        let tile = MapTileId { z: 2, x: 1, y: 1 };
1
        old_cache.insert_tile(
1
            tile,
1
            TileEntry::Ready {
1
                svg: "<svg/>".into(),
1
            },
        );
1
        let new_cache = MapTileCache::new(MapTileLayer::default(), viewport_at(2.0));
1
        let mut merged = merge_map_tile_cache(RefAny::new(new_cache), RefAny::new(old_cache));
1
        let g = merged.downcast_ref::<MapTileCache>().unwrap();
        // The build's viewport wins…
1
        approx(
1
            f64::from(g.viewport.zoom),
1
            f64::from(viewport_at(2.0).zoom),
            1e-6,
        );
        // …while the fetched tiles survive in the same allocation.
1
        assert!(
1
            g.tile_entry(tile).is_some(),
            "fetched tiles must survive the merge (workers write into this cache)"
        );
1
    }
    #[test]
1
    fn tile_range_is_the_visible_tiles_plus_one_per_axis() {
        // 512x512 at zoom-scale 1 (256 px tiles), centred on a tile corner: the
        // view covers exactly tiles 7..=8 on each axis. One tile of padding per
        // axis (edges tie, so it goes low) → 6..=8, three tiles each way.
1
        let (x0, x1, y0, y1) = visible_tile_range(8.0, 8.0, 512.0, 512.0, 1.0, 16);
1
        assert_eq!((x0, x1), (6, 8));
1
        assert_eq!((y0, y1), (6, 8));
1
    }
    /// The live map that exposed the over-fetch: 874x523 at z6, centre tile
    /// (34.91, 22.19). Visible = 4x3; with one tile of padding per axis on the
    /// nearer side = 5x4 = 20. It used to request 7x6 = 42.
    #[test]
1
    fn the_async_example_map_requests_twenty_tiles_not_forty_two() {
1
        let (x0, x1, y0, y1) = visible_tile_range(34.91, 22.19, 874.0, 523.0, 1.0, 64);
        // left edge 33.20 is 0.20 from tile 32, right edge 36.62 is 0.38 from 37
1
        assert_eq!(
1
            (x0, x1),
            (32, 36),
            "4 visible columns + 1 on the nearer (left) side"
        );
        // top edge 21.17 is 0.17 from row 20, bottom edge 23.21 is 0.79 from 24
1
        assert_eq!(
1
            (y0, y1),
            (20, 23),
            "3 visible rows + 1 on the nearer (top) side"
        );
1
        assert_eq!((x1 - x0 + 1) * (y1 - y0 + 1), 20);
1
    }
    #[test]
1
    fn padding_that_would_cross_a_pole_goes_to_the_other_side() {
        // A 256px-tall view whose top sits at row 0: nothing above the pole, so
        // the padding row goes below instead of being clamped away.
1
        let (_, _, y0, y1) = visible_tile_range(8.0, 0.5, 256.0, 256.0, 1.0, 16);
1
        assert_eq!((y0, y1), (0, 1));
1
    }
    #[test]
1
    fn wrap_tile_x_wraps_both_directions() {
        // rem_euclid semantics: west of the antimeridian wraps to the east side.
1
        assert_eq!(wrap_tile_x(-1, 4), 3);
1
        assert_eq!(wrap_tile_x(0, 4), 0);
1
        assert_eq!(wrap_tile_x(3, 4), 3);
1
        assert_eq!(wrap_tile_x(4, 4), 0);
1
        assert_eq!(wrap_tile_x(-5, 4), 3);
        // Single-tile world: every column resolves to the one tile.
1
        assert_eq!(wrap_tile_x(7, 1), 0);
1
        assert_eq!(wrap_tile_x(-3, 1), 0);
1
    }
    #[test]
1
    fn tile_range_y_clamps_but_x_wraps_at_zoom0() {
        // zoom 0 → tile_count 1. y stays pinned to row 0 (no data past the
        // poles); x is unclamped (the column over-scans to fill the width) but
        // every column wraps to the single tile.
1
        let (x0, x1, y0, y1) = visible_tile_range(0.5, 0.5, 256.0, 256.0, 1.0, 1);
1
        assert_eq!((y0, y1), (0, 0));
2
        for x in x0..=x1 {
2
            assert_eq!(wrap_tile_x(x, 1), 0);
        }
1
    }
    #[test]
1
    fn tile_range_widens_with_viewport() {
1
        let (nx0, nx1, ..) = visible_tile_range(8.0, 8.0, 512.0, 512.0, 1.0, 16);
1
        let (wx0, wx1, ..) = visible_tile_range(8.0, 8.0, 1024.0, 512.0, 1.0, 16);
1
        assert!(
1
            (wx1 - wx0) > (nx1 - nx0),
            "a wider viewport must request more columns"
        );
1
    }
    #[test]
1
    fn tile_range_clamps_y_but_wraps_x_at_edges() {
        // y is clamped to the valid band at both poles (no over-scan past the
        // Web-Mercator edges)…
1
        let (x0, _, y0, _) = visible_tile_range(0.0, 0.0, 512.0, 512.0, 1.0, 16);
1
        assert!(y0 >= 0);
        // Centred half a tile in from the east edge so the view genuinely crosses
        // the antimeridian (a view ENDING exactly on it touches no further column).
1
        let (_, x1, _, y1) = visible_tile_range(15.5, 15.0, 512.0, 512.0, 1.0, 16);
1
        assert!(y1 <= 15);
        // …but x is unclamped so the world wraps: a west-edge viewport over-scans
        // into negative columns and an east-edge one past tile_count-1; both wrap
        // back into 0..tile_count via wrap_tile_x.
1
        assert!(
1
            x0 < 0,
            "west-edge viewport should over-scan into wrapped columns"
        );
1
        assert!(
1
            x1 > 15,
            "east-edge viewport should over-scan into wrapped columns"
        );
1
        assert_eq!(wrap_tile_x(x0, 16), x0.rem_euclid(16) as u32);
1
        assert_eq!(wrap_tile_x(x1, 16), x1.rem_euclid(16) as u32);
1
    }
2
    fn test_cache() -> MapTileCache {
2
        let layer = MapTileLayer {
2
            url_template: AzString::from("{z}/{x}/{y}"),
2
            min_zoom: 0,
2
            max_zoom: 19,
2
            attribution: AzString::from(""),
2
            style_css: AzString::from(""),
2
            theme: MapTheme::System,
2
        };
2
        let viewport = MapViewport {
2
            centre_lat_deg: 0.0,
2
            centre_lon_deg: 0.0,
2
            zoom: 4.0,
2
            bearing_deg: 0.0,
2
            pitch_deg: 0.0,
2
        };
2
        MapTileCache::new(layer, viewport)
2
    }
    #[test]
1
    fn prune_evicts_distant_tiles_keeps_near_and_inflight() {
1
        let mut cache = test_cache();
        // Centre at z4 is tile (8, 8). Fill a big z4 grid (Ready) — far more than
        // the 192 cap — plus a near Pending tile and a near Ready tile.
21
        for x in 0..20u32 {
420
            for y in 0..20u32 {
400
                cache.insert_tile(
400
                    MapTileId { z: 4, x, y },
400
                    TileEntry::Ready {
400
                        svg: AzString::from("<svg/>"),
400
                    },
400
                );
400
            }
        }
        // A near, in-flight tile (must NEVER be evicted).
1
        cache.insert_tile(MapTileId { z: 4, x: 8, y: 8 }, TileEntry::Pending);
        // A near, ready tile (should survive — low distance score).
1
        cache.insert_tile(
1
            MapTileId { z: 4, x: 9, y: 8 },
1
            TileEntry::Ready {
1
                svg: AzString::from("<svg/>"),
1
            },
        );
        // A very far ready tile (should be evicted first).
1
        cache.insert_tile(
1
            MapTileId { z: 4, x: 0, y: 0 },
1
            TileEntry::Ready {
1
                svg: AzString::from("<svg/>"),
1
            },
        );
1
        assert!(cache.tiles.len() > 192, "precondition: over the cap");
1
        cache.prune_distant_tiles();
1
        assert!(
1
            cache.tiles.len() <= 192,
            "cache must be bounded after prune"
        );
        // In-flight tile survives.
1
        assert!(matches!(
1
            cache.tile_entry(MapTileId { z: 4, x: 8, y: 8 }),
            Some(TileEntry::Pending)
        ));
        // Near tile survives; the corner tile is gone.
1
        assert!(cache.tile_entry(MapTileId { z: 4, x: 9, y: 8 }).is_some());
1
        assert!(cache.tile_entry(MapTileId { z: 4, x: 0, y: 0 }).is_none());
1
    }
    #[test]
1
    fn prune_is_noop_under_cap() {
1
        let mut cache = test_cache();
5
        for x in 0..4u32 {
4
            cache.insert_tile(
4
                MapTileId { z: 4, x, y: 8 },
4
                TileEntry::Ready {
4
                    svg: AzString::from("<svg/>"),
4
                },
4
            );
4
        }
1
        cache.prune_distant_tiles();
1
        assert_eq!(cache.tiles.len(), 4, "under the cap → nothing evicted");
1
    }
}
// ────────── Adversarial autotest coverage ────────────────────────────
//
// Boundary / malformed / overflow probes for the widget's pure numeric core,
// its builders, and its callback surface. Everything here is deliberately fed
// values a real app can produce (a zero-size container, a NaN viewport, a
// tile id past the antimeridian, a garbage tile payload) and asserts the
// function *contains* them rather than panicking.
#[cfg(test)]
#[allow(
    clippy::cast_possible_truncation,
    clippy::cast_precision_loss,
    clippy::cast_sign_loss,
    clippy::cast_possible_wrap,
    clippy::float_cmp,
    clippy::too_many_lines,
    clippy::unreadable_literal
)]
mod autotest_generated {
    use std::sync::{Arc, Mutex};
    use azul_core::{
        callbacks::{HidpiAdjustedBounds, VirtualViewCallbackReason},
        dom::{DomId, DomNodeId},
        geom::{LogicalPosition, LogicalSize, OptionLogicalPosition},
        gl::OptionGlContextPtr,
        hit_test::ScrollPosition,
        resources::{DpiScaleFactor, ImageCache, RendererResources},
        styled_dom::NodeHierarchyItemId,
        task::ThreadReceiver,
        window::{MonitorVec, RawWindowHandle, WindowTheme},
    };
    use azul_css::system::SystemStyle;
    use rust_fontconfig::FcFontCache;
    use super::*;
    #[cfg(feature = "icu")]
    use crate::icu::IcuLocalizerHandle;
    use crate::{
        callbacks::{CallbackChange, CallbackInfoRefData, ExternalSystemCallbacks},
        thread::{ThreadCallback, ThreadCallbackType, ThreadSender},
        window::LayoutWindow,
        window_state::FullWindowState,
    };
    // ------------------------------------------------------------------
    // Helpers
    // ------------------------------------------------------------------
    fn close(a: f64, b: f64, eps: f64) {
        assert!((a - b).abs() <= eps, "expected {a} ≈ {b} (within {eps})");
    }
    fn layer_zoom(min_zoom: u8, max_zoom: u8) -> MapTileLayer {
        MapTileLayer {
            url_template: AzString::from("https://tiles.invalid/{z}/{x}/{y}.pbf"),
            min_zoom,
            max_zoom,
            attribution: AzString::from("attr"),
            style_css: AzString::from(""),
            theme: MapTheme::System,
        }
    }
    fn view(lat: f64, lon: f64, zoom: f32) -> MapViewport {
        MapViewport {
            centre_lat_deg: lat,
            centre_lon_deg: lon,
            zoom,
            bearing_deg: 0.0,
            pitch_deg: 0.0,
        }
    }
    fn cache_at(lat: f64, lon: f64, zoom: f32) -> MapTileCache {
        MapTileCache::new(layer_zoom(0, 19), view(lat, lon, zoom))
    }
    /// Records everything the widget's user hooks are handed.
    #[derive(Default)]
    struct HookLog {
        viewports: Vec<MapViewport>,
        coords: Vec<MapLatLon>,
    }
    extern "C" fn record_viewport(
        mut data: RefAny,
        _: CallbackInfo,
        viewport: MapViewport,
    ) -> Update {
        if let Some(mut log) = data.downcast_mut::<HookLog>() {
            log.viewports.push(viewport);
        }
        Update::DoNothing
    }
    extern "C" fn record_pin(mut data: RefAny, _: CallbackInfo, coord: MapLatLon) -> Update {
        if let Some(mut log) = data.downcast_mut::<HookLog>() {
            log.coords.push(coord);
        }
        Update::RefreshDom
    }
    /// A worker that returns immediately - enough to exercise the spawn path
    /// without any I/O.
    extern "C" fn noop_worker(_: RefAny, _: ThreadSender, _: ThreadReceiver) {}
    extern "C" fn other_noop_worker(_: RefAny, _: ThreadSender, _: ThreadReceiver) {}
    fn hook_log(data: &mut RefAny) -> (usize, usize) {
        let log = data
            .downcast_ref::<HookLog>()
            .expect("payload must still be a HookLog");
        (log.viewports.len(), log.coords.len())
    }
    /// Runs `f` against a real `CallbackInfo` over an empty `LayoutWindow`,
    /// with `cursor` reported as the cursor position relative to the hit node.
    /// Returns `f`'s value plus every `CallbackChange` the callback recorded.
    fn with_callback_info_at<R>(
        cursor: OptionLogicalPosition,
        f: impl FnOnce(CallbackInfo) -> R,
    ) -> (R, Vec<CallbackChange>) {
        let layout_window =
            LayoutWindow::new(FcFontCache::default()).expect("LayoutWindow::new failed");
        let renderer_resources = RendererResources::default();
        let previous_window_state: Option<FullWindowState> = None;
        let current_window_state = FullWindowState::default();
        let gl_context = OptionGlContextPtr::None;
        let scroll_states: BTreeMap<DomId, BTreeMap<NodeHierarchyItemId, ScrollPosition>> =
            BTreeMap::new();
        let window_handle = RawWindowHandle::Unsupported;
        let system_callbacks = ExternalSystemCallbacks::rust_internal();
        let ref_data = CallbackInfoRefData {
            layout_window: &layout_window,
            renderer_resources: &renderer_resources,
            previous_window_state: &previous_window_state,
            current_window_state: &current_window_state,
            gl_context: &gl_context,
            current_scroll_manager: &scroll_states,
            current_window_handle: &window_handle,
            system_callbacks: &system_callbacks,
            system_style: Arc::new(SystemStyle::default()),
            monitors: Arc::new(Mutex::new(MonitorVec::from_const_slice(&[]))),
            #[cfg(feature = "icu")]
            icu_localizer: IcuLocalizerHandle::default(),
            ctx: OptionRefAny::None,
        };
        let changes: Arc<Mutex<Vec<CallbackChange>>> = Arc::new(Mutex::new(Vec::new()));
        let info = CallbackInfo::new(
            &ref_data,
            &changes,
            DomNodeId {
                dom: DomId::ROOT_ID,
                node: NodeHierarchyItemId::NONE,
            },
            cursor,
            OptionLogicalPosition::None,
        );
        let out = f(info);
        let recorded = core::mem::take(&mut *changes.lock().expect("change log poisoned"));
        (out, recorded)
    }
    fn with_callback_info<R>(f: impl FnOnce(CallbackInfo) -> R) -> (R, Vec<CallbackChange>) {
        with_callback_info_at(OptionLogicalPosition::None, f)
    }
    fn cursor_at(x: f32, y: f32) -> OptionLogicalPosition {
        OptionLogicalPosition::Some(LogicalPosition::new(x, y))
    }
    /// Runs `f` against a `VirtualViewCallbackInfo` reporting `w x h` bounds.
    fn with_virtual_view_info<R>(
        w: f32,
        h: f32,
        f: impl FnOnce(VirtualViewCallbackInfo) -> R,
    ) -> R {
        let fonts = FcFontCache::default();
        let images = ImageCache::default();
        let size = LogicalSize::new(w, h);
        let info = VirtualViewCallbackInfo::new(
            VirtualViewCallbackReason::InitialRender,
            &fonts,
            &images,
            WindowTheme::LightMode,
            azul_core::window::WindowFrame::Normal,
            HidpiAdjustedBounds {
                logical_size: size,
                hidpi_factor: DpiScaleFactor::new(1.0),
            },
            azul_core::geom::LogicalRect::new(LogicalPosition::zero(), size),
            azul_core::geom::LogicalRect::new(LogicalPosition::zero(), size),
            LogicalPosition::zero(),
        );
        f(info)
    }
    fn rendered_child_count(ret: &VirtualViewReturn) -> Option<usize> {
        match &ret.dom {
            OptionDom::Some(d) => Some(d.children.as_slice().len()),
            OptionDom::None => None,
        }
    }
    // ==================================================================
    // wrap_lon  (numeric)
    // ==================================================================
    #[test]
    fn wrap_lon_zero_and_negative_zero_are_zero() {
        assert_eq!(wrap_lon(0.0), 0.0);
        assert_eq!(wrap_lon(-0.0), 0.0);
    }
    #[test]
    fn wrap_lon_nan_and_infinities_are_nan_not_panic() {
        // `rem_euclid` on a non-finite dividend is NaN (fmod(inf, x) == NaN);
        // the documented, non-panicking outcome.
        assert!(wrap_lon(f64::NAN).is_nan());
        assert!(wrap_lon(f64::INFINITY).is_nan());
        assert!(wrap_lon(f64::NEG_INFINITY).is_nan());
    }
    #[test]
    fn wrap_lon_extreme_finite_inputs_stay_bounded_and_finite() {
        for raw in [
            f64::MAX,
            f64::MIN,
            f64::MIN_POSITIVE,
            -f64::MIN_POSITIVE,
            1.0e300,
            -1.0e300,
            1.0e18,
            -1.0e18,
            360.0 * 1.0e9,
        ] {
            let w = wrap_lon(raw);
            assert!(w.is_finite(), "{raw} → {w} is not finite");
            assert!((-180.0..=180.0).contains(&w), "{raw} → {w} out of range");
        }
    }
    #[test]
    fn wrap_lon_is_idempotent_on_representative_inputs() {
        for (raw, expected) in [
            (0.0_f64, 0.0_f64),
            (45.0, 45.0),
            (-45.0, -45.0),
            (181.0, -179.0),
            (-181.0, 179.0),
            (720.0, 0.0),
            (-720.0, 0.0),
            (1.0e6, -80.0),
        ] {
            let once = wrap_lon(raw);
            close(once, expected, 1e-9);
            close(wrap_lon(once), once, 1e-9);
        }
    }
    // ==================================================================
    // lon_to_tile_x / tile_x_to_lon  (numeric)
    // ==================================================================
    #[test]
    fn lon_to_tile_x_zero_tile_count_collapses_to_zero() {
        for lon in [-180.0, -1.0, 0.0, 1.0, 180.0] {
            assert_eq!(lon_to_tile_x(lon, 0.0), 0.0, "lon {lon} at tile_count 0");
        }
    }
    #[test]
    fn lon_to_tile_x_nan_inf_are_defined_not_panics() {
        assert!(lon_to_tile_x(f64::NAN, 4.0).is_nan());
        assert!(lon_to_tile_x(0.0, f64::NAN).is_nan());
        assert_eq!(lon_to_tile_x(f64::INFINITY, 4.0), f64::INFINITY);
        assert_eq!(lon_to_tile_x(f64::NEG_INFINITY, 4.0), f64::NEG_INFINITY);
        // inf * 0 is the one genuinely undefined product → NaN, not a panic.
        assert!(lon_to_tile_x(f64::INFINITY, 0.0).is_nan());
    }
    #[test]
    fn lon_to_tile_x_is_monotonic_and_saturates_on_huge_counts() {
        let mut prev = f64::NEG_INFINITY;
        for lon in [-180.0, -90.0, -0.5, 0.0, 0.5, 90.0, 180.0] {
            let x = lon_to_tile_x(lon, 256.0);
            assert!(x > prev, "lon_to_tile_x must increase with longitude");
            prev = x;
        }
        assert_eq!(lon_to_tile_x(180.0, f64::MAX), f64::MAX);
        assert!(lon_to_tile_x(180.0, f64::INFINITY).is_infinite());
    }
    #[test]
    fn tile_x_to_lon_degenerate_tile_counts_do_not_panic() {
        // 0/0 is the only NaN; a non-zero column over a zero-wide world is +inf.
        assert!(tile_x_to_lon(0.0, 0.0).is_nan());
        assert!(tile_x_to_lon(1.0, 0.0).is_infinite());
        assert!(tile_x_to_lon(f64::NAN, 4.0).is_nan());
        assert!(tile_x_to_lon(f64::MAX, f64::MIN_POSITIVE).is_infinite());
    }
    #[test]
    fn lon_tile_x_round_trips_across_zooms_and_edges() {
        for z in [0u32, 1, 5, 14, 22] {
            let tc = f64::from(1u32 << z);
            for lon in [-180.0, -179.999, -122.4194, 0.0, 0.1, 151.2093, 180.0] {
                let x = lon_to_tile_x(lon, tc);
                close(tile_x_to_lon(x, tc), lon, 1e-9);
            }
        }
    }
    // ==================================================================
    // lat_to_tile_y / tile_y_to_lat  (numeric)
    // ==================================================================
    #[test]
    fn lat_to_tile_y_inside_the_mercator_band_is_finite_and_ordered() {
        let tc = 256.0;
        let mut prev = f64::NEG_INFINITY;
        // y grows southward, so iterate north → south and expect a rise.
        for lat in [85.0, 60.0, 30.0, 0.0, -30.0, -60.0, -85.0] {
            let y = lat_to_tile_y(lat, tc);
            assert!(y.is_finite(), "lat {lat} → {y}");
            assert!((0.0..=tc).contains(&y), "lat {lat} → {y} outside the grid");
            assert!(y > prev, "tile-y must increase as latitude decreases");
            prev = y;
        }
    }
    #[test]
    fn lat_to_tile_y_nan_and_infinite_latitudes_are_nan() {
        assert!(lat_to_tile_y(f64::NAN, 4.0).is_nan());
        // tan(±inf) is NaN, so the whole Mercator term degrades to NaN.
        assert!(lat_to_tile_y(f64::INFINITY, 4.0).is_nan());
        assert!(lat_to_tile_y(f64::NEG_INFINITY, 4.0).is_nan());
        assert!(lat_to_tile_y(0.0, f64::NAN).is_nan());
    }
    #[test]
    fn lat_to_tile_y_past_the_poles_does_not_panic() {
        // Beyond ±85.05° the projection is undefined; assert only that every
        // one of these returns (reaching the length check means no panic).
        let outs: Vec<f64> = [90.0, -90.0, 89.9999, -89.9999, 180.0, -180.0, 1.0e9]
            .iter()
            .map(|lat| lat_to_tile_y(*lat, 4.0))
            .collect();
        assert_eq!(outs.len(), 7);
    }
    #[test]
    fn tile_y_to_lat_saturates_at_the_poles_for_out_of_range_rows() {
        for (y, expected) in [(-1.0e9_f64, 90.0_f64), (1.0e9, -90.0)] {
            let lat = tile_y_to_lat(y, 4.0);
            close(lat, expected, 1e-9);
        }
        // No finite row can ever escape ±90°.
        for y in [-1.0e300, -1000.0, -1.0, 0.0, 2.0, 1000.0, 1.0e300] {
            let lat = tile_y_to_lat(y, 4.0);
            assert!(
                (-90.0 - 1e-9..=90.0 + 1e-9).contains(&lat),
                "row {y} → {lat} outside ±90"
            );
        }
    }
    #[test]
    fn tile_y_to_lat_degenerate_tile_counts_do_not_panic() {
        assert!(tile_y_to_lat(0.0, 0.0).is_nan()); // 0/0
        close(tile_y_to_lat(1.0, 0.0), -90.0, 1e-9); // +inf rows south
        assert!(tile_y_to_lat(f64::NAN, 4.0).is_nan());
    }
    #[test]
    fn lat_tile_y_round_trips_at_the_mercator_edges() {
        for z in [0u32, 3, 14, 22] {
            let tc = f64::from(1u32 << z);
            for lat in [-85.05, -45.0, -0.0001, 0.0, 0.0001, 45.0, 85.05] {
                let y = lat_to_tile_y(lat, tc);
                close(tile_y_to_lat(y, tc), lat, 1e-6);
            }
        }
    }
    // ==================================================================
    // pan_viewport  (numeric)
    // ==================================================================
    #[test]
    fn pan_viewport_nan_inputs_propagate_without_panicking() {
        let (lon, lat) = pan_viewport(f64::NAN, 0.0, 2.0, 10.0, 10.0);
        assert!(lat.is_nan(), "NaN centre latitude must stay NaN, got {lat}");
        assert!(lon.is_nan() || (-180.0..=180.0).contains(&lon));
        let (lon, _) = pan_viewport(0.0, f64::NAN, 2.0, 10.0, 10.0);
        assert!(lon.is_nan());
        let (lon, lat) = pan_viewport(0.0, 0.0, 2.0, f64::NAN, f64::NAN);
        assert!(lon.is_nan() && lat.is_nan());
    }
    #[test]
    fn pan_viewport_infinite_zoom_is_a_no_op() {
        // world_px = inf → every pixel delta maps to a zero angular delta.
        let (lon, lat) = pan_viewport(37.0, -122.0, f64::INFINITY, 1.0e6, -1.0e6);
        close(lon, -122.0, 1e-9);
        close(lat, 37.0, 1e-9);
    }
    #[test]
    fn pan_viewport_negative_infinite_zoom_saturates_latitude_not_panics() {
        // world_px underflows to 0 → the longitude delta is ±inf (→ NaN through
        // wrap_lon) and the latitude delta saturates against the ±85 clamp.
        let (lon, lat) = pan_viewport(0.0, 0.0, f64::NEG_INFINITY, 100.0, 100.0);
        assert!(lon.is_nan(), "expected NaN longitude, got {lon}");
        close(lat, 85.0, 1e-9);
        let (_, lat_south) = pan_viewport(0.0, 0.0, f64::NEG_INFINITY, 0.0, -100.0);
        close(lat_south, -85.0, 1e-9);
    }
    #[test]
    fn pan_viewport_extreme_pixel_deltas_stay_inside_the_world() {
        for dx in [-1.0e18_f64, -1.0e9, -1.0, 0.0, 1.0, 1.0e9, 1.0e18] {
            for dy in [-1.0e18_f64, 0.0, 1.0e18] {
                let (lon, lat) = pan_viewport(37.0, -122.0, 0.0, dx, dy);
                assert!(lon.is_finite(), "dx {dx} dy {dy} → lon {lon}");
                assert!((-180.0..=180.0).contains(&lon), "lon {lon} out of range");
                assert!((-85.0..=85.0).contains(&lat), "lat {lat} out of range");
            }
        }
    }
    #[test]
    fn pan_viewport_zero_zoom_and_zero_delta_is_the_identity() {
        let (lon, lat) = pan_viewport(0.0, 0.0, 0.0, 0.0, 0.0);
        assert_eq!((lon, lat), (0.0, 0.0));
    }
    #[test]
    fn pan_viewport_latitude_step_shrinks_towards_the_poles() {
        // d_lat carries a cos(lat) factor, so the same drag moves less near a pole.
        let (_, at_equator) = pan_viewport(0.0, 0.0, 2.0, 0.0, 100.0);
        let (_, at_80) = pan_viewport(80.0, 0.0, 2.0, 0.0, 100.0);
        assert!(
            (at_80 - 80.0).abs() < at_equator.abs(),
            "pole-adjacent pan {at_80} must move less than equatorial {at_equator}"
        );
    }
    // ==================================================================
    // MapWidget::latlon_at_px / px_at_latlon  (numeric)
    // ==================================================================
    #[test]
    fn latlon_at_px_centre_pixel_is_the_viewport_centre() {
        let viewport = view(51.5074, -0.1278, 11.0);
        let container = LogicalSize::new(800.0, 600.0);
        let coord =
            MapWidget::latlon_at_px(viewport, LogicalPosition::new(400.0, 300.0), container);
        close(coord.lat_deg, 51.5074, 1e-9);
        close(coord.lon_deg, -0.1278, 1e-9);
    }
    #[test]
    fn latlon_at_px_result_is_always_clamped_or_nan() {
        let container = LogicalSize::new(800.0, 600.0);
        for zoom in [0.0_f32, 2.0, 11.0, 22.0] {
            for px in [
                LogicalPosition::new(0.0, 0.0),
                LogicalPosition::new(-1.0e9, -1.0e9),
                LogicalPosition::new(1.0e9, 1.0e9),
                LogicalPosition::new(f32::MAX, f32::MIN),
            ] {
                let c = MapWidget::latlon_at_px(view(0.0, 0.0, zoom), px, container);
                assert!(
                    (-180.0..=180.0).contains(&c.lon_deg),
                    "zoom {zoom} px {px:?} → lon {}",
                    c.lon_deg
                );
                assert!(
                    (-85.0..=85.0).contains(&c.lat_deg),
                    "zoom {zoom} px {px:?} → lat {}",
                    c.lat_deg
                );
            }
        }
    }
    #[test]
    fn latlon_at_px_non_finite_zoom_does_not_panic() {
        let container = LogicalSize::new(800.0, 600.0);
        let px = LogicalPosition::new(10.0, 10.0);
        // +inf zoom → infinitely large world → the centre pixel wins.
        let c = MapWidget::latlon_at_px(view(0.0, 0.0, f32::INFINITY), px, container);
        close(c.lon_deg, 0.0, 1e-9);
        close(c.lat_deg, 0.0, 1e-9);
        // -inf zoom → zero-size world → the clamp saturates instead of overflowing.
        let c = MapWidget::latlon_at_px(view(0.0, 0.0, f32::NEG_INFINITY), px, container);
        assert!((-180.0..=180.0).contains(&c.lon_deg));
        assert!((-85.0..=85.0).contains(&c.lat_deg));
        // NaN zoom → NaN out, never a panic.
        let c = MapWidget::latlon_at_px(view(0.0, 0.0, f32::NAN), px, container);
        assert!(c.lon_deg.is_nan() && c.lat_deg.is_nan());
    }
    #[test]
    fn latlon_at_px_zero_sized_container_is_still_defined() {
        let c = MapWidget::latlon_at_px(
            view(10.0, 20.0, 4.0),
            LogicalPosition::new(0.0, 0.0),
            LogicalSize::new(0.0, 0.0),
        );
        close(c.lat_deg, 10.0, 1e-9);
        close(c.lon_deg, 20.0, 1e-9);
    }
    #[test]
    fn px_at_latlon_centre_coord_is_the_container_centre() {
        let viewport = view(37.7749, -122.4194, 12.0);
        let container = LogicalSize::new(1024.0, 768.0);
        let p = MapWidget::px_at_latlon(
            viewport,
            MapLatLon {
                lat_deg: viewport.centre_lat_deg,
                lon_deg: viewport.centre_lon_deg,
            },
            container,
        );
        close(f64::from(p.x), 512.0, 1e-3);
        close(f64::from(p.y), 384.0, 1e-3);
    }
    #[test]
    fn px_at_latlon_saturates_instead_of_overflowing_f32() {
        // world = 256 * 2^f32::MAX overflows to +inf; the f64→f32 cast must
        // saturate (Rust's `as` is saturating) rather than trap.
        let container = LogicalSize::new(800.0, 600.0);
        let p = MapWidget::px_at_latlon(
            view(0.0, 0.0, f32::MAX),
            MapLatLon {
                lat_deg: 10.0,
                lon_deg: 10.0,
            },
            container,
        );
        assert!(!p.x.is_finite(), "expected a saturated x, got {}", p.x);
        assert!(!p.y.is_finite(), "expected a saturated y, got {}", p.y);
    }
    #[test]
    fn px_at_latlon_at_a_pole_centre_does_not_panic() {
        // cos(90°) is ~6e-17, not exactly 0 — the division is huge but finite.
        let container = LogicalSize::new(800.0, 600.0);
        let p = MapWidget::px_at_latlon(
            view(90.0, 0.0, 2.0),
            MapLatLon {
                lat_deg: 0.0,
                lon_deg: 0.0,
            },
            container,
        );
        assert!(p.x.is_finite(), "x should stay finite, got {}", p.x);
        assert!(
            !p.y.is_nan(),
            "y must be a number or an infinity, got {}",
            p.y
        );
    }
    #[test]
    fn projection_px_round_trips_within_the_clamped_band() {
        let container = LogicalSize::new(800.0, 600.0);
        for zoom in [2.0_f32, 8.0, 14.0] {
            let viewport = view(37.7749, -122.4194, zoom);
            for (dlat, dlon) in [(0.0, 0.0), (0.01, 0.02), (-0.03, 0.04)] {
                let coord = MapLatLon {
                    lat_deg: viewport.centre_lat_deg + dlat,
                    lon_deg: viewport.centre_lon_deg + dlon,
                };
                let px = MapWidget::px_at_latlon(viewport, coord, container);
                let back = MapWidget::latlon_at_px(viewport, px, container);
                close(back.lat_deg, coord.lat_deg, 1e-4);
                close(back.lon_deg, coord.lon_deg, 1e-4);
            }
        }
    }
    // ==================================================================
    // visible_tile_range  (numeric)
    // ==================================================================
    #[test]
    fn visible_tile_range_zero_zoom_scale_saturates_to_the_i32_extremes() {
        // tile_px = 0 → the half-extent is +inf → the floor/ceil casts saturate.
        // x is unclamped (world wrap), so the caller receives the FULL i32 span.
        let (x0, x1, y0, y1) = visible_tile_range(8.0, 8.0, 800.0, 600.0, 0.0, 16);
        assert_eq!((x0, x1), (i32::MIN, i32::MAX));
        assert_eq!((y0, y1), (0, 15));
    }
    #[test]
    fn visible_tile_range_infinite_dimensions_saturate_the_same_way() {
        let (x0, x1, y0, y1) = visible_tile_range(8.0, 8.0, f32::INFINITY, f32::INFINITY, 1.0, 16);
        assert_eq!((x0, x1), (i32::MIN, i32::MAX));
        assert_eq!((y0, y1), (0, 15));
    }
    #[test]
    fn visible_tile_range_nan_inputs_collapse_to_a_single_cell() {
        // `NaN as i32` is 0 in Rust (saturating cast), so a non-finite viewport
        // degenerates to the (0,0) cell instead of an unbounded loop.
        assert_eq!(
            visible_tile_range(8.0, 8.0, f32::NAN, f32::NAN, 1.0, 16),
            (0, 0, 0, 0)
        );
        assert_eq!(
            visible_tile_range(f32::NAN, f32::NAN, 512.0, 512.0, 1.0, 16),
            (0, 0, 0, 0)
        );
        assert_eq!(
            visible_tile_range(8.0, 8.0, 512.0, 512.0, f32::NAN, 16),
            (0, 0, 0, 0)
        );
    }
    #[test]
    fn visible_tile_range_negative_dimensions_are_taken_absolutely() {
        let positive = visible_tile_range(8.0, 8.0, 512.0, 384.0, 1.0, 16);
        let negative = visible_tile_range(8.0, 8.0, -512.0, -384.0, 1.0, 16);
        assert_eq!(positive, negative);
    }
    #[test]
    fn visible_tile_range_zero_tile_count_yields_an_empty_row_span() {
        // max_idx = -1, so y_min (>= 0) is above y_max — the caller's
        // `for y in y_min..=y_max` loop body never runs. No tiles, no panic.
        let (_, _, y0, y1) = visible_tile_range(0.0, 0.0, 512.0, 512.0, 1.0, 0);
        assert!(y0 > y1, "expected an empty row span, got {y0}..={y1}");
    }
    #[test]
    fn visible_tile_range_u32_max_tile_count_wraps_to_an_empty_row_span() {
        // `u32::MAX as i32` is -1 → max_idx -2 → again an empty (safe) span.
        let (_, _, y0, y1) = visible_tile_range(0.0, 0.0, 512.0, 512.0, 1.0, u32::MAX);
        assert!(y0 > y1, "expected an empty row span, got {y0}..={y1}");
    }
    #[test]
    fn visible_tile_range_always_pads_by_one_tile() {
        // Even a 1x1-pixel viewport gets one tile of padding per axis. It sits on
        // the corner of tiles 7 and 8, so it touches both; padding makes three.
        let (x0, x1, y0, y1) = visible_tile_range(8.0, 8.0, 1.0, 1.0, 1.0, 16);
        assert_eq!(x1 - x0 + 1, 3, "x span {x0}..={x1}");
        assert_eq!(y1 - y0 + 1, 3, "y span {y0}..={y1}");
    }
    #[test]
    fn visible_tile_range_extreme_zoom_scale_shrinks_to_one_tile_plus_padding() {
        // A gigantic tile_px makes the viewport a point at tile 8's corner: that
        // tile plus one tile of padding per axis.
        let (x0, x1, y0, y1) = visible_tile_range(8.0, 8.0, 800.0, 600.0, f32::MAX, 16);
        assert_eq!((x0, x1), (7, 8));
        assert_eq!((y0, y1), (7, 8));
    }
    // ==================================================================
    // wrap_tile_x  (numeric)
    // ==================================================================
    #[test]
    fn wrap_tile_x_zero_tile_count_is_zero_never_a_division_by_zero() {
        for x in [i32::MIN, -7, -1, 0, 1, 7, i32::MAX] {
            assert_eq!(wrap_tile_x(x, 0), 0, "column {x} at tile_count 0");
        }
    }
    #[test]
    fn wrap_tile_x_extremes_stay_inside_the_band() {
        for tile_count in [1u32, 2, 4, 256, 65_536, 1 << 30, 1 << 31] {
            for x in [i32::MIN, -1_000_000, -1, 0, 1, 1_000_000, i32::MAX] {
                let wrapped = wrap_tile_x(x, tile_count);
                assert!(
                    wrapped < tile_count,
                    "column {x} at tile_count {tile_count} → {wrapped} (out of band)"
                );
            }
        }
    }
    #[test]
    fn wrap_tile_x_matches_rem_euclid_for_realistic_zooms() {
        for z in 0u32..=20 {
            let tile_count = 1u32 << z;
            for x in [i32::MIN, -3, -1, 0, 1, 3, i32::MAX] {
                assert_eq!(
                    wrap_tile_x(x, tile_count),
                    x.rem_euclid(tile_count as i32) as u32
                );
            }
        }
    }
    #[test]
    fn wrap_tile_x_is_periodic_in_tile_count() {
        for tile_count in [1u32, 2, 4, 16, 1024] {
            for x in [-9i32, -1, 0, 1, 9] {
                let shifted = x
                    .checked_add(tile_count as i32)
                    .expect("shift must stay in range");
                assert_eq!(wrap_tile_x(x, tile_count), wrap_tile_x(shifted, tile_count));
            }
        }
    }
    // ==================================================================
    // map_visible_tiles  (numeric)
    // ==================================================================
    #[test]
    fn map_visible_tiles_zero_bounds_still_covers_the_centre() {
        let layer = MapTileLayer::default();
        let tiles = map_visible_tiles(&view(0.0, 0.0, 2.0), LogicalSize::new(0.0, 0.0), &layer);
        assert!(
            !tiles.is_empty(),
            "the one-tile margin must survive 0x0 bounds"
        );
        for t in &tiles {
            assert_eq!(t.z, 2);
            assert!(t.x < 4 && t.y < 4, "tile {t:?} escaped the z2 grid");
        }
    }
    #[test]
    fn map_visible_tiles_non_finite_bounds_degenerate_to_one_tile() {
        let layer = MapTileLayer::default();
        let tiles = map_visible_tiles(
            &view(0.0, 0.0, 2.0),
            LogicalSize::new(f32::NAN, f32::NAN),
            &layer,
        );
        assert_eq!(tiles.len(), 1, "NaN bounds must not enumerate a grid");
    }
    #[test]
    fn map_visible_tiles_nan_zoom_degenerates_to_one_tile() {
        let layer = MapTileLayer::default();
        let tiles = map_visible_tiles(
            &view(0.0, 0.0, f32::NAN),
            LogicalSize::new(800.0, 600.0),
            &layer,
        );
        assert_eq!(tiles.len(), 1);
        assert_eq!(tiles[0].z, 0, "a NaN zoom clamps to the layer minimum");
    }
    #[test]
    fn map_visible_tiles_positive_infinite_zoom_stays_bounded() {
        let layer = MapTileLayer::default();
        let tiles = map_visible_tiles(
            &view(0.0, 0.0, f32::INFINITY),
            LogicalSize::new(800.0, 600.0),
            &layer,
        );
        assert!(!tiles.is_empty());
        assert!(tiles.len() < 64, "+inf zoom produced {} tiles", tiles.len());
        for t in &tiles {
            assert_eq!(t.z, layer.max_zoom, "+inf zoom must clamp to max_zoom");
        }
    }
    #[test]
    fn map_visible_tiles_negative_bounds_match_positive_bounds() {
        let layer = MapTileLayer::default();
        let viewport = view(48.0, 11.0, 6.0);
        let positive = map_visible_tiles(&viewport, LogicalSize::new(640.0, 480.0), &layer);
        let negative = map_visible_tiles(&viewport, LogicalSize::new(-640.0, -480.0), &layer);
        assert_eq!(positive, negative);
    }
    #[test]
    fn map_visible_tiles_ids_always_live_inside_the_grid() {
        for (min_zoom, max_zoom) in [(0u8, 14u8), (3, 5), (0, 0)] {
            let layer = layer_zoom(min_zoom, max_zoom);
            for zoom in [-1.0_f32, 0.0, 2.5, 7.0, 99.0] {
                for (lat, lon) in [(0.0, 0.0), (85.0, 180.0), (-85.0, -180.0), (60.0, 179.99)] {
                    let viewport = view(lat, lon, zoom);
                    let tiles =
                        map_visible_tiles(&viewport, LogicalSize::new(800.0, 600.0), &layer);
                    let expected_z =
                        (zoom.floor() as i32).clamp(i32::from(min_zoom), i32::from(max_zoom)) as u8;
                    let tile_count = 1u32 << u32::from(expected_z);
                    for t in &tiles {
                        assert_eq!(t.z, expected_z, "zoom {zoom} produced z {}", t.z);
                        assert!(t.x < tile_count, "column {} >= {tile_count}", t.x);
                        assert!(t.y < tile_count, "row {} >= {tile_count}", t.y);
                    }
                }
            }
        }
    }
    #[test]
    fn map_visible_tiles_wraps_columns_across_the_antimeridian() {
        // A viewport pinned to +180° over-scans past the east edge; every id
        // must still be a legal column (the wrap), never tile_count or above.
        let layer = layer_zoom(0, 14);
        let tiles = map_visible_tiles(
            &view(0.0, 180.0, 3.0),
            LogicalSize::new(1024.0, 256.0),
            &layer,
        );
        assert!(!tiles.is_empty());
        assert!(
            tiles.iter().any(|t| t.x == 0),
            "panning past +180° must surface the west-edge column"
        );
        for t in &tiles {
            assert!(t.x < 8, "column {} escaped the z3 grid", t.x);
        }
    }
    // ==================================================================
    // MapWidget builders  (constructors)
    // ==================================================================
    #[test]
    fn create_uses_the_given_layer_and_neutral_defaults() {
        let layer = layer_zoom(2, 9);
        let widget = MapWidget::create(layer.clone());
        assert_eq!(widget.layer, layer);
        assert_eq!(widget.viewport, MapViewport::default());
        assert!(
            widget.container_style.as_ref().is_none(),
            "a fresh widget has no opinion on its container style"
        );
        assert!(matches!(
            widget.on_viewport_changed,
            OptionMapViewportChanged::None
        ));
        assert!(matches!(widget.on_pin_tap, OptionMapPinTap::None));
    }
    #[test]
    fn create_accepts_degenerate_layers_without_panicking() {
        // An inverted zoom band and an empty template are nonsense but must
        // still build - the widget validates nothing at construction time.
        let widget = MapWidget::create(MapTileLayer {
            url_template: AzString::from(""),
            min_zoom: 30,
            max_zoom: 0,
            attribution: AzString::from(""),
            style_css: AzString::from(""),
            theme: MapTheme::System,
        });
        assert_eq!(widget.layer.min_zoom, 30);
        assert_eq!(widget.layer.max_zoom, 0);
    }
    #[test]
    fn with_viewport_stores_extreme_values_verbatim() {
        let widget = MapWidget::create(MapTileLayer::default()).with_viewport(view(
            1.0e300,
            -1.0e300,
            f32::MAX,
        ));
        assert_eq!(widget.viewport.centre_lat_deg, 1.0e300);
        assert_eq!(widget.viewport.centre_lon_deg, -1.0e300);
        assert_eq!(widget.viewport.zoom, f32::MAX);
        let widget = MapWidget::create(MapTileLayer::default()).with_viewport(view(
            f64::NAN,
            f64::INFINITY,
            f32::NEG_INFINITY,
        ));
        assert!(widget.viewport.centre_lat_deg.is_nan());
        assert!(widget.viewport.centre_lon_deg.is_infinite());
        assert!(widget.viewport.zoom.is_infinite());
    }
    #[test]
    fn with_viewport_is_last_write_wins() {
        let widget = MapWidget::create(MapTileLayer::default())
            .with_viewport(view(1.0, 2.0, 3.0))
            .with_viewport(view(4.0, 5.0, 6.0));
        assert_eq!(widget.viewport, view(4.0, 5.0, 6.0));
    }
    #[test]
    fn with_container_style_replaces_the_style_vec() {
        let css = CssPropertyWithConditionsVec::parse("width: 100px; height: 50px;");
        let parsed_len = css.as_slice().len();
        assert!(parsed_len > 0, "positive control: the style must parse");
        let widget = MapWidget::create(MapTileLayer::default()).with_container_style(css);
        assert_eq!(
            widget
                .container_style
                .as_ref()
                .expect("the builder stores what it was given")
                .as_slice()
                .len(),
            parsed_len
        );
        // An unparseable style yields an EMPTY vec, and the builder stores that
        // as `Some(empty)` — a real answer ("no properties"), distinct from the
        // `None` a fresh widget carries.
        let widget = MapWidget::create(MapTileLayer::default())
            .with_container_style(CssPropertyWithConditionsVec::parse(""));
        assert!(widget
            .container_style
            .as_ref()
            .expect("an explicit empty style is still an opinion")
            .as_slice()
            .is_empty());
    }
    #[test]
    fn with_container_style_tolerates_garbage_and_unicode() {
        for style in [
            "\u{1F600}: \u{1F600};",
            "   ",
            ";;;;",
            "width",
            "width: ;",
            "}{",
            "color: \u{0301}\u{0301};",
        ] {
            let widget = MapWidget::create(MapTileLayer::default())
                .with_container_style(CssPropertyWithConditionsVec::parse(style));
            // Reaching here means neither the parser nor the builder panicked.
            let _ = widget.container_style.as_ref().map(|c| c.as_slice().len());
        }
    }
    #[test]
    fn with_on_viewport_changed_installs_and_replaces_the_hook() {
        let mut widget = MapWidget::create(MapTileLayer::default()).with_on_viewport_changed(
            RefAny::new(HookLog::default()),
            record_viewport as MapViewportChangedCallbackType,
        );
        assert!(matches!(
            widget.on_viewport_changed,
            OptionMapViewportChanged::Some(_)
        ));
        // Re-setting must overwrite, not accumulate.
        widget.set_on_viewport_changed(
            RefAny::new(HookLog::default()),
            record_viewport as MapViewportChangedCallbackType,
        );
        assert!(matches!(
            widget.on_viewport_changed,
            OptionMapViewportChanged::Some(_)
        ));
        assert!(matches!(widget.on_pin_tap, OptionMapPinTap::None));
    }
    #[test]
    fn with_on_pin_tap_installs_and_replaces_the_hook() {
        let mut widget = MapWidget::create(MapTileLayer::default()).with_on_pin_tap(
            RefAny::new(HookLog::default()),
            record_pin as MapPinTapCallbackType,
        );
        assert!(matches!(widget.on_pin_tap, OptionMapPinTap::Some(_)));
        widget.set_on_pin_tap(
            RefAny::new(HookLog::default()),
            record_pin as MapPinTapCallbackType,
        );
        assert!(matches!(widget.on_pin_tap, OptionMapPinTap::Some(_)));
        assert!(matches!(
            widget.on_viewport_changed,
            OptionMapViewportChanged::None
        ));
    }
    #[test]
    fn builder_chain_order_does_not_matter_for_independent_fields() {
        let layer = layer_zoom(1, 12);
        let viewport = view(10.0, 20.0, 5.0);
        let a = MapWidget::create(layer.clone())
            .with_viewport(viewport)
            .with_container_style(CssPropertyWithConditionsVec::parse("width: 10px;"));
        let b = MapWidget::create(layer)
            .with_container_style(CssPropertyWithConditionsVec::parse("width: 10px;"))
            .with_viewport(viewport);
        assert_eq!(a, b);
    }
    // ==================================================================
    // MapWidget::dom / mount
    // ==================================================================
    #[test]
    fn dom_builds_a_single_virtual_view_child_with_a_dataset() {
        let mut dom = MapWidget::create(MapTileLayer::default())
            .with_viewport(view(0.0, 0.0, 3.0))
            .dom();
        assert_eq!(dom.children.as_slice().len(), 1, "one VirtualView child");
        let dataset = dom
            .root
            .get_dataset_mut()
            .expect("the widget div must carry a MapTileCache dataset");
        let cache = dataset
            .downcast_ref::<MapTileCache>()
            .expect("the dataset must be a MapTileCache");
        assert_eq!(cache.viewport.zoom, 3.0);
        assert!(cache.tiles.is_empty());
        assert!(cache.fetch_callback.is_none(), "dom() wires no worker");
    }
    #[test]
    fn dom_survives_degenerate_viewports_and_layers() {
        for viewport in [
            view(f64::NAN, f64::NAN, f32::NAN),
            view(1.0e300, -1.0e300, f32::INFINITY),
            view(0.0, 0.0, f32::NEG_INFINITY),
        ] {
            let dom = MapWidget::create(layer_zoom(200, 1))
                .with_viewport(viewport)
                .dom();
            assert_eq!(dom.children.as_slice().len(), 1);
        }
    }
    #[test]
    fn dom_with_a_container_style_still_builds_the_grid() {
        let dom = MapWidget::create(MapTileLayer::default())
            .with_container_style(CssPropertyWithConditionsVec::parse(
                "position: relative; width: 320px; height: 240px;",
            ))
            .dom();
        assert_eq!(dom.children.as_slice().len(), 1);
    }
    #[test]
    fn mounting_installs_the_registered_worker_and_building_the_dom_never_does() {
        // The fetcher is a process-wide registration (first one wins), so the
        // test accepts whichever worker is installed.
        let _ = register_map_tile_fetcher(ThreadCallback::new(noop_worker));
        assert!(has_map_tile_fetcher());
        let mut dom = MapWidget::create(MapTileLayer::default()).dom();
        let mut dataset = dom.root.get_dataset_mut().expect("dataset").clone();
        assert!(
            dataset
                .downcast_ref::<MapTileCache>()
                .expect("cache")
                .fetch_callback
                .is_none(),
            "building the Dom only describes the UI"
        );
        with_callback_info(|info| mount_map(&mut dataset, &info));
        let cache = dataset.downcast_ref::<MapTileCache>().expect("cache");
        let cb = cache
            .fetch_callback
            .as_ref()
            .expect("mount must install the registered worker");
        assert_eq!(
            cb.cb as usize,
            MAP_TILE_FETCHER.get().expect("registered").cb as usize
        );
        assert_eq!(
            cache.setup,
            MapSetup::new(),
            "without a hook nothing is shared"
        );
    }
    extern "C" fn pooled_setup(_: RefAny, _: CallbackInfo, setup: MapSetup) -> MapSetup {
        setup
            .with_thread_pool(ThreadPool::create(2))
            .with_max_in_flight(8)
    }
    #[test]
    fn the_mount_hook_decides_the_setup_and_a_rebuild_keeps_it() {
        let build = || {
            MapWidget::create(MapTileLayer::default())
                .with_on_mount(RefAny::new(()), pooled_setup as MapMountCallbackType)
                .dom()
        };
        let mut first = build();
        let mut mounted = first.root.get_dataset_mut().expect("dataset").clone();
        with_callback_info(|info| mount_map(&mut mounted, &info));
        let pool = {
            let cache = mounted.downcast_ref::<MapTileCache>().expect("cache");
            assert_eq!(cache.setup.max_in_flight, 8);
            match &cache.setup.thread_pool {
                OptionThreadPool::Some(pool) => pool.clone(),
                OptionThreadPool::None => panic!("the hook's pool must be installed"),
            }
        };
        // A rebuild produces a fresh, never-mounted cache; the merge must keep
        // the mounted one and everything mount put into it.
        let mut second = build();
        let rebuilt = second.root.get_dataset_mut().expect("dataset").clone();
        let mut kept = merge_map_tile_cache(rebuilt, mounted);
        let cache = kept.downcast_ref::<MapTileCache>().expect("cache");
        assert_eq!(cache.setup.max_in_flight, 8);
        assert_eq!(
            cache.setup.thread_pool,
            OptionThreadPool::Some(pool),
            "the same pool, not a new one"
        );
    }
    #[test]
    fn max_in_flight_leaves_room_only_for_fetches_not_already_running() {
        let mut cache = cache_at(0.0, 0.0, 4.0);
        assert_eq!(
            spawn_budget(&cache),
            MAX_SPAWN_PER_CALL,
            "no limit: the per-pass cap"
        );
        cache.setup.max_in_flight = 8;
        for x in 0..5 {
            cache.insert_tile(MapTileId { z: 4, x, y: 0 }, TileEntry::Fetching);
        }
        // Queued and finished tiles are not running.
        cache.insert_tile(MapTileId { z: 4, x: 9, y: 0 }, TileEntry::Pending);
        cache.insert_tile(
            MapTileId { z: 4, x: 10, y: 0 },
            TileEntry::Ready {
                svg: AzString::from("<svg/>"),
            },
        );
        assert_eq!(spawn_budget(&cache), 3);
        for x in 0..7 {
            cache.insert_tile(MapTileId { z: 4, x, y: 1 }, TileEntry::Fetching);
        }
        assert_eq!(spawn_budget(&cache), 0, "over the limit starts nothing");
    }
    #[test]
    fn dom_carries_the_user_hooks_into_the_cache() {
        let mut dom = MapWidget::create(MapTileLayer::default())
            .with_on_viewport_changed(
                RefAny::new(HookLog::default()),
                record_viewport as MapViewportChangedCallbackType,
            )
            .with_on_pin_tap(
                RefAny::new(HookLog::default()),
                record_pin as MapPinTapCallbackType,
            )
            .dom();
        let dataset = dom.root.get_dataset_mut().expect("dataset");
        let cache = dataset.downcast_ref::<MapTileCache>().expect("cache");
        assert!(matches!(
            cache.on_viewport_changed,
            OptionMapViewportChanged::Some(_)
        ));
        assert!(matches!(cache.on_pin_tap, OptionMapPinTap::Some(_)));
    }
    // ==================================================================
    // MapTileCache  (constructor + mutators)
    // ==================================================================
    #[test]
    fn tile_cache_new_starts_empty_and_idle() {
        let cache = MapTileCache::new(layer_zoom(0, 14), view(1.0, 2.0, 3.0));
        assert!(cache.tiles.is_empty());
        assert!(cache.fetch_callback.is_none());
        assert!(cache.drag_anchor.is_none());
        assert!(cache.pinch_anchor.is_none());
        assert!(cache.press_origin.is_none());
        assert_eq!(cache.viewport, view(1.0, 2.0, 3.0));
        assert_eq!(cache.layer.max_zoom, 14);
    }
    #[test]
    fn mark_tile_ready_and_failed_overwrite_each_other() {
        let mut cache = cache_at(0.0, 0.0, 4.0);
        let tile = MapTileId { z: 4, x: 8, y: 8 };
        let key = cache.key_at_current_look(tile);
        cache.mark_tile_ready(key, AzString::from("<svg/>"));
        assert!(matches!(
            cache.tile_entry(tile),
            Some(TileEntry::Ready { .. })
        ));
        cache.mark_tile_failed(key, AzString::from("boom"));
        assert!(matches!(
            cache.tile_entry(tile),
            Some(TileEntry::Failed { .. })
        ));
        cache.mark_tile_ready(key, AzString::from(""));
        assert!(matches!(
            cache.tile_entry(tile),
            Some(TileEntry::Ready { .. })
        ));
        assert_eq!(cache.tiles.len(), 1, "the same id must not duplicate");
    }
    #[test]
    fn mark_tile_ready_accepts_empty_unicode_and_huge_payloads() {
        let mut cache = cache_at(0.0, 0.0, 4.0);
        let payloads = [
            AzString::from(""),
            AzString::from("\u{1F600}\u{4F60}\u{597D}e\u{0301}"),
            AzString::from("\0\u{FFFD}\r\n\t"),
            AzString::from("x".repeat(1_000_000)),
        ];
        for (i, svg) in payloads.into_iter().enumerate() {
            cache.insert_tile(
                MapTileId {
                    z: 4,
                    x: i as u32,
                    y: 0,
                },
                TileEntry::Ready { svg },
            );
        }
        assert_eq!(cache.tiles.len(), 4);
    }
    #[test]
    fn mark_tile_ready_accepts_out_of_range_tile_ids() {
        // Nothing validates an id at insert time - a bogus id from an FFI
        // caller must land in the map rather than panic.
        let mut cache = cache_at(0.0, 0.0, 4.0);
        for tile in [
            MapTileId { z: 0, x: 0, y: 0 },
            MapTileId {
                z: 31,
                x: u32::MAX,
                y: u32::MAX,
            },
            MapTileId {
                z: 4,
                x: u32::MAX,
                y: 0,
            },
        ] {
            cache.insert_tile(
                tile,
                TileEntry::Failed {
                    error: AzString::from("e"),
                },
            );
        }
        assert_eq!(cache.tiles.len(), 3);
    }
    // ==================================================================
    // MapTileCache::prune_distant_tiles
    // ==================================================================
    fn fill_ready_grid(cache: &mut MapTileCache, z: u8, side: u32) {
        for x in 0..side {
            for y in 0..side {
                cache.insert_tile(
                    MapTileId { z, x, y },
                    TileEntry::Ready {
                        svg: AzString::from("<svg/>"),
                    },
                );
            }
        }
    }
    #[test]
    fn prune_never_evicts_in_flight_tiles_even_far_over_the_cap() {
        let mut cache = cache_at(0.0, 0.0, 4.0);
        for x in 0..16u32 {
            for y in 0..16u32 {
                cache.insert_tile(
                    MapTileId { z: 4, x, y },
                    if (x + y) % 2 == 0 {
                        TileEntry::Pending
                    } else {
                        TileEntry::Fetching
                    },
                );
            }
        }
        assert_eq!(cache.tiles.len(), 256);
        cache.prune_distant_tiles();
        assert_eq!(
            cache.tiles.len(),
            256,
            "in-flight tiles are unevictable, so the cap can be exceeded"
        );
    }
    #[test]
    fn prune_with_a_nan_viewport_centre_still_bounds_the_cache() {
        let mut cache = MapTileCache::new(layer_zoom(0, 19), view(f64::NAN, f64::NAN, 4.0));
        fill_ready_grid(&mut cache, 4, 16);
        assert_eq!(cache.tiles.len(), 256);
        // Every score is NaN → `partial_cmp` returns None → the sort falls back
        // to Equal. The eviction count must still be honoured.
        cache.prune_distant_tiles();
        assert_eq!(cache.tiles.len(), 192);
    }
    #[test]
    fn prune_with_non_finite_zoom_clamps_to_the_layer_band() {
        for zoom in [f32::INFINITY, f32::NEG_INFINITY, f32::NAN, 1.0e30, -1.0e30] {
            let mut cache = MapTileCache::new(layer_zoom(0, 19), view(0.0, 0.0, zoom));
            fill_ready_grid(&mut cache, 4, 16);
            cache.prune_distant_tiles();
            assert_eq!(
                cache.tiles.len(),
                192,
                "zoom {zoom} must still bound the cache"
            );
        }
    }
    #[test]
    fn prune_is_idempotent_once_under_the_cap() {
        let mut cache = cache_at(0.0, 0.0, 4.0);
        fill_ready_grid(&mut cache, 4, 16);
        cache.prune_distant_tiles();
        let first = cache.tiles.len();
        let survivors: Vec<MapTileId> = cache.tiles.keys().map(|k| k.tile).collect();
        cache.prune_distant_tiles();
        assert_eq!(cache.tiles.len(), first);
        assert_eq!(
            cache.tiles.keys().map(|k| k.tile).collect::<Vec<_>>(),
            survivors,
            "a second prune under the cap must change nothing"
        );
    }
    #[test]
    fn prune_drops_the_farthest_tiles_first_across_zoom_levels() {
        // A mixed-zoom cache: same-zoom near tiles must outlive a wrong-zoom
        // tile (the score adds 10_000 per zoom level of mismatch).
        let mut cache = cache_at(0.0, 0.0, 4.0);
        fill_ready_grid(&mut cache, 4, 16);
        let wrong_zoom = MapTileId {
            z: 9,
            x: 256,
            y: 256,
        };
        cache.insert_tile(
            wrong_zoom,
            TileEntry::Ready {
                svg: AzString::from("<svg/>"),
            },
        );
        let near = MapTileId { z: 4, x: 8, y: 8 };
        cache.prune_distant_tiles();
        assert!(cache.tiles.len() <= 192);
        assert!(
            cache.tile_entry(wrong_zoom).is_none(),
            "a zoom-mismatched tile must be evicted before same-zoom neighbours"
        );
        assert!(
            cache.tile_entry(near).is_some(),
            "the centre tile must survive"
        );
    }
    // ==================================================================
    // merge_map_tile_cache
    // ==================================================================
    #[test]
    fn merge_with_a_wrong_typed_new_dataset_returns_the_old_one_intact() {
        let mut old_cache = cache_at(0.0, 0.0, 5.0);
        let tile = MapTileId { z: 5, x: 1, y: 1 };
        old_cache.insert_tile(
            tile,
            TileEntry::Ready {
                svg: AzString::from("<svg/>"),
            },
        );
        let mut merged = merge_map_tile_cache(RefAny::new(0u32), RefAny::new(old_cache));
        let cache = merged.downcast_ref::<MapTileCache>().expect("old cache");
        assert_eq!(
            cache.viewport.zoom, 5.0,
            "no adoption from a bogus new dataset"
        );
        assert!(cache.tile_entry(tile).is_some());
    }
    #[test]
    fn merge_with_a_wrong_typed_old_dataset_returns_it_unchanged() {
        let new_cache = cache_at(0.0, 0.0, 9.0);
        let mut merged = merge_map_tile_cache(RefAny::new(new_cache), RefAny::new(7u64));
        assert!(
            merged.downcast_ref::<MapTileCache>().is_none(),
            "the merge must not fabricate a cache out of a wrong-typed dataset"
        );
        assert_eq!(*merged.downcast_ref::<u64>().expect("u64 payload"), 7);
    }
    #[test]
    fn merge_of_two_aliases_of_one_dataset_does_not_panic() {
        // Both handles share one allocation, so the shared borrow taken for
        // `new_data` blocks the exclusive borrow for `old_data`. The merge must
        // degrade to "no adoption" rather than deadlock or panic.
        let dataset = RefAny::new(cache_at(0.0, 0.0, 5.0));
        let mut merged = merge_map_tile_cache(dataset.clone(), dataset);
        let cache = merged.downcast_ref::<MapTileCache>().expect("cache");
        assert_eq!(cache.viewport.zoom, 5.0);
    }
    #[test]
    fn merge_keeps_what_mount_installed_and_takes_neither_worker_nor_setup_from_a_rebuild() {
        // The mounted cache: worker and setup installed.
        let mut old_cache = cache_at(0.0, 0.0, 5.0);
        old_cache.fetch_callback = Some(ThreadCallback::new(noop_worker));
        old_cache.setup = MapSetup::new().with_max_in_flight(4);
        // A rebuild that (wrongly) carries different ones must not replace them.
        let mut new_cache = cache_at(0.0, 0.0, 6.0);
        new_cache.fetch_callback = Some(ThreadCallback::new(other_noop_worker));
        new_cache.setup = MapSetup::new().with_max_in_flight(9);
        let mut merged = merge_map_tile_cache(RefAny::new(new_cache), RefAny::new(old_cache));
        let cache = merged.downcast_ref::<MapTileCache>().expect("cache");
        let cb = cache.fetch_callback.as_ref().expect("kept worker");
        assert_eq!(cb.cb as usize, noop_worker as ThreadCallbackType as usize);
        assert_eq!(cache.setup.max_in_flight, 4);
        // An unmounted old cache stays unwired until mount runs.
        let old_cache = cache_at(0.0, 0.0, 5.0);
        let mut new_cache = cache_at(0.0, 0.0, 6.0);
        new_cache.fetch_callback = Some(ThreadCallback::new(noop_worker));
        let mut merged = merge_map_tile_cache(RefAny::new(new_cache), RefAny::new(old_cache));
        let cache = merged.downcast_ref::<MapTileCache>().expect("cache");
        assert!(cache.fetch_callback.is_none());
    }
    #[test]
    fn merge_adopts_the_build_layer_and_viewport_even_when_degenerate() {
        let old_cache = MapTileCache::new(layer_zoom(0, 19), view(10.0, 20.0, 5.0));
        let new_cache =
            MapTileCache::new(layer_zoom(3, 7), view(f64::NAN, f64::INFINITY, f32::NAN));
        let mut merged = merge_map_tile_cache(RefAny::new(new_cache), RefAny::new(old_cache));
        let cache = merged.downcast_ref::<MapTileCache>().expect("cache");
        assert!(cache.viewport.centre_lat_deg.is_nan());
        assert!(cache.viewport.zoom.is_nan());
        assert_eq!(cache.layer.min_zoom, 3);
        assert_eq!(cache.layer.max_zoom, 7);
    }
    // ==================================================================
    // build_tile_url  (parser-ish substitution)
    // ==================================================================
    #[test]
    fn build_tile_url_without_placeholders_is_the_identity() {
        let tile = MapTileId { z: 1, x: 2, y: 3 };
        assert_eq!(build_tile_url("", tile), "");
        assert_eq!(
            build_tile_url("https://t.example/fixed", tile),
            "https://t.example/fixed"
        );
        // Unknown placeholders are left verbatim, not eaten.
        assert_eq!(build_tile_url("{q}/{Z}/{ x }", tile), "{q}/{Z}/{ x }");
    }
    #[test]
    fn build_tile_url_substitutes_extreme_tile_ids() {
        let tile = MapTileId {
            z: u8::MAX,
            x: u32::MAX,
            y: 0,
        };
        assert_eq!(build_tile_url("{z}/{x}/{y}", tile), "255/4294967295/0");
    }
    #[test]
    fn build_tile_url_handles_unicode_and_unbalanced_braces() {
        let tile = MapTileId { z: 7, x: 8, y: 9 };
        assert_eq!(
            build_tile_url("\u{1F600}/{z}/\u{4F60}\u{597D}/{y}", tile),
            "\u{1F600}/7/\u{4F60}\u{597D}/9"
        );
        assert_eq!(build_tile_url("{{z}}", tile), "{7}");
        assert_eq!(build_tile_url("{z", tile), "{z");
        assert_eq!(build_tile_url("z}", tile), "z}");
    }
    #[test]
    fn build_tile_url_substitution_is_not_re_scanned() {
        // `{z}` expands to a number, so no expansion can create a new
        // placeholder — but a template that already spells one out must not be
        // touched twice either.
        let tile = MapTileId { z: 1, x: 2, y: 3 };
        assert_eq!(build_tile_url("{z}{x}{y}{z}", tile), "1231");
    }
    #[test]
    fn build_tile_url_extremely_long_template_does_not_hang() {
        let template = "{z}/".repeat(100_000);
        let url = build_tile_url(&template, MapTileId { z: 14, x: 0, y: 0 });
        assert_eq!(url.len(), 100_000 * 3);
        assert!(url.starts_with("14/14/"));
    }
    // ==================================================================
    // svg_string_to_dom  (parser)
    // ==================================================================
    // --- xml + cpurender: the SVG is rasterised into an image node ---
    #[cfg(all(feature = "xml", feature = "cpurender"))]
    const MINIMAL_TILE_SVG: &str =
        r#"<svg viewBox="0 0 16 16"><rect x="0" y="0" width="16" height="16" fill="red"/></svg>"#;
    #[cfg(all(feature = "xml", feature = "cpurender"))]
    #[test]
    fn svg_raster_valid_minimal_is_the_positive_control() {
        assert!(
            svg_string_to_dom(MINIMAL_TILE_SVG).is_some(),
            "a well-formed <svg> must rasterise into a Dom"
        );
    }
    #[cfg(all(feature = "xml", feature = "cpurender"))]
    #[test]
    fn svg_raster_empty_whitespace_and_garbage_return_none() {
        for bad in [
            "",
            " ",
            "   \t\n\r ",
            "garbage",
            "<<<>>>",
            "<svg",
            "</svg>",
            "<html><body/></html>",
            "\u{0}\u{1}\u{2}",
        ] {
            assert!(
                svg_string_to_dom(bad).is_none(),
                "{bad:?} must be rejected, not rendered"
            );
        }
    }
    #[cfg(all(feature = "xml", feature = "cpurender"))]
    #[test]
    fn svg_raster_boundary_numeric_attributes_do_not_panic() {
        for svg in [
            r#"<svg viewBox="0 0 16 16"><rect width="0" height="-0" fill="red"/></svg>"#,
            r#"<svg viewBox="0 0 16 16"><rect width="NaN" height="inf" fill="red"/></svg>"#,
            r#"<svg viewBox="0 0 16 16"><rect width="1e400" height="1e-400" fill="red"/></svg>"#,
            r#"<svg viewBox="0 0 16 16"><rect width="9223372036854775807" height="8"/></svg>"#,
            r#"<svg viewBox="0 0 0 0"><rect width="8" height="8" fill="red"/></svg>"#,
            r#"<svg viewBox="NaN NaN NaN NaN"><rect width="8" height="8" fill="red"/></svg>"#,
        ] {
            // Reaching the next iteration means the rasteriser did not panic.
            let _ = svg_string_to_dom(svg).is_some();
        }
    }
    #[cfg(all(feature = "xml", feature = "cpurender"))]
    #[test]
    fn svg_raster_unicode_content_does_not_panic() {
        let svg = "<svg viewBox=\"0 0 16 16\"><title>\u{1F600} \u{4F60}\u{597D} e\u{0301} \
                   \u{202E}</title><rect width=\"16\" height=\"16\" fill=\"red\"/></svg>";
        assert!(svg_string_to_dom(svg).is_some());
    }
    #[cfg(all(feature = "xml", feature = "cpurender"))]
    #[test]
    fn svg_raster_leading_and_trailing_junk_is_deterministic() {
        for svg in [
            "  <svg viewBox=\"0 0 8 8\"><rect width=\"8\" height=\"8\"/></svg>  ",
            "<svg viewBox=\"0 0 8 8\"><rect width=\"8\" height=\"8\"/></svg>;garbage",
            "junk<svg viewBox=\"0 0 8 8\"><rect width=\"8\" height=\"8\"/></svg>",
        ] {
            // Whatever the verdict, it must be stable across calls.
            assert_eq!(
                svg_string_to_dom(svg).is_some(),
                svg_string_to_dom(svg).is_some(),
                "{svg:?} parsed non-deterministically"
            );
        }
    }
    #[cfg(all(feature = "xml", feature = "cpurender"))]
    #[test]
    fn svg_raster_extremely_long_input_does_not_hang() {
        let svg = alloc::format!(
            "<svg viewBox=\"0 0 8 8\"><desc>{}</desc><rect width=\"8\" height=\"8\" \
             fill=\"red\"/></svg>",
            "a".repeat(1_000_000)
        );
        assert!(svg_string_to_dom(&svg).is_some());
    }
    #[cfg(all(feature = "xml", feature = "cpurender"))]
    #[test]
    fn svg_raster_deeply_nested_groups_do_not_stack_overflow() {
        // The rasteriser recurses per group; give it a generous stack so a real
        // 2000-deep document is a clean test rather than a crash.
        let ok = std::thread::Builder::new()
            .stack_size(128 * 1024 * 1024)
            .spawn(|| {
                const DEPTH: usize = 2_000;
                let svg = alloc::format!(
                    "<svg viewBox=\"0 0 8 8\">{}<rect width=\"8\" height=\"8\" \
                     fill=\"red\"/>{}</svg>",
                    "<g>".repeat(DEPTH),
                    "</g>".repeat(DEPTH)
                );
                svg_string_to_dom(&svg).is_some()
            })
            .expect("spawn")
            .join()
            .expect("2000-deep nesting must not overflow the stack");
        assert!(ok);
    }
    // --- xml without cpurender: the SVG goes through the XML→DOM path ---
    #[cfg(all(feature = "xml", not(feature = "cpurender")))]
    #[test]
    fn svg_dom_valid_minimal_is_the_positive_control() {
        assert!(svg_string_to_dom("<svg><g/></svg>").is_some());
    }
    #[cfg(all(feature = "xml", not(feature = "cpurender")))]
    #[test]
    fn svg_dom_malformed_markup_returns_none() {
        for bad in ["<<<>>>", "<svg", "</svg>", "<a></b>", "\u{0}\u{1}"] {
            assert!(svg_string_to_dom(bad).is_none(), "{bad:?} must be rejected");
        }
    }
    #[cfg(all(feature = "xml", not(feature = "cpurender")))]
    #[test]
    fn svg_dom_empty_whitespace_and_unicode_do_not_panic() {
        for input in ["", " ", "   \t\n\r ", "\u{1F600}", "e\u{0301}"] {
            assert_eq!(
                svg_string_to_dom(input).is_some(),
                svg_string_to_dom(input).is_some()
            );
        }
    }
    #[cfg(all(feature = "xml", not(feature = "cpurender")))]
    #[test]
    fn svg_dom_extremely_long_input_does_not_hang() {
        let svg = alloc::format!("<svg><desc>{}</desc></svg>", "a".repeat(1_000_000));
        let _ = svg_string_to_dom(&svg).is_some();
    }
    // --- no xml feature: the stub always declines ---
    #[cfg(not(feature = "xml"))]
    #[test]
    fn svg_stub_returns_none_for_every_input() {
        for input in [
            "",
            "   ",
            "<svg/>",
            "<svg><g/></svg>",
            "\u{1F600}",
            "<<<>>>",
        ] {
            assert!(svg_string_to_dom(input).is_none());
        }
        let long = "a".repeat(1_000_000);
        assert!(svg_string_to_dom(&long).is_none());
    }
    // ==================================================================
    // User-hook invocation  (invoke_viewport_changed / invoke_pin_tap)
    // ==================================================================
    #[test]
    fn invoke_viewport_changed_without_a_hook_is_do_nothing() {
        let (update, changes) = with_callback_info(|info| {
            invoke_viewport_changed(&OptionMapViewportChanged::None, &info, view(0.0, 0.0, 2.0))
        });
        assert_eq!(update, Update::DoNothing);
        assert!(changes.is_empty());
    }
    #[test]
    fn invoke_viewport_changed_forwards_even_a_nan_viewport() {
        let mut log = RefAny::new(HookLog::default());
        let hook = OptionMapViewportChanged::Some(MapViewportChanged {
            refany: log.clone(),
            callback: (record_viewport as MapViewportChangedCallbackType).into(),
        });
        let viewport = view(f64::NAN, f64::INFINITY, f32::NAN);
        let (update, _) =
            with_callback_info(|info| invoke_viewport_changed(&hook, &info, viewport));
        assert_eq!(update, Update::DoNothing);
        assert_eq!(hook_log(&mut log), (1, 0));
    }
    extern "C" fn record_viewport_and_refresh(
        mut data: RefAny,
        _: CallbackInfo,
        viewport: MapViewport,
    ) -> Update {
        if let Some(mut log) = data.downcast_mut::<HookLog>() {
            log.viewports.push(viewport);
        }
        Update::RefreshDom
    }
    #[test]
    fn finish_viewport_change_returns_the_hooks_update_and_rerenders_only_without_a_rebuild() {
        // No hook: the handler owes the in-place VirtualView re-render itself.
        let (update, changes) = with_callback_info(|mut info| {
            finish_viewport_change(
                &OptionMapViewportChanged::None,
                &mut info,
                view(1.0, 2.0, 3.0),
            )
        });
        assert_eq!(update, Update::DoNothing);
        assert!(
            changes
                .iter()
                .any(|c| matches!(c, CallbackChange::UpdateAllVirtualViews)),
            "without a rebuild the view must be re-rendered in place: {changes:?}"
        );
        // A hook that answers DoNothing: same, plus the hook saw the viewport.
        let mut log = RefAny::new(HookLog::default());
        let hook = OptionMapViewportChanged::Some(MapViewportChanged {
            refany: log.clone(),
            callback: (record_viewport as MapViewportChangedCallbackType).into(),
        });
        let (update, changes) = with_callback_info(|mut info| {
            finish_viewport_change(&hook, &mut info, view(1.0, 2.0, 3.0))
        });
        assert_eq!(update, Update::DoNothing);
        assert!(changes
            .iter()
            .any(|c| matches!(c, CallbackChange::UpdateAllVirtualViews)));
        assert_eq!(hook_log(&mut log), (1, 0));
        // A hook that asks for RefreshDom: the handler RETURNS it (the bug:
        // every handler returned DoNothing and the app's readout froze), and
        // does not also queue the in-place re-render — the full path renders.
        let mut log = RefAny::new(HookLog::default());
        let hook = OptionMapViewportChanged::Some(MapViewportChanged {
            refany: log.clone(),
            callback: (record_viewport_and_refresh as MapViewportChangedCallbackType).into(),
        });
        let (update, changes) = with_callback_info(|mut info| {
            finish_viewport_change(&hook, &mut info, view(1.0, 2.0, 3.0))
        });
        assert_eq!(
            update,
            Update::RefreshDom,
            "the user's Update must come out of the handler"
        );
        assert!(
            !changes
                .iter()
                .any(|c| matches!(c, CallbackChange::UpdateAllVirtualViews)),
            "a rebuild already re-invokes the view; rendering it twice is waste: {changes:?}"
        );
        assert_eq!(hook_log(&mut log), (1, 0));
    }
    #[test]
    fn invoke_pin_tap_without_a_hook_is_do_nothing() {
        let (update, _) = with_callback_info(|info| {
            invoke_pin_tap(
                &OptionMapPinTap::None,
                &info,
                MapLatLon {
                    lat_deg: 0.0,
                    lon_deg: 0.0,
                },
            )
        });
        assert_eq!(update, Update::DoNothing);
    }
    #[test]
    fn invoke_pin_tap_returns_the_users_update_verbatim() {
        let mut log = RefAny::new(HookLog::default());
        let hook = OptionMapPinTap::Some(MapPinTap {
            refany: log.clone(),
            callback: (record_pin as MapPinTapCallbackType).into(),
        });
        let (update, _) = with_callback_info(|info| {
            invoke_pin_tap(
                &hook,
                &info,
                MapLatLon {
                    lat_deg: f64::NAN,
                    lon_deg: -1.0e300,
                },
            )
        });
        assert_eq!(update, Update::RefreshDom);
        assert_eq!(hook_log(&mut log), (0, 1));
    }
    // ==================================================================
    // Pointer / scroll callbacks
    // ==================================================================
    #[test]
    fn pointer_down_without_a_cursor_is_a_no_op() {
        let mut dataset = RefAny::new(cache_at(0.0, 0.0, 4.0));
        let (update, _) = with_callback_info(|info| map_on_pointer_down(dataset.clone(), info));
        assert_eq!(update, Update::DoNothing);
        let cache = dataset.downcast_ref::<MapTileCache>().expect("cache");
        assert!(cache.drag_anchor.is_none());
        assert!(cache.press_origin.is_none());
    }
    #[test]
    fn pointer_down_records_both_anchors() {
        let mut dataset = RefAny::new(cache_at(0.0, 0.0, 4.0));
        let (update, _) = with_callback_info_at(cursor_at(120.0, 80.0), |info| {
            map_on_pointer_down(dataset.clone(), info)
        });
        assert_eq!(update, Update::DoNothing);
        let cache = dataset.downcast_ref::<MapTileCache>().expect("cache");
        let anchor = cache.drag_anchor.expect("drag anchor");
        let press = cache.press_origin.expect("press origin");
        assert_eq!((anchor.x, anchor.y), (120.0, 80.0));
        assert_eq!((press.x, press.y), (120.0, 80.0));
    }
    #[test]
    fn pointer_down_on_a_wrong_typed_dataset_is_a_no_op() {
        let dataset = RefAny::new(0u16);
        let (update, _) = with_callback_info_at(cursor_at(1.0, 1.0), |info| {
            map_on_pointer_down(dataset.clone(), info)
        });
        assert_eq!(update, Update::DoNothing);
    }
    #[test]
    fn pointer_move_without_an_anchor_does_not_pan() {
        let mut dataset = RefAny::new(cache_at(37.0, -122.0, 4.0));
        let (update, _) = with_callback_info_at(cursor_at(500.0, 500.0), |info| {
            map_on_pointer_move(dataset.clone(), info)
        });
        assert_eq!(update, Update::DoNothing);
        let cache = dataset.downcast_ref::<MapTileCache>().expect("cache");
        assert_eq!(cache.viewport.centre_lon_deg, -122.0);
        assert_eq!(cache.viewport.centre_lat_deg, 37.0);
    }
    #[test]
    fn pointer_move_pans_by_the_exact_mercator_delta_and_re_anchors() {
        let mut cache = cache_at(0.0, 0.0, 2.0);
        cache.drag_anchor = Some(LogicalPosition::new(100.0, 100.0));
        let mut dataset = RefAny::new(cache);
        let (_, changes) = with_callback_info_at(cursor_at(150.0, 100.0), |info| {
            map_on_pointer_move(dataset.clone(), info)
        });
        let cache = dataset.downcast_ref::<MapTileCache>().expect("cache");
        // world_px = 256 * 2^2 = 1024 → d_lon = -50 * 360 / 1024.
        close(cache.viewport.centre_lon_deg, -50.0 * 360.0 / 1024.0, 1e-9);
        close(cache.viewport.centre_lat_deg, 0.0, 1e-9);
        let anchor = cache.drag_anchor.expect("anchor must follow the cursor");
        assert_eq!((anchor.x, anchor.y), (150.0, 100.0));
        drop(cache);
        assert!(
            changes
                .iter()
                .any(|c| matches!(c, CallbackChange::UpdateAllVirtualViews)),
            "a pan must request a virtual-view re-render"
        );
    }
    #[test]
    fn pointer_move_ignores_sub_half_pixel_jitter() {
        let mut cache = cache_at(10.0, 20.0, 4.0);
        cache.drag_anchor = Some(LogicalPosition::new(100.0, 100.0));
        let mut dataset = RefAny::new(cache);
        let (update, _) = with_callback_info_at(cursor_at(100.4, 99.7), |info| {
            map_on_pointer_move(dataset.clone(), info)
        });
        assert_eq!(update, Update::DoNothing);
        let cache = dataset.downcast_ref::<MapTileCache>().expect("cache");
        assert_eq!(cache.viewport.centre_lon_deg, 20.0, "jitter must not pan");
        assert_eq!(cache.viewport.centre_lat_deg, 10.0);
    }
    #[test]
    fn pointer_move_with_a_nan_viewport_does_not_panic() {
        let mut cache = MapTileCache::new(layer_zoom(0, 19), view(f64::NAN, f64::NAN, f32::NAN));
        cache.drag_anchor = Some(LogicalPosition::new(0.0, 0.0));
        let mut dataset = RefAny::new(cache);
        let (update, _) = with_callback_info_at(cursor_at(400.0, 400.0), |info| {
            map_on_pointer_move(dataset.clone(), info)
        });
        assert_eq!(update, Update::DoNothing);
        let cache = dataset.downcast_ref::<MapTileCache>().expect("cache");
        assert!(cache.viewport.centre_lon_deg.is_nan());
    }
    #[test]
    fn pointer_move_fires_the_viewport_hook_once_per_pan() {
        let mut log = RefAny::new(HookLog::default());
        let mut cache = cache_at(0.0, 0.0, 4.0);
        cache.drag_anchor = Some(LogicalPosition::new(0.0, 0.0));
        cache.on_viewport_changed = OptionMapViewportChanged::Some(MapViewportChanged {
            refany: log.clone(),
            callback: (record_viewport as MapViewportChangedCallbackType).into(),
        });
        let dataset = RefAny::new(cache);
        let _ = with_callback_info_at(cursor_at(60.0, 60.0), |info| {
            map_on_pointer_move(dataset.clone(), info)
        });
        assert_eq!(hook_log(&mut log), (1, 0));
    }
    #[test]
    fn pointer_up_clears_every_gesture_anchor() {
        let mut cache = cache_at(0.0, 0.0, 4.0);
        cache.drag_anchor = Some(LogicalPosition::new(5.0, 5.0));
        cache.pinch_anchor = Some(120.0);
        cache.press_origin = Some(LogicalPosition::new(5.0, 5.0));
        let mut dataset = RefAny::new(cache);
        let (update, _) = with_callback_info_at(cursor_at(5.0, 5.0), |info| {
            map_on_pointer_up(dataset.clone(), info)
        });
        assert_eq!(update, Update::DoNothing);
        let cache = dataset.downcast_ref::<MapTileCache>().expect("cache");
        assert!(cache.drag_anchor.is_none());
        assert!(cache.pinch_anchor.is_none());
        assert!(cache.press_origin.is_none());
    }
    #[test]
    fn pointer_up_fires_pin_tap_for_a_tap_but_not_for_a_drag() {
        // A release within 6px of the press point is a tap.
        let mut log = RefAny::new(HookLog::default());
        let mut cache = cache_at(0.0, 0.0, 4.0);
        cache.press_origin = Some(LogicalPosition::new(10.0, 10.0));
        cache.on_pin_tap = OptionMapPinTap::Some(MapPinTap {
            refany: log.clone(),
            callback: (record_pin as MapPinTapCallbackType).into(),
        });
        let dataset = RefAny::new(cache);
        let _ = with_callback_info_at(cursor_at(12.0, 12.0), |info| {
            map_on_pointer_up(dataset.clone(), info)
        });
        assert_eq!(hook_log(&mut log), (0, 1), "a 2px release is a tap");
        // A release 90px away is a drag, not a tap.
        let mut log = RefAny::new(HookLog::default());
        let mut cache = cache_at(0.0, 0.0, 4.0);
        cache.press_origin = Some(LogicalPosition::new(10.0, 10.0));
        cache.on_pin_tap = OptionMapPinTap::Some(MapPinTap {
            refany: log.clone(),
            callback: (record_pin as MapPinTapCallbackType).into(),
        });
        let dataset = RefAny::new(cache);
        let _ = with_callback_info_at(cursor_at(100.0, 100.0), |info| {
            map_on_pointer_up(dataset.clone(), info)
        });
        assert_eq!(hook_log(&mut log), (0, 0), "a 90px release is a drag");
    }
    #[test]
    fn pointer_up_without_a_press_origin_never_taps() {
        let mut log = RefAny::new(HookLog::default());
        let mut cache = cache_at(0.0, 0.0, 4.0);
        cache.on_pin_tap = OptionMapPinTap::Some(MapPinTap {
            refany: log.clone(),
            callback: (record_pin as MapPinTapCallbackType).into(),
        });
        let dataset = RefAny::new(cache);
        let _ = with_callback_info_at(cursor_at(10.0, 10.0), |info| {
            map_on_pointer_up(dataset.clone(), info)
        });
        assert_eq!(hook_log(&mut log), (0, 0));
    }
    #[test]
    fn pointer_up_on_a_wrong_typed_dataset_is_a_no_op() {
        // A `MapViewport` is the most plausible mix-up: same widget, wrong payload.
        let dataset = RefAny::new(MapViewport::default());
        let (update, _) = with_callback_info_at(cursor_at(1.0, 1.0), |info| {
            map_on_pointer_up(dataset.clone(), info)
        });
        assert_eq!(update, Update::DoNothing);
    }
    #[test]
    fn scroll_without_a_wheel_delta_is_a_no_op() {
        // The harness has no hit node, so `get_scroll_delta` yields 0 - the
        // handler must bail before touching the viewport.
        let mut dataset = RefAny::new(cache_at(0.0, 0.0, 4.0));
        let (update, changes) = with_callback_info(|info| map_on_scroll(dataset.clone(), info));
        assert_eq!(update, Update::DoNothing);
        assert!(changes.is_empty(), "a zero-delta scroll must queue nothing");
        let cache = dataset.downcast_ref::<MapTileCache>().expect("cache");
        assert_eq!(cache.viewport.zoom, 4.0);
        assert!(cache.tiles.is_empty());
    }
    #[test]
    fn scroll_on_a_wrong_typed_dataset_is_a_no_op() {
        let dataset = RefAny::new(0u8);
        let (update, _) = with_callback_info(|info| map_on_scroll(dataset.clone(), info));
        assert_eq!(update, Update::DoNothing);
    }
    // ==================================================================
    // Fetch spawning + writeback
    // ==================================================================
    #[test]
    fn spawn_pending_tile_fetches_is_a_no_op_without_a_worker() {
        let mut cache = cache_at(0.0, 0.0, 4.0);
        for x in 0..4u32 {
            cache.insert_tile(MapTileId { z: 4, x, y: 8 }, TileEntry::Pending);
        }
        let mut dataset = RefAny::new(cache);
        let (_, changes) = with_callback_info(|info| {
            let mut info = info;
            spawn_pending_tile_fetches(&mut dataset.clone(), &mut info);
        });
        assert!(changes.is_empty(), "no worker → no threads queued");
        let cache = dataset.downcast_ref::<MapTileCache>().expect("cache");
        assert!(
            cache
                .tiles
                .values()
                .all(|e| matches!(e, TileEntry::Pending)),
            "tiles must stay Pending so the placeholder grid renders"
        );
    }
    #[test]
    fn spawn_pending_tile_fetches_caps_the_burst_at_sixteen() {
        let mut cache = cache_at(0.0, 0.0, 4.0);
        cache.fetch_callback = Some(ThreadCallback::new(noop_worker));
        // Queue at the look the spawn pass will resolve. The harness's
        // `SystemStyle::default()` is a light theme, and the spawn dequeues
        // `Pending` jobs left over from a look the widget has moved on from —
        // this test is about the burst CAP, not about that re-keying.
        cache.cascade_scheme = Some(MapColorScheme::Light);
        for x in 0..20u32 {
            cache.insert_tile(MapTileId { z: 4, x, y: 8 }, TileEntry::Pending);
        }
        let mut dataset = RefAny::new(cache);
        // `(fetching, pending)` counts, as a plain fn so the two call sites
        // don't share one inferred closure borrow.
        fn count_states(ds: &mut RefAny) -> (usize, usize) {
            let cache = ds.downcast_ref::<MapTileCache>().expect("cache");
            let fetching = cache
                .tiles
                .values()
                .filter(|e| matches!(e, TileEntry::Fetching))
                .count();
            let pending = cache
                .tiles
                .values()
                .filter(|e| matches!(e, TileEntry::Pending))
                .count();
            (fetching, pending)
        }
        let (_, changes) = with_callback_info(|info| {
            let mut info = info;
            spawn_pending_tile_fetches(&mut dataset.clone(), &mut info);
        });
        assert_eq!(
            count_states(&mut dataset),
            (16, 4),
            "one call spawns at most 16"
        );
        assert_eq!(
            changes
                .iter()
                .filter(|c| matches!(c, CallbackChange::AddThread { .. }))
                .count(),
            16
        );
        // The second call drains the remainder - the cap bounds a burst, it
        // does not drop work.
        let _ = with_callback_info(|info| {
            let mut info = info;
            spawn_pending_tile_fetches(&mut dataset.clone(), &mut info);
        });
        assert_eq!(count_states(&mut dataset), (20, 0));
    }
    #[test]
    fn spawn_pending_tile_fetches_on_a_wrong_typed_dataset_is_a_no_op() {
        let mut dataset = RefAny::new(1234u32);
        let (_, changes) = with_callback_info(|info| {
            let mut info = info;
            spawn_pending_tile_fetches(&mut dataset, &mut info);
        });
        assert!(changes.is_empty());
    }
    #[test]
    fn tile_writeback_marks_ready_on_an_empty_error_and_failed_otherwise() {
        let tile = MapTileId { z: 4, x: 1, y: 2 };
        let mut dataset = RefAny::new(cache_at(0.0, 0.0, 4.0));
        let ok = RefAny::new(TileReadyMsg {
            look: MapTheme::System.look(MapColorScheme::Light),
            tile,
            svg: AzString::from("<svg/>"),
            error: AzString::from(""),
            bytes: azul_css::U8Vec::from_vec(Vec::new()),
        });
        let (update, changes) =
            with_callback_info(|info| map_tile_writeback(dataset.clone(), ok.clone(), info));
        assert_eq!(update, Update::DoNothing);
        assert!(changes
            .iter()
            .any(|c| matches!(c, CallbackChange::UpdateAllVirtualViews)));
        {
            let cache = dataset.downcast_ref::<MapTileCache>().expect("cache");
            assert!(matches!(
                cache.tile_entry(tile),
                Some(TileEntry::Ready { .. })
            ));
        }
        let failed = RefAny::new(TileReadyMsg {
            look: MapTheme::System.look(MapColorScheme::Light),
            tile,
            svg: AzString::from(""),
            error: AzString::from("404"),
            bytes: azul_css::U8Vec::from_vec(Vec::new()),
        });
        let (update, _) =
            with_callback_info(|info| map_tile_writeback(dataset.clone(), failed.clone(), info));
        assert_eq!(update, Update::DoNothing);
        let cache = dataset.downcast_ref::<MapTileCache>().expect("cache");
        assert!(matches!(
            cache.tile_entry(tile),
            Some(TileEntry::Failed { .. })
        ));
    }
    #[test]
    fn tile_writeback_accepts_a_huge_payload_and_an_out_of_range_id() {
        let tile = MapTileId {
            z: 31,
            x: u32::MAX,
            y: u32::MAX,
        };
        let mut dataset = RefAny::new(cache_at(0.0, 0.0, 4.0));
        let msg = RefAny::new(TileReadyMsg {
            look: MapTheme::System.look(MapColorScheme::Light),
            tile,
            svg: AzString::from("<svg/>".repeat(50_000)),
            error: AzString::from(""),
            bytes: azul_css::U8Vec::from_vec(Vec::new()),
        });
        let (update, _) =
            with_callback_info(|info| map_tile_writeback(dataset.clone(), msg.clone(), info));
        assert_eq!(update, Update::DoNothing);
        let cache = dataset.downcast_ref::<MapTileCache>().expect("cache");
        assert!(cache.tile_entry(tile).is_some());
    }
    #[test]
    fn tile_writeback_with_a_wrong_typed_message_is_a_no_op() {
        let mut dataset = RefAny::new(cache_at(0.0, 0.0, 4.0));
        let (update, changes) =
            with_callback_info(|info| map_tile_writeback(dataset.clone(), RefAny::new(0u32), info));
        assert_eq!(update, Update::DoNothing);
        assert!(
            changes.is_empty(),
            "a bogus message must not force a re-render"
        );
        let cache = dataset.downcast_ref::<MapTileCache>().expect("cache");
        assert!(cache.tiles.is_empty());
    }
    #[test]
    fn tile_writeback_with_a_wrong_typed_cache_is_a_no_op() {
        let msg = RefAny::new(TileReadyMsg {
            look: MapTheme::System.look(MapColorScheme::Light),
            tile: MapTileId { z: 1, x: 0, y: 0 },
            svg: AzString::from("<svg/>"),
            error: AzString::from(""),
            bytes: azul_css::U8Vec::from_vec(Vec::new()),
        });
        let (update, changes) =
            with_callback_info(|info| map_tile_writeback(RefAny::new(9u64), msg.clone(), info));
        assert_eq!(update, Update::DoNothing);
        assert!(changes.is_empty());
    }
    // ── The two axes: cartography (the app's) vs light/dark (the cascade's) ──
    #[test]
    fn a_look_is_a_theme_taken_to_a_colour_scheme_and_every_theme_has_both() {
        // The property that makes (theme, scheme) a real product rather than
        // ten ad-hoc enum variants: every cartography renders in either
        // scheme, and the look you get back says which scheme it is.
        for theme in [
            MapTheme::Positron,
            MapTheme::Bright,
            MapTheme::Liberty,
            MapTheme::Google,
            MapTheme::Apple,
        ] {
            for scheme in [MapColorScheme::Light, MapColorScheme::Dark] {
                let look = theme.look(scheme);
                assert_eq!(
                    look.is_dark(),
                    matches!(scheme, MapColorScheme::Dark),
                    "{theme:?} in {scheme:?} must render as {scheme:?}"
                );
                assert!(
                    !look.stylesheet().as_str().is_empty(),
                    "{theme:?} in {scheme:?} must have a sheet to decode with"
                );
            }
        }
    }
    #[test]
    fn no_theme_pins_a_scheme_the_cascade_always_decides() {
        // There is no "light Apple" and "dark Apple": Apple is one theme and
        // the window's scheme picks the half. `System` only adds the platform
        // family on top.
        assert_eq!(
            MapTheme::System.look(MapColorScheme::Dark),
            MapTheme::platform_family().look(MapColorScheme::Dark),
        );
        assert_eq!(
            MapTheme::Apple.look(MapColorScheme::Dark),
            MapLook::AppleDark
        );
        assert_eq!(
            MapTheme::Apple.look(MapColorScheme::Light),
            MapLook::AppleLight
        );
        assert_ne!(
            MapTheme::Google.look(MapColorScheme::Dark),
            MapTheme::Google.look(MapColorScheme::Light)
        );
        assert_eq!(MapTheme::Custom.look(MapColorScheme::Dark), MapLook::Custom);
    }
    #[test]
    fn a_colour_scheme_flip_re_keys_the_cache_and_discards_no_decoded_tile() {
        // THE REGRESSION. This used to push every decoded tile back to
        // `Pending`, so a colour scheme that settled one frame late cost a
        // second download of the entire viewport.
        let tile = MapTileId { z: 2, x: 1, y: 1 };
        let mut cache = cache_at(0.0, 0.0, 2.0);
        let light = MapTheme::Apple.look(MapColorScheme::Light);
        let dark = MapTheme::Apple.look(MapColorScheme::Dark);
        cache.mark_tile_ready(
            TileStyleKey { tile, look: light },
            AzString::from("<svg id='l'/>"),
        );
        assert!(cache.set_active_look(dark), "the look moved");
        assert!(
            matches!(
                cache.tiles.get(&TileStyleKey { tile, look: light }),
                Some(TileEntry::Ready { .. })
            ),
            "the light tile is still correct data for the light look and must survive the flip — \
             re-key, never invalidate"
        );
        assert_eq!(cache.active_look, dark);
    }
    #[test]
    fn an_unstyled_look_falls_back_to_a_decoded_one_instead_of_a_grey_placeholder() {
        let tile = MapTileId { z: 2, x: 1, y: 1 };
        let mut cache = cache_at(0.0, 0.0, 2.0);
        let light = MapTheme::Apple.look(MapColorScheme::Light);
        let dark = MapTheme::Apple.look(MapColorScheme::Dark);
        cache.mark_tile_ready(
            TileStyleKey { tile, look: light },
            AzString::from("<svg id='l'/>"),
        );
        let (got, svg) = cache
            .best_available_svg(tile, dark)
            .expect("the light tile must stand in while the dark one styles");
        assert_eq!(got, light, "real cartography beats a grey box");
        assert_eq!(svg.as_str(), "<svg id='l'/>");
        // An exact hit always wins over the stand-in.
        cache.mark_tile_ready(
            TileStyleKey { tile, look: dark },
            AzString::from("<svg id='d'/>"),
        );
        let (got, svg) = cache.best_available_svg(tile, dark).expect("exact hit");
        assert_eq!(got, dark);
        assert_eq!(svg.as_str(), "<svg id='d'/>");
    }
    #[test]
    fn the_writeback_files_a_tile_under_its_own_look_and_caches_the_payload() {
        // The old writeback compared the arriving theme against the cache's
        // single `decoded_theme` and, on a mismatch, threw the decoded tile
        // away and re-queued it — the second fetch of every tile. And it logged
        // `ok=true` on the way past, before the check that discarded it.
        let tile = MapTileId { z: 4, x: 1, y: 2 };
        let mut dataset = RefAny::new(cache_at(0.0, 0.0, 4.0));
        let arrived_for = MapTheme::Apple.look(MapColorScheme::Dark);
        {
            let mut c = dataset.downcast_mut::<MapTileCache>().expect("cache");
            c.set_active_look(MapTheme::Apple.look(MapColorScheme::Light));
        }
        let msg = RefAny::new(TileReadyMsg {
            tile,
            svg: AzString::from("<svg/>"),
            error: AzString::from(""),
            look: arrived_for,
            bytes: azul_css::U8Vec::from_vec(Vec::from([1u8, 2, 3])),
        });
        let _ = with_callback_info(|info| map_tile_writeback(dataset.clone(), msg.clone(), info));
        let cache = dataset.downcast_ref::<MapTileCache>().expect("cache");
        assert!(
            matches!(
                cache.tiles.get(&TileStyleKey {
                    tile,
                    look: arrived_for
                }),
                Some(TileEntry::Ready { .. })
            ),
            "a result for a look the widget has since left is still correct data for that look: \
             it must be filed, not re-queued"
        );
        assert_eq!(
            cache.tile_bytes.get(&tile).map(|b| b.as_ref().to_vec()),
            Some(Vec::from([1u8, 2, 3])),
            "the payload must be cached so no look ever re-downloads this tile"
        );
    }
    #[test]
    fn the_byte_cache_is_keyed_by_coordinates_so_every_look_shares_one_download() {
        let tile = MapTileId { z: 2, x: 1, y: 1 };
        let mut cache = cache_at(0.0, 0.0, 2.0);
        cache
            .tile_bytes
            .insert(tile, azul_css::U8Vec::from_vec(Vec::from([9u8])));
        for scheme in [MapColorScheme::Light, MapColorScheme::Dark] {
            cache.mark_tile_ready(
                TileStyleKey {
                    tile,
                    look: MapTheme::Google.look(scheme),
                },
                AzString::from("<svg/>"),
            );
        }
        assert_eq!(cache.tiles.len(), 2, "two looks are two styled entries");
        assert_eq!(
            cache.tile_bytes.len(),
            1,
            "but the geometry behind them is downloaded exactly once"
        );
    }
    #[test]
    fn after_mount_installs_the_sweep_timer_and_asks_for_a_re_render() {
        let dataset = RefAny::new(cache_at(0.0, 0.0, 4.0));
        let (update, changes) =
            with_callback_info(|info| map_on_after_mount(dataset.clone(), info));
        assert_eq!(update, Update::DoNothing);
        assert_eq!(
            changes
                .iter()
                .filter(|c| matches!(c, CallbackChange::AddTimer { .. }))
                .count(),
            1,
            "exactly one sweep timer per mount"
        );
        assert!(changes
            .iter()
            .any(|c| matches!(c, CallbackChange::UpdateAllVirtualViews)));
    }
    #[test]
    fn after_mount_on_a_wrong_typed_dataset_still_installs_the_timer() {
        // The timer is unconditional; only the fetch spawn depends on the cache.
        let dataset = RefAny::new(0u8);
        let (update, changes) =
            with_callback_info(|info| map_on_after_mount(dataset.clone(), info));
        assert_eq!(update, Update::DoNothing);
        assert!(changes
            .iter()
            .any(|c| matches!(c, CallbackChange::AddTimer { .. })));
    }
    // ==================================================================
    // map_widget_render  (VirtualView callback)
    // ==================================================================
    #[test]
    fn render_with_non_finite_or_empty_bounds_emits_no_dom() {
        let dataset = RefAny::new(cache_at(0.0, 0.0, 3.0));
        for (w, h) in [
            (0.0_f32, 0.0_f32),
            (0.0, 600.0),
            (800.0, 0.0),
            (-800.0, -600.0),
            (f32::NAN, 600.0),
            (800.0, f32::NAN),
            (f32::INFINITY, 600.0),
            (800.0, f32::NEG_INFINITY),
        ] {
            let ret = with_virtual_view_info(w, h, |info| map_widget_render(dataset.clone(), info));
            assert!(
                rendered_child_count(&ret).is_none(),
                "bounds {w}x{h} must render nothing until layout settles"
            );
        }
    }
    #[test]
    fn render_with_a_wrong_typed_dataset_emits_no_dom() {
        let dataset = RefAny::new(0u32);
        let ret = with_virtual_view_info(800.0, 600.0, |info| {
            map_widget_render(dataset.clone(), info)
        });
        assert!(rendered_child_count(&ret).is_none());
    }
    #[test]
    fn render_marks_every_visible_tile_pending_and_emits_one_div_each() {
        let mut dataset = RefAny::new(cache_at(0.0, 0.0, 2.0));
        let expected = map_visible_tiles(
            &view(0.0, 0.0, 2.0),
            LogicalSize::new(800.0, 600.0),
            &layer_zoom(0, 19),
        );
        let ret = with_virtual_view_info(800.0, 600.0, |info| {
            map_widget_render(dataset.clone(), info)
        });
        assert_eq!(
            rendered_child_count(&ret),
            Some(expected.len()),
            "one div per visible tile"
        );
        let cache = dataset.downcast_ref::<MapTileCache>().expect("cache");
        for tile in &expected {
            assert!(
                matches!(cache.tile_entry(*tile), Some(TileEntry::Pending)),
                "tile {tile:?} must be queued by the render pass"
            );
        }
    }
    #[test]
    fn render_reports_the_bounds_back_as_the_scroll_size() {
        let dataset = RefAny::new(cache_at(0.0, 0.0, 2.0));
        let ret = with_virtual_view_info(640.0, 480.0, |info| {
            map_widget_render(dataset.clone(), info)
        });
        assert_eq!(ret.materialized.size.width, 640.0);
        assert_eq!(ret.materialized.size.height, 480.0);
        assert_eq!(ret.virtual_rect.size.width, 640.0);
        assert_eq!(ret.virtual_rect.size.height, 480.0);
        assert_eq!(
            (ret.materialized.origin.x, ret.materialized.origin.y),
            (0.0, 0.0)
        );
        assert_eq!(
            (ret.virtual_rect.origin.x, ret.virtual_rect.origin.y),
            (0.0, 0.0)
        );
    }
    #[test]
    fn render_falls_back_to_a_glyph_when_a_ready_tile_holds_garbage() {
        // A worker can hand back anything; an unparseable payload must degrade
        // to the placeholder text child, not panic the render pass.
        let mut cache = cache_at(0.0, 0.0, 2.0);
        for tile in map_visible_tiles(
            &view(0.0, 0.0, 2.0),
            LogicalSize::new(512.0, 512.0),
            &layer_zoom(0, 19),
        ) {
            cache.insert_tile(
                tile,
                TileEntry::Ready {
                    svg: AzString::from("not xml at all <<<"),
                },
            );
        }
        let dataset = RefAny::new(cache);
        let ret = with_virtual_view_info(512.0, 512.0, |info| {
            map_widget_render(dataset.clone(), info)
        });
        assert!(
            rendered_child_count(&ret).is_some_and(|n| n > 0),
            "garbage tiles still render their placeholder"
        );
    }
    #[test]
    fn render_handles_mixed_tile_states_including_failures() {
        let mut cache = cache_at(0.0, 0.0, 2.0);
        let tiles = map_visible_tiles(
            &view(0.0, 0.0, 2.0),
            LogicalSize::new(512.0, 512.0),
            &layer_zoom(0, 19),
        );
        for (i, tile) in tiles.iter().enumerate() {
            match i % 4 {
                0 => cache.insert_tile(*tile, TileEntry::Pending),
                1 => cache.insert_tile(*tile, TileEntry::Fetching),
                2 => cache.insert_tile(
                    *tile,
                    TileEntry::Failed {
                        error: AzString::from("\u{1F600} failed"),
                    },
                ),
                _ => cache.insert_tile(
                    *tile,
                    TileEntry::Ready {
                        svg: AzString::from(""),
                    },
                ),
            }
        }
        let dataset = RefAny::new(cache);
        let ret = with_virtual_view_info(512.0, 512.0, |info| {
            map_widget_render(dataset.clone(), info)
        });
        assert_eq!(rendered_child_count(&ret), Some(tiles.len()));
    }
    #[test]
    fn render_clamps_an_out_of_band_zoom_and_stays_bounded() {
        // A single-zoom layer with a viewport far outside it: the grid must
        // collapse onto the one supported zoom, not enumerate a huge range.
        //
        // NOTE: `min_zoom > max_zoom` is deliberately NOT exercised here - the
        // `i32::clamp(min, max)` in `map_widget_render` panics on an inverted
        // band (see the report accompanying these tests).
        for zoom in [0.0_f32, 1.0, 3.0, 25.0, f32::INFINITY] {
            let cache = MapTileCache::new(layer_zoom(3, 3), view(0.0, 0.0, zoom));
            let dataset = RefAny::new(cache);
            let ret = with_virtual_view_info(800.0, 600.0, |info| {
                map_widget_render(dataset.clone(), info)
            });
            let n = rendered_child_count(&ret).expect("a finite box must render");
            assert!(n > 0 && n < 4096, "zoom {zoom} produced {n} tiles");
        }
    }
    #[test]
    fn render_is_stable_across_repeated_invocations() {
        let dataset = RefAny::new(cache_at(48.1372, 11.5756, 5.0));
        let first = with_virtual_view_info(800.0, 600.0, |info| {
            map_widget_render(dataset.clone(), info)
        });
        let second = with_virtual_view_info(800.0, 600.0, |info| {
            map_widget_render(dataset.clone(), info)
        });
        assert_eq!(rendered_child_count(&first), rendered_child_count(&second));
    }
    #[test]
    fn wanted_tiles_contains_exactly_what_it_lists_across_the_antimeridian() {
        // z3 has 8 columns; x -2..=1 wraps to columns 6, 7, 0, 1.
        let wanted = WantedTiles {
            z: 3,
            x_min: -2,
            x_max: 1,
            y_min: 2,
            y_max: 4,
        };
        let listed: alloc::collections::BTreeSet<MapTileId> = wanted.tiles().collect();
        assert_eq!(listed.len(), 12);
        for x in 0..8 {
            for y in 0..8 {
                let tile = MapTileId { z: 3, x, y };
                assert_eq!(wanted.contains(tile), listed.contains(&tile), "{tile:?}");
            }
        }
        assert!(
            !wanted.contains(MapTileId { z: 4, x: 0, y: 2 }),
            "another zoom"
        );
    }
    #[test]
    fn a_range_wider_than_the_world_wants_every_column() {
        let wanted = WantedTiles {
            z: 1,
            x_min: -3,
            x_max: 3,
            y_min: 0,
            y_max: 1,
        };
        assert!(wanted.contains(MapTileId { z: 1, x: 0, y: 0 }));
        assert!(wanted.contains(MapTileId { z: 1, x: 1, y: 1 }));
    }
    #[test]
    fn a_spawn_pass_forgets_queued_tiles_the_view_moved_away_from() {
        let mut cache = cache_at(0.0, 0.0, 4.0);
        let look = cache.current_look();
        let before = WantedTiles {
            z: 4,
            x_min: 0,
            x_max: 3,
            y_min: 0,
            y_max: 3,
        };
        cache.want_tiles(before, look);
        // One of the old tiles is already downloading, one is done.
        let running = MapTileId { z: 4, x: 0, y: 0 };
        let done = MapTileId { z: 4, x: 1, y: 0 };
        cache.insert_tile(running, TileEntry::Fetching);
        cache.insert_tile(
            done,
            TileEntry::Ready {
                svg: AzString::from("<svg/>"),
            },
        );
        // The view moves to a range that shares no tile with the old one.
        let after = WantedTiles {
            z: 4,
            x_min: 8,
            x_max: 9,
            y_min: 8,
            y_max: 9,
        };
        cache.want_tiles(after, look);
        cache.drop_unwanted_pending();
        for (key, entry) in &cache.tiles {
            if matches!(entry, TileEntry::Pending) {
                assert!(
                    after.contains(key.tile),
                    "{:?} should have been forgotten",
                    key.tile
                );
            }
        }
        assert_eq!(cache.pending_tiles_nearest_first().len(), 4);
        assert!(
            matches!(cache.tile_entry(running), Some(TileEntry::Fetching)),
            "a running download is left alone"
        );
        assert!(matches!(
            cache.tile_entry(done),
            Some(TileEntry::Ready { .. })
        ));
    }
    #[test]
    fn nothing_is_forgotten_before_the_first_render_says_what_it_wants() {
        let mut cache = cache_at(0.0, 0.0, 4.0);
        cache.insert_tile(MapTileId { z: 4, x: 3, y: 3 }, TileEntry::Pending);
        cache.drop_unwanted_pending();
        assert_eq!(cache.pending_tiles_nearest_first().len(), 1);
    }
}