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
//! Edit > Angle Sweep...: one tier's angle over a range, every angle solved and scored, as
//! a table, a chart and CSV (`ui/components/sweep_dialog.slint`, state in
//! `ui/models/sweep.slint`).
//!
//! The sweep itself is `indicatrix_editor::sweep` (shared with the web app); this group is
//! the desktop's glue around it:
//!
//! - [`form`] -- which tiers the combo offers, a tier's starting range, and what the three
//!   number fields add up to. Slint-free.
//! - [`state`] -- what the dialog remembers between callbacks (rows, selection, lines).
//!   Slint-free.
//! - [`run`] -- the worker thread and the handle the UI thread polls.
//! - [`present`] -- the table rows, chart lines and sentences, and the pushes into
//!   `SweepModel`.
//! - this file -- the [`Controller`] that answers the model's callbacks.
//!
//! A sweep solves every angle on a clone of the design, so the design is never touched while
//! it runs; "Use this angle" is the one edit, a single undo step through
//! `indicatrix_editor::sweep::apply_sweep_angle` (the tiers whose angles follow a relation
//! follow in the same step). It is refused once the design has changed since the sweep
//! started, because the rows no longer describe it.

mod form;
mod present;
mod run;
mod state;

use self::{
    form::{Checked, Fields},
    run::{Job, Poll, RunHandle},
    state::Dialog,
};
use super::{relation_ui::refresh_with_followers, state::EditorState};
use crate::{
    EditorModel, MainWindow, SweepModel, ViewportModel,
    bridge::render_thread::RenderContext,
    gui::{
        library::clipboard::copy_to_clipboard_with_toast,
        pickers::{PickerFilter, PickerKind, PickerRequest, pick},
        show_toast,
        solid_preview::preview_state::{SolidLastSolved, SolidPreviewState},
    },
};
use indicatrix::optics::LightingPreset;
use indicatrix_editor::{
    optimize_view::default_optimize_material_ri,
    sweep::{
        SweepMetric, SweepOptions, SweepOutcome, SweepPlan, apply_sweep_angle,
        default_worker_count, sweep_csv,
    },
};
use slint::{ComponentHandle, Timer, TimerMode, Weak};
use std::{
    cell::RefCell,
    rc::Rc,
    sync::{Arc, Mutex, PoisonError},
    time::Duration,
};

/// How often the UI thread looks at a running sweep.
const POLL_INTERVAL: Duration = Duration::from_millis(100);

/// What the dialog says when the design has no tier a sweep can vary.
const NOTHING_TO_SWEEP: &str = "No tier of this design has an angle a sweep can vary. A table, \
    a culet, a girdle and a tier whose angle follows a relation cannot be swept.";

/// What "Use this angle" says once the design has moved on.
const DESIGN_MOVED_ON: &str = "The design has changed since this sweep ran, so its rows no \
    longer describe it. Run the sweep again.";

/// Answers `SweepModel`'s callbacks.
struct Controller {
    ui: Weak<MainWindow>,
    editor: Rc<RefCell<EditorState>>,
    render_ctx: Arc<Mutex<RenderContext>>,
    preview_state: Arc<SolidPreviewState>,
    solid_last_solved: SolidLastSolved,
    dialog: RefCell<Dialog>,
    /// Polls a running sweep; stopped whenever none runs.
    timer: Timer,
    /// How many threads a sweep uses (the machine's, less one).
    workers: usize,
}

/// Registers `SweepModel`'s callbacks: the dialog's open and close, the form, Run and
/// Cancel, the table and chart, "Use this angle" and the two CSV buttons.
pub(super) fn setup_sweep_callbacks(
    ui: &MainWindow,
    state: &Rc<RefCell<EditorState>>,
    render_ctx: &Arc<Mutex<RenderContext>>,
    preview_state: &Arc<SolidPreviewState>,
    solid_last_solved: &SolidLastSolved,
) {
    let controller = Rc::new(Controller {
        ui: ui.as_weak(),
        editor: Rc::clone(state),
        render_ctx: Arc::clone(render_ctx),
        preview_state: Arc::clone(preview_state),
        solid_last_solved: Arc::clone(solid_last_solved),
        dialog: RefCell::new(Dialog::new()),
        timer: Timer::default(),
        workers: default_worker_count(),
    });
    let model = ui.global::<SweepModel>();

    let c = Rc::clone(&controller);
    model.on_open_dialog(move || c.open());
    let c = Rc::clone(&controller);
    model.on_close_dialog(move || c.close());
    let c = Rc::clone(&controller);
    model.on_form_changed(move || c.form_changed());
    let c = Rc::clone(&controller);
    model.on_tier_changed(move || c.tier_changed());
    let c = Rc::clone(&controller);
    model.on_run(move || c.run());
    let c = Rc::clone(&controller);
    model.on_cancel(move || c.cancel());
    let c = Rc::clone(&controller);
    model.on_select_row(move |row| c.select_row(row));
    let c = Rc::clone(&controller);
    model.on_toggle_series(move |series| c.toggle_series(series));
    let c = Rc::clone(&controller);
    model.on_chart_hover(move |fraction| c.chart_hover(fraction));
    let c = Rc::clone(&controller);
    model.on_use_selected(move || c.use_selected());
    let c = Rc::clone(&controller);
    model.on_copy_csv(move || c.copy_csv());
    model.on_save_csv(move || controller.save_csv());
}

/// Shows what the form comes to under it: the summary line, or why it cannot run.
fn push_check(ui: &MainWindow, checked: &Checked) {
    let model = ui.global::<SweepModel>();
    match checked {
        Checked::Ready { summary, .. } => {
            model.set_summary_text(summary.as_str().into());
            model.set_form_ok(true);
        }
        Checked::Refused(message) => {
            model.set_summary_text(message.as_str().into());
            model.set_form_ok(false);
        }
    }
}

/// Puts a tier's range into the three number fields.
fn push_fields(ui: &MainWindow, fields: &Fields) {
    let model = ui.global::<SweepModel>();
    model.set_from_text(fields.from.as_str().into());
    model.set_to_text(fields.to.as_str().into());
    model.set_step_text(fields.step.as_str().into());
}

impl Controller {
    /// Edit > Angle Sweep...: fills the form for the selected tier and shows the dialog.
    fn open(&self) {
        let Some(ui) = self.ui.upgrade() else {
            return;
        };
        let model = ui.global::<SweepModel>();
        if model.get_dialog_open() {
            return;
        }
        let selected = usize::try_from(ui.global::<EditorModel>().get_selected_tier_index()).ok();
        let tilt_average = model.get_tilt_average();
        let opening = {
            let st = self.editor.borrow();
            form::open(&st.design, selected, tilt_average, self.workers)
        };
        let Some(opening) = opening else {
            show_toast(&ui, NOTHING_TO_SWEEP, "info");
            return;
        };
        {
            let mut dialog = self.dialog.borrow_mut();
            dialog.tiers = opening.choices.iter().map(|choice| choice.tier).collect();
            dialog.run = None;
            dialog.clear_results();
        }
        let labels = opening.choices.into_iter().map(|choice| choice.label);
        model.set_tier_names(present::tier_names(labels.collect()));
        model.set_tier_index(i32::try_from(opening.chosen).unwrap_or(0));
        push_fields(&ui, &opening.fields);
        model.set_notice_text(opening.notice.into());
        push_check(&ui, &opening.checked);
        model.set_running(false);
        model.set_progress(0.0);
        model.set_status_text("".into());
        present::push_results(&ui, &self.dialog.borrow());
        model.set_dialog_open(true);
    }

    /// Closes the dialog; a sweep still running is stopped and its rows are dropped.
    fn close(&self) {
        let Some(ui) = self.ui.upgrade() else {
            return;
        };
        self.timer.stop();
        {
            // Dropping the run handle stops the sweep.
            let mut dialog = self.dialog.borrow_mut();
            dialog.run = None;
            dialog.clear_results();
        }
        present::push_results(&ui, &self.dialog.borrow());
        let model = ui.global::<SweepModel>();
        model.set_running(false);
        model.set_dialog_open(false);
    }

    /// The tier the combo has chosen and the form's three fields and tilt choice.
    fn read_form(&self, ui: &MainWindow) -> Option<(usize, Fields, bool)> {
        let model = ui.global::<SweepModel>();
        let index = usize::try_from(model.get_tier_index()).ok()?;
        let tier = *self.dialog.borrow().tiers.get(index)?;
        let fields = Fields {
            from: model.get_from_text().to_string(),
            to: model.get_to_text().to_string(),
            step: model.get_step_text().to_string(),
        };
        Some((tier, fields, model.get_tilt_average()))
    }

    /// A number field or the tilt option changed: say what the form comes to.
    fn form_changed(&self) {
        let Some(ui) = self.ui.upgrade() else {
            return;
        };
        let Some((tier, fields, tilt_average)) = self.read_form(&ui) else {
            return;
        };
        let checked = {
            let st = self.editor.borrow();
            form::check(&st.design, tier, &fields, tilt_average, self.workers)
        };
        push_check(&ui, &checked);
    }

    /// Another tier was chosen: its own range goes into the fields.
    fn tier_changed(&self) {
        let Some(ui) = self.ui.upgrade() else {
            return;
        };
        let Some((tier, _, tilt_average)) = self.read_form(&ui) else {
            return;
        };
        let range = {
            let st = self.editor.borrow();
            form::retier(&st.design, tier, tilt_average, self.workers)
        };
        if let Some((fields, checked)) = range {
            push_fields(&ui, &fields);
            push_check(&ui, &checked);
        }
    }

    /// Everything a sweep of `plan` reads, taken from the design now.
    fn job(
        &self,
        st: &EditorState,
        plan: SweepPlan,
        tilt_average: bool,
        lighting: LightingPreset,
    ) -> Job {
        let custom_materials = self
            .render_ctx
            .lock()
            .unwrap_or_else(PoisonError::into_inner)
            .custom_materials
            .as_ref()
            .clone();
        // A design that names no material is scored at its own refractive index, not as
        // diamond -- the same defaulting Optimize does.
        let mut material = st.design.material.clone();
        let _ = default_optimize_material_ri(&st.design, &mut material, &custom_materials);
        Job {
            design: st.design.clone(),
            plan,
            material,
            custom_materials,
            lighting,
            options: SweepOptions {
                tilt_average,
                workers: self.workers,
            },
        }
    }

    /// Run: starts the sweep on a worker thread and the timer that watches it.
    fn run(self: &Rc<Self>) {
        let Some(ui) = self.ui.upgrade() else {
            return;
        };
        if self.dialog.borrow().run.is_some() {
            return;
        }
        let Some((tier, fields, tilt_average)) = self.read_form(&ui) else {
            return;
        };
        let lighting =
            LightingPreset::from_index(ui.global::<ViewportModel>().get_selected_lighting_index());
        let prepared = {
            let st = self.editor.borrow();
            match form::check(&st.design, tier, &fields, tilt_average, self.workers) {
                Checked::Ready { plan, .. } => Ok((
                    self.job(&st, plan, tilt_average, lighting),
                    st.current_generation(),
                )),
                Checked::Refused(message) => Err(message),
            }
        };
        match prepared {
            Ok((job, generation)) => self.begin(&ui, job, generation),
            Err(message) => show_toast(&ui, &message, "error"),
        }
    }

    /// Starts `job` and shows the dialog as running.
    fn begin(self: &Rc<Self>, ui: &MainWindow, job: Job, generation: u64) {
        let total = job.plan.angles.len();
        let handle = match run::start(job) {
            Ok(handle) => handle,
            Err(message) => {
                show_toast(ui, &message, "error");
                return;
            }
        };
        {
            let mut dialog = self.dialog.borrow_mut();
            dialog.clear_results();
            dialog.generation = generation;
            dialog.run = Some(handle);
        }
        let model = ui.global::<SweepModel>();
        model.set_running(true);
        model.set_progress(0.0);
        model.set_status_text(present::running_status(0, total).into());
        present::push_results(ui, &self.dialog.borrow());
        let weak = Rc::downgrade(self);
        self.timer
            .start(TimerMode::Repeated, POLL_INTERVAL, move || {
                if let Some(controller) = weak.upgrade() {
                    controller.poll();
                }
            });
    }

    /// The timer's tick: progress while the sweep runs, the rows when it is over.
    fn poll(&self) {
        let Some(ui) = self.ui.upgrade() else {
            self.timer.stop();
            return;
        };
        let polled = {
            let mut dialog = self.dialog.borrow_mut();
            dialog.run.as_mut().map(|run| (run.poll(), run.total()))
        };
        let model = ui.global::<SweepModel>();
        match polled {
            None => self.timer.stop(),
            Some((Poll::Running { done }, total)) => {
                model.set_progress(done as f32 / total.max(1) as f32);
                model.set_status_text(present::running_status(done, total).into());
            }
            Some((Poll::Finished(outcome), _)) => self.finish(&ui, outcome),
            Some((Poll::Died, _)) => {
                self.timer.stop();
                self.dialog.borrow_mut().run = None;
                model.set_running(false);
                model.set_status_text("The sweep stopped unexpectedly.".into());
                show_toast(&ui, "The sweep stopped unexpectedly.", "error");
            }
        }
    }

    /// A sweep is over (finished or stopped): shows its rows.
    fn finish(&self, ui: &MainWindow, outcome: SweepOutcome) {
        self.timer.stop();
        let status = present::finished_status(&outcome);
        {
            let mut dialog = self.dialog.borrow_mut();
            dialog.run = None;
            dialog.clear_results();
            // A sweep stopped before its first angle has no rows to show.
            dialog.outcome = (!outcome.rows.is_empty()).then_some(outcome);
        }
        let model = ui.global::<SweepModel>();
        model.set_running(false);
        model.set_progress(1.0);
        model.set_status_text(status.into());
        present::push_results(ui, &self.dialog.borrow());
    }

    /// Cancel: asks the sweep to stop; the rows finished so far still arrive.
    fn cancel(&self) {
        let Some(ui) = self.ui.upgrade() else {
            return;
        };
        let stopping = self
            .dialog
            .borrow()
            .run
            .as_ref()
            .map(RunHandle::cancel)
            .is_some();
        if stopping {
            ui.global::<SweepModel>()
                .set_status_text("Stopping...".into());
        }
    }

    /// A table row or the chart was clicked.
    fn select_row(&self, row: i32) {
        let Some(ui) = self.ui.upgrade() else {
            return;
        };
        let Ok(index) = usize::try_from(row) else {
            return;
        };
        if self.dialog.borrow_mut().select(index) {
            present::push_pointer(&ui, &self.dialog.borrow());
        }
    }

    /// A figure's pill was clicked: its line goes on or off the chart.
    fn toggle_series(&self, series: i32) {
        let Some(ui) = self.ui.upgrade() else {
            return;
        };
        let Some(metric) = usize::try_from(series)
            .ok()
            .and_then(SweepMetric::from_index)
        else {
            return;
        };
        self.dialog.borrow_mut().toggle(metric);
        present::push_lines(&ui, &self.dialog.borrow());
    }

    /// The pointer moved over the chart (`fraction` across the plot, negative for left it).
    fn chart_hover(&self, fraction: f32) {
        let Some(ui) = self.ui.upgrade() else {
            return;
        };
        let fraction = (fraction >= 0.0).then(|| f64::from(fraction));
        if self.dialog.borrow_mut().hover_at(fraction) {
            present::push_pointer(&ui, &self.dialog.borrow());
        }
    }

    /// "Use this angle": the selected row's angle becomes the tier's angle, as one undo step.
    fn use_selected(&self) {
        let Some(ui) = self.ui.upgrade() else {
            return;
        };
        let (pick, generation) = {
            let dialog = self.dialog.borrow();
            (dialog.pick(), dialog.generation)
        };
        let Some(pick) = pick else {
            return;
        };
        let mut st = self.editor.borrow_mut();
        if st.current_generation() != generation {
            drop(st);
            show_toast(&ui, DESIGN_MOVED_ON, "error");
            return;
        }
        let name = indicatrix_editor::retarget::plan::tier_display_name(&st.design, pick.tier);
        match apply_sweep_angle(&mut st, pick.tier, pick.angle_deg) {
            Ok(Some(_)) => {
                refresh_with_followers(
                    &ui,
                    &self.render_ctx,
                    &self.preview_state,
                    &self.solid_last_solved,
                    &st,
                    [pick.tier],
                );
                let after = st.current_generation();
                drop(st);
                self.dialog.borrow_mut().mark_applied(pick.row, after);
                present::push_results(&ui, &self.dialog.borrow());
                show_toast(
                    &ui,
                    &format!(
                        "{name} is now {:.2}\u{b0}. Undo (Ctrl+Z) puts it back.",
                        pick.angle_deg.abs()
                    ),
                    "success",
                );
            }
            Ok(None) => {
                drop(st);
                show_toast(&ui, "No change.", "info");
            }
            Err(error) => {
                drop(st);
                show_toast(&ui, &error.to_string(), "error");
            }
        }
    }

    /// The shown rows as CSV text.
    fn csv_text(&self) -> Option<String> {
        self.dialog.borrow().outcome.as_ref().map(sweep_csv)
    }

    /// Copy CSV: the clipboard gets the table.
    fn copy_csv(&self) {
        let Some(ui) = self.ui.upgrade() else {
            return;
        };
        if let Some(csv) = self.csv_text() {
            copy_to_clipboard_with_toast(&ui, csv, "Copied the sweep as CSV.".to_owned());
        }
    }

    /// Save CSV...: asks for a file, then writes the table to it.
    fn save_csv(&self) {
        let Some(ui) = self.ui.upgrade() else {
            return;
        };
        let Some(csv) = self.csv_text() else {
            return;
        };
        let tier_name = self
            .dialog
            .borrow()
            .outcome
            .as_ref()
            .map(|outcome| outcome.tier_name.clone())
            .unwrap_or_default();
        let design_title = self
            .editor
            .borrow()
            .design
            .meta
            .headers
            .first()
            .cloned()
            .unwrap_or_default();
        let request = PickerRequest {
            kind: PickerKind::SaveFile,
            title: Some("Save the angle sweep as CSV".to_owned()),
            filters: vec![PickerFilter::single("csv")],
            default_file_name: Some(present::csv_file_name(&design_title, &tier_name)),
            starting_dir: None,
        };
        pick(&ui, request, move |ui, picked| {
            // A dismissed dialog is the cutter's own cancel: no toast.
            let Some(path) = picked else {
                return;
            };
            let ui_weak = ui.as_weak();
            std::thread::spawn(move || {
                let result = std::fs::write(&path, csv.as_bytes())
                    .map_err(|error| format!("Failed to write {}: {error}", path.display()));
                let _ = ui_weak.upgrade_in_event_loop(move |ui| match result {
                    Ok(()) => show_toast(
                        &ui,
                        &format!("Saved the sweep to {}.", path.display()),
                        "success",
                    ),
                    Err(message) => show_toast(&ui, &message, "error"),
                });
            });
        });
    }
}