| 1 | use gpui_kit::component::checkbox::Checkbox; |
| 2 | use gpui_kit::component::input::{Input, Textarea}; |
| 3 | use gpui_kit::component::radio::{Radio, RadioGroup}; |
| 4 | use gpui_kit::component::select::Select; |
| 5 | use gpui_kit::component::v_flex; |
| 6 | use gpui_kit::prelude::FluentBuilder as _; |
| 7 | use gpui_kit::*; |
| 8 | use teams_core::{CardInput, CardInputKind, ChoiceInput, ToggleInput}; |
| 9 | |
| 10 | use crate::app_state::AppHandle; |
| 11 | use crate::card_inputs::{InputField, uses_select}; |
| 12 | use crate::card_state::{CardScope, CardState}; |
| 13 | use crate::theme; |
| 14 | |
| 15 | const INPUT_GAP: f32 = 4.; |
| 16 | const LABEL_SIZE: f32 = 12.5; |
| 17 | const ERROR_SIZE: f32 = 12.; |
| 18 | const FIELD_RADIUS: f32 = 6.; |
| 19 | const CHOICE_GAP: f32 = 6.; |
| 20 | |
| 21 | pub fn input_view(input: &CardInput, scope: &CardScope, state: &CardState) -> AnyElement { |
| 22 | let key = scope.input_key(&input.id); |
| 23 | let error = state.input_error(&key).map(str::to_owned); |
| 24 | let label = input |
| 25 | .label |
| 26 | .as_deref() |
| 27 | .filter(|label| !label.trim().is_empty()); |
| 28 | v_flex() |
| 29 | .id(ElementId::Name(format!("{key}-input").into())) |
| 30 | .w_full() |
| 31 | .gap(px(INPUT_GAP)) |
| 32 | .on_click(|_, _, cx| cx.stop_propagation()) |
| 33 | .when_some(label, |column, label| { |
| 34 | column.child( |
| 35 | div() |
| 36 | .text_size(px(LABEL_SIZE)) |
| 37 | .font_weight(FontWeight::SEMIBOLD) |
| 38 | .text_color(theme::text_strong()) |
| 39 | .child(if input.required { |
| 40 | format!("{label} *") |
| 41 | } else { |
| 42 | label.to_owned() |
| 43 | }), |
| 44 | ) |
| 45 | }) |
| 46 | .child( |
| 47 | div() |
| 48 | .w_full() |
| 49 | .rounded(px(FIELD_RADIUS)) |
| 50 | .border_1() |
| 51 | .border_color(if error.is_some() { |
| 52 | theme::red() |
| 53 | } else { |
| 54 | transparent_black() |
| 55 | }) |
| 56 | .child(control_view(input, &key, scope, state)), |
| 57 | ) |
| 58 | .when_some(error, |column, error| { |
| 59 | column.child( |
| 60 | div() |
| 61 | .text_size(px(ERROR_SIZE)) |
| 62 | .text_color(theme::red_soft()) |
| 63 | .child(error), |
| 64 | ) |
| 65 | }) |
| 66 | .into_any_element() |
| 67 | } |
| 68 | |
| 69 | fn control_view(input: &CardInput, key: &str, scope: &CardScope, state: &CardState) -> AnyElement { |
| 70 | let current = state.input_value(key).to_owned(); |
| 71 | match &input.kind { |
| 72 | CardInputKind::Toggle(toggle) => toggle_view(toggle, key, ¤t, scope), |
| 73 | CardInputKind::Choice(choice) if !uses_select(choice) => { |
| 74 | choices_view(choice, key, ¤t, scope) |
| 75 | } |
| 76 | _ => match state.input_field(key) { |
| 77 | Some(InputField::Line(field)) => Input::new(field).into_any_element(), |
| 78 | Some(InputField::Area(field)) => Textarea::new(field).into_any_element(), |
| 79 | Some(InputField::Select(field)) => { |
| 80 | let placeholder = match &input.kind { |
| 81 | CardInputKind::Choice(choice) => choice.placeholder.clone(), |
| 82 | _ => None, |
| 83 | }; |
| 84 | Select::new(field) |
| 85 | .w_full() |
| 86 | .when_some(placeholder, |select, placeholder| { |
| 87 | select.placeholder(placeholder) |
| 88 | }) |
| 89 | .into_any_element() |
| 90 | } |
| 91 | None => div().into_any_element(), |
| 92 | }, |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | fn toggle_view(toggle: &ToggleInput, key: &str, current: &str, scope: &CardScope) -> AnyElement { |
| 97 | let (value_on, value_off) = (toggle.value_on.clone(), toggle.value_off.clone()); |
| 98 | let (scope, input_key) = (scope.clone(), key.to_owned()); |
| 99 | Checkbox::new(ElementId::Name(format!("{key}-toggle").into())) |
| 100 | .label(toggle.title.clone()) |
| 101 | .checked(toggle.is_on(current)) |
| 102 | .on_click(move |checked, _, cx| { |
| 103 | let value = if *checked { &value_on } else { &value_off }; |
| 104 | pick(&scope, &input_key, value.clone(), cx); |
| 105 | }) |
| 106 | .into_any_element() |
| 107 | } |
| 108 | |
| 109 | fn choices_view(choice: &ChoiceInput, key: &str, current: &str, scope: &CardScope) -> AnyElement { |
| 110 | let (scope, input_key) = (scope.clone(), key.to_owned()); |
| 111 | if choice.multi { |
| 112 | return v_flex() |
| 113 | .gap(px(CHOICE_GAP)) |
| 114 | .children(choice.choices.iter().enumerate().map(|(index, candidate)| { |
| 115 | let (choice, scope, input_key, current, value) = ( |
| 116 | choice.clone(), |
| 117 | scope.clone(), |
| 118 | input_key.clone(), |
| 119 | current.to_owned(), |
| 120 | candidate.value.clone(), |
| 121 | ); |
| 122 | Checkbox::new(ElementId::Name(format!("{key}-choice-{index}").into())) |
| 123 | .label(candidate.title.clone()) |
| 124 | .checked(choice.is_selected(¤t, &value)) |
| 125 | .on_click(move |_, _, cx| { |
| 126 | pick(&scope, &input_key, choice.toggled(¤t, &value), cx) |
| 127 | }) |
| 128 | })) |
| 129 | .into_any_element(); |
| 130 | } |
| 131 | let selected = choice |
| 132 | .choices |
| 133 | .iter() |
| 134 | .position(|candidate| candidate.value == current); |
| 135 | let values: Vec<String> = choice |
| 136 | .choices |
| 137 | .iter() |
| 138 | .map(|candidate| candidate.value.clone()) |
| 139 | .collect(); |
| 140 | RadioGroup::new(ElementId::Name(format!("{key}-choices").into())) |
| 141 | .selected_index(selected) |
| 142 | .children(choice.choices.iter().enumerate().map(|(index, candidate)| { |
| 143 | Radio::new(ElementId::Name(format!("{key}-choice-{index}").into())) |
| 144 | .label(candidate.title.clone()) |
| 145 | })) |
| 146 | .on_click(move |index, _, cx| { |
| 147 | if let Some(value) = values.get(*index) { |
| 148 | pick(&scope, &input_key, value.clone(), cx); |
| 149 | } |
| 150 | }) |
| 151 | .into_any_element() |
| 152 | } |
| 153 | |
| 154 | fn pick(scope: &CardScope, key: &str, value: String, cx: &mut App) { |
| 155 | cx.global::<AppHandle>() |
| 156 | .0 |
| 157 | .clone() |
| 158 | .update(cx, |state, cx| state.pick_card_input(scope, key, value, cx)); |
| 159 | } |