siplot 0.4.1

silx-style scientific plotting for egui, rendered with wgpu
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
//! Cursor position readout bar ported from silx
//! `gui/plot/tools/PositionInfo.py`.
//!
//! [`PositionInfo`] is a horizontal label bar showing the current cursor data
//! coordinates through a list of named converter functions
//! `(name, fn(x, y) -> String)`. It mirrors silx `PositionInfo`
//! (PositionInfo.py:64-318):
//!
//! - The default converters display `X` and `Y` (PositionInfo.py:117).
//! - A polar converter pair (`Radius`, `Angle`) is provided as the silx
//!   documentation example (PositionInfo.py:92-94).
//! - When no cursor position is available the value fields show `"------"`
//!   (PositionInfo.py:131).
//! - Numeric converter results are formatted with `%.7g` (silx
//!   `valueToString`, PositionInfo.py:315) via [`format_value`].
//!
//! The pure snapping kernel (PositionInfo.py:236-292) is provided by
//! [`snap_to_nearest`] / [`SNAP_THRESHOLD_DIST`]: given the cursor and
//! candidate data points already projected to pixel space, it returns the
//! nearest point within the snap radius. Candidate *selection*
//! (`SNAPPING_CURVE`/`SNAPPING_SCATTER`/`SNAPPING_ACTIVE_ONLY`/…) is
//! [`snapping_candidates`]. The two are wired against a live plot by
//! [`PlotWidget::snap_cursor`](crate::widget::high_level::PlotWidget::snap_cursor),
//! which builds the [`SnapItem`] list from the plot's items, projects each
//! candidate curve's vertices through the cached display transform, and returns
//! the [`Snap`]; the host then feeds `snap.data` to [`PositionInfo::ui_snapped`],
//! which reddens the labels in the no-snap state (silx :200).

/// A converter mapping cursor data coordinates `(x, y)` to a display string.
///
/// This is the boxed `Fn(f64, f64) -> String` half of the silx
/// `(name, function)` converter pair (PositionInfo.py:127); the convenience
/// constructors wrap numeric silx converters with [`format_value`] (`%.7g`).
pub type Converter = Box<dyn Fn(f64, f64) -> String>;

/// A horizontal cursor-coordinate readout bar.
///
/// Holds an ordered list of `(label, converter)` pairs. Each converter maps
/// the cursor data coordinates `(x, y)` to a display string; numeric silx
/// converters are wrapped with [`format_value`] (`%.7g`) by the convenience
/// constructors.
pub struct PositionInfo {
    converters: Vec<(String, Converter)>,
}

impl Default for PositionInfo {
    /// The silx default: `X` and `Y` columns (PositionInfo.py:117).
    fn default() -> Self {
        Self::with_xy()
    }
}

impl PositionInfo {
    /// Create a readout bar from an explicit list of converters.
    pub fn new(converters: Vec<(String, Converter)>) -> Self {
        Self { converters }
    }

    /// The silx default converters: `X -> x`, `Y -> y` (PositionInfo.py:117),
    /// each formatted with `%.7g`.
    pub fn with_xy() -> Self {
        Self::new(vec![
            ("X".to_owned(), Box::new(|x: f64, _y: f64| format_value(x))),
            ("Y".to_owned(), Box::new(|_x: f64, y: f64| format_value(y))),
        ])
    }

    /// The silx documentation polar example (PositionInfo.py:92-94):
    /// `Radius -> sqrt(x*x + y*y)`, `Angle -> degrees(atan2(y, x))`, each
    /// formatted with `%.7g`.
    pub fn polar() -> Self {
        Self::new(vec![
            (
                "Radius".to_owned(),
                Box::new(|x: f64, y: f64| format_value(x.hypot(y))),
            ),
            (
                "Angle".to_owned(),
                Box::new(|x: f64, y: f64| format_value(y.atan2(x).to_degrees())),
            ),
        ])
    }

    /// Append a converter `(label, fn)` to the bar.
    pub fn push(&mut self, label: impl Into<String>, converter: Converter) {
        self.converters.push((label.into(), converter));
    }

    /// Convenience: append a numeric converter, formatting its result with
    /// `%.7g` (silx `valueToString`, PositionInfo.py:315).
    pub fn push_numeric(
        &mut self,
        label: impl Into<String>,
        converter: impl Fn(f64, f64) -> f64 + 'static,
    ) {
        self.converters.push((
            label.into(),
            Box::new(move |x, y| format_value(converter(x, y))),
        ));
    }

    /// The number of converter columns.
    pub fn len(&self) -> usize {
        self.converters.len()
    }

    /// Whether the bar has no converters.
    pub fn is_empty(&self) -> bool {
        self.converters.is_empty()
    }

    /// Compute the display string for each converter at `(x, y)`.
    ///
    /// Returns one string per column. This is the pure core of [`Self::ui`];
    /// `None` cursor yields the silx empty placeholder `"------"`
    /// (PositionInfo.py:131) for every column.
    pub fn values(&self, cursor: Option<[f64; 2]>) -> Vec<String> {
        match cursor {
            None => vec![EMPTY_PLACEHOLDER.to_owned(); self.converters.len()],
            Some([x, y]) => self
                .converters
                .iter()
                .map(|(_label, func)| func(x, y))
                .collect(),
        }
    }

    /// Render the readout bar: a horizontal row of `<label>: <value>` pairs.
    ///
    /// `cursor` is the current cursor position in data coordinates, or `None`
    /// when the cursor is outside the plot area (silx shows `"------"`).
    pub fn ui(&self, ui: &mut egui::Ui, cursor: Option<[f64; 2]>) {
        ui.horizontal(|ui| {
            let values = self.values(cursor);
            for ((label, _func), value) in self.converters.iter().zip(values) {
                ui.strong(format!("{label}:"));
                ui.label(value);
                ui.add_space(8.0);
            }
        });
    }

    /// Render the readout bar, reddening the value labels when `snapped` is
    /// `false` (silx PositionInfo's "not snapped" red style, PositionInfo.py:200;
    /// the normal style is restored when the cursor snaps to a point, :288).
    ///
    /// Use with [`PlotWidget::snap_cursor`](crate::widget::high_level::PlotWidget::snap_cursor)
    /// while a [`SnappingMode`] is engaged: pass the snapped data coordinate as
    /// `cursor` (or the raw cursor when nothing snapped) and `snapped =
    /// snap.is_some()`. When snapping is *disabled*, call the plain [`Self::ui`]
    /// instead — silx only reddens once snapping is engaged but unmatched.
    pub fn ui_snapped(&self, ui: &mut egui::Ui, cursor: Option<[f64; 2]>, snapped: bool) {
        ui.horizontal(|ui| {
            let values = self.values(cursor);
            for ((label, _func), value) in self.converters.iter().zip(values) {
                ui.strong(format!("{label}:"));
                if snapped {
                    ui.label(value);
                } else {
                    ui.colored_label(egui::Color32::RED, value);
                }
                ui.add_space(8.0);
            }
        });
    }
}

/// The silx empty-field placeholder shown when no cursor is available
/// (PositionInfo.py:131).
const EMPTY_PLACEHOLDER: &str = "------";

/// Format a numeric value like silx `valueToString` (`"%.7g"`,
/// PositionInfo.py:315): up to 7 significant digits, trailing zeros trimmed,
/// switching to exponential notation for very large/small magnitudes.
///
/// Non-finite values format as `"nan"` / `"inf"` like C `%g` would; we keep
/// Rust's spelling for those edge cases since silx delegates to Python's
/// `%g` which also prints `nan` / `inf`.
pub fn format_value(value: f64) -> String {
    if value.is_nan() {
        return "nan".to_owned();
    }
    if value.is_infinite() {
        return if value < 0.0 {
            "-inf".to_owned()
        } else {
            "inf".to_owned()
        };
    }
    crate::widget::stats_widget::format_significant(value, 7)
}

/// Snap radius in logical pixels (silx `PositionInfo.SNAP_THRESHOLD_DIST`,
/// PositionInfo.py:107).
///
/// silx scales this by the device-pixel ratio before squaring
/// (PositionInfo.py:237); a caller working in physical pixels should pass
/// `SNAP_THRESHOLD_DIST * device_pixel_ratio` as the threshold to
/// [`snap_to_nearest`].
pub const SNAP_THRESHOLD_DIST: f64 = 5.0;

/// A successful snap: the nearest candidate within the snap radius.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Snap {
    /// Index of the snapped point in the candidate slice passed to
    /// [`snap_to_nearest`].
    pub index: usize,
    /// The snapped data coordinate (the candidate's data position), to feed to
    /// [`PositionInfo::values`] in place of the raw cursor.
    pub data: [f64; 2],
}

/// Snap a cursor to the nearest candidate point, porting the inner loop of
/// silx `PositionInfo._updateStatusBar` (PositionInfo.py:236-292).
///
/// `cursor_px` and every `candidates[i].0` are positions in the same pixel
/// space; `candidates[i].1` is that point's data coordinate. Returns the
/// candidate with the smallest squared pixel distance to the cursor, provided
/// that distance is `<= threshold_px²` (silx `closestSqDistInPixels <=
/// sqDistInPixels`, :286). Candidates whose squared distance is not finite are
/// skipped (silx `numpy.isfinite` guard, :281).
///
/// silx walks items one at a time, shrinking the live threshold to the best
/// distance found so far (:292); a single pass that keeps the global nearest
/// within the original threshold is equivalent, since a point only wins when it
/// is both within `threshold_px²` and no farther than the current best. Ties
/// resolve to the later candidate, matching silx's `<=` update order.
///
/// Returns `None` when nothing is within the radius — silx then leaves the
/// readout at the raw cursor and styles the labels red (:200); a `Some` result
/// is the "snapped" state that clears the style (:288).
pub fn snap_to_nearest(
    cursor_px: [f64; 2],
    candidates: &[([f64; 2], [f64; 2])],
    threshold_px: f64,
) -> Option<Snap> {
    let mut best: Option<Snap> = None;
    let mut best_sq = threshold_px * threshold_px;
    for (index, &(px, data)) in candidates.iter().enumerate() {
        let dx = px[0] - cursor_px[0];
        let dy = px[1] - cursor_px[1];
        let sq = dx * dx + dy * dy;
        if !sq.is_finite() {
            continue;
        }
        if sq <= best_sq {
            best = Some(Snap { index, data });
            best_sq = sq;
        }
    }
    best
}

/// The snapping mode bitfield — silx `PositionInfo` `SNAPPING_*` flags
/// (PositionInfo.py:322-337), combinable with `|`.
///
/// A data-kind flag ([`Self::CURVE`] and/or [`Self::SCATTER`]) selects which
/// item kinds are snap candidates; the modifiers restrict that set:
/// [`Self::ACTIVE_ONLY`] to the active item, [`Self::SYMBOLS_ONLY`] to items
/// showing a symbol. [`Self::CROSSHAIR`] gates snapping on the crosshair being
/// active (a live-cursor condition handled by the caller, not by
/// [`snapping_candidates`]).
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
pub struct SnappingMode(u8);

impl SnappingMode {
    /// No snapping (silx `SNAPPING_DISABLED`).
    pub const DISABLED: Self = Self(0);
    /// Snap only while the crosshair cursor is active (silx `SNAPPING_CROSSHAIR`).
    pub const CROSSHAIR: Self = Self(1 << 0);
    /// Restrict candidates to the active curve/scatter (silx `SNAPPING_ACTIVE_ONLY`).
    pub const ACTIVE_ONLY: Self = Self(1 << 1);
    /// Restrict candidates to items showing a symbol (silx `SNAPPING_SYMBOLS_ONLY`).
    pub const SYMBOLS_ONLY: Self = Self(1 << 2);
    /// Snap to curves (and histograms) (silx `SNAPPING_CURVE`).
    pub const CURVE: Self = Self(1 << 3);
    /// Snap to scatters (silx `SNAPPING_SCATTER`).
    pub const SCATTER: Self = Self(1 << 4);

    /// Whether every flag in `flag` is set in `self`.
    #[must_use]
    pub fn contains(self, flag: Self) -> bool {
        self.0 & flag.0 == flag.0
    }

    /// The raw bits (silx integer mode value).
    #[must_use]
    pub fn bits(self) -> u8 {
        self.0
    }
}

impl std::ops::BitOr for SnappingMode {
    type Output = Self;
    fn bitor(self, rhs: Self) -> Self {
        Self(self.0 | rhs.0)
    }
}

/// The kind of a plot item as seen by snapping selection — the silx `isinstance`
/// branches in `PositionInfo._updateStatusBar` (PositionInfo.py:217-246).
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum SnapItemKind {
    /// A curve (silx `items.Curve`).
    Curve,
    /// A histogram (silx `items.Histogram`) — snapped under [`SnappingMode::CURVE`].
    Histogram,
    /// A scatter (silx `items.Scatter`) — snapped under [`SnappingMode::SCATTER`].
    Scatter,
    /// Any other item, never a snap candidate.
    Other,
}

/// One plot item described for snapping candidate selection.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct SnapItem {
    /// The item's kind (silx `isinstance` test).
    pub kind: SnapItemKind,
    /// Whether the item is visible (silx `item.isVisible()`).
    pub visible: bool,
    /// Whether the item shows a symbol (silx `isinstance(item, SymbolMixIn) and
    /// item.getSymbol()` truthy).
    pub has_symbol: bool,
    /// Whether the item is the active curve/scatter (silx `getActiveCurve()` /
    /// `getActiveScatter()`).
    pub active: bool,
}

/// Select the snap-candidate items for `mode`, porting silx
/// `PositionInfo._updateStatusBar`'s item selection (PositionInfo.py:196-244).
///
/// Returns the indices into `items` that should be projected to pixels and fed
/// to [`snap_to_nearest`]. Empty when neither [`SnappingMode::CURVE`] nor
/// [`SnappingMode::SCATTER`] is set (silx engages snapping only then, :197).
///
/// Faithful asymmetry: in the all-items path silx's `CURVE` kind list is
/// `(Curve, Histogram)` (:217-219) and the candidates are filtered by
/// `isVisible()` (:225); the [`SnappingMode::ACTIVE_ONLY`] path instead takes
/// `getActiveCurve()` (a `Curve`, never a histogram) and `getActiveScatter()`
/// (:202-213) regardless of the visible flag. [`SnappingMode::SYMBOLS_ONLY`]
/// then drops items without a symbol on either path (:240-244).
///
/// The [`SnappingMode::CROSSHAIR`] gate (:198) is a live-cursor precondition the
/// caller applies; it does not affect which kinds are candidates.
#[must_use]
pub fn snapping_candidates(mode: SnappingMode, items: &[SnapItem]) -> Vec<usize> {
    let want_curve = mode.contains(SnappingMode::CURVE);
    let want_scatter = mode.contains(SnappingMode::SCATTER);
    if !want_curve && !want_scatter {
        return Vec::new();
    }
    let active_only = mode.contains(SnappingMode::ACTIVE_ONLY);
    let symbols_only = mode.contains(SnappingMode::SYMBOLS_ONLY);

    items
        .iter()
        .enumerate()
        .filter(|(_, item)| {
            let kind_match = match item.kind {
                // The active-only path uses getActiveCurve (a Curve), so a
                // histogram is a CURVE candidate only in the all-items path.
                SnapItemKind::Curve => want_curve,
                SnapItemKind::Histogram => want_curve && !active_only,
                SnapItemKind::Scatter => want_scatter,
                SnapItemKind::Other => false,
            };
            if !kind_match {
                return false;
            }
            if active_only {
                if !item.active {
                    return false;
                }
            } else if !item.visible {
                // All-items path filters by visibility (silx :225); the
                // active-only path does not.
                return false;
            }
            if symbols_only && !item.has_symbol {
                return false;
            }
            true
        })
        .map(|(i, _)| i)
        .collect()
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn default_is_xy() {
        let p = PositionInfo::default();
        assert_eq!(p.len(), 2);
        let v = p.values(Some([3.0, 4.0]));
        assert_eq!(v, vec!["3".to_owned(), "4".to_owned()]);
    }

    #[test]
    fn no_cursor_shows_placeholder() {
        let p = PositionInfo::default();
        let v = p.values(None);
        assert_eq!(v, vec!["------".to_owned(), "------".to_owned()]);
    }

    #[test]
    fn polar_radius_is_hypot() {
        let p = PositionInfo::polar();
        // (3,4) -> radius 5, angle atan2(4,3) deg ~= 53.13010...
        let v = p.values(Some([3.0, 4.0]));
        assert_eq!(v[0], "5");
        // %.7g of 53.13010235... -> "53.1301" (trailing zero trimmed by %g).
        assert_eq!(v[1], "53.1301");
    }

    #[test]
    fn polar_angle_on_axes() {
        let p = PositionInfo::polar();
        // (1, 0) -> radius 1, angle 0.
        let v = p.values(Some([1.0, 0.0]));
        assert_eq!(v[0], "1");
        assert_eq!(v[1], "0");
        // (0, 1) -> angle 90.
        let v = p.values(Some([0.0, 1.0]));
        assert_eq!(v[1], "90");
    }

    #[test]
    fn custom_numeric_converter() {
        let mut p = PositionInfo::new(vec![]);
        p.push_numeric("Sum", |x, y| x + y);
        assert!(!p.is_empty());
        let v = p.values(Some([2.5, 1.5]));
        assert_eq!(v, vec!["4".to_owned()]);
    }

    #[test]
    fn custom_string_converter() {
        let p = PositionInfo::new(vec![(
            "Quad".to_owned(),
            Box::new(|x: f64, y: f64| {
                if x >= 0.0 && y >= 0.0 {
                    "I".to_owned()
                } else {
                    "other".to_owned()
                }
            }),
        )]);
        assert_eq!(p.values(Some([1.0, 1.0])), vec!["I".to_owned()]);
        assert_eq!(p.values(Some([-1.0, 1.0])), vec!["other".to_owned()]);
    }

    #[test]
    fn format_value_seven_sig_figs() {
        // %.7g of 1/3.
        assert_eq!(format_value(1.0 / 3.0), "0.3333333");
        assert_eq!(format_value(1234567.0), "1234567");
        // 8 sig figs collapses to exponential under %g (exp >= 7).
        assert_eq!(format_value(12345678.0), "1.234568e+07");
    }

    #[test]
    fn format_value_non_finite() {
        assert_eq!(format_value(f64::NAN), "nan");
        assert_eq!(format_value(f64::INFINITY), "inf");
        assert_eq!(format_value(f64::NEG_INFINITY), "-inf");
    }

    #[test]
    fn snap_picks_nearest_within_radius() {
        // Two candidates; cursor closest to the second. Both within 5px.
        let cursor = [10.0, 10.0];
        let candidates = [
            ([13.0, 10.0], [1.0, 2.0]), // 3px away
            ([10.0, 12.0], [3.0, 4.0]), // 2px away (closest)
        ];
        let snap = snap_to_nearest(cursor, &candidates, SNAP_THRESHOLD_DIST).unwrap();
        assert_eq!(snap.index, 1);
        assert_eq!(snap.data, [3.0, 4.0]);
    }

    #[test]
    fn snap_returns_none_when_all_outside_radius() {
        // Nearest is 6px away, threshold 5px -> no snap (silx red label state).
        let cursor = [0.0, 0.0];
        let candidates = [([6.0, 0.0], [1.0, 1.0])];
        assert_eq!(
            snap_to_nearest(cursor, &candidates, SNAP_THRESHOLD_DIST),
            None
        );
    }

    #[test]
    fn snap_includes_point_exactly_on_radius() {
        // silx uses `<=` (closestSqDistInPixels <= sqDistInPixels): a point at
        // exactly the threshold distance snaps.
        let cursor = [0.0, 0.0];
        let candidates = [([5.0, 0.0], [7.0, 8.0])];
        let snap = snap_to_nearest(cursor, &candidates, SNAP_THRESHOLD_DIST).unwrap();
        assert_eq!(snap.data, [7.0, 8.0]);
    }

    #[test]
    fn snap_skips_non_finite_candidates() {
        // A NaN pixel position is skipped (silx isfinite guard); the finite
        // in-range point still wins.
        let cursor = [0.0, 0.0];
        let candidates = [([f64::NAN, 0.0], [9.0, 9.0]), ([2.0, 0.0], [1.0, 1.0])];
        let snap = snap_to_nearest(cursor, &candidates, SNAP_THRESHOLD_DIST).unwrap();
        assert_eq!(snap.index, 1);
        assert_eq!(snap.data, [1.0, 1.0]);
    }

    #[test]
    fn snap_empty_candidates_is_none() {
        assert_eq!(snap_to_nearest([0.0, 0.0], &[], SNAP_THRESHOLD_DIST), None);
    }

    #[test]
    fn snap_tie_resolves_to_later_candidate() {
        // Equal distances: silx's `<=` update keeps the later item's point.
        let cursor = [0.0, 0.0];
        let candidates = [([3.0, 0.0], [1.0, 1.0]), ([0.0, 3.0], [2.0, 2.0])];
        let snap = snap_to_nearest(cursor, &candidates, SNAP_THRESHOLD_DIST).unwrap();
        assert_eq!(snap.index, 1);
        assert_eq!(snap.data, [2.0, 2.0]);
    }

    fn item(kind: SnapItemKind, visible: bool, has_symbol: bool, active: bool) -> SnapItem {
        SnapItem {
            kind,
            visible,
            has_symbol,
            active,
        }
    }

    #[test]
    fn snapping_disabled_without_a_kind_flag() {
        // No CURVE / SCATTER bit -> silx never engages snapping (:197), even
        // with the modifier bits set.
        let items = [item(SnapItemKind::Curve, true, true, true)];
        assert!(snapping_candidates(SnappingMode::DISABLED, &items).is_empty());
        assert!(
            snapping_candidates(
                SnappingMode::ACTIVE_ONLY | SnappingMode::SYMBOLS_ONLY,
                &items
            )
            .is_empty()
        );
    }

    #[test]
    fn snapping_curve_all_items_takes_visible_curves_and_histograms() {
        // CURVE all-items path: visible Curve + Histogram, skipping Scatter,
        // invisible items, and Other (silx kinds = (Curve, Histogram), :217-225).
        let items = [
            item(SnapItemKind::Curve, true, false, false),
            item(SnapItemKind::Histogram, true, false, false),
            item(SnapItemKind::Curve, false, false, false), // invisible
            item(SnapItemKind::Scatter, true, false, false), // wrong kind
            item(SnapItemKind::Other, true, false, false),
        ];
        assert_eq!(snapping_candidates(SnappingMode::CURVE, &items), vec![0, 1]);
    }

    #[test]
    fn snapping_scatter_all_items_takes_visible_scatters_only() {
        let items = [
            item(SnapItemKind::Scatter, true, false, false),
            item(SnapItemKind::Curve, true, false, false),
            item(SnapItemKind::Scatter, false, false, false), // invisible
        ];
        assert_eq!(snapping_candidates(SnappingMode::SCATTER, &items), vec![0]);
        // CURVE|SCATTER takes both kinds.
        assert_eq!(
            snapping_candidates(SnappingMode::CURVE | SnappingMode::SCATTER, &items),
            vec![0, 1]
        );
    }

    #[test]
    fn snapping_active_only_ignores_visibility_and_excludes_histograms() {
        // Active-only uses getActiveCurve/getActiveScatter: the active item is
        // taken even when invisible, but a histogram is NOT an "active curve".
        let items = [
            item(SnapItemKind::Curve, false, false, true), // active, invisible -> kept
            item(SnapItemKind::Curve, true, false, false), // not active -> skipped
            item(SnapItemKind::Histogram, true, false, true), // active histogram -> excluded
            item(SnapItemKind::Scatter, false, false, true), // active scatter -> kept
        ];
        assert_eq!(
            snapping_candidates(
                SnappingMode::CURVE | SnappingMode::SCATTER | SnappingMode::ACTIVE_ONLY,
                &items
            ),
            vec![0, 3]
        );
    }

    #[test]
    fn snapping_symbols_only_drops_items_without_a_symbol() {
        // SYMBOLS_ONLY filters on both paths (silx :240-244).
        let items = [
            item(SnapItemKind::Curve, true, true, false), // has symbol -> kept
            item(SnapItemKind::Curve, true, false, false), // no symbol -> dropped
        ];
        assert_eq!(
            snapping_candidates(SnappingMode::CURVE | SnappingMode::SYMBOLS_ONLY, &items),
            vec![0]
        );
    }

    #[test]
    fn snapping_mode_bits_and_contains() {
        let mode = SnappingMode::CURVE | SnappingMode::ACTIVE_ONLY;
        assert!(mode.contains(SnappingMode::CURVE));
        assert!(mode.contains(SnappingMode::ACTIVE_ONLY));
        assert!(!mode.contains(SnappingMode::SCATTER));
        // silx flag values: CURVE=1<<3, ACTIVE_ONLY=1<<1.
        assert_eq!(mode.bits(), (1 << 3) | (1 << 1));
    }
}