datui_lib/app/
feedback.rs1#[derive(Default)]
5pub struct ErrorModal {
6 pub active: bool,
7 pub message: String,
8 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 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
33pub struct Flash {
39 pub message: String,
40 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 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
67pub(crate) enum Confirm {
70 ReadAll,
72 OpenLink(String),
73 ClearRecents,
74 QualityFullScan,
76 HideExamples,
77 DeleteView(String),
78 ForgetPlace(std::path::PathBuf),
79 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,
89 Leave(crate::Leaving),
91}
92
93pub struct ConfirmationModal {
94 pub active: bool,
95 pub message: String,
96 pub focus_yes: bool, pub yes_label: &'static str,
99 pub no_label: &'static str,
101 pub scroll: usize,
103 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; self.yes_label = "Yes";
131 self.no_label = "No";
132 self.scroll = 0;
133 self.asking = Some(asking);
134 }
135
136 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 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 pub(crate) fn take(&mut self) -> Option<Confirm> {
164 let asking = self.asking.take();
165 self.hide();
166 asking
167 }
168
169 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}