tablo_core/column/computed.rs
1//! [`ComputedColumn`]: a display-only column projecting any text off the row.
2
3use std::sync::Arc;
4
5use derive_where::derive_where;
6use topcoat::{context::Cx, view::BoxView};
7
8use super::{
9 Column, ColumnToggle, ColumnWidth, Includes,
10 cell::{TextCell, text_cell},
11};
12use crate::display::{Alignment, Color};
13
14/// A display-only column rendering any text from the row, with no query predicate.
15///
16/// ```rust
17/// # #[derive(Debug, Clone, toasty::Model, tablo_core::PanelModel)]
18/// # struct Post { #[key] #[auto] id: uuid::Uuid, body: String }
19/// tablo_core::ComputedColumn::new("Words", |_, p: &Post| {
20/// p.body.split_whitespace().count().to_string()
21/// });
22/// ```
23///
24/// A closure that reads a relation declares it with [`include_unscoped`](Self::include_unscoped),
25/// so the page loads it. One relation's record or count is a
26/// [`RelationColumn`](super::RelationColumn) or a [`CountColumn`](super::CountColumn), which
27/// declare their own and show only what the related resource shows the user.
28///
29/// It maps to no column, so it neither searches nor sorts.
30///
31/// A [`badge`](Self::badge) shows the text in an outline badge, with a dot in the
32/// [`color`](Self::color) the row gives it:
33///
34/// ```rust
35/// # #[derive(Debug, Clone, toasty::Model, tablo_core::PanelModel)]
36/// # struct Order { #[key] #[auto] id: uuid::Uuid, total_cents: i64 }
37/// use tablo_core::{Color, ComputedColumn, Reads, lens};
38///
39/// ComputedColumn::new("Size", |_, o: &Order| {
40/// if o.total_cents > 100_00 {
41/// "Large"
42/// } else {
43/// "Small"
44/// }
45/// .to_string()
46/// })
47/// .badge()
48/// .color(|_, o: &Order| (o.total_cents > 100_00).then_some(Color::Success))
49/// .reading(Reads::of(lens!(Order.total_cents)));
50/// ```
51#[derive_where(Clone, Debug)]
52pub struct ComputedColumn<M> {
53 name: String,
54 label: crate::Text,
55 #[derive_where(skip(Debug))]
56 project: super::ReadFn<M, String>,
57 width: ColumnWidth,
58 alignment: Alignment,
59 toggle: ColumnToggle,
60 /// Relations the projection reads.
61 includes: Includes<M>,
62 /// The fields the projection reads, when declared.
63 reads: Option<crate::lens::Reads<M>>,
64 badge: bool,
65 #[derive_where(skip(Debug))]
66 color: Option<super::ReadFn<M, Option<Color>>>,
67}
68
69impl<M> ComputedColumn<M>
70where
71 M: toasty::schema::Model,
72{
73 /// Declare a column headed `label` rendering `project(cx, row)`, named after the label: its
74 /// plain text in lowercase, or its message's id.
75 pub fn new(
76 label: impl Into<crate::Text>,
77 project: impl Fn(&Cx, &M) -> String + Send + Sync + 'static,
78 ) -> Self {
79 let label = label.into();
80 Self {
81 name: label.key(),
82 label,
83 project: Arc::new(project),
84 width: ColumnWidth::Auto,
85 alignment: Alignment::Start,
86 toggle: ColumnToggle::Fixed,
87 includes: Includes::new(),
88 reads: None,
89 badge: false,
90 color: None,
91 }
92 }
93
94 /// Show the text as a badge, with a dot in its [`color`](Self::color).
95 pub fn badge(mut self) -> Self {
96 self.badge = true;
97 self
98 }
99
100 /// Color the text by `color(cx, row)`: its badge's dot, or the text itself when it is no
101 /// badge. [`reading`](Self::reading) declares what it reads, beside the projection.
102 pub fn color(
103 mut self,
104 color: impl Fn(&Cx, &M) -> Option<Color> + Send + Sync + 'static,
105 ) -> Self {
106 self.color = Some(Arc::new(color));
107 self
108 }
109
110 /// Declare a relation the projection reads, so the table loads it. The projection reads
111 /// whatever the include loads, under no resource's query or policy, as an
112 /// [`unscoped`](super::RelationColumn::unscoped) relation column does: another tenant's record,
113 /// or one the user may not view, reaches it.
114 pub fn include_unscoped<T>(mut self, relation: impl Into<toasty::stmt::Include<M, T>>) -> Self {
115 self.includes = self.includes.with(relation);
116 self
117 }
118
119 /// Declare the fields the projection and its [`color`](Self::color) read, which a resource
120 /// restricting a field with [`field_access`](crate::ResourceDef::field_access) needs: the
121 /// column shows only to a user who may view every one.
122 pub fn reading(mut self, reads: crate::lens::Reads<M>) -> Self {
123 self.reads = Some(reads);
124 self
125 }
126
127 /// Declare this column's width.
128 pub fn width(mut self, width: ColumnWidth) -> Self {
129 self.width = width;
130 self
131 }
132
133 /// Align the header and the cells.
134 pub fn align(mut self, alignment: Alignment) -> Self {
135 self.alignment = alignment;
136 self
137 }
138
139 toggle_builders!(toggle);
140}
141
142impl<M> Column<M> for ComputedColumn<M>
143where
144 M: toasty::schema::Model + Send + Sync + 'static,
145{
146 fn name(&self) -> &str {
147 &self.name
148 }
149
150 fn label(&self) -> crate::Text {
151 self.label.clone()
152 }
153
154 fn text(&self, cx: &Cx, row: &M) -> String {
155 (self.project)(cx, row)
156 }
157
158 fn cell<'a>(&self, cx: &'a Cx, row: &M) -> BoxView<'a> {
159 let text = (self.project)(cx, row);
160 let color = self
161 .color
162 .as_ref()
163 .filter(|_| !text.trim().is_empty())
164 .and_then(|color| color(cx, row));
165 text_cell(
166 cx,
167 TextCell {
168 text,
169 color,
170 badge: self.badge,
171 ..TextCell::default()
172 },
173 )
174 }
175
176 fn column_width(&self) -> ColumnWidth {
177 self.width
178 }
179
180 fn toggle(&self) -> ColumnToggle {
181 self.toggle
182 }
183
184 fn alignment(&self) -> Alignment {
185 self.alignment
186 }
187
188 fn includes(&self) -> Includes<M> {
189 self.includes.clone()
190 }
191
192 fn reads(&self) -> Option<crate::lens::Reads<M>> {
193 self.reads.clone()
194 }
195}