Skip to main content

tablo_core/resource/header_action/
def.rs

1//! [`HeaderActionDef`]: how a [`HeaderAction`](super::HeaderAction)'s button reads and who it
2//! runs for.
3
4use std::sync::Arc;
5
6use topcoat::{context::Cx, icon::IconData};
7
8use crate::{Color, Text};
9
10/// A check on the request alone.
11pub(crate) type RequestCheck = Arc<dyn Fn(&Cx) -> bool + Send + Sync>;
12
13/// What a [`HeaderAction`](super::HeaderAction) declares besides its name, input and body: how
14/// its button reads, whether it asks first, and who it runs for beyond the policy or the page's
15/// access. Every setting has a default, so [`HeaderActionDef::new`] alone is a gray button
16/// labelled after the action's `NAME`.
17///
18/// ```rust
19/// # use tablo_core::{Color, HeaderActionDef};
20/// let def = HeaderActionDef::new()
21///     .icon(tablo_ui::icons::CHECK)
22///     .color(Color::Warning)
23///     .confirm();
24/// # let _ = def;
25/// ```
26pub struct HeaderActionDef {
27    pub(super) confirm: bool,
28    pub(super) confirmation: Option<Text>,
29    pub(super) icon: Option<IconData>,
30    pub(super) color: Color,
31    pub(super) label: Option<Text>,
32    pub(super) can_run: Option<RequestCheck>,
33    pub(super) success: Option<Text>,
34}
35
36impl Default for HeaderActionDef {
37    fn default() -> Self {
38        Self {
39            confirm: false,
40            confirmation: None,
41            icon: None,
42            color: Color::Gray,
43            label: None,
44            can_run: None,
45            success: None,
46        }
47    }
48}
49
50impl std::fmt::Debug for HeaderActionDef {
51    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
52        f.debug_struct("HeaderActionDef")
53            .field("confirm", &self.confirm)
54            .field("label", &self.label)
55            .finish_non_exhaustive()
56    }
57}
58
59impl HeaderActionDef {
60    /// Every default: a gray button labelled after the action's `NAME`, running without
61    /// confirmation for whoever the policy or the page's access admits.
62    #[must_use]
63    pub fn new() -> Self {
64        Self::default()
65    }
66
67    /// Asks first, through a confirmation dialog with destructive wording.
68    ///
69    /// An action with input confirms in its input dialog, or on its input page, instead. An
70    /// unconfirmed POST that would write answers 400.
71    #[must_use]
72    pub fn confirm(mut self) -> Self {
73        self.confirm = true;
74        self
75    }
76
77    /// Asks first, as [`confirm`](Self::confirm) does, with `question` in place of the message
78    /// `tablo-confirm-run`, "Run this action?". An action with input confirms in its input
79    /// dialog instead, which its label titles.
80    #[must_use]
81    pub fn confirmation(mut self, question: impl Into<Text>) -> Self {
82        self.confirm = true;
83        self.confirmation = Some(question.into());
84        self
85    }
86
87    /// The icon before the button's label. Defaults to none.
88    #[must_use]
89    pub fn icon(mut self, icon: IconData) -> Self {
90        self.icon = Some(icon);
91        self
92    }
93
94    /// The colour the button is tinted in, as Delete's is in [`Color::Danger`]. Defaults to
95    /// [`Color::Gray`], the untinted button.
96    ///
97    /// It changes how the button looks, not what it asks: [`confirm`](Self::confirm) asks first.
98    #[must_use]
99    pub fn color(mut self, color: Color) -> Self {
100        self.color = color;
101        self
102    }
103
104    /// The button text. Defaults to the message `action-{NAME}` of the panel's catalogs, else
105    /// `NAME` in sentence case: `"recount-tags"` reads "Recount tags".
106    #[must_use]
107    pub fn label(mut self, label: impl Into<Text>) -> Self {
108        self.label = Some(label.into());
109        self
110    }
111
112    /// Whether the request may run the action, beyond the policy or the page's access: a
113    /// feature flag, a role the policy does not model. Defaults to every request.
114    #[must_use]
115    pub fn can_run(mut self, check: impl Fn(&Cx) -> bool + Send + Sync + 'static) -> Self {
116        self.can_run = Some(Arc::new(check));
117        self
118    }
119
120    /// The success notification after a commit. Defaults to the label: `"Recount tags: done"`.
121    #[must_use]
122    pub fn success(mut self, message: impl Into<Text>) -> Self {
123        self.success = Some(message.into());
124        self
125    }
126}