1use std::{ops::Deref, sync::Arc};
4
5use topcoat::{context::Cx, icon::IconData};
6
7use super::TableSignals;
8use crate::{
9 Color,
10 resource::{InputSpec, Trashed},
11 table::{BoxColumn, ColumnChoice, Table, TableState},
12};
13
14pub(crate) type RowPolicy<M> = Arc<dyn Fn(&M) -> RowActions + Send + Sync>;
16
17pub(crate) type DisabledReason<M> = Arc<dyn Fn(&M) -> Option<String> + Send + Sync>;
19
20pub(crate) type RecordFace<M> = Arc<dyn Fn(&M) -> (String, Option<IconData>) + Send + Sync>;
22
23#[derive(Debug, Clone, Copy, PartialEq, Eq)]
27pub(crate) struct RowActions {
28 pub view: bool,
30 pub edit: bool,
32 pub delete: bool,
34}
35
36impl RowActions {
37 pub const ALL: Self = Self {
39 view: true,
40 edit: true,
41 delete: true,
42 };
43}
44
45#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
47pub(crate) struct TableChrome {
48 pub(crate) delete: bool,
50 pub(crate) edit: bool,
52 pub(crate) view: bool,
54 pub(crate) actions: bool,
56}
57
58pub(crate) struct TableAction<M> {
60 pub(crate) name: &'static str,
61 pub(crate) label: String,
62 pub(crate) row: bool,
63 pub(crate) bulk: bool,
64 pub(crate) confirm: bool,
66 pub(crate) prompt: String,
68 pub(crate) bulk_prompt: String,
70 pub(crate) icon: Option<IconData>,
72 pub(crate) record_face: RecordFace<M>,
74 pub(crate) tooltip: Option<String>,
76 pub(crate) color: Color,
78 pub(crate) input: Option<InputSpec>,
80 pub(crate) trashed: Trashed,
82 pub(crate) allowed: Arc<dyn Fn(&M) -> bool + Send + Sync>,
83 pub(crate) disabled: DisabledReason<M>,
85}
86
87pub(crate) struct CellEdits<M> {
90 pub(crate) changes: Arc<dyn Fn(&str) -> bool + Send + Sync>,
93 pub(crate) version: fn(&M) -> Option<u64>,
95}
96
97pub(crate) struct Wiring<M> {
103 row_policy: Option<RowPolicy<M>>,
104 delete_prefix: Option<String>,
105 edit_prefix: Option<String>,
106 cell_edits: Option<CellEdits<M>>,
108 view_prefix: Option<String>,
109 bulk_delete: bool,
110 custom_actions: Vec<TableAction<M>>,
112 actions_prefix: Option<String>,
113 return_to: Option<String>,
115 select_matching: bool,
118 prefix: Option<String>,
120 choice: ColumnChoice,
122 columns_url: Option<String>,
124}
125
126impl<M> Default for Wiring<M> {
127 fn default() -> Self {
128 Self {
129 row_policy: None,
130 delete_prefix: None,
131 edit_prefix: None,
132 cell_edits: None,
133 view_prefix: None,
134 bulk_delete: false,
135 custom_actions: Vec::new(),
136 actions_prefix: None,
137 return_to: None,
138 select_matching: false,
139 prefix: None,
140 choice: ColumnChoice::default(),
141 columns_url: None,
142 }
143 }
144}
145
146pub struct WiredTable<M> {
152 table: Arc<Table<M>>,
153 pub(super) wiring: Wiring<M>,
154}
155
156impl<M> Deref for WiredTable<M> {
157 type Target = Table<M>;
158
159 fn deref(&self) -> &Table<M> {
160 &self.table
161 }
162}
163
164impl<M> std::fmt::Debug for WiredTable<M> {
165 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
166 let wiring = &self.wiring;
167 f.debug_struct("WiredTable")
168 .field("table", &self.table)
169 .field("row_policy", &wiring.row_policy.is_some())
170 .field("delete_prefix", &wiring.delete_prefix)
171 .field("edit_prefix", &wiring.edit_prefix)
172 .field("cell_edits", &wiring.cell_edits.is_some())
173 .field("view_prefix", &wiring.view_prefix)
174 .field("bulk_delete", &wiring.bulk_delete)
175 .field(
176 "custom_actions",
177 &wiring
178 .custom_actions
179 .iter()
180 .map(|a| a.name)
181 .collect::<Vec<_>>(),
182 )
183 .field("return_to", &wiring.return_to)
184 .field("prefix", &wiring.prefix)
185 .field("choice", &wiring.choice)
186 .field("columns_url", &wiring.columns_url)
187 .finish()
188 }
189}
190
191impl<M> WiredTable<M> {
192 pub(crate) fn new(table: Arc<Table<M>>) -> Self {
194 Self {
195 table,
196 wiring: Wiring::default(),
197 }
198 }
199
200 pub(crate) fn row_actions(
202 mut self,
203 policy: impl Fn(&M) -> RowActions + Send + Sync + 'static,
204 ) -> Self {
205 self.wiring.row_policy = Some(Arc::new(policy));
206 self
207 }
208
209 pub(crate) fn with_delete(mut self, prefix: String) -> Self {
211 if self.is_addressable() {
212 self.wiring.delete_prefix = Some(prefix);
213 }
214 self
215 }
216
217 pub(crate) fn with_edit(mut self, prefix: String) -> Self {
219 if self.is_addressable() {
220 self.wiring.edit_prefix = Some(prefix);
221 }
222 self
223 }
224
225 pub(crate) fn editing_cells(
229 mut self,
230 changes: impl Fn(&str) -> bool + Send + Sync + 'static,
231 version: fn(&M) -> Option<u64>,
232 ) -> Self {
233 if self.is_addressable() {
234 self.wiring.cell_edits = Some(CellEdits {
235 changes: Arc::new(changes),
236 version,
237 });
238 }
239 self
240 }
241
242 pub(crate) fn with_view(mut self, prefix: String) -> Self {
244 if self.is_addressable() {
245 self.wiring.view_prefix = Some(prefix);
246 }
247 self
248 }
249
250 pub(crate) fn with_bulk_delete(mut self, enabled: bool) -> Self {
252 self.wiring.bulk_delete = enabled;
253 self
254 }
255
256 pub(crate) fn with_custom_actions(
258 mut self,
259 prefix: String,
260 actions: Vec<TableAction<M>>,
261 ) -> Self {
262 if self.is_addressable() {
263 self.wiring.actions_prefix = Some(prefix);
264 self.wiring.custom_actions = actions;
265 }
266 self
267 }
268
269 pub(crate) fn with_column_choice(mut self, choice: ColumnChoice, url: Option<String>) -> Self {
272 let shows_any = self
273 .declared_columns()
274 .iter()
275 .any(|col| choice.shows(col.name(), col.toggle()));
276 if shows_any {
277 self.wiring.choice = choice;
278 }
279 self.wiring.columns_url = url;
280 self
281 }
282
283 pub(crate) fn selecting_matching(mut self) -> Self {
287 self.wiring.select_matching = true;
288 self
289 }
290
291 pub(crate) fn returning_to(mut self, url: String) -> Self {
293 self.wiring.return_to = Some(url);
294 self
295 }
296
297 #[must_use]
300 pub fn prefixed(mut self, prefix: impl Into<String>) -> Self {
301 self.wiring.prefix = Some(prefix.into());
302 self
303 }
304
305 pub(crate) fn taking(mut self, shown: Trashed) -> Self {
308 let overlaps = |taken: Trashed| {
310 [false, true]
311 .into_iter()
312 .any(|trashed| shown.admits(trashed) && taken.admits(trashed))
313 };
314 self.wiring
315 .custom_actions
316 .retain(|action| overlaps(action.trashed));
317 if !overlaps(Trashed::Without) {
318 self.wiring.bulk_delete = false;
319 }
320 self
321 }
322
323 pub(crate) fn browser_state(&self, cx: &Cx) -> (TableSignals, TableState) {
326 let prefix = self.wiring.prefix.as_deref();
327 let signals = TableSignals::new(cx, prefix);
328 let state = self.normalize_state(&signals.state(prefix));
329 (signals, state)
330 }
331
332 pub(crate) fn actions_for(&self, record: &M) -> RowActions {
334 self.wiring
335 .row_policy
336 .as_ref()
337 .map_or(RowActions::ALL, |policy| policy(record))
338 }
339
340 pub(super) fn shows(&self, col: &BoxColumn<M>) -> bool {
342 self.wiring.choice.shows(col.name(), col.toggle())
343 }
344
345 pub(super) fn visible_columns(&self) -> impl Iterator<Item = &BoxColumn<M>> {
347 self.declared_columns().iter().filter(|col| self.shows(col))
348 }
349
350 pub(super) fn prefix(&self) -> Option<&str> {
352 self.wiring.prefix.as_deref()
353 }
354
355 pub(super) fn columns_url(&self) -> Option<&str> {
357 self.wiring.columns_url.as_deref()
358 }
359
360 pub(super) fn return_to(&self) -> Option<&str> {
361 self.wiring.return_to.as_deref()
362 }
363
364 pub(super) fn select_matching(&self) -> bool {
365 self.wiring.select_matching && self.wiring.prefix.is_none()
366 }
367
368 pub(super) fn delete_prefix(&self) -> Option<&str> {
369 self.wiring.delete_prefix.as_deref()
370 }
371
372 pub(super) fn edit_prefix(&self) -> Option<&str> {
373 self.wiring.edit_prefix.as_deref()
374 }
375
376 pub(super) fn cell_edits(&self) -> Option<&CellEdits<M>> {
378 self.wiring
379 .cell_edits
380 .as_ref()
381 .filter(|_| self.wiring.edit_prefix.is_some())
382 }
383
384 pub(super) fn view_prefix(&self) -> Option<&str> {
385 self.wiring.view_prefix.as_deref()
386 }
387
388 pub(super) fn actions_prefix(&self) -> Option<&str> {
389 self.wiring.actions_prefix.as_deref()
390 }
391
392 pub(super) fn row_custom_actions(&self) -> impl Iterator<Item = &TableAction<M>> {
393 self.wiring
394 .custom_actions
395 .iter()
396 .filter(|a| a.row && self.wiring.actions_prefix.is_some())
397 }
398
399 pub(super) fn bulk_custom_actions(&self) -> impl Iterator<Item = &TableAction<M>> {
400 self.wiring
401 .custom_actions
402 .iter()
403 .filter(|a| a.bulk && self.wiring.actions_prefix.is_some())
404 }
405
406 pub(super) fn bulk_delete_enabled(&self) -> bool {
407 self.wiring.bulk_delete && self.wiring.delete_prefix.is_some()
408 }
409}
410
411#[cfg(test)]
412impl<M> Table<M> {
413 pub(crate) fn wired(self) -> WiredTable<M> {
415 WiredTable::new(Arc::new(self))
416 }
417}