BlueGit

ict rusty-teams

Files

rusty-teams / crates / app / src / views / card_input.rs

card_input.rs

card_input.rs 13.7 KiB Download Raw
1use gpui_kit::assets::IconName;
2use gpui_kit::component::checkbox::Checkbox;
3use gpui_kit::component::date_picker::DatePicker;
4use gpui_kit::component::input::{Input, Textarea};
5use gpui_kit::component::radio::{Radio, RadioGroup};
6use gpui_kit::component::select::Select;
7use gpui_kit::component::time_field::TimeField;
8use gpui_kit::component::{h_flex, v_flex};
9use gpui_kit::prelude::FluentBuilder as _;
10use gpui_kit::*;
11use teams_core::{
12 CardActionKind, CardInput, CardInputKind, ChoiceInput, DATE_PLACEHOLDER, InlineAction,
13 RatingColor, RatingDisplay, RatingInput, RatingSize, RatingStyle, ToggleInput,
14};
15
16use super::widgets::{icon, symbol};
17use crate::app_state::AppHandle;
18use crate::card_inputs::{InputField, inline_action_key, uses_select};
19use crate::card_state::{ActionPhase, CardScope, CardState};
20use crate::theme;
21
22const INPUT_GAP: f32 = 4.;
23const LABEL_SIZE: f32 = 12.5;
24const ERROR_SIZE: f32 = 12.;
25const FIELD_RADIUS: f32 = 6.;
26const CHOICE_GAP: f32 = 6.;
27const STAR_GAP: f32 = 2.;
28const STAR_MEDIUM: f32 = 20.;
29const STAR_LARGE: f32 = 28.;
30const RATING_TEXT_SIZE: f32 = 13.;
31const RATING_TEXT_GAP: f32 = 6.;
32const HALF: f64 = 0.5;
33const INLINE_ICON_SIZE: f32 = 16.;
34const INLINE_BUTTON_PADDING: f32 = 4.;
35const INLINE_BUSY_OPACITY: f32 = 0.5;
36
37pub fn input_view(
38 input: &CardInput,
39 scope: &CardScope,
40 state: &CardState,
41 inputs: &[CardInput],
42) -> AnyElement {
43 let key = scope.input_key(&input.id);
44 let error = state.input_error(&key).map(str::to_owned);
45 let label = input
46 .label
47 .as_deref()
48 .filter(|label| !label.trim().is_empty());
49 v_flex()
50 .id(ElementId::Name(format!("{key}-input").into()))
51 .w_full()
52 .gap(px(INPUT_GAP))
53 .on_click(|_, _, cx| cx.stop_propagation())
54 .when_some(label, |column, label| {
55 column.child(
56 div()
57 .text_size(px(LABEL_SIZE))
58 .font_weight(FontWeight::SEMIBOLD)
59 .text_color(theme::text_strong())
60 .child(if input.required {
61 format!("{label} *")
62 } else {
63 label.to_owned()
64 }),
65 )
66 })
67 .child(
68 div()
69 .w_full()
70 .rounded(px(FIELD_RADIUS))
71 .border_1()
72 .border_color(if error.is_some() {
73 theme::red()
74 } else {
75 transparent_black()
76 })
77 .child(control_view(input, &key, scope, state, inputs)),
78 )
79 .when_some(error, |column, error| {
80 column.child(
81 div()
82 .text_size(px(ERROR_SIZE))
83 .text_color(theme::red_soft())
84 .child(error),
85 )
86 })
87 .into_any_element()
88}
89
90fn control_view(
91 input: &CardInput,
92 key: &str,
93 scope: &CardScope,
94 state: &CardState,
95 inputs: &[CardInput],
96) -> AnyElement {
97 let current = state.input_value(key).to_owned();
98 match &input.kind {
99 CardInputKind::Toggle(toggle) => toggle_view(toggle, key, &current, scope),
100 CardInputKind::Rating(rating) => rating_input_view(rating, key, &current, scope),
101 CardInputKind::Choice(choice) if !uses_select(choice) => {
102 choices_view(choice, key, &current, scope)
103 }
104 _ => match state.input_field(key) {
105 Some(InputField::Line(field)) => {
106 let inline = match &input.kind {
107 CardInputKind::Text(text) => text.inline_action.as_ref(),
108 _ => None,
109 };
110 Input::new(field)
111 .when_some(inline, |line, inline| {
112 line.suffix(inline_action_view(inline, key, scope, state, inputs))
113 })
114 .into_any_element()
115 }
116 Some(InputField::Area(field)) => Textarea::new(field).into_any_element(),
117 Some(InputField::Date(field)) => DatePicker::new(field)
118 .placeholder(DATE_PLACEHOLDER)
119 .cleanable(!input.required)
120 .into_any_element(),
121 Some(InputField::Time(field)) => TimeField::new(field)
122 .invalid(state.input_error(key).is_some())
123 .into_any_element(),
124 Some(InputField::Select(field)) => {
125 let placeholder = match &input.kind {
126 CardInputKind::Choice(choice) => choice.placeholder.clone(),
127 _ => None,
128 };
129 Select::new(field)
130 .w_full()
131 .when_some(placeholder, |select, placeholder| {
132 select.placeholder(placeholder)
133 })
134 .into_any_element()
135 }
136 None => div().into_any_element(),
137 },
138 }
139}
140
141fn inline_action_view(
142 inline: &InlineAction,
143 key: &str,
144 scope: &CardScope,
145 state: &CardState,
146 inputs: &[CardInput],
147) -> AnyElement {
148 let action_key = inline_action_key(key);
149 let phase = state.phase(&action_key).cloned();
150 let glyph = match (&phase, &inline.icon_url) {
151 (Some(ActionPhase::Done), _) => {
152 symbol("done", INLINE_ICON_SIZE, theme::green()).into_any_element()
153 }
154 (_, Some(url)) if url.starts_with("https://") => img(url.clone())
155 .size(px(INLINE_ICON_SIZE))
156 .into_any_element(),
157 _ => {
158 let name = match inline.action.kind {
159 CardActionKind::OpenUrl(_) => IconName::ExternalLink,
160 _ => IconName::SendHorizontal,
161 };
162 icon(name, INLINE_ICON_SIZE, theme::accent_text()).into_any_element()
163 }
164 };
165 let (scope, action, inputs) = (scope.clone(), inline.action.clone(), inputs.to_vec());
166 div()
167 .id(ElementId::Name(action_key.clone().into()))
168 .p(px(INLINE_BUTTON_PADDING))
169 .rounded(px(FIELD_RADIUS))
170 .cursor_pointer()
171 .hover(|button| button.bg(theme::row_hover()))
172 .when(phase == Some(ActionPhase::Busy), |button| {
173 button.opacity(INLINE_BUSY_OPACITY)
174 })
175 .child(glyph)
176 .on_click(move |_, _, cx| {
177 cx.stop_propagation();
178 let (scope, action, action_key, inputs) = (
179 scope.clone(),
180 action.clone(),
181 action_key.clone(),
182 inputs.clone(),
183 );
184 cx.global::<AppHandle>().0.clone().update(cx, |state, cx| {
185 state.run_inline_action(&scope, action_key, &action, &inputs, cx)
186 });
187 })
188 .into_any_element()
189}
190
191fn star_pixels(size: RatingSize) -> f32 {
192 match size {
193 RatingSize::Medium => STAR_MEDIUM,
194 RatingSize::Large => STAR_LARGE,
195 }
196}
197
198fn star_color(color: RatingColor) -> Hsla {
199 match color {
200 RatingColor::Neutral => theme::text_strong(),
201 RatingColor::Marigold => theme::amber(),
202 }
203}
204
205fn star_icon(rating: f64, position: u32, pixels: f32, color: Hsla) -> AnyElement {
206 let position = f64::from(position);
207 if rating >= position {
208 icon(IconName::StarFill, pixels, color).into_any_element()
209 } else if rating >= position - HALF {
210 div()
211 .relative()
212 .size(px(pixels))
213 .child(icon(IconName::Star, pixels, theme::text_muted()))
214 .child(
215 div()
216 .absolute()
217 .top_0()
218 .left_0()
219 .w(px(pixels / 2.))
220 .h(px(pixels))
221 .overflow_hidden()
222 .child(icon(IconName::StarFill, pixels, color)),
223 )
224 .into_any_element()
225 } else {
226 icon(IconName::Star, pixels, theme::text_muted()).into_any_element()
227 }
228}
229
230fn rating_input_view(
231 rating: &RatingInput,
232 key: &str,
233 current: &str,
234 scope: &CardScope,
235) -> AnyElement {
236 let value = rating.rating(current);
237 let pixels = star_pixels(rating.size);
238 let color = star_color(rating.color);
239 h_flex()
240 .gap(px(STAR_GAP))
241 .children((1..=rating.max).map(|position| {
242 let zones: Vec<f64> = if rating.allow_half {
243 vec![f64::from(position) - HALF, f64::from(position)]
244 } else {
245 vec![f64::from(position)]
246 };
247 div()
248 .relative()
249 .flex_none()
250 .size(px(pixels))
251 .child(star_icon(value, position, pixels, color))
252 .child(
253 h_flex()
254 .absolute()
255 .inset_0()
256 .children(
257 zones
258 .into_iter()
259 .enumerate()
260 .map(|(zone_index, zone_value)| {
261 let (scope, input_key, rating) =
262 (scope.clone(), key.to_owned(), rating.clone());
263 div()
264 .id(ElementId::Name(
265 format!("{key}-star-{position}-{zone_index}").into(),
266 ))
267 .flex_1()
268 .h_full()
269 .cursor_pointer()
270 .on_click(move |_, _, cx| {
271 cx.stop_propagation();
272 pick(
273 &scope,
274 &input_key,
275 rating.value_text(zone_value),
276 cx,
277 );
278 })
279 }),
280 ),
281 )
282 }))
283 .into_any_element()
284}
285
286pub fn rating_display_view(display: &RatingDisplay) -> AnyElement {
287 let pixels = star_pixels(display.size);
288 let color = star_color(RatingColor::Marigold);
289 let count = display.count.map(|count| format!("({count})"));
290 let row = match display.style {
291 RatingStyle::Compact => h_flex()
292 .gap(px(RATING_TEXT_GAP))
293 .child(star_icon(display.value, 1, pixels, color))
294 .child(format!("{:.1}", display.value)),
295 RatingStyle::Default => h_flex().gap(px(STAR_GAP)).children(
296 (1..=display.max).map(|position| star_icon(display.value, position, pixels, color)),
297 ),
298 };
299 row.items_center()
300 .text_size(px(RATING_TEXT_SIZE))
301 .text_color(theme::text_strong())
302 .when_some(count, |row, count| {
303 row.child(
304 div()
305 .ml(px(RATING_TEXT_GAP - STAR_GAP))
306 .text_color(theme::text_muted())
307 .child(count),
308 )
309 })
310 .into_any_element()
311}
312
313fn toggle_view(toggle: &ToggleInput, key: &str, current: &str, scope: &CardScope) -> AnyElement {
314 let (value_on, value_off) = (toggle.value_on.clone(), toggle.value_off.clone());
315 let (scope, input_key) = (scope.clone(), key.to_owned());
316 Checkbox::new(ElementId::Name(format!("{key}-toggle").into()))
317 .label(toggle.title.clone())
318 .checked(toggle.is_on(current))
319 .when(!toggle.wrap, |checkbox| {
320 checkbox.whitespace_nowrap().text_ellipsis()
321 })
322 .on_click(move |checked, _, cx| {
323 let value = if *checked { &value_on } else { &value_off };
324 pick(&scope, &input_key, value.clone(), cx);
325 })
326 .into_any_element()
327}
328
329fn choices_view(choice: &ChoiceInput, key: &str, current: &str, scope: &CardScope) -> AnyElement {
330 let (scope, input_key) = (scope.clone(), key.to_owned());
331 if choice.multi {
332 return v_flex()
333 .gap(px(CHOICE_GAP))
334 .children(choice.choices.iter().enumerate().map(|(index, candidate)| {
335 let (choice, scope, input_key, current, value) = (
336 choice.clone(),
337 scope.clone(),
338 input_key.clone(),
339 current.to_owned(),
340 candidate.value.clone(),
341 );
342 Checkbox::new(ElementId::Name(format!("{key}-choice-{index}").into()))
343 .label(candidate.title.clone())
344 .checked(choice.is_selected(&current, &value))
345 .on_click(move |_, _, cx| {
346 pick(&scope, &input_key, choice.toggled(&current, &value), cx)
347 })
348 }))
349 .into_any_element();
350 }
351 let selected = choice
352 .choices
353 .iter()
354 .position(|candidate| candidate.value == current);
355 let values: Vec<String> = choice
356 .choices
357 .iter()
358 .map(|candidate| candidate.value.clone())
359 .collect();
360 RadioGroup::new(ElementId::Name(format!("{key}-choices").into()))
361 .selected_index(selected)
362 .children(choice.choices.iter().enumerate().map(|(index, candidate)| {
363 Radio::new(ElementId::Name(format!("{key}-choice-{index}").into()))
364 .label(candidate.title.clone())
365 }))
366 .on_click(move |index, _, cx| {
367 if let Some(value) = values.get(*index) {
368 pick(&scope, &input_key, value.clone(), cx);
369 }
370 })
371 .into_any_element()
372}
373
374fn pick(scope: &CardScope, key: &str, value: String, cx: &mut App) {
375 cx.global::<AppHandle>()
376 .0
377 .clone()
378 .update(cx, |state, cx| state.pick_card_input(scope, key, value, cx));
379}