indicatrix-cut 0.7.2

Desktop faceting-design editor: library browsing, spectral 3D rendering, material retargeting, and a solid inspection view.
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
//! Placing the three handles on the selected facet, hit-testing them and lighting the
//! one under the pointer.

use super::{
    CachedFacetMap, SESSION, Shared, Target, diagram, kind_to_int, set_hint, slice, snap_mode,
    tier_label,
};
use crate::{
    EditorModel, MainWindow, ManipulateModel, PreferencesModel,
    gui::{
        editor::callbacks::{
            map_to_pick_coordinates, pick_margin, pick_pixels_to_logical, selected_facet_id,
        },
        solid_preview::{facet_map::FacetMap, preview_state::FrameGeometry},
    },
};
use glam::Vec3;
use indicatrix::{geometry::meet_solver::SolvedTier, optics::raytracer::Camera};
use indicatrix_editor::manipulate::{
    FacetFrame, HANDLE_HIT_RADIUS_PX, HandleKind, HandleLayout, ScreenPoint, ScreenSize,
    handle_layout, hit_test, text, tiers_meeting,
};
use slint::ComponentHandle as _;
use std::{rc::Rc, sync::PoisonError};

/// The field of view the solid raster and the path tracer share.
pub(super) const CAMERA_FOV_DEG: f32 = indicatrix::optics::raytracer::DEFAULT_FOV_DEG;

/// How long each handle is, as a fraction of the shown stone's bounding radius.
const HANDLE_LENGTH_FRACTION: f64 = 0.35;

/// The handles' anchor and tips in the viewport's LOGICAL coordinates.
#[derive(Debug, Clone, Copy, PartialEq)]
pub(super) struct LogicalHandles {
    pub(super) anchor: (f32, f32),
    pub(super) angle_tip: (f32, f32),
    pub(super) depth_tip: (f32, f32),
    pub(super) index_tip: (f32, f32),
}

/// `layout` (pick-frame pixels) converted to logical viewport coordinates with the
/// inverse of `map_to_pick_coordinates`'s arithmetic: `(pick + margin) / scale`.
pub(super) fn layout_to_logical(
    layout: &HandleLayout,
    scale: f32,
    margin: (f32, f32),
) -> LogicalHandles {
    let convert = |p: ScreenPoint| pick_pixels_to_logical((p.x, p.y), scale, margin);
    LogicalHandles {
        anchor: convert(layout.anchor),
        angle_tip: convert(layout.angle_tip),
        depth_tip: convert(layout.depth_tip),
        index_tip: convert(layout.index_tip),
    }
}

/// The facet the handles anchor on: the last clicked facet when it belongs to `tier`
/// and has a centroid, else the tier's first facet that has one (a selection made from
/// the tier table names no facet).
pub(super) fn pick_facet(
    remembered: Option<u32>,
    tier: usize,
    facets_of_tier: &[u32],
    tier_of: impl Fn(u32) -> Option<usize>,
    has_centroid: impl Fn(u32) -> bool,
) -> Option<u32> {
    remembered
        .filter(|&id| tier_of(id) == Some(tier) && has_centroid(id))
        .or_else(|| facets_of_tier.iter().copied().find(|&id| has_centroid(id)))
}

/// Whether the solved masts describe the design's tiers one to one -- a stale cache
/// (a tier was just added or removed) would put every facet id on the wrong tier.
pub(super) const fn masts_aligned(tier_count: usize, masts_len: usize) -> bool {
    tier_count == masts_len
}

/// Whether the facet map and the frame's geometry share one facet-id space. They differ
/// while a frame is behind the design, and under the Cut slider (the mesh then holds
/// only the first tiers' planes); the handles hide rather than land on the wrong facet.
pub(super) const fn ids_aligned(map_facets: usize, centroid_count: usize) -> bool {
    map_facets == centroid_count
}

/// The handle whose tip lies within `radius` of `p`. A tier with no index-wheel
/// positions has no index handle to grab.
pub(super) fn hit_kind(
    layout: &HandleLayout,
    indexless: bool,
    p: ScreenPoint,
    radius: f32,
) -> Option<HandleKind> {
    let mut layout = *layout;
    if indexless {
        // A NaN tip is never within any radius.
        layout.index_tip = ScreenPoint::new(f32::NAN, f32::NAN);
    }
    hit_test(&layout, p, radius)
}

/// [`hit_kind`] for a tier whose angle follows a relation: it has no angle handle either,
/// so only the depth and index tips can be grabbed.
pub(super) fn hit_kind_without_angle(
    layout: &HandleLayout,
    indexless: bool,
    p: ScreenPoint,
    radius: f32,
) -> Option<HandleKind> {
    let mut layout = *layout;
    layout.angle_tip = ScreenPoint::new(f32::NAN, f32::NAN);
    hit_kind(&layout, indexless, p, radius)
}

/// The relation (as a cutter reads it, `P1 - 2`) the angle of committed tier `tier`
/// follows, if it follows one -- such a tier has no angle handle. `None` for a free angle,
/// for the provisional slice tier (it is not in the design yet) and while the editor state
/// is borrowed.
pub(super) fn driven_relation(ctx: &Shared, tier: usize) -> Option<String> {
    ctx.state
        .try_borrow()
        .ok()
        .and_then(|st| st.design.relation_text(tier))
}

/// Whether the handles just landed on a NEW committed selection whose angle follows a
/// relation -- the moment to say why it has no angle handle (a hidden handle cannot be
/// hovered to ask). Never for the provisional slice tier and never for a frame that merely
/// re-places the handles already on screen.
pub(super) fn driven_hint_due(previous: Option<SelectionKey>, now: SelectionKey) -> bool {
    !now.1 && previous != Some(now)
}

/// The solid-preview pick-frame pixel the logical pointer `(x, y)` lands on.
pub(super) fn pointer_to_pick(ui: &MainWindow, x: f32, y: f32) -> ScreenPoint {
    let (px, py) = map_to_pick_coordinates(ui, x, y);
    ScreenPoint::new(px, py)
}

/// `(generation, length)` of the cached solved masts, `None` before the first solve.
fn solved_summary(ctx: &Shared) -> Option<(u64, usize)> {
    ctx.solid_last_solved
        .lock()
        .unwrap_or_else(PoisonError::into_inner)
        .as_ref()
        .map(|(generation, masts)| (*generation, masts.len()))
}

/// A copy of the cached solved masts when they describe exactly `tier_count` tiers.
pub(super) fn aligned_masts(ctx: &Shared, tier_count: usize) -> Option<Vec<SolvedTier>> {
    ctx.solid_last_solved
        .lock()
        .unwrap_or_else(PoisonError::into_inner)
        .as_ref()
        .filter(|(_, masts)| masts_aligned(tier_count, masts.len()))
        .map(|(_, masts)| masts.clone())
}

/// The facet map of `design` against the cached masts, rebuilt only when the design
/// generation or the solved-masts generation moved since the last call. `None` when the
/// masts do not describe the design.
pub(super) fn facet_map_for(
    ctx: &Shared,
    design_generation: u64,
    design: &indicatrix_cut_core::Design,
) -> Option<Rc<FacetMap>> {
    let (solved_generation, masts_len) = solved_summary(ctx)?;
    if !masts_aligned(design.tiers.len(), masts_len) {
        return None;
    }
    let cached = SESSION.with(|cell| {
        cell.borrow()
            .facet_map
            .as_ref()
            .filter(|c| {
                c.design_generation == design_generation && c.solved_generation == solved_generation
            })
            .map(|c| Rc::clone(&c.map))
    });
    if cached.is_some() {
        return cached;
    }
    let masts = aligned_masts(ctx, design.tiers.len())?;
    let map = Rc::new(FacetMap::from_design(design, &masts));
    SESSION.with(|cell| {
        cell.borrow_mut().facet_map = Some(CachedFacetMap {
            design_generation,
            solved_generation,
            map: Rc::clone(&map),
        });
    });
    Some(map)
}

/// The centroid of `facet_id` in `centroids`, if it has one.
fn centroid_of(centroids: &[Option<Vec3>], facet_id: u32) -> Option<Vec3> {
    centroids.get(facet_id as usize).copied().flatten()
}

/// The handle target on `tier` of `design`, whose facets `map` describes and `geometry`
/// (the frame on screen) has centroids for: the `remembered` facet when it belongs to
/// the tier, else the tier's first facet with a centroid. `None` when the map and the
/// frame hold different facet-id spaces, no facet of the tier is on screen, or a
/// handle would land behind the camera.
pub(super) fn target_for(
    geometry: &FrameGeometry,
    map: &FacetMap,
    design: &indicatrix_cut_core::Design,
    tier: usize,
    remembered: Option<u32>,
    provisional: bool,
) -> Option<Target> {
    let tier_data = design.tiers.get(tier)?;
    let centroids = geometry.facet_centroids.as_slice();
    if !ids_aligned(map.facet_count(), centroids.len()) {
        return None;
    }
    let facet_id = pick_facet(
        remembered,
        tier,
        map.facets_of_tier(tier),
        |id| map.tier_of(id as usize),
        |id| centroid_of(centroids, id).is_some(),
    )?;
    let centroid = centroid_of(centroids, facet_id)?;
    // The facet map places a facet at `2 pi (index + reference) / teeth`: the frame needs the
    // design's gear reference angle too, or the handles sit on a different azimuth than the
    // facet (the Diagram view's `diagram2d::facet_frame` follows the same rule).
    let frame = FacetFrame::from_tier_with_reference(
        tier_data,
        f64::from(map.index_on_gear(facet_id as usize)),
        design.meta.gear_reference_angle as f32,
        design.meta.gear_teeth_abs(),
        centroid,
    );
    let pose = geometry.camera;
    let camera = Camera::new(pose.yaw, pose.pitch, pose.distance, CAMERA_FOV_DEG);
    let size = ScreenSize::new(geometry.size.0 as f32, geometry.size.1 as f32);
    let length = (HANDLE_LENGTH_FRACTION * geometry.bounding_radius) as f32;
    let layout = handle_layout(&frame, &camera, size, length)?;
    Some(Target {
        tier,
        frame,
        layout,
        label: tier_label(tier_data, tier),
        provisional,
        diagram: None,
    })
}

/// Works out where the handles go for the current selection and frame, or `None` when
/// they should be hidden: no selection, no geometry, an unsolved or misaligned design, a
/// tier with no facet on screen, or a facet behind the camera. In the Diagram view they
/// sit on one of its panels instead (see `diagram::place`). In Slice mode the only
/// handles are the provisional tier's.
fn place(ui: &MainWindow, ctx: &Shared) -> Option<Target> {
    let geometry = ctx
        .geometry
        .lock()
        .unwrap_or_else(PoisonError::into_inner)
        .clone()?;
    if diagram::is_diagram_view(ui) {
        return diagram::place(ui, ctx, &geometry);
    }
    // The last frame is still a Diagram one (the view was just switched away from it):
    // its camera pose is meaningless to the 3D handles, so wait for the next frame.
    if geometry.diagram.is_some() {
        return None;
    }
    if ui.global::<ManipulateModel>().get_slice_mode() {
        return slice::place_provisional(&geometry);
    }
    let tier = usize::try_from(ui.global::<EditorModel>().get_selected_tier_index()).ok()?;
    let st = ctx.state.try_borrow().ok()?;
    let map = facet_map_for(ctx, st.current_generation(), &st.design)?;
    target_for(
        &geometry,
        &map,
        &st.design,
        tier,
        selected_facet_id(),
        false,
    )
}

/// What the handles sit on, for noticing a new selection: the tier's index and whether it
/// is the provisional slice tier.
pub(super) type SelectionKey = (usize, bool);

/// The [`SelectionKey`] of `target`.
const fn selection_key(target: &Target) -> SelectionKey {
    (target.tier, target.provisional)
}

/// Whether the handles just landed on a NEW committed selection while "Larger handles" is
/// on -- the moment to word what the three handles do, because a touch screen or pen has
/// no hover to show it. Never for the provisional slice tier (its own hint is showing) and
/// never for a frame that merely re-places the handles already on screen.
pub(super) fn selection_hint_due(
    large_handles: bool,
    previous: Option<SelectionKey>,
    now: SelectionKey,
) -> bool {
    large_handles && !now.1 && previous != Some(now)
}

/// Whether the hint line is free to take the selection hint: no drag, no Slice tool, and
/// the pointer is not resting on a handle (whose own hover hint wins).
fn hint_line_is_free(ui: &MainWindow) -> bool {
    let model = ui.global::<ManipulateModel>();
    !model.get_dragging()
        && !model.get_slice_mode()
        && SESSION.with(|cell| cell.borrow().hovered.is_none())
}

/// Shows `hint` (the selection hint: what the handles do, or why the angle has none).
/// Marked as ours (`hover_hint_active`) so hiding the handles, or the pointer touching and
/// leaving a handle, clears it again.
fn show_selection_hint(ui: &MainWindow, hint: &str) {
    SESSION.with(|cell| cell.borrow_mut().hover_hint_active = true);
    set_hint(ui, hint);
}

/// The hint a newly selected facet gets, if any. A tier whose angle follows a relation
/// says why it has no angle handle, whatever the handle size; any other tier gets the
/// selection hint only with "Larger handles" on.
pub(super) fn selection_hint_text(
    label: &str,
    driven_by: Option<&str>,
    large_handles: bool,
    previous: Option<SelectionKey>,
    now: SelectionKey,
) -> Option<String> {
    if let Some(relation) = driven_by {
        return driven_hint_due(previous, now)
            .then(|| text::angle_follows_relation_hint(label, relation));
    }
    selection_hint_due(large_handles, previous, now).then(|| text::handle_select_hint(label))
}

/// Re-places the handles for the frame that just landed, or hides them -- see [`place`].
/// A newly selected facet also gets the selection hint (see [`selection_hint_text`]).
pub(super) fn refresh_handles(ui: &MainWindow, ctx: &Shared) {
    let previous = SESSION.with(|cell| cell.borrow().target.as_ref().map(selection_key));
    match place(ui, ctx) {
        Some(target) => {
            let key = selection_key(&target);
            let label = target.label.clone();
            let driven_by = driven_relation(ctx, target.tier);
            show(ui, target, driven_by.is_none());
            let large = ui.global::<PreferencesModel>().get_large_handles();
            let hint = selection_hint_text(&label, driven_by.as_deref(), large, previous, key);
            if let Some(hint) = hint
                && hint_line_is_free(ui)
            {
                show_selection_hint(ui, &hint);
            }
        }
        None => hide(ui),
    }
}

/// The radius, in pick-frame pixels, within which a press grabs a handle tip:
/// [`HANDLE_HIT_RADIUS_PX`] at the window's scale factor, times `handle_scale`
/// (`ManipulateModel.handle_scale`, bigger with the "Larger handles" preference). The
/// markers are drawn `handle_scale` times larger, so what you see is what you can hit.
pub(super) const fn hit_radius_px(window_scale: f32, handle_scale: f32) -> f32 {
    HANDLE_HIT_RADIUS_PX * window_scale * handle_scale
}

/// Stores `target` in the session and pushes its logical positions to the model.
/// `angle_visible` is `false` for a tier whose angle follows a relation: its angle handle
/// is neither drawn nor grabbable.
fn show(ui: &MainWindow, target: Target, angle_visible: bool) {
    let scale = ui.window().scale_factor();
    let logical = layout_to_logical(&target.layout, scale, pick_margin(ui));
    let indexless = target.frame.is_indexless();
    // On a Diagram panel some handles are not offered (foreshortened or edge-on levers).
    let offered = target.diagram.map(|placed| placed.available);
    // A handle the panel does not offer has a NaN tip (so no pointer ever hits it); the
    // model gets the anchor instead, and the visibility flags below keep it undrawn.
    let finite_tip = |tip: (f32, f32)| {
        if tip.0.is_finite() && tip.1.is_finite() {
            tip
        } else {
            logical.anchor
        }
    };
    let (angle_tip, depth_tip, index_tip) = (
        finite_tip(logical.angle_tip),
        finite_tip(logical.depth_tip),
        finite_tip(logical.index_tip),
    );
    let model = ui.global::<ManipulateModel>();
    model.set_anchor_x(logical.anchor.0);
    model.set_anchor_y(logical.anchor.1);
    model.set_angle_tip_x(angle_tip.0);
    model.set_angle_tip_y(angle_tip.1);
    model.set_depth_tip_x(depth_tip.0);
    model.set_depth_tip_y(depth_tip.1);
    model.set_index_tip_x(index_tip.0);
    model.set_index_tip_y(index_tip.1);
    model.set_index_visible(!indexless && offered.is_none_or(|a| a.index));
    model.set_angle_visible(angle_visible && offered.is_none_or(|a| a.angle));
    model.set_depth_visible(offered.is_none_or(|a| a.depth));
    model.set_handles_visible(true);
    SESSION.with(|cell| cell.borrow_mut().target = Some(target));
}

/// Hides the handles and drops the hover hint if it is ours.
fn hide(ui: &MainWindow) {
    let had_hint = SESSION.with(|cell| {
        let mut session = cell.borrow_mut();
        session.target = None;
        session.hovered = None;
        std::mem::take(&mut session.hover_hint_active)
    });
    let model = ui.global::<ManipulateModel>();
    model.set_handles_visible(false);
    model.set_hovered_handle(-1);
    if had_hint {
        slice::resting_hint(ui);
    }
}

/// The handle under the logical pointer `(x, y)`, per the last placed layout.
fn hit_kind_at(ui: &MainWindow, x: f32, y: f32) -> Option<HandleKind> {
    let (layout, indexless) = SESSION.with(|cell| {
        cell.borrow()
            .target
            .as_ref()
            .map(|t| (t.layout, t.frame.is_indexless()))
    })?;
    let model = ui.global::<ManipulateModel>();
    let radius = diagram::hit_radius(
        ui,
        hit_radius_px(ui.window().scale_factor(), model.get_handle_scale()),
    );
    let pointer = pointer_to_pick(ui, x, y);
    if model.get_angle_visible() {
        hit_kind(&layout, indexless, pointer, radius)
    } else {
        hit_kind_without_angle(&layout, indexless, pointer, radius)
    }
}

/// `ManipulateModel.handle_hit_test`: the handle under the pointer as an int, `-1` none.
pub(super) fn hit_test_int(ui: &MainWindow, x: f32, y: f32) -> i32 {
    hit_kind_at(ui, x, y).map_or(-1, kind_to_int)
}

/// The hover hint for `kind` on the current target: what dragging does, how it snaps
/// and how many tiers meet this one by name.
fn hover_hint(ui: &MainWindow, ctx: &Shared, kind: HandleKind) -> Option<String> {
    let (tier, label, provisional) = SESSION.with(|cell| {
        cell.borrow()
            .target
            .as_ref()
            .map(|t| (t.tier, t.label.clone(), t.provisional))
    })?;
    // A provisional tier is pinned by a scale reference: nothing meets it by name.
    let meeting = if provisional {
        0
    } else {
        ctx.state
            .try_borrow()
            .map_or(0, |st| tiers_meeting(&st.design, tier).len())
    };
    let snap = snap_mode(false, ui.global::<ManipulateModel>().get_snap_off());
    Some(text::handle_hover_hint(kind, &label, meeting, snap))
}

/// Lights `kind`'s handle and words the hint (or clears both for `None`).
fn apply_hover(ui: &MainWindow, ctx: &Shared, kind: Option<HandleKind>) {
    let model = ui.global::<ManipulateModel>();
    model.set_hovered_handle(kind.map_or(-1, kind_to_int));
    let Some(hint) = kind.and_then(|kind| hover_hint(ui, ctx, kind)) else {
        // Off every handle: drop the hint, but only if it is still ours.
        let had_hint =
            SESSION.with(|cell| std::mem::take(&mut cell.borrow_mut().hover_hint_active));
        if had_hint {
            slice::resting_hint(ui);
        }
        return;
    };
    SESSION.with(|cell| cell.borrow_mut().hover_hint_active = true);
    set_hint(ui, &hint);
}

/// `ManipulateModel.handle_hover`: the pointer moved with no drag in progress.
pub(super) fn hover(ui: &MainWindow, ctx: &Shared, x: f32, y: f32) {
    if ui.global::<ManipulateModel>().get_dragging() {
        return;
    }
    // In the Diagram view the handles first follow the pointer onto the panel it entered.
    diagram::follow_pointer(ui, ctx, x, y);
    let kind = hit_kind_at(ui, x, y);
    let changed = SESSION.with(|cell| {
        let mut session = cell.borrow_mut();
        let changed = session.hovered != kind;
        session.hovered = kind;
        changed
    });
    if changed {
        apply_hover(ui, ctx, kind);
    }
}

/// Re-words the hover hint for the handle still under the pointer (the Snap pill just
/// changed what dragging it would snap to).
pub(super) fn refresh_hover_hint(ui: &MainWindow, ctx: &Shared) {
    if ui.global::<ManipulateModel>().get_dragging() {
        return;
    }
    let hovered = SESSION.with(|cell| cell.borrow().hovered);
    if hovered.is_some() {
        apply_hover(ui, ctx, hovered);
    }
}

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

    /// The anchor at `(100, 100)` with the three tips a fixed distance away.
    fn layout() -> HandleLayout {
        HandleLayout {
            anchor: ScreenPoint::new(100.0, 100.0),
            angle_tip: ScreenPoint::new(100.0, 40.0),
            depth_tip: ScreenPoint::new(160.0, 100.0),
            index_tip: ScreenPoint::new(40.0, 100.0),
            angle_dir: (0.0, -1.0),
            depth_dir: (1.0, 0.0),
            index_dir: (-1.0, 0.0),
            pixels_per_degree: 2.0,
            pixels_per_mast_unit: 100.0,
            pixels_per_tooth: 10.0,
        }
    }

    #[test]
    fn a_tier_that_follows_a_relation_has_no_angle_handle_to_grab() {
        let layout = layout();
        let on_angle_tip = ScreenPoint::new(100.0, 40.0);
        assert_eq!(
            hit_kind(&layout, false, on_angle_tip, 12.0),
            Some(HandleKind::Angle)
        );
        assert_eq!(
            hit_kind_without_angle(&layout, false, on_angle_tip, 12.0),
            None
        );
        // Depth and index stay.
        assert_eq!(
            hit_kind_without_angle(&layout, false, ScreenPoint::new(160.0, 100.0), 12.0),
            Some(HandleKind::Depth)
        );
        assert_eq!(
            hit_kind_without_angle(&layout, false, ScreenPoint::new(40.0, 100.0), 12.0),
            Some(HandleKind::Index)
        );
        // No index positions either: only the depth handle is left.
        assert_eq!(
            hit_kind_without_angle(&layout, true, ScreenPoint::new(40.0, 100.0), 12.0),
            None
        );
    }

    #[test]
    fn the_why_hint_is_due_once_per_new_selection_and_never_for_the_slice_tier() {
        assert!(driven_hint_due(None, (3, false)));
        assert!(driven_hint_due(Some((2, false)), (3, false)));
        assert!(!driven_hint_due(Some((3, false)), (3, false)), "same frame");
        assert!(!driven_hint_due(None, (3, true)), "the provisional tier");
    }

    #[test]
    fn a_driven_tier_says_why_whatever_the_handle_size_and_others_keep_the_old_rule() {
        let driven = |large, previous| {
            selection_hint_text("P2", Some("P1 - 2"), large, previous, (1, false))
        };
        let expected = text::angle_follows_relation_hint("P2", "P1 - 2");
        assert_eq!(driven(false, None), Some(expected.clone()));
        assert_eq!(driven(true, None), Some(expected));
        assert_eq!(driven(true, Some((1, false))), None, "already announced");

        let free = |large, previous| selection_hint_text("P1", None, large, previous, (0, false));
        assert_eq!(free(true, None), Some(text::handle_select_hint("P1")));
        assert_eq!(free(false, None), None, "only with Larger handles");
        assert_eq!(free(true, Some((0, false))), None);
    }
}