Skip to main content

datui_lib/app/
feedback.rs

1//! What the user is told: the error modal, the completion flash, and the confirmation
2//! modal.
3
4#[derive(Default)]
5pub struct ErrorModal {
6    pub active: bool,
7    pub message: String,
8    /// How far a long message is scrolled; the render clamps it.
9    pub scroll: usize,
10}
11
12impl ErrorModal {
13    pub fn new() -> Self {
14        Self::default()
15    }
16
17    pub fn show(&mut self, message: String) {
18        // Every failure the user is shown, background work's included, so a bug
19        // report has the message after the modal is gone.
20        log::error!(target: "datui", "{message}");
21        self.active = true;
22        self.message = message;
23        self.scroll = 0;
24    }
25
26    pub fn hide(&mut self) {
27        self.active = false;
28        self.message.clear();
29        self.scroll = 0;
30    }
31}
32
33/// A completion flash (the Feedback rules' second rung): one plain sentence on
34/// the footer, cleared by the next keypress or after two seconds,
35/// whichever comes first. Every screen's completions go here, the home screen's
36/// included. The home screen's own status line beside the filter is for what a
37/// key could not do and why, which has to survive until it is read.
38pub struct Flash {
39    pub message: String,
40    /// Where a path the message ends in starts: cut short, the path loses its
41    /// beginning rather than its file name.
42    pub(crate) path_from: Option<usize>,
43    pub(crate) expires: std::time::Instant,
44}
45
46impl Flash {
47    pub(crate) fn new(message: String) -> Self {
48        Self {
49            message,
50            path_from: None,
51            expires: std::time::Instant::now() + std::time::Duration::from_secs(2),
52        }
53    }
54
55    /// `prefix` then `path`, as in `Exported to /data/out.csv`.
56    pub(crate) fn path(prefix: &str, path: &std::path::Path) -> Self {
57        let mut flash = Self::new(format!("{prefix}{}", path.display()));
58        flash.path_from = Some(prefix.len());
59        flash
60    }
61
62    pub(crate) fn expired(&self) -> bool {
63        std::time::Instant::now() >= self.expires
64    }
65}
66
67/// What a confirmation's Yes does. One question is up at a time, so one of these is
68/// armed at a time, and closing the question disarms it.
69pub(crate) enum Confirm {
70    /// Read every row, as the sample.
71    ReadAll,
72    OpenLink(String),
73    ClearRecents,
74    /// Run Setup's full scan, past the question.
75    QualityFullScan,
76    HideExamples,
77    DeleteView(String),
78    ForgetPlace(std::path::PathBuf),
79    /// Overwrite the file the report's export asked about.
80    QualityExport(
81        std::path::PathBuf,
82        crate::analysis::quality_export::ReportFormat,
83    ),
84    ChartExport(Box<crate::chart::chart_export::ChartExportRequest>),
85    Export(Box<crate::ExportRequest>),
86    Copy(crate::clipboard::CopyFormat, bool),
87    /// Download a remote file, or read a large one whole: the open in flight asks.
88    Download,
89    /// Stop a recording or keep it, on the way out; either choice leaves.
90    Leave(crate::Leaving),
91}
92
93pub struct ConfirmationModal {
94    pub active: bool,
95    pub message: String,
96    pub focus_yes: bool, // true = Yes focused, false = No focused
97    /// What Enter-on-Yes does, named: "Overwrite", not a generic "Yes".
98    pub yes_label: &'static str,
99    /// What Enter-on-No does: "No", unless declining does something of its own.
100    pub no_label: &'static str,
101    /// How far a long message is scrolled; the render clamps it.
102    pub scroll: usize,
103    /// What is being asked about.
104    pub(crate) asking: Option<Confirm>,
105}
106
107impl Default for ConfirmationModal {
108    fn default() -> Self {
109        Self {
110            active: false,
111            message: String::new(),
112            focus_yes: true,
113            yes_label: "Yes",
114            no_label: "No",
115            scroll: 0,
116            asking: None,
117        }
118    }
119}
120
121impl ConfirmationModal {
122    pub fn new() -> Self {
123        Self::default()
124    }
125
126    pub(crate) fn show(&mut self, message: String, asking: Confirm) {
127        self.active = true;
128        self.message = message;
129        self.focus_yes = true; // Default to Yes
130        self.yes_label = "Yes";
131        self.no_label = "No";
132        self.scroll = 0;
133        self.asking = Some(asking);
134    }
135
136    /// A choice between two things to do, each named; Esc does neither.
137    pub(crate) fn show_choice(
138        &mut self,
139        message: String,
140        yes_label: &'static str,
141        no_label: &'static str,
142        asking: Confirm,
143    ) {
144        self.show(message, asking);
145        self.yes_label = yes_label;
146        self.no_label = no_label;
147    }
148
149    /// A confirmation whose Yes destroys something: it starts on No, so a
150    /// reflexive second Enter declines, and the action is named on the choice.
151    pub(crate) fn show_destructive(
152        &mut self,
153        message: String,
154        yes_label: &'static str,
155        asking: Confirm,
156    ) {
157        self.show(message, asking);
158        self.focus_yes = false;
159        self.yes_label = yes_label;
160    }
161
162    /// Close the question, and hand back what it was asking about.
163    pub(crate) fn take(&mut self) -> Option<Confirm> {
164        let asking = self.asking.take();
165        self.hide();
166        asking
167    }
168
169    /// Whether the question up is the full-scan one Setup's Run asks.
170    pub fn asks_full_scan(&self) -> bool {
171        self.active && matches!(self.asking, Some(Confirm::QualityFullScan))
172    }
173
174    pub fn hide(&mut self) {
175        self.active = false;
176        self.message.clear();
177        self.focus_yes = true;
178        self.yes_label = "Yes";
179        self.no_label = "No";
180        self.scroll = 0;
181        self.asking = None;
182    }
183}