| 1 | use std::rc::Rc; |
| 2 | |
| 3 | use gpui::{ |
| 4 | AnyElement, App, ElementId, Entity, InteractiveElement, IntoElement, ParentElement, RenderOnce, |
| 5 | Role, SharedString, StatefulInteractiveElement, StyleRefinement, Styled, Window, div, |
| 6 | prelude::FluentBuilder as _, svg, |
| 7 | }; |
| 8 | use gpui_base::RadioGroup; |
| 9 | use rust_i18n::t; |
| 10 | |
| 11 | use crate::{ |
| 12 | ActiveTheme as _, IconName, Sizable, Size, StyledExt as _, ThemeStyled as _, |
| 13 | button::{Button, ButtonVariants as _}, |
| 14 | icon::IconNamed as _, |
| 15 | input::Input, |
| 16 | kbd::Kbd, |
| 17 | }; |
| 18 | |
| 19 | use gpui_base::questionnaire::{ |
| 20 | QuestionnaireChoiceControl, QuestionnaireChoiceState, QuestionnaireState, |
| 21 | QuestionnaireValidationError, |
| 22 | }; |
| 23 | |
| 24 | type ChoiceRenderer = |
| 25 | Rc<dyn Fn(&QuestionnaireChoiceState, &mut Window, &mut App) -> AnyElement + 'static>; |
| 26 | |
| 27 | /// Where a questionnaire's scale lives between the root and its parts. |
| 28 | /// |
| 29 | /// GPUI has no style cascade, and `with_rem_size` resolves during layout, which |
| 30 | /// a `RenderOnce` part cannot reach. So the root records the scale it was given |
| 31 | /// under its state's id, and every part of that questionnaire reads it back. A |
| 32 | /// part still accepts its own `with_size`, which wins. |
| 33 | /// |
| 34 | /// A root renders before its children, so the entry is in place by the time a |
| 35 | /// part looks for it; a part rendered outside a root falls back to `Medium`. |
| 36 | #[derive(Default)] |
| 37 | struct QuestionnaireSizes(std::collections::HashMap<gpui::EntityId, Size>); |
| 38 | |
| 39 | impl gpui::Global for QuestionnaireSizes {} |
| 40 | |
| 41 | /// Bounded so an application that builds and drops many questionnaires cannot |
| 42 | /// grow the table without end. Every root rewrites its entry on the next frame, |
| 43 | /// so clearing it costs at most one frame at the default scale. |
| 44 | const MAX_TRACKED_QUESTIONNAIRES: usize = 128; |
| 45 | |
| 46 | fn publish_size(state: &Entity<QuestionnaireState>, size: Size, cx: &mut App) { |
| 47 | let id = state.entity_id(); |
| 48 | let sizes = cx.default_global::<QuestionnaireSizes>(); |
| 49 | if sizes.0.len() >= MAX_TRACKED_QUESTIONNAIRES && !sizes.0.contains_key(&id) { |
| 50 | sizes.0.clear(); |
| 51 | } |
| 52 | sizes.0.insert(id, size); |
| 53 | } |
| 54 | |
| 55 | fn resolve_size(own: Option<Size>, state: &Entity<QuestionnaireState>, cx: &App) -> Size { |
| 56 | own.unwrap_or_else(|| { |
| 57 | cx.try_global::<QuestionnaireSizes>() |
| 58 | .and_then(|sizes| sizes.0.get(&state.entity_id()).copied()) |
| 59 | .unwrap_or(Size::Medium) |
| 60 | }) |
| 61 | } |
| 62 | |
| 63 | /// The questionnaire skin's geometry, following the ReUI `base-nova` |
| 64 | /// questionnaire at `Medium`. Every number comes from the semantic spacing and |
| 65 | /// radius tokens; the size picks which of them apply. |
| 66 | #[derive(Clone, Copy)] |
| 67 | struct QuestionnaireMetrics { |
| 68 | root_gap: gpui::Pixels, |
| 69 | item_gap: gpui::Pixels, |
| 70 | choices_gap: gpui::Pixels, |
| 71 | choice_gap: gpui::Pixels, |
| 72 | content_gap: gpui::Pixels, |
| 73 | choice_padding_x: gpui::Pixels, |
| 74 | choice_padding_y: gpui::Pixels, |
| 75 | choice_min_height: gpui::Pixels, |
| 76 | choice_radius: gpui::Pixels, |
| 77 | indicator_size: gpui::Pixels, |
| 78 | indicator_mark_size: gpui::Pixels, |
| 79 | indicator_check_size: gpui::Pixels, |
| 80 | shortcut_size: gpui::Pixels, |
| 81 | shortcut_text_size: gpui::Pixels, |
| 82 | shortcut_radius: gpui::Pixels, |
| 83 | } |
| 84 | |
| 85 | impl QuestionnaireMetrics { |
| 86 | fn new(size: Size, cx: &App) -> Self { |
| 87 | let tokens = cx.theme().semantic_tokens(); |
| 88 | let spacing = tokens.spacing; |
| 89 | let radius = tokens.radius; |
| 90 | match size { |
| 91 | Size::XSmall => Self { |
| 92 | root_gap: spacing.sm, |
| 93 | item_gap: spacing.sm, |
| 94 | choices_gap: spacing.xs, |
| 95 | choice_gap: spacing.xs + spacing.xxs, |
| 96 | content_gap: spacing.xxs, |
| 97 | choice_padding_x: spacing.sm, |
| 98 | choice_padding_y: spacing.xs, |
| 99 | choice_min_height: spacing.xl + spacing.xs, |
| 100 | choice_radius: radius.md, |
| 101 | indicator_size: spacing.md, |
| 102 | indicator_mark_size: spacing.xs + spacing.xxs * 0.5, |
| 103 | indicator_check_size: spacing.sm + spacing.xxs, |
| 104 | shortcut_size: spacing.lg, |
| 105 | shortcut_text_size: spacing.sm, |
| 106 | shortcut_radius: radius.sm, |
| 107 | }, |
| 108 | Size::Small => Self { |
| 109 | root_gap: spacing.md, |
| 110 | item_gap: spacing.md, |
| 111 | choices_gap: spacing.xs + spacing.xxs, |
| 112 | choice_gap: spacing.sm, |
| 113 | content_gap: spacing.xxs, |
| 114 | choice_padding_x: spacing.sm + spacing.xxs, |
| 115 | choice_padding_y: spacing.sm, |
| 116 | choice_min_height: spacing.xxl + spacing.xs, |
| 117 | choice_radius: radius.lg, |
| 118 | indicator_size: spacing.md + spacing.xxs, |
| 119 | indicator_mark_size: spacing.xs + spacing.xxs, |
| 120 | indicator_check_size: spacing.md, |
| 121 | shortcut_size: spacing.lg + spacing.xxs, |
| 122 | shortcut_text_size: spacing.sm + spacing.xxs * 0.5, |
| 123 | shortcut_radius: radius.md, |
| 124 | }, |
| 125 | Size::Large => Self { |
| 126 | root_gap: spacing.xl, |
| 127 | item_gap: spacing.xl, |
| 128 | choices_gap: spacing.sm + spacing.xxs, |
| 129 | choice_gap: spacing.md, |
| 130 | content_gap: spacing.xs, |
| 131 | choice_padding_x: spacing.lg, |
| 132 | choice_padding_y: spacing.md, |
| 133 | choice_min_height: spacing.xxl + spacing.lg, |
| 134 | choice_radius: radius.xl, |
| 135 | indicator_size: spacing.lg + spacing.xxs, |
| 136 | indicator_mark_size: spacing.sm + spacing.xxs, |
| 137 | indicator_check_size: spacing.lg, |
| 138 | shortcut_size: spacing.xl, |
| 139 | shortcut_text_size: spacing.md, |
| 140 | shortcut_radius: radius.lg, |
| 141 | }, |
| 142 | Size::Size(value) => Self { |
| 143 | root_gap: value, |
| 144 | item_gap: value, |
| 145 | choices_gap: value * 0.5, |
| 146 | choice_gap: value * 0.625, |
| 147 | content_gap: value * 0.125, |
| 148 | choice_padding_x: value * 0.75, |
| 149 | choice_padding_y: value * 0.625, |
| 150 | choice_min_height: value * 2.75, |
| 151 | choice_radius: radius.lg, |
| 152 | indicator_size: value, |
| 153 | indicator_mark_size: value * 0.5, |
| 154 | indicator_check_size: value * 0.875, |
| 155 | shortcut_size: value * 1.25, |
| 156 | shortcut_text_size: value * 0.625, |
| 157 | shortcut_radius: radius.md, |
| 158 | }, |
| 159 | Size::Medium => Self { |
| 160 | root_gap: spacing.lg, |
| 161 | item_gap: spacing.lg, |
| 162 | choices_gap: spacing.sm, |
| 163 | choice_gap: spacing.sm + spacing.xxs, |
| 164 | content_gap: spacing.xxs, |
| 165 | choice_padding_x: spacing.md, |
| 166 | choice_padding_y: spacing.sm + spacing.xxs, |
| 167 | choice_min_height: spacing.xxl + spacing.md, |
| 168 | choice_radius: radius.lg, |
| 169 | indicator_size: spacing.lg, |
| 170 | indicator_mark_size: spacing.sm, |
| 171 | indicator_check_size: spacing.md + spacing.xxs, |
| 172 | shortcut_size: spacing.lg + spacing.xs, |
| 173 | shortcut_text_size: spacing.sm + spacing.xxs, |
| 174 | shortcut_radius: radius.md, |
| 175 | }, |
| 176 | } |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | /// Answer text matches the Checkbox and Radio family's label at each size. |
| 181 | fn text_style<T: Styled>(element: T, size: Size, cx: &App) -> T { |
| 182 | let typography = cx.theme().semantic_tokens().typography; |
| 183 | match size { |
| 184 | Size::XSmall => apply_text_token(element, typography.xs), |
| 185 | Size::Small => apply_text_token(element, typography.sm), |
| 186 | Size::Large => apply_text_token(element, typography.lg), |
| 187 | Size::Size(value) => element.text_size(value), |
| 188 | Size::Medium => apply_text_token(element, typography.md), |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | /// Secondary text sits one step below the answer text. |
| 193 | fn secondary_text_style<T: Styled>(element: T, size: Size, cx: &App) -> T { |
| 194 | let typography = cx.theme().semantic_tokens().typography; |
| 195 | match size { |
| 196 | Size::XSmall | Size::Small => apply_text_token(element, typography.xs), |
| 197 | Size::Large => apply_text_token(element, typography.md), |
| 198 | Size::Size(value) => element.text_size(value * 0.875), |
| 199 | Size::Medium => apply_text_token(element, typography.sm), |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | fn progress_text_style<T: Styled>(element: T, size: Size, cx: &App) -> T { |
| 204 | let typography = cx.theme().semantic_tokens().typography; |
| 205 | match size { |
| 206 | Size::Large => apply_text_token(element, typography.sm), |
| 207 | Size::Size(value) => element.text_size(value * 0.75), |
| 208 | _ => apply_text_token(element, typography.xs), |
| 209 | } |
| 210 | .font_weight(gpui::FontWeight::MEDIUM) |
| 211 | } |
| 212 | |
| 213 | fn description_text_style<T: Styled>(element: T, size: Size, cx: &App) -> T { |
| 214 | secondary_text_style(element, size, cx) |
| 215 | } |
| 216 | |
| 217 | fn title_text_style<T: Styled>(element: T, size: Size, cx: &App) -> T { |
| 218 | let typography = cx.theme().semantic_tokens().typography; |
| 219 | match size { |
| 220 | Size::XSmall => apply_text_token(element, typography.sm), |
| 221 | Size::Small => apply_text_token(element, typography.md), |
| 222 | Size::Large => apply_text_token(element, typography.xl), |
| 223 | Size::Size(value) => element.text_size(value * 1.125), |
| 224 | Size::Medium => apply_text_token(element, typography.lg), |
| 225 | } |
| 226 | .font_weight(gpui::FontWeight::MEDIUM) |
| 227 | } |
| 228 | |
| 229 | /// The line box the answer label occupies. An indicator or a shortcut badge |
| 230 | /// centers on that first line, so a two-line answer keeps them beside the |
| 231 | /// label rather than drifting toward the description. |
| 232 | fn answer_line_height(size: Size, cx: &App) -> gpui::Pixels { |
| 233 | let typography = cx.theme().semantic_tokens().typography; |
| 234 | match size { |
| 235 | Size::XSmall => typography.xs.line_height, |
| 236 | Size::Small => typography.sm.line_height, |
| 237 | Size::Large => typography.lg.line_height, |
| 238 | Size::Size(value) => value * 1.5, |
| 239 | Size::Medium => typography.md.line_height, |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | /// How far to push an adornment of `height` down so it centers on that line. |
| 244 | fn center_on_answer_line(height: gpui::Pixels, size: Size, cx: &App) -> gpui::Pixels { |
| 245 | ((answer_line_height(size, cx) - height) * 0.5).max(gpui::Pixels::ZERO) |
| 246 | } |
| 247 | |
| 248 | fn apply_text_token<T: Styled>(element: T, token: gpui_base::TextStyleToken) -> T { |
| 249 | element |
| 250 | .text_size(token.size) |
| 251 | .line_height(token.line_height) |
| 252 | .font_weight(token.weight) |
| 253 | } |
| 254 | |
| 255 | fn item_label( |
| 256 | definition: &gpui_base::questionnaire::QuestionnaireItemDefinition, |
| 257 | ) -> Option<SharedString> { |
| 258 | Some(definition.accessibility_label().clone()) |
| 259 | } |
| 260 | |
| 261 | fn item_description( |
| 262 | definition: &gpui_base::questionnaire::QuestionnaireItemDefinition, |
| 263 | ) -> Option<SharedString> { |
| 264 | definition.description().cloned() |
| 265 | } |
| 266 | |
| 267 | /// A part addresses its question by name. A name the schema does not define |
| 268 | /// renders nothing, which is silent enough to hide a typo, so a debug build |
| 269 | /// names what went missing. It is a warning rather than an assertion because |
| 270 | /// one view may legitimately render the parts of several questionnaires and |
| 271 | /// hand each a state that defines only some of them. |
| 272 | #[cfg(debug_assertions)] |
| 273 | #[track_caller] |
| 274 | fn report_unknown_item(item: &SharedString) { |
| 275 | tracing::warn!("questionnaire has no item named `{item}`; the part renders nothing"); |
| 276 | } |
| 277 | |
| 278 | /// The same for a choice value inside a question that does exist. |
| 279 | #[cfg(debug_assertions)] |
| 280 | #[track_caller] |
| 281 | fn report_unknown_choice(item: &SharedString, value: &SharedString) { |
| 282 | tracing::warn!( |
| 283 | "questionnaire item `{item}` has no choice named `{value}`; the part renders nothing" |
| 284 | ); |
| 285 | } |
| 286 | |
| 287 | #[cfg(not(debug_assertions))] |
| 288 | fn report_unknown_item(_: &SharedString) {} |
| 289 | |
| 290 | #[cfg(not(debug_assertions))] |
| 291 | fn report_unknown_choice(_: &SharedString, _: &SharedString) {} |
| 292 | |
| 293 | fn element_id(state: &Entity<QuestionnaireState>, suffix: impl std::fmt::Display) -> ElementId { |
| 294 | ElementId::Name(format!("questionnaire-{}-{suffix}", state.entity_id()).into()) |
| 295 | } |
| 296 | |
| 297 | /// The composable questionnaire root. It owns layout and keyboard routing while |
| 298 | /// [`QuestionnaireState`] remains the single source of behavioral state. |
| 299 | #[derive(IntoElement)] |
| 300 | pub struct Questionnaire { |
| 301 | state: Entity<QuestionnaireState>, |
| 302 | style: StyleRefinement, |
| 303 | size: Option<Size>, |
| 304 | children: Vec<AnyElement>, |
| 305 | } |
| 306 | |
| 307 | impl Questionnaire { |
| 308 | pub fn new(state: &Entity<QuestionnaireState>) -> Self { |
| 309 | Self { |
| 310 | state: state.clone(), |
| 311 | style: StyleRefinement::default(), |
| 312 | size: None, |
| 313 | children: Vec::new(), |
| 314 | } |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | impl Sizable for Questionnaire { |
| 319 | fn with_size(mut self, size: impl Into<Size>) -> Self { |
| 320 | self.size = Some(size.into()); |
| 321 | self |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | impl Styled for Questionnaire { |
| 326 | fn style(&mut self) -> &mut StyleRefinement { |
| 327 | &mut self.style |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | impl ParentElement for Questionnaire { |
| 332 | fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) { |
| 333 | self.children.extend(elements); |
| 334 | } |
| 335 | } |
| 336 | |
| 337 | impl RenderOnce for Questionnaire { |
| 338 | fn render(self, _: &mut Window, cx: &mut App) -> impl IntoElement { |
| 339 | // The root is the one place a caller names the scale, so it records it |
| 340 | // for the parts before any of them render. |
| 341 | let size = self.size.unwrap_or(Size::Medium); |
| 342 | publish_size(&self.state, size, cx); |
| 343 | let metrics = QuestionnaireMetrics::new(size, cx); |
| 344 | let focus_handle = self.state.read(cx).focus_handle().clone(); |
| 345 | let state = self.state.clone(); |
| 346 | let debug_selector = format!("questionnaire-{}-root", self.state.entity_id()); |
| 347 | |
| 348 | div() |
| 349 | .id(element_id(&self.state, "root")) |
| 350 | .debug_selector(move || debug_selector) |
| 351 | .role(Role::Form) |
| 352 | .key_context("Questionnaire") |
| 353 | .track_focus(&focus_handle) |
| 354 | .capture_key_down(move |event, window, cx| { |
| 355 | gpui_base::questionnaire::handle_key_down(&state, event, window, cx) |
| 356 | }) |
| 357 | .flex() |
| 358 | .flex_col() |
| 359 | .min_w_0() |
| 360 | .gap(metrics.root_gap) |
| 361 | .w_full() |
| 362 | .refine_style(&self.style) |
| 363 | .children(self.children) |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | /// Textual progress, following the ReUI `base-nova` questionnaire skin. |
| 368 | #[derive(IntoElement)] |
| 369 | pub struct QuestionnaireProgress { |
| 370 | state: Entity<QuestionnaireState>, |
| 371 | style: StyleRefinement, |
| 372 | size: Option<Size>, |
| 373 | children: Vec<AnyElement>, |
| 374 | } |
| 375 | |
| 376 | impl QuestionnaireProgress { |
| 377 | pub fn new(state: &Entity<QuestionnaireState>) -> Self { |
| 378 | Self { |
| 379 | state: state.clone(), |
| 380 | style: StyleRefinement::default(), |
| 381 | size: None, |
| 382 | children: Vec::new(), |
| 383 | } |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | impl Sizable for QuestionnaireProgress { |
| 388 | fn with_size(mut self, size: impl Into<Size>) -> Self { |
| 389 | self.size = Some(size.into()); |
| 390 | self |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | impl Styled for QuestionnaireProgress { |
| 395 | fn style(&mut self) -> &mut StyleRefinement { |
| 396 | &mut self.style |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | impl ParentElement for QuestionnaireProgress { |
| 401 | fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) { |
| 402 | self.children.extend(elements); |
| 403 | } |
| 404 | } |
| 405 | |
| 406 | impl RenderOnce for QuestionnaireProgress { |
| 407 | fn render(self, _: &mut Window, cx: &mut App) -> impl IntoElement { |
| 408 | let progress = self.state.read(cx).progress(); |
| 409 | let current = progress.current(); |
| 410 | let total = progress.total(); |
| 411 | let label: SharedString = |
| 412 | t!("Questionnaire.progress", current = current, total = total).into(); |
| 413 | let colors = cx.theme().semantic_tokens().colors; |
| 414 | let has_children = !self.children.is_empty(); |
| 415 | |
| 416 | let size = resolve_size(self.size, &self.state, cx); |
| 417 | progress_text_style( |
| 418 | div() |
| 419 | .id(element_id(&self.state, "progress")) |
| 420 | .role(Role::ProgressIndicator) |
| 421 | .aria_label(label.clone()) |
| 422 | .aria_min_numeric_value(0.) |
| 423 | .aria_max_numeric_value(total as f64) |
| 424 | .aria_numeric_value(current as f64) |
| 425 | .text_color(colors.muted_foreground), |
| 426 | size, |
| 427 | cx, |
| 428 | ) |
| 429 | .refine_style(&self.style) |
| 430 | .when(!has_children, |this| this.child(label)) |
| 431 | .children(self.children) |
| 432 | } |
| 433 | } |
| 434 | |
| 435 | macro_rules! questionnaire_item_part { |
| 436 | ($name:ident, $fallback:ident, $style:ident, $color:ident, $closes_item_gap:expr) => { |
| 437 | #[derive(IntoElement)] |
| 438 | pub struct $name { |
| 439 | state: Entity<QuestionnaireState>, |
| 440 | item: SharedString, |
| 441 | style: StyleRefinement, |
| 442 | size: Option<Size>, |
| 443 | children: Vec<AnyElement>, |
| 444 | } |
| 445 | |
| 446 | impl $name { |
| 447 | pub fn new(state: &Entity<QuestionnaireState>, item: impl Into<SharedString>) -> Self { |
| 448 | Self { |
| 449 | state: state.clone(), |
| 450 | item: item.into(), |
| 451 | style: StyleRefinement::default(), |
| 452 | size: None, |
| 453 | children: Vec::new(), |
| 454 | } |
| 455 | } |
| 456 | } |
| 457 | |
| 458 | impl Sizable for $name { |
| 459 | fn with_size(mut self, size: impl Into<Size>) -> Self { |
| 460 | self.size = Some(size.into()); |
| 461 | self |
| 462 | } |
| 463 | } |
| 464 | |
| 465 | impl Styled for $name { |
| 466 | fn style(&mut self) -> &mut StyleRefinement { |
| 467 | &mut self.style |
| 468 | } |
| 469 | } |
| 470 | |
| 471 | impl ParentElement for $name { |
| 472 | fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) { |
| 473 | self.children.extend(elements); |
| 474 | } |
| 475 | } |
| 476 | |
| 477 | impl RenderOnce for $name { |
| 478 | fn render(self, _: &mut Window, cx: &mut App) -> impl IntoElement { |
| 479 | let Some(definition) = self.state.read(cx).item_definition(&self.item) else { |
| 480 | report_unknown_item(&self.item); |
| 481 | return gpui::Empty.into_any_element(); |
| 482 | }; |
| 483 | let fallback = $fallback(definition); |
| 484 | let has_children = !self.children.is_empty(); |
| 485 | if !has_children && fallback.is_none() { |
| 486 | return gpui::Empty.into_any_element(); |
| 487 | } |
| 488 | let colors = cx.theme().semantic_tokens().colors; |
| 489 | // The item stacks its parts on one gap. A title with no |
| 490 | // description of its own closes the gap the description would |
| 491 | // have filled, so answers never crowd the question. |
| 492 | let closes_item_gap = $closes_item_gap && item_description(definition).is_none(); |
| 493 | let size = resolve_size(self.size, &self.state, cx); |
| 494 | $style(div().w_full().text_color(colors.$color), size, cx) |
| 495 | .when(closes_item_gap, |this| { |
| 496 | this.mb(QuestionnaireMetrics::new(size, cx).item_gap) |
| 497 | }) |
| 498 | .refine_style(&self.style) |
| 499 | .when(!has_children, |this| { |
| 500 | this.when_some(fallback, |this, fallback| this.child(fallback)) |
| 501 | }) |
| 502 | .children(self.children) |
| 503 | .into_any_element() |
| 504 | } |
| 505 | } |
| 506 | }; |
| 507 | } |
| 508 | |
| 509 | questionnaire_item_part!( |
| 510 | QuestionnaireTitle, |
| 511 | item_label, |
| 512 | title_text_style, |
| 513 | foreground, |
| 514 | true |
| 515 | ); |
| 516 | questionnaire_item_part!( |
| 517 | QuestionnaireDescription, |
| 518 | item_description, |
| 519 | description_text_style, |
| 520 | muted_foreground, |
| 521 | false |
| 522 | ); |
| 523 | |
| 524 | /// The active question group. Inactive or disabled items do not enter layout, |
| 525 | /// focus traversal, or the accessibility tree. |
| 526 | #[derive(IntoElement)] |
| 527 | pub struct QuestionnaireItem { |
| 528 | state: Entity<QuestionnaireState>, |
| 529 | item: SharedString, |
| 530 | style: StyleRefinement, |
| 531 | size: Option<Size>, |
| 532 | children: Vec<AnyElement>, |
| 533 | } |
| 534 | |
| 535 | impl QuestionnaireItem { |
| 536 | pub fn new(state: &Entity<QuestionnaireState>, item: impl Into<SharedString>) -> Self { |
| 537 | Self { |
| 538 | state: state.clone(), |
| 539 | item: item.into(), |
| 540 | style: StyleRefinement::default(), |
| 541 | size: None, |
| 542 | children: Vec::new(), |
| 543 | } |
| 544 | } |
| 545 | } |
| 546 | |
| 547 | impl Sizable for QuestionnaireItem { |
| 548 | fn with_size(mut self, size: impl Into<Size>) -> Self { |
| 549 | self.size = Some(size.into()); |
| 550 | self |
| 551 | } |
| 552 | } |
| 553 | |
| 554 | impl Styled for QuestionnaireItem { |
| 555 | fn style(&mut self) -> &mut StyleRefinement { |
| 556 | &mut self.style |
| 557 | } |
| 558 | } |
| 559 | |
| 560 | impl ParentElement for QuestionnaireItem { |
| 561 | fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) { |
| 562 | self.children.extend(elements); |
| 563 | } |
| 564 | } |
| 565 | |
| 566 | impl RenderOnce for QuestionnaireItem { |
| 567 | fn render(self, _: &mut Window, cx: &mut App) -> impl IntoElement { |
| 568 | let state = self.state.read(cx); |
| 569 | let active = state.current_item().is_some_and(|name| name == &self.item); |
| 570 | let Some(item_state) = state.item_state(&self.item) else { |
| 571 | report_unknown_item(&self.item); |
| 572 | return gpui::Empty.into_any_element(); |
| 573 | }; |
| 574 | if !active || item_state.is_disabled() { |
| 575 | return gpui::Empty.into_any_element(); |
| 576 | } |
| 577 | let Some(definition) = state.item_definition(&self.item) else { |
| 578 | return gpui::Empty.into_any_element(); |
| 579 | }; |
| 580 | let focus_handle = state.item_focus_handle(&self.item).cloned(); |
| 581 | let label = definition.accessibility_label().clone(); |
| 582 | let description = definition.description().cloned(); |
| 583 | let metrics = QuestionnaireMetrics::new(resolve_size(self.size, &self.state, cx), cx); |
| 584 | |
| 585 | div() |
| 586 | .id(element_id(&self.state, format!("item-{}", self.item))) |
| 587 | .role(Role::Group) |
| 588 | .aria_label(label) |
| 589 | .when_some(description, |this, description| { |
| 590 | this.aria_description(description) |
| 591 | }) |
| 592 | .when_some(focus_handle, |this, focus_handle| { |
| 593 | this.track_focus(&focus_handle.tab_index(-1).tab_stop(false)) |
| 594 | }) |
| 595 | .flex() |
| 596 | .flex_col() |
| 597 | .gap(metrics.item_gap) |
| 598 | .w_full() |
| 599 | .refine_style(&self.style) |
| 600 | .children(self.children) |
| 601 | .into_any_element() |
| 602 | } |
| 603 | } |
| 604 | |
| 605 | /// Container for an item's answer controls. |
| 606 | #[derive(IntoElement)] |
| 607 | pub struct QuestionnaireChoices { |
| 608 | state: Entity<QuestionnaireState>, |
| 609 | item: SharedString, |
| 610 | style: StyleRefinement, |
| 611 | size: Option<Size>, |
| 612 | children: Vec<AnyElement>, |
| 613 | } |
| 614 | |
| 615 | impl QuestionnaireChoices { |
| 616 | pub fn new(state: &Entity<QuestionnaireState>, item: impl Into<SharedString>) -> Self { |
| 617 | Self { |
| 618 | state: state.clone(), |
| 619 | item: item.into(), |
| 620 | style: StyleRefinement::default(), |
| 621 | size: None, |
| 622 | children: Vec::new(), |
| 623 | } |
| 624 | } |
| 625 | } |
| 626 | |
| 627 | impl Sizable for QuestionnaireChoices { |
| 628 | fn with_size(mut self, size: impl Into<Size>) -> Self { |
| 629 | self.size = Some(size.into()); |
| 630 | self |
| 631 | } |
| 632 | } |
| 633 | |
| 634 | impl Styled for QuestionnaireChoices { |
| 635 | fn style(&mut self) -> &mut StyleRefinement { |
| 636 | &mut self.style |
| 637 | } |
| 638 | } |
| 639 | |
| 640 | impl ParentElement for QuestionnaireChoices { |
| 641 | fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) { |
| 642 | self.children.extend(elements); |
| 643 | } |
| 644 | } |
| 645 | |
| 646 | impl RenderOnce for QuestionnaireChoices { |
| 647 | fn render(self, _: &mut Window, cx: &mut App) -> impl IntoElement { |
| 648 | let state = self.state.read(cx); |
| 649 | let active = state.current_item().is_some_and(|name| name == &self.item); |
| 650 | let Some(item) = state.item_state(&self.item) else { |
| 651 | report_unknown_item(&self.item); |
| 652 | return gpui::Empty.into_any_element(); |
| 653 | }; |
| 654 | if !active || item.is_disabled() { |
| 655 | return gpui::Empty.into_any_element(); |
| 656 | } |
| 657 | let metrics = QuestionnaireMetrics::new(resolve_size(self.size, &self.state, cx), cx); |
| 658 | |
| 659 | if item.is_multiple() { |
| 660 | div() |
| 661 | .id(element_id(&self.state, format!("choices-{}", self.item))) |
| 662 | .role(Role::Group) |
| 663 | .flex() |
| 664 | .flex_col() |
| 665 | .gap(metrics.choices_gap) |
| 666 | .w_full() |
| 667 | .refine_style(&self.style) |
| 668 | .children(self.children) |
| 669 | .into_any_element() |
| 670 | } else { |
| 671 | RadioGroup::new(element_id(&self.state, format!("choices-{}", self.item))) |
| 672 | .flex() |
| 673 | .flex_col() |
| 674 | .gap(metrics.choices_gap) |
| 675 | .w_full() |
| 676 | .refine_style(&self.style) |
| 677 | .children(self.children) |
| 678 | .into_any_element() |
| 679 | } |
| 680 | } |
| 681 | } |
| 682 | |
| 683 | /// A selectable choice card, following the ReUI `base-nova` questionnaire skin. |
| 684 | #[derive(IntoElement)] |
| 685 | pub struct QuestionnaireChoice { |
| 686 | state: Entity<QuestionnaireState>, |
| 687 | item: SharedString, |
| 688 | value: SharedString, |
| 689 | style: StyleRefinement, |
| 690 | indicator_style: StyleRefinement, |
| 691 | content_style: StyleRefinement, |
| 692 | shortcut_style: StyleRefinement, |
| 693 | size: Option<Size>, |
| 694 | children: Vec<AnyElement>, |
| 695 | indicator_renderer: Option<ChoiceRenderer>, |
| 696 | shortcut_renderer: Option<ChoiceRenderer>, |
| 697 | } |
| 698 | |
| 699 | impl QuestionnaireChoice { |
| 700 | pub fn new( |
| 701 | state: &Entity<QuestionnaireState>, |
| 702 | item: impl Into<SharedString>, |
| 703 | value: impl Into<SharedString>, |
| 704 | ) -> Self { |
| 705 | Self { |
| 706 | state: state.clone(), |
| 707 | item: item.into(), |
| 708 | value: value.into(), |
| 709 | style: StyleRefinement::default(), |
| 710 | indicator_style: StyleRefinement::default(), |
| 711 | content_style: StyleRefinement::default(), |
| 712 | shortcut_style: StyleRefinement::default(), |
| 713 | size: None, |
| 714 | children: Vec::new(), |
| 715 | indicator_renderer: None, |
| 716 | shortcut_renderer: None, |
| 717 | } |
| 718 | } |
| 719 | |
| 720 | pub fn indicator_style(mut self, style: StyleRefinement) -> Self { |
| 721 | self.indicator_style = style; |
| 722 | self |
| 723 | } |
| 724 | |
| 725 | pub fn content_style(mut self, style: StyleRefinement) -> Self { |
| 726 | self.content_style = style; |
| 727 | self |
| 728 | } |
| 729 | |
| 730 | pub fn shortcut_style(mut self, style: StyleRefinement) -> Self { |
| 731 | self.shortcut_style = style; |
| 732 | self |
| 733 | } |
| 734 | |
| 735 | pub fn render_indicator( |
| 736 | mut self, |
| 737 | renderer: impl Fn(&QuestionnaireChoiceState, &mut Window, &mut App) -> AnyElement + 'static, |
| 738 | ) -> Self { |
| 739 | self.indicator_renderer = Some(Rc::new(renderer)); |
| 740 | self |
| 741 | } |
| 742 | |
| 743 | pub fn render_shortcut( |
| 744 | mut self, |
| 745 | renderer: impl Fn(&QuestionnaireChoiceState, &mut Window, &mut App) -> AnyElement + 'static, |
| 746 | ) -> Self { |
| 747 | self.shortcut_renderer = Some(Rc::new(renderer)); |
| 748 | self |
| 749 | } |
| 750 | } |
| 751 | |
| 752 | impl Sizable for QuestionnaireChoice { |
| 753 | fn with_size(mut self, size: impl Into<Size>) -> Self { |
| 754 | self.size = Some(size.into()); |
| 755 | self |
| 756 | } |
| 757 | } |
| 758 | |
| 759 | impl Styled for QuestionnaireChoice { |
| 760 | fn style(&mut self) -> &mut StyleRefinement { |
| 761 | &mut self.style |
| 762 | } |
| 763 | } |
| 764 | |
| 765 | impl ParentElement for QuestionnaireChoice { |
| 766 | fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) { |
| 767 | self.children.extend(elements); |
| 768 | } |
| 769 | } |
| 770 | |
| 771 | #[allow(clippy::too_many_arguments)] |
| 772 | fn style_choice_card<T>( |
| 773 | base: T, |
| 774 | indicator: AnyElement, |
| 775 | content: AnyElement, |
| 776 | shortcut: AnyElement, |
| 777 | metrics: QuestionnaireMetrics, |
| 778 | selected: bool, |
| 779 | disabled: bool, |
| 780 | invalid: bool, |
| 781 | focused: bool, |
| 782 | instance_style: &StyleRefinement, |
| 783 | window: &Window, |
| 784 | cx: &App, |
| 785 | ) -> T |
| 786 | where |
| 787 | T: Styled + ParentElement + StatefulInteractiveElement + gpui::prelude::FluentBuilder, |
| 788 | { |
| 789 | let tokens = cx.theme().semantic_tokens(); |
| 790 | base.flex() |
| 791 | .items_start() |
| 792 | .gap(metrics.choice_gap) |
| 793 | .w_full() |
| 794 | .min_h(metrics.choice_min_height) |
| 795 | .px(metrics.choice_padding_x) |
| 796 | .py(metrics.choice_padding_y) |
| 797 | .border_1() |
| 798 | .border_color(if invalid { |
| 799 | tokens.colors.destructive |
| 800 | } else if selected { |
| 801 | tokens.colors.primary.opacity(0.4) |
| 802 | } else { |
| 803 | tokens.colors.input |
| 804 | }) |
| 805 | .bg(if selected { |
| 806 | tokens.colors.muted |
| 807 | } else if cx.theme().is_dark() { |
| 808 | tokens.colors.input.opacity(0.2) |
| 809 | } else { |
| 810 | tokens.colors.background.opacity(0.) |
| 811 | }) |
| 812 | .rounded(metrics.choice_radius) |
| 813 | .when(!disabled, |this| { |
| 814 | this.hover(|style| style.bg(tokens.colors.muted.opacity(0.5))) |
| 815 | }) |
| 816 | .when(focused, |this| this.focus_ring_style(window, cx)) |
| 817 | .when(disabled, |this| this.opacity(0.5)) |
| 818 | .refine_style(instance_style) |
| 819 | .child(indicator) |
| 820 | .child(content) |
| 821 | .child(shortcut) |
| 822 | } |
| 823 | |
| 824 | impl RenderOnce for QuestionnaireChoice { |
| 825 | fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement { |
| 826 | let state = self.state.read(cx); |
| 827 | let active = state.current_item().is_some_and(|name| name == &self.item); |
| 828 | let Some(choice_state) = state.choice_state(&self.item, &self.value) else { |
| 829 | if state.item_state(&self.item).is_none() { |
| 830 | report_unknown_item(&self.item); |
| 831 | } else { |
| 832 | report_unknown_choice(&self.item, &self.value); |
| 833 | } |
| 834 | return gpui::Empty.into_any_element(); |
| 835 | }; |
| 836 | if !active { |
| 837 | return gpui::Empty.into_any_element(); |
| 838 | } |
| 839 | let Some(item) = state.item_state(&self.item) else { |
| 840 | report_unknown_item(&self.item); |
| 841 | return gpui::Empty.into_any_element(); |
| 842 | }; |
| 843 | let Some(definition) = state.choice_definition(&self.item, &self.value) else { |
| 844 | return gpui::Empty.into_any_element(); |
| 845 | }; |
| 846 | let multiple = item.is_multiple(); |
| 847 | let label = definition.accessibility_label().clone(); |
| 848 | let description = definition.description().cloned(); |
| 849 | let selected = choice_state.is_selected(); |
| 850 | let disabled = choice_state.is_disabled(); |
| 851 | let invalid = choice_state.is_invalid(); |
| 852 | let shortcut = choice_state.shortcut().cloned(); |
| 853 | let focus_handle = state.choice_focus_handle(&self.item, &self.value).cloned(); |
| 854 | let colors = cx.theme().semantic_tokens().colors; |
| 855 | let radius = cx.theme().semantic_tokens().radius; |
| 856 | let indicator_background = cx.theme().input_background(); |
| 857 | let mono_font = cx.theme().semantic_tokens().typography.mono.clone(); |
| 858 | let size = resolve_size(self.size, &self.state, cx); |
| 859 | let metrics = QuestionnaireMetrics::new(size, cx); |
| 860 | let indicator_offset = center_on_answer_line(metrics.indicator_size, size, cx); |
| 861 | let shortcut_offset = center_on_answer_line(metrics.shortcut_size, size, cx); |
| 862 | let focused = focus_handle |
| 863 | .as_ref() |
| 864 | .is_some_and(|focus_handle| focus_handle.is_focused(window)); |
| 865 | let has_children = !self.children.is_empty(); |
| 866 | |
| 867 | let default_indicator = || { |
| 868 | div() |
| 869 | .relative() |
| 870 | .flex() |
| 871 | .items_center() |
| 872 | .justify_center() |
| 873 | .flex_shrink_0() |
| 874 | .size(metrics.indicator_size) |
| 875 | .border_1() |
| 876 | .border_color(if selected { |
| 877 | colors.primary |
| 878 | } else { |
| 879 | colors.input |
| 880 | }) |
| 881 | .bg(if selected { |
| 882 | colors.primary |
| 883 | } else { |
| 884 | indicator_background |
| 885 | }) |
| 886 | .when(multiple, |this| this.rounded(radius.sm)) |
| 887 | .when(!multiple, |this| this.rounded(radius.full)) |
| 888 | .refine_style(&self.indicator_style) |
| 889 | .when(selected && multiple, |this| { |
| 890 | this.child( |
| 891 | svg() |
| 892 | .size(metrics.indicator_check_size) |
| 893 | .path(IconName::Check.path()) |
| 894 | .text_color(colors.primary_foreground), |
| 895 | ) |
| 896 | }) |
| 897 | .when(selected && !multiple, |this| { |
| 898 | this.child( |
| 899 | div() |
| 900 | .size(metrics.indicator_mark_size) |
| 901 | .rounded(radius.full) |
| 902 | .bg(colors.primary_foreground), |
| 903 | ) |
| 904 | }) |
| 905 | .into_any_element() |
| 906 | }; |
| 907 | |
| 908 | // The slot, not the element, owns the vertical alignment, so a custom |
| 909 | // indicator lands on the label's line without having to know the metrics. |
| 910 | let indicator = div() |
| 911 | .flex_shrink_0() |
| 912 | .mt(indicator_offset) |
| 913 | .child( |
| 914 | self.indicator_renderer |
| 915 | .as_ref() |
| 916 | .map(|renderer| renderer(&choice_state, window, cx)) |
| 917 | .unwrap_or_else(default_indicator), |
| 918 | ) |
| 919 | .into_any_element(); |
| 920 | |
| 921 | let content = div() |
| 922 | .flex() |
| 923 | .flex_1() |
| 924 | .flex_col() |
| 925 | .gap(metrics.content_gap) |
| 926 | .refine_style(&self.content_style) |
| 927 | .when(!has_children, |this| { |
| 928 | this.child(text_style( |
| 929 | div().text_color(colors.foreground).child(label.clone()), |
| 930 | size, |
| 931 | cx, |
| 932 | )) |
| 933 | .when_some(description.clone(), |this, description| { |
| 934 | this.child(secondary_text_style( |
| 935 | div().text_color(colors.muted_foreground).child(description), |
| 936 | size, |
| 937 | cx, |
| 938 | )) |
| 939 | }) |
| 940 | }) |
| 941 | .children(self.children); |
| 942 | |
| 943 | let default_shortcut = || { |
| 944 | let Some(shortcut) = shortcut.clone() else { |
| 945 | return gpui::Empty.into_any_element(); |
| 946 | }; |
| 947 | let Ok(keystroke) = gpui::Keystroke::parse(&shortcut.to_lowercase()) else { |
| 948 | return gpui::Empty.into_any_element(); |
| 949 | }; |
| 950 | Kbd::new(keystroke) |
| 951 | .outline() |
| 952 | .flex() |
| 953 | .items_center() |
| 954 | .justify_center() |
| 955 | .size(metrics.shortcut_size) |
| 956 | .p_0() |
| 957 | .bg(colors.background) |
| 958 | .border_color(colors.input) |
| 959 | .text_color(colors.muted_foreground) |
| 960 | .font_family(mono_font.clone()) |
| 961 | .text_size(metrics.shortcut_text_size) |
| 962 | .font_weight(gpui::FontWeight::MEDIUM) |
| 963 | .rounded(metrics.shortcut_radius) |
| 964 | .refine_style(&self.shortcut_style) |
| 965 | .into_any_element() |
| 966 | }; |
| 967 | let shortcut_element = div() |
| 968 | .flex_shrink_0() |
| 969 | .mt(shortcut_offset) |
| 970 | .child( |
| 971 | self.shortcut_renderer |
| 972 | .as_ref() |
| 973 | .map(|renderer| renderer(&choice_state, window, cx)) |
| 974 | .unwrap_or_else(default_shortcut), |
| 975 | ) |
| 976 | .into_any_element(); |
| 977 | |
| 978 | let id = element_id(&self.state, format!("choice-{}-{}", self.item, self.value)); |
| 979 | let instance_style = self.style.clone(); |
| 980 | let item_name = self.item.clone(); |
| 981 | let choice_value = self.value.clone(); |
| 982 | |
| 983 | let Some(control) = |
| 984 | QuestionnaireChoiceControl::new(&self.state, item_name, choice_value, id, cx) |
| 985 | else { |
| 986 | return gpui::Empty.into_any_element(); |
| 987 | }; |
| 988 | match control { |
| 989 | QuestionnaireChoiceControl::Checkbox(base) => style_choice_card( |
| 990 | base, |
| 991 | indicator, |
| 992 | content.into_any_element(), |
| 993 | shortcut_element, |
| 994 | metrics, |
| 995 | selected, |
| 996 | disabled, |
| 997 | invalid, |
| 998 | focused, |
| 999 | &instance_style, |
| 1000 | window, |
| 1001 | cx, |
| 1002 | ) |
| 1003 | .into_any_element(), |
| 1004 | QuestionnaireChoiceControl::Radio(base) => style_choice_card( |
| 1005 | base, |
| 1006 | indicator, |
| 1007 | content.into_any_element(), |
| 1008 | shortcut_element, |
| 1009 | metrics, |
| 1010 | selected, |
| 1011 | disabled, |
| 1012 | invalid, |
| 1013 | focused, |
| 1014 | &instance_style, |
| 1015 | window, |
| 1016 | cx, |
| 1017 | ) |
| 1018 | .into_any_element(), |
| 1019 | } |
| 1020 | } |
| 1021 | } |
| 1022 | |
| 1023 | /// Secondary text for custom choice compositions. |
| 1024 | #[derive(IntoElement)] |
| 1025 | pub struct QuestionnaireChoiceDescription { |
| 1026 | style: StyleRefinement, |
| 1027 | size: Option<Size>, |
| 1028 | children: Vec<AnyElement>, |
| 1029 | } |
| 1030 | |
| 1031 | impl QuestionnaireChoiceDescription { |
| 1032 | pub fn new() -> Self { |
| 1033 | Self { |
| 1034 | style: StyleRefinement::default(), |
| 1035 | size: None, |
| 1036 | children: Vec::new(), |
| 1037 | } |
| 1038 | } |
| 1039 | } |
| 1040 | |
| 1041 | impl Default for QuestionnaireChoiceDescription { |
| 1042 | fn default() -> Self { |
| 1043 | Self::new() |
| 1044 | } |
| 1045 | } |
| 1046 | |
| 1047 | impl Sizable for QuestionnaireChoiceDescription { |
| 1048 | fn with_size(mut self, size: impl Into<Size>) -> Self { |
| 1049 | self.size = Some(size.into()); |
| 1050 | self |
| 1051 | } |
| 1052 | } |
| 1053 | |
| 1054 | impl Styled for QuestionnaireChoiceDescription { |
| 1055 | fn style(&mut self) -> &mut StyleRefinement { |
| 1056 | &mut self.style |
| 1057 | } |
| 1058 | } |
| 1059 | |
| 1060 | impl ParentElement for QuestionnaireChoiceDescription { |
| 1061 | fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) { |
| 1062 | self.children.extend(elements); |
| 1063 | } |
| 1064 | } |
| 1065 | |
| 1066 | impl RenderOnce for QuestionnaireChoiceDescription { |
| 1067 | fn render(self, _: &mut Window, cx: &mut App) -> impl IntoElement { |
| 1068 | let colors = cx.theme().semantic_tokens().colors; |
| 1069 | secondary_text_style( |
| 1070 | div().text_color(colors.muted_foreground), |
| 1071 | self.size.unwrap_or(Size::Medium), |
| 1072 | cx, |
| 1073 | ) |
| 1074 | .refine_style(&self.style) |
| 1075 | .children(self.children) |
| 1076 | } |
| 1077 | } |
| 1078 | |
| 1079 | /// The optional freeform answer input for an item. |
| 1080 | #[derive(IntoElement)] |
| 1081 | pub struct QuestionnaireInput { |
| 1082 | state: Entity<QuestionnaireState>, |
| 1083 | item: SharedString, |
| 1084 | style: StyleRefinement, |
| 1085 | size: Option<Size>, |
| 1086 | } |
| 1087 | |
| 1088 | impl QuestionnaireInput { |
| 1089 | pub fn new(state: &Entity<QuestionnaireState>, item: impl Into<SharedString>) -> Self { |
| 1090 | Self { |
| 1091 | state: state.clone(), |
| 1092 | item: item.into(), |
| 1093 | style: StyleRefinement::default(), |
| 1094 | size: None, |
| 1095 | } |
| 1096 | } |
| 1097 | } |
| 1098 | |
| 1099 | impl Sizable for QuestionnaireInput { |
| 1100 | fn with_size(mut self, size: impl Into<Size>) -> Self { |
| 1101 | self.size = Some(size.into()); |
| 1102 | self |
| 1103 | } |
| 1104 | } |
| 1105 | |
| 1106 | impl Styled for QuestionnaireInput { |
| 1107 | fn style(&mut self) -> &mut StyleRefinement { |
| 1108 | &mut self.style |
| 1109 | } |
| 1110 | } |
| 1111 | |
| 1112 | impl RenderOnce for QuestionnaireInput { |
| 1113 | fn render(self, _: &mut Window, cx: &mut App) -> impl IntoElement { |
| 1114 | let state = self.state.read(cx); |
| 1115 | let active = state.current_item().is_some_and(|name| name == &self.item); |
| 1116 | let Some(item_state) = state.item_state(&self.item) else { |
| 1117 | report_unknown_item(&self.item); |
| 1118 | return gpui::Empty.into_any_element(); |
| 1119 | }; |
| 1120 | let Some(definition) = state.item_definition(&self.item) else { |
| 1121 | return gpui::Empty.into_any_element(); |
| 1122 | }; |
| 1123 | let Some(input_definition) = definition.input() else { |
| 1124 | return gpui::Empty.into_any_element(); |
| 1125 | }; |
| 1126 | if !active { |
| 1127 | return gpui::Empty.into_any_element(); |
| 1128 | } |
| 1129 | |
| 1130 | let size = resolve_size(self.size, &self.state, cx); |
| 1131 | let metrics = QuestionnaireMetrics::new(size, cx); |
| 1132 | |
| 1133 | Input::new(input_definition.state()) |
| 1134 | .aria_label(input_definition.accessibility_label().clone()) |
| 1135 | .disabled(item_state.is_disabled() || input_definition.is_disabled()) |
| 1136 | .with_size(size) |
| 1137 | // The freeform answer is one of the answers, so its text starts on |
| 1138 | // the same edge a choice's indicator does — the card padding. |
| 1139 | .pl(metrics.choice_padding_x) |
| 1140 | .rounded(metrics.choice_radius) |
| 1141 | .when(item_state.is_invalid(), |this| { |
| 1142 | this.border_color(cx.theme().semantic_tokens().colors.destructive) |
| 1143 | }) |
| 1144 | .refine_style(&self.style) |
| 1145 | .into_any_element() |
| 1146 | } |
| 1147 | } |
| 1148 | |
| 1149 | /// Validation error for an item. It only enters the tree while invalid. |
| 1150 | #[derive(IntoElement)] |
| 1151 | pub struct QuestionnaireError { |
| 1152 | state: Entity<QuestionnaireState>, |
| 1153 | item: SharedString, |
| 1154 | style: StyleRefinement, |
| 1155 | size: Option<Size>, |
| 1156 | children: Vec<AnyElement>, |
| 1157 | } |
| 1158 | |
| 1159 | impl QuestionnaireError { |
| 1160 | pub fn new(state: &Entity<QuestionnaireState>, item: impl Into<SharedString>) -> Self { |
| 1161 | Self { |
| 1162 | state: state.clone(), |
| 1163 | item: item.into(), |
| 1164 | style: StyleRefinement::default(), |
| 1165 | size: None, |
| 1166 | children: Vec::new(), |
| 1167 | } |
| 1168 | } |
| 1169 | } |
| 1170 | |
| 1171 | impl Sizable for QuestionnaireError { |
| 1172 | fn with_size(mut self, size: impl Into<Size>) -> Self { |
| 1173 | self.size = Some(size.into()); |
| 1174 | self |
| 1175 | } |
| 1176 | } |
| 1177 | |
| 1178 | impl Styled for QuestionnaireError { |
| 1179 | fn style(&mut self) -> &mut StyleRefinement { |
| 1180 | &mut self.style |
| 1181 | } |
| 1182 | } |
| 1183 | |
| 1184 | impl ParentElement for QuestionnaireError { |
| 1185 | fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) { |
| 1186 | self.children.extend(elements); |
| 1187 | } |
| 1188 | } |
| 1189 | |
| 1190 | fn questionnaire_error_root(id: ElementId) -> gpui::Stateful<gpui::Div> { |
| 1191 | div().id(id).role(Role::Alert) |
| 1192 | } |
| 1193 | |
| 1194 | /// Base reports why an item failed; the skin owns the sentence a person reads. |
| 1195 | fn error_text(error: &QuestionnaireValidationError) -> SharedString { |
| 1196 | match error { |
| 1197 | QuestionnaireValidationError::Required => t!("Questionnaire.error.required").into(), |
| 1198 | QuestionnaireValidationError::Unanswered => t!("Questionnaire.error.optional").into(), |
| 1199 | QuestionnaireValidationError::Message(message) => message.clone(), |
| 1200 | _ => SharedString::default(), |
| 1201 | } |
| 1202 | } |
| 1203 | |
| 1204 | impl RenderOnce for QuestionnaireError { |
| 1205 | fn render(self, _: &mut Window, cx: &mut App) -> impl IntoElement { |
| 1206 | let state = self.state.read(cx); |
| 1207 | let Some(item) = state.item_state(&self.item) else { |
| 1208 | report_unknown_item(&self.item); |
| 1209 | return gpui::Empty.into_any_element(); |
| 1210 | }; |
| 1211 | let error = state.error(&self.item).cloned(); |
| 1212 | if !item.is_invalid() || error.is_none() { |
| 1213 | return gpui::Empty.into_any_element(); |
| 1214 | } |
| 1215 | let has_children = !self.children.is_empty(); |
| 1216 | let colors = cx.theme().semantic_tokens().colors; |
| 1217 | let spacing = cx.theme().semantic_tokens().spacing; |
| 1218 | let size = resolve_size(self.size, &self.state, cx); |
| 1219 | |
| 1220 | secondary_text_style( |
| 1221 | questionnaire_error_root(element_id(&self.state, format!("error-{}", self.item))) |
| 1222 | .mt(spacing.sm) |
| 1223 | .text_color(colors.destructive), |
| 1224 | size, |
| 1225 | cx, |
| 1226 | ) |
| 1227 | .refine_style(&self.style) |
| 1228 | .when(!has_children, |this| { |
| 1229 | this.when_some(error, |this, error| this.child(error_text(&error))) |
| 1230 | }) |
| 1231 | .children(self.children) |
| 1232 | .into_any_element() |
| 1233 | } |
| 1234 | } |
| 1235 | |
| 1236 | /// Layout part for questionnaire navigation actions. |
| 1237 | #[derive(IntoElement)] |
| 1238 | pub struct QuestionnaireActions { |
| 1239 | state: Entity<QuestionnaireState>, |
| 1240 | style: StyleRefinement, |
| 1241 | size: Option<Size>, |
| 1242 | children: Vec<AnyElement>, |
| 1243 | } |
| 1244 | |
| 1245 | impl QuestionnaireActions { |
| 1246 | pub fn new(state: &Entity<QuestionnaireState>) -> Self { |
| 1247 | Self { |
| 1248 | state: state.clone(), |
| 1249 | style: StyleRefinement::default(), |
| 1250 | size: None, |
| 1251 | children: Vec::new(), |
| 1252 | } |
| 1253 | } |
| 1254 | } |
| 1255 | |
| 1256 | impl Sizable for QuestionnaireActions { |
| 1257 | fn with_size(mut self, size: impl Into<Size>) -> Self { |
| 1258 | self.size = Some(size.into()); |
| 1259 | self |
| 1260 | } |
| 1261 | } |
| 1262 | |
| 1263 | impl Styled for QuestionnaireActions { |
| 1264 | fn style(&mut self) -> &mut StyleRefinement { |
| 1265 | &mut self.style |
| 1266 | } |
| 1267 | } |
| 1268 | |
| 1269 | impl ParentElement for QuestionnaireActions { |
| 1270 | fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) { |
| 1271 | self.children.extend(elements); |
| 1272 | } |
| 1273 | } |
| 1274 | |
| 1275 | impl RenderOnce for QuestionnaireActions { |
| 1276 | fn render(self, _: &mut Window, cx: &mut App) -> impl IntoElement { |
| 1277 | let metrics = QuestionnaireMetrics::new(resolve_size(self.size, &self.state, cx), cx); |
| 1278 | let debug_selector = format!("questionnaire-{}-actions", self.state.entity_id()); |
| 1279 | div() |
| 1280 | .id(element_id(&self.state, "actions")) |
| 1281 | .debug_selector(move || debug_selector) |
| 1282 | .flex() |
| 1283 | .min_w_0() |
| 1284 | .items_center() |
| 1285 | .justify_start() |
| 1286 | .gap(metrics.choices_gap) |
| 1287 | .w_full() |
| 1288 | .refine_style(&self.style) |
| 1289 | .children(self.children) |
| 1290 | } |
| 1291 | } |
| 1292 | |
| 1293 | #[derive(Clone, Copy)] |
| 1294 | enum QuestionnaireAction { |
| 1295 | Previous, |
| 1296 | Skip, |
| 1297 | Next, |
| 1298 | Submit, |
| 1299 | } |
| 1300 | |
| 1301 | macro_rules! questionnaire_action_part { |
| 1302 | ($name:ident, $action:ident, $translation:literal, $outline:expr, $primary:expr) => { |
| 1303 | #[derive(IntoElement)] |
| 1304 | pub struct $name { |
| 1305 | state: Entity<QuestionnaireState>, |
| 1306 | style: StyleRefinement, |
| 1307 | size: Option<Size>, |
| 1308 | children: Vec<AnyElement>, |
| 1309 | } |
| 1310 | |
| 1311 | impl $name { |
| 1312 | pub fn new(state: &Entity<QuestionnaireState>) -> Self { |
| 1313 | Self { |
| 1314 | state: state.clone(), |
| 1315 | style: StyleRefinement::default(), |
| 1316 | size: None, |
| 1317 | children: Vec::new(), |
| 1318 | } |
| 1319 | } |
| 1320 | } |
| 1321 | |
| 1322 | impl Styled for $name { |
| 1323 | fn style(&mut self) -> &mut StyleRefinement { |
| 1324 | &mut self.style |
| 1325 | } |
| 1326 | } |
| 1327 | |
| 1328 | impl Sizable for $name { |
| 1329 | fn with_size(mut self, size: impl Into<Size>) -> Self { |
| 1330 | self.size = Some(size.into()); |
| 1331 | self |
| 1332 | } |
| 1333 | } |
| 1334 | |
| 1335 | impl ParentElement for $name { |
| 1336 | fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) { |
| 1337 | self.children.extend(elements); |
| 1338 | } |
| 1339 | } |
| 1340 | |
| 1341 | impl RenderOnce for $name { |
| 1342 | fn render(self, _: &mut Window, cx: &mut App) -> impl IntoElement { |
| 1343 | let navigation = self.state.read(cx).navigation_state(); |
| 1344 | let action = QuestionnaireAction::$action; |
| 1345 | let visible = match action { |
| 1346 | QuestionnaireAction::Previous => navigation.is_previous_visible(), |
| 1347 | QuestionnaireAction::Skip => navigation.is_skip_visible(), |
| 1348 | QuestionnaireAction::Next => navigation.is_next_visible(), |
| 1349 | QuestionnaireAction::Submit => navigation.is_submit_visible(), |
| 1350 | }; |
| 1351 | if !visible { |
| 1352 | return gpui::Empty.into_any_element(); |
| 1353 | } |
| 1354 | |
| 1355 | let anchors_trailing_actions = match action { |
| 1356 | QuestionnaireAction::Skip => true, |
| 1357 | QuestionnaireAction::Next | QuestionnaireAction::Submit => { |
| 1358 | !navigation.is_skip_visible() |
| 1359 | } |
| 1360 | QuestionnaireAction::Previous => false, |
| 1361 | }; |
| 1362 | let state = self.state.clone(); |
| 1363 | let has_children = !self.children.is_empty(); |
| 1364 | let debug_selector = format!( |
| 1365 | "questionnaire-{}-{}", |
| 1366 | self.state.entity_id(), |
| 1367 | stringify!($action) |
| 1368 | ); |
| 1369 | Button::new(element_id(&self.state, stringify!($action))) |
| 1370 | .debug_selector(move || debug_selector) |
| 1371 | .with_size(resolve_size(self.size, &self.state, cx)) |
| 1372 | .when($outline, |this| this.outline()) |
| 1373 | .when($primary, |this| this.primary()) |
| 1374 | .when(anchors_trailing_actions, |this| this.ml_auto()) |
| 1375 | .on_click(move |_, window, cx| { |
| 1376 | state.update(cx, |state, cx| match action { |
| 1377 | QuestionnaireAction::Previous => state.go_previous(window, cx), |
| 1378 | QuestionnaireAction::Skip => state.skip_current(window, cx), |
| 1379 | QuestionnaireAction::Next => state.go_next(window, cx), |
| 1380 | QuestionnaireAction::Submit => state.submit(window, cx), |
| 1381 | }); |
| 1382 | }) |
| 1383 | .refine_style(&self.style) |
| 1384 | .when(!has_children, |this| this.label(t!($translation))) |
| 1385 | .children(self.children) |
| 1386 | .into_any_element() |
| 1387 | } |
| 1388 | } |
| 1389 | }; |
| 1390 | } |
| 1391 | |
| 1392 | questionnaire_action_part!( |
| 1393 | QuestionnairePrevious, |
| 1394 | Previous, |
| 1395 | "Questionnaire.previous", |
| 1396 | true, |
| 1397 | false |
| 1398 | ); |
| 1399 | questionnaire_action_part!(QuestionnaireSkip, Skip, "Questionnaire.skip", true, false); |
| 1400 | questionnaire_action_part!(QuestionnaireNext, Next, "Questionnaire.next", false, true); |
| 1401 | questionnaire_action_part!( |
| 1402 | QuestionnaireSubmit, |
| 1403 | Submit, |
| 1404 | "Questionnaire.submit", |
| 1405 | false, |
| 1406 | true |
| 1407 | ); |
| 1408 | |
| 1409 | #[cfg(test)] |
| 1410 | mod tests { |
| 1411 | use super::*; |
| 1412 | use gpui::{ |
| 1413 | AppContext as _, Context, Element as _, Focusable as _, KeyDownEvent, Keystroke, Render, |
| 1414 | TestAppContext, VisualTestContext, accesskit, px, |
| 1415 | }; |
| 1416 | |
| 1417 | use gpui_base::questionnaire::{ |
| 1418 | QuestionnaireChoiceDefinition, QuestionnaireEvent, QuestionnaireInputDefinition, |
| 1419 | QuestionnaireItemDefinition, QuestionnaireShortcutMode, |
| 1420 | }; |
| 1421 | |
| 1422 | #[test] |
| 1423 | fn compound_parts_support_builder_customization() { |
| 1424 | let _ = QuestionnaireChoiceDescription::new() |
| 1425 | .opacity(0.8) |
| 1426 | .child("Description"); |
| 1427 | } |
| 1428 | |
| 1429 | struct QuestionnaireHarness { |
| 1430 | state: Entity<QuestionnaireState>, |
| 1431 | override_skip_margin: bool, |
| 1432 | } |
| 1433 | |
| 1434 | impl Render for QuestionnaireHarness { |
| 1435 | fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement { |
| 1436 | Questionnaire::new(&self.state) |
| 1437 | .size(px(480.)) |
| 1438 | .child( |
| 1439 | QuestionnaireItem::new(&self.state, "choice") |
| 1440 | .child( |
| 1441 | QuestionnaireChoices::new(&self.state, "choice") |
| 1442 | .child(QuestionnaireChoice::new(&self.state, "choice", "alpha")), |
| 1443 | ) |
| 1444 | .child(QuestionnaireInput::new(&self.state, "choice")), |
| 1445 | ) |
| 1446 | .child( |
| 1447 | QuestionnaireItem::new(&self.state, "first") |
| 1448 | .child( |
| 1449 | QuestionnaireChoices::new(&self.state, "first") |
| 1450 | .child(QuestionnaireChoice::new(&self.state, "first", "alpha")) |
| 1451 | .child(QuestionnaireChoice::new(&self.state, "first", "beta")), |
| 1452 | ) |
| 1453 | .child(QuestionnaireInput::new(&self.state, "first")), |
| 1454 | ) |
| 1455 | .child( |
| 1456 | QuestionnaireItem::new(&self.state, "second") |
| 1457 | .child(QuestionnaireInput::new(&self.state, "second")), |
| 1458 | ) |
| 1459 | .child( |
| 1460 | QuestionnaireActions::new(&self.state) |
| 1461 | .child(QuestionnairePrevious::new(&self.state)) |
| 1462 | .child( |
| 1463 | QuestionnaireSkip::new(&self.state) |
| 1464 | .when(self.override_skip_margin, |this| this.ml(px(0.))), |
| 1465 | ) |
| 1466 | .child(QuestionnaireNext::new(&self.state)) |
| 1467 | .child(QuestionnaireSubmit::new(&self.state)), |
| 1468 | ) |
| 1469 | } |
| 1470 | } |
| 1471 | |
| 1472 | fn visual_harness( |
| 1473 | cx: &mut TestAppContext, |
| 1474 | items: Vec<QuestionnaireItemDefinition>, |
| 1475 | shortcuts: Option<QuestionnaireShortcutMode>, |
| 1476 | ) -> (&mut VisualTestContext, Entity<QuestionnaireState>) { |
| 1477 | cx.update(crate::init); |
| 1478 | let (view, cx) = cx.add_window_view(move |_, cx| { |
| 1479 | let state = cx.new(|cx| { |
| 1480 | let state = QuestionnaireState::new(items, cx).unwrap(); |
| 1481 | match shortcuts { |
| 1482 | Some(shortcuts) => state.with_shortcuts(shortcuts), |
| 1483 | None => state, |
| 1484 | } |
| 1485 | }); |
| 1486 | QuestionnaireHarness { |
| 1487 | state, |
| 1488 | override_skip_margin: false, |
| 1489 | } |
| 1490 | }); |
| 1491 | cx.update(|window, cx| window.draw(cx).clear(cx)); |
| 1492 | let state = cx.update(|_, cx| view.read(cx).state.clone()); |
| 1493 | cx.update(|window, cx| { |
| 1494 | let focus_handle = state.read(cx).focus_handle().clone(); |
| 1495 | focus_handle.focus(window, cx); |
| 1496 | }); |
| 1497 | (cx, state) |
| 1498 | } |
| 1499 | |
| 1500 | fn input_visual_harness( |
| 1501 | cx: &mut TestAppContext, |
| 1502 | multiple: bool, |
| 1503 | ) -> (&mut VisualTestContext, Entity<QuestionnaireState>) { |
| 1504 | cx.update(crate::init); |
| 1505 | let (view, cx) = cx.add_window_view(move |window, cx| { |
| 1506 | let input = cx.new(|cx| crate::input::InputState::new(window, cx)); |
| 1507 | let state = cx.new(|cx| { |
| 1508 | QuestionnaireState::new( |
| 1509 | vec![ |
| 1510 | QuestionnaireItemDefinition::new("first", "First") |
| 1511 | .with_required(true) |
| 1512 | .with_multiple(multiple) |
| 1513 | .with_choices([ |
| 1514 | QuestionnaireChoiceDefinition::new("alpha", "Alpha"), |
| 1515 | QuestionnaireChoiceDefinition::new("beta", "Beta"), |
| 1516 | ]) |
| 1517 | .with_input(QuestionnaireInputDefinition::new(input, "Other")), |
| 1518 | QuestionnaireItemDefinition::new("second", "Second"), |
| 1519 | ], |
| 1520 | cx, |
| 1521 | ) |
| 1522 | .unwrap() |
| 1523 | }); |
| 1524 | QuestionnaireHarness { |
| 1525 | state, |
| 1526 | override_skip_margin: false, |
| 1527 | } |
| 1528 | }); |
| 1529 | cx.update(|window, cx| window.draw(cx).clear(cx)); |
| 1530 | let state = cx.update(|_, cx| view.read(cx).state.clone()); |
| 1531 | (cx, state) |
| 1532 | } |
| 1533 | |
| 1534 | fn actions_visual_harness( |
| 1535 | cx: &mut TestAppContext, |
| 1536 | override_skip_margin: bool, |
| 1537 | ) -> (&mut VisualTestContext, Entity<QuestionnaireState>) { |
| 1538 | cx.update(crate::init); |
| 1539 | let (view, cx) = cx.add_window_view(|_, cx| { |
| 1540 | let state = cx.new(|cx| { |
| 1541 | QuestionnaireState::new( |
| 1542 | vec![ |
| 1543 | QuestionnaireItemDefinition::new("first", "First").with_required(true), |
| 1544 | QuestionnaireItemDefinition::new("second", "Second"), |
| 1545 | QuestionnaireItemDefinition::new("third", "Third").with_required(true), |
| 1546 | ], |
| 1547 | cx, |
| 1548 | ) |
| 1549 | .unwrap() |
| 1550 | .with_current_item("second") |
| 1551 | .unwrap() |
| 1552 | }); |
| 1553 | QuestionnaireHarness { |
| 1554 | state, |
| 1555 | override_skip_margin, |
| 1556 | } |
| 1557 | }); |
| 1558 | cx.update(|window, cx| window.draw(cx).clear(cx)); |
| 1559 | let state = cx.update(|_, cx| view.read(cx).state.clone()); |
| 1560 | (cx, state) |
| 1561 | } |
| 1562 | |
| 1563 | fn focus_input(cx: &mut VisualTestContext, state: &Entity<QuestionnaireState>, item: &str) { |
| 1564 | cx.update(|window, cx| { |
| 1565 | let input = state.read(cx).input_state(item).unwrap(); |
| 1566 | let focus_handle = input.read(cx).focus_handle(cx); |
| 1567 | focus_handle.focus(window, cx); |
| 1568 | }); |
| 1569 | } |
| 1570 | |
| 1571 | fn simulate_key(cx: &mut VisualTestContext, key: &str, is_held: bool, simulate_ime: bool) { |
| 1572 | let mut keystroke = Keystroke::parse(key).unwrap(); |
| 1573 | if simulate_ime { |
| 1574 | keystroke = keystroke.with_simulated_ime(); |
| 1575 | } |
| 1576 | cx.simulate_event(KeyDownEvent { |
| 1577 | keystroke, |
| 1578 | is_held, |
| 1579 | prefer_character_input: false, |
| 1580 | }); |
| 1581 | } |
| 1582 | |
| 1583 | #[gpui::test] |
| 1584 | fn progress_projects_numeric_accessibility(cx: &mut TestAppContext) { |
| 1585 | cx.update(crate::init); |
| 1586 | let window = cx.add_empty_window(); |
| 1587 | window.update(|window, cx| { |
| 1588 | let state = cx.new(|cx| { |
| 1589 | QuestionnaireState::new( |
| 1590 | vec![ |
| 1591 | QuestionnaireItemDefinition::new("first", "First"), |
| 1592 | QuestionnaireItemDefinition::new("second", "Second"), |
| 1593 | ], |
| 1594 | cx, |
| 1595 | ) |
| 1596 | .unwrap() |
| 1597 | }); |
| 1598 | let mut node = accesskit::Node::new(Role::ProgressIndicator); |
| 1599 | QuestionnaireProgress::new(&state) |
| 1600 | .render(window, cx) |
| 1601 | .into_element() |
| 1602 | .write_a11y_info(&mut node); |
| 1603 | |
| 1604 | assert_eq!(node.numeric_value(), Some(1.)); |
| 1605 | assert_eq!(node.min_numeric_value(), Some(0.)); |
| 1606 | assert_eq!(node.max_numeric_value(), Some(2.)); |
| 1607 | |
| 1608 | let root = Questionnaire::new(&state).render(window, cx).into_element(); |
| 1609 | assert_eq!(root.a11y_role(), Some(Role::Form)); |
| 1610 | }); |
| 1611 | } |
| 1612 | |
| 1613 | #[gpui::test] |
| 1614 | fn actions_stay_inside_questionnaire_width(cx: &mut TestAppContext) { |
| 1615 | let (cx, state) = actions_visual_harness(cx, false); |
| 1616 | let entity_id = state.entity_id(); |
| 1617 | let root_id = Box::leak(format!("questionnaire-{entity_id}-root").into_boxed_str()); |
| 1618 | let actions_id = Box::leak(format!("questionnaire-{entity_id}-actions").into_boxed_str()); |
| 1619 | let previous_id = Box::leak(format!("questionnaire-{entity_id}-Previous").into_boxed_str()); |
| 1620 | let skip_id = Box::leak(format!("questionnaire-{entity_id}-Skip").into_boxed_str()); |
| 1621 | let next_id = Box::leak(format!("questionnaire-{entity_id}-Next").into_boxed_str()); |
| 1622 | let submit_id = Box::leak(format!("questionnaire-{entity_id}-Submit").into_boxed_str()); |
| 1623 | let root = cx.debug_bounds(root_id).expect("questionnaire rendered"); |
| 1624 | let actions = cx.debug_bounds(actions_id).expect("actions rendered"); |
| 1625 | let previous = cx.debug_bounds(previous_id).expect("previous rendered"); |
| 1626 | let skip = cx.debug_bounds(skip_id).expect("skip rendered"); |
| 1627 | let next = cx.debug_bounds(next_id).expect("next rendered"); |
| 1628 | |
| 1629 | assert!(actions.left() >= root.left()); |
| 1630 | assert!(actions.right() <= root.right()); |
| 1631 | assert_eq!(previous.left(), actions.left()); |
| 1632 | assert!(previous.right() <= skip.left()); |
| 1633 | assert!(skip.right() <= next.left()); |
| 1634 | assert_eq!(next.right(), actions.right()); |
| 1635 | |
| 1636 | cx.update(|window, cx| { |
| 1637 | state |
| 1638 | .update(cx, |state, cx| state.set_current_item("first", window, cx)) |
| 1639 | .unwrap(); |
| 1640 | window.draw(cx).clear(cx); |
| 1641 | }); |
| 1642 | let first_actions = cx.debug_bounds(actions_id).expect("first actions rendered"); |
| 1643 | let first_next = cx.debug_bounds(next_id).expect("first next rendered"); |
| 1644 | assert!(first_next.left() >= first_actions.left()); |
| 1645 | assert_eq!(first_next.right(), first_actions.right()); |
| 1646 | |
| 1647 | cx.update(|window, cx| { |
| 1648 | state |
| 1649 | .update(cx, |state, cx| state.set_current_item("third", window, cx)) |
| 1650 | .unwrap(); |
| 1651 | window.draw(cx).clear(cx); |
| 1652 | }); |
| 1653 | let last_actions = cx.debug_bounds(actions_id).expect("last actions rendered"); |
| 1654 | let last_previous = cx |
| 1655 | .debug_bounds(previous_id) |
| 1656 | .expect("last previous rendered"); |
| 1657 | let submit = cx.debug_bounds(submit_id).expect("submit rendered"); |
| 1658 | assert_eq!(last_previous.left(), last_actions.left()); |
| 1659 | assert!(last_previous.right() <= submit.left()); |
| 1660 | assert_eq!(submit.right(), last_actions.right()); |
| 1661 | } |
| 1662 | |
| 1663 | #[gpui::test] |
| 1664 | fn action_instance_style_overrides_default_trailing_anchor(cx: &mut TestAppContext) { |
| 1665 | let (cx, state) = actions_visual_harness(cx, true); |
| 1666 | let entity_id = state.entity_id(); |
| 1667 | let actions_id = Box::leak(format!("questionnaire-{entity_id}-actions").into_boxed_str()); |
| 1668 | let previous_id = Box::leak(format!("questionnaire-{entity_id}-Previous").into_boxed_str()); |
| 1669 | let skip_id = Box::leak(format!("questionnaire-{entity_id}-Skip").into_boxed_str()); |
| 1670 | let next_id = Box::leak(format!("questionnaire-{entity_id}-Next").into_boxed_str()); |
| 1671 | let actions = cx.debug_bounds(actions_id).expect("actions rendered"); |
| 1672 | let previous = cx.debug_bounds(previous_id).expect("previous rendered"); |
| 1673 | let skip = cx.debug_bounds(skip_id).expect("skip rendered"); |
| 1674 | let next = cx.debug_bounds(next_id).expect("next rendered"); |
| 1675 | |
| 1676 | assert_eq!(previous.left(), actions.left()); |
| 1677 | assert!(previous.right() <= skip.left()); |
| 1678 | assert!(skip.right() <= next.left()); |
| 1679 | assert!(next.right() < actions.right()); |
| 1680 | } |
| 1681 | |
| 1682 | struct ScaleHarness { |
| 1683 | state: Entity<QuestionnaireState>, |
| 1684 | root_size: Size, |
| 1685 | part_size: Option<Size>, |
| 1686 | } |
| 1687 | |
| 1688 | impl Render for ScaleHarness { |
| 1689 | fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement { |
| 1690 | let part_size = self.part_size; |
| 1691 | Questionnaire::new(&self.state) |
| 1692 | .with_size(self.root_size) |
| 1693 | .size(px(480.)) |
| 1694 | .child( |
| 1695 | QuestionnaireItem::new(&self.state, "scale") |
| 1696 | .when_some(part_size, |this, size| this.with_size(size)) |
| 1697 | .child( |
| 1698 | QuestionnaireChoices::new(&self.state, "scale") |
| 1699 | .when_some(part_size, |this, size| this.with_size(size)) |
| 1700 | .child( |
| 1701 | div() |
| 1702 | .id("scale-choice") |
| 1703 | .debug_selector(|| "scale-choice".to_string()) |
| 1704 | .child( |
| 1705 | QuestionnaireChoice::new(&self.state, "scale", "alpha") |
| 1706 | .when_some(part_size, |this, size| { |
| 1707 | this.with_size(size) |
| 1708 | }), |
| 1709 | ), |
| 1710 | ), |
| 1711 | ), |
| 1712 | ) |
| 1713 | } |
| 1714 | } |
| 1715 | |
| 1716 | fn scale_harness( |
| 1717 | cx: &mut TestAppContext, |
| 1718 | root_size: Size, |
| 1719 | part_size: Option<Size>, |
| 1720 | ) -> gpui::Bounds<gpui::Pixels> { |
| 1721 | cx.update(crate::init); |
| 1722 | let (_view, cx) = cx.add_window_view(move |_, cx| { |
| 1723 | let state = cx.new(|cx| { |
| 1724 | QuestionnaireState::new( |
| 1725 | vec![ |
| 1726 | QuestionnaireItemDefinition::new("scale", "Scale") |
| 1727 | .with_choice(QuestionnaireChoiceDefinition::new("alpha", "Alpha")), |
| 1728 | ], |
| 1729 | cx, |
| 1730 | ) |
| 1731 | .unwrap() |
| 1732 | }); |
| 1733 | ScaleHarness { |
| 1734 | state, |
| 1735 | root_size, |
| 1736 | part_size, |
| 1737 | } |
| 1738 | }); |
| 1739 | cx.update(|window, cx| window.draw(cx).clear(cx)); |
| 1740 | cx.debug_bounds("scale-choice").expect("choice rendered") |
| 1741 | } |
| 1742 | |
| 1743 | #[gpui::test] |
| 1744 | fn parts_take_their_scale_from_the_root_and_a_part_may_override_it(cx: &mut TestAppContext) { |
| 1745 | // A part left alone must land exactly where the same part told to use |
| 1746 | // the root's size lands: the root publishes the scale for all of them. |
| 1747 | let inherited = scale_harness(cx, Size::Small, None); |
| 1748 | let explicit = scale_harness(cx, Size::Small, Some(Size::Small)); |
| 1749 | assert_eq!(inherited.size, explicit.size); |
| 1750 | |
| 1751 | // A different root scale has to move the part, or nothing was inherited. |
| 1752 | let large_root = scale_harness(cx, Size::Large, None); |
| 1753 | assert!(large_root.size.height > inherited.size.height); |
| 1754 | |
| 1755 | // A part that names its own size keeps it whatever the root says. |
| 1756 | let overridden = scale_harness(cx, Size::Large, Some(Size::Small)); |
| 1757 | assert_eq!(overridden.size, inherited.size); |
| 1758 | } |
| 1759 | |
| 1760 | #[gpui::test] |
| 1761 | fn shortcut_guards_held_keys_before_activation(cx: &mut TestAppContext) { |
| 1762 | let (cx, state) = visual_harness( |
| 1763 | cx, |
| 1764 | vec![ |
| 1765 | QuestionnaireItemDefinition::new("choice", "Choice") |
| 1766 | .with_choice(QuestionnaireChoiceDefinition::new("alpha", "Alpha")), |
| 1767 | QuestionnaireItemDefinition::new("second", "Second"), |
| 1768 | ], |
| 1769 | Some(QuestionnaireShortcutMode::Letters), |
| 1770 | ); |
| 1771 | |
| 1772 | simulate_key(cx, "a", true, true); |
| 1773 | simulate_key(cx, "a", false, false); |
| 1774 | simulate_key(cx, "shift-a", false, true); |
| 1775 | cx.update(|_, cx| assert!(state.read(cx).answer("choice").unwrap().is_empty())); |
| 1776 | |
| 1777 | simulate_key(cx, "a", false, true); |
| 1778 | cx.update(|window, cx| { |
| 1779 | assert_eq!( |
| 1780 | state.read(cx).answer("choice").unwrap().choices(), |
| 1781 | &[SharedString::from("alpha")] |
| 1782 | ); |
| 1783 | assert_eq!( |
| 1784 | state |
| 1785 | .read(cx) |
| 1786 | .focused_current_choice(window) |
| 1787 | .map(SharedString::as_ref), |
| 1788 | Some("alpha") |
| 1789 | ); |
| 1790 | }); |
| 1791 | simulate_key(cx, "enter", false, false); |
| 1792 | cx.update(|_, cx| { |
| 1793 | assert_eq!(state.read(cx).current_item().unwrap().as_ref(), "second"); |
| 1794 | }); |
| 1795 | } |
| 1796 | |
| 1797 | #[gpui::test] |
| 1798 | fn root_keyboard_preserves_navigation_and_radio_semantics(cx: &mut TestAppContext) { |
| 1799 | let (cx, state) = visual_harness( |
| 1800 | cx, |
| 1801 | vec![ |
| 1802 | QuestionnaireItemDefinition::new("first", "First") |
| 1803 | .with_required(true) |
| 1804 | .with_choices([ |
| 1805 | QuestionnaireChoiceDefinition::new("alpha", "Alpha"), |
| 1806 | QuestionnaireChoiceDefinition::new("beta", "Beta"), |
| 1807 | ]), |
| 1808 | QuestionnaireItemDefinition::new("second", "Second"), |
| 1809 | ], |
| 1810 | None, |
| 1811 | ); |
| 1812 | |
| 1813 | simulate_key(cx, "right", false, false); |
| 1814 | cx.update(|_, cx| { |
| 1815 | assert_eq!(state.read(cx).current_item().unwrap().as_ref(), "first"); |
| 1816 | assert!(state.read(cx).answer("first").unwrap().is_empty()); |
| 1817 | }); |
| 1818 | |
| 1819 | cx.update(|window, cx| { |
| 1820 | let focus_handle = state |
| 1821 | .read(cx) |
| 1822 | .choice_focus_handle("first", "alpha") |
| 1823 | .unwrap() |
| 1824 | .clone(); |
| 1825 | focus_handle.focus(window, cx); |
| 1826 | }); |
| 1827 | simulate_key(cx, "enter", false, false); |
| 1828 | cx.update(|_, cx| { |
| 1829 | assert_eq!(state.read(cx).current_item().unwrap().as_ref(), "first"); |
| 1830 | assert!(state.read(cx).answer("first").unwrap().is_empty()); |
| 1831 | }); |
| 1832 | |
| 1833 | simulate_key(cx, "right", false, false); |
| 1834 | cx.update(|window, cx| window.draw(cx).clear(cx)); |
| 1835 | cx.update(|_, cx| { |
| 1836 | assert_eq!( |
| 1837 | state.read(cx).answer("first").unwrap().choices(), |
| 1838 | &[SharedString::from("beta")] |
| 1839 | ); |
| 1840 | assert_eq!(state.read(cx).current_item().unwrap().as_ref(), "first"); |
| 1841 | }); |
| 1842 | |
| 1843 | simulate_key(cx, "enter", false, false); |
| 1844 | cx.update(|_, cx| { |
| 1845 | assert_eq!(state.read(cx).current_item().unwrap().as_ref(), "second"); |
| 1846 | }); |
| 1847 | } |
| 1848 | |
| 1849 | #[gpui::test] |
| 1850 | fn empty_input_enter_stays_put_and_arrows_move_to_answers(cx: &mut TestAppContext) { |
| 1851 | let (cx, state) = input_visual_harness(cx, false); |
| 1852 | focus_input(cx, &state, "first"); |
| 1853 | |
| 1854 | simulate_key(cx, "enter", false, false); |
| 1855 | cx.update(|_, cx| { |
| 1856 | assert_eq!(state.read(cx).current_item().unwrap().as_ref(), "first"); |
| 1857 | assert!(state.read(cx).answer("first").unwrap().is_empty()); |
| 1858 | assert!(state.read(cx).error("first").is_none()); |
| 1859 | }); |
| 1860 | |
| 1861 | simulate_key(cx, "secondary-enter", false, false); |
| 1862 | cx.update(|_, cx| { |
| 1863 | assert_eq!(state.read(cx).current_item().unwrap().as_ref(), "first"); |
| 1864 | assert!(state.read(cx).error("first").is_some()); |
| 1865 | }); |
| 1866 | |
| 1867 | focus_input(cx, &state, "first"); |
| 1868 | simulate_key(cx, "up", false, false); |
| 1869 | cx.update(|window, cx| { |
| 1870 | assert_eq!( |
| 1871 | state |
| 1872 | .read(cx) |
| 1873 | .focused_current_choice(window) |
| 1874 | .map(SharedString::as_ref), |
| 1875 | Some("beta") |
| 1876 | ); |
| 1877 | }); |
| 1878 | |
| 1879 | simulate_key(cx, "down", false, false); |
| 1880 | cx.update(|window, cx| { |
| 1881 | assert!(state.read(cx).is_current_input_focused(window)); |
| 1882 | assert_eq!( |
| 1883 | state.read(cx).answer("first").unwrap().choices(), |
| 1884 | &[SharedString::from("beta")] |
| 1885 | ); |
| 1886 | }); |
| 1887 | |
| 1888 | simulate_key(cx, "down", false, false); |
| 1889 | cx.update(|window, cx| { |
| 1890 | assert_eq!( |
| 1891 | state |
| 1892 | .read(cx) |
| 1893 | .focused_current_choice(window) |
| 1894 | .map(SharedString::as_ref), |
| 1895 | Some("alpha") |
| 1896 | ); |
| 1897 | assert_eq!( |
| 1898 | state.read(cx).answer("first").unwrap().choices(), |
| 1899 | &[SharedString::from("alpha")] |
| 1900 | ); |
| 1901 | }); |
| 1902 | |
| 1903 | simulate_key(cx, "down", false, false); |
| 1904 | cx.update(|window, cx| { |
| 1905 | assert_eq!( |
| 1906 | state |
| 1907 | .read(cx) |
| 1908 | .focused_current_choice(window) |
| 1909 | .map(SharedString::as_ref), |
| 1910 | Some("beta") |
| 1911 | ); |
| 1912 | assert_eq!( |
| 1913 | state.read(cx).answer("first").unwrap().choices(), |
| 1914 | &[SharedString::from("beta")] |
| 1915 | ); |
| 1916 | }); |
| 1917 | |
| 1918 | cx.update(|window, cx| { |
| 1919 | state.update(cx, |state, cx| { |
| 1920 | state |
| 1921 | .set_input_value("first", "Preserved draft", window, cx) |
| 1922 | .unwrap(); |
| 1923 | state.activate_choice("first", "alpha", cx).unwrap(); |
| 1924 | }); |
| 1925 | }); |
| 1926 | focus_input(cx, &state, "first"); |
| 1927 | simulate_key(cx, "enter", false, false); |
| 1928 | cx.update(|_, cx| { |
| 1929 | let answer = state.read(cx).answer("first").unwrap(); |
| 1930 | assert_eq!(state.read(cx).current_item().unwrap().as_ref(), "first"); |
| 1931 | assert_eq!(answer.choices(), &[SharedString::from("alpha")]); |
| 1932 | assert!(answer.freeform().is_none()); |
| 1933 | }); |
| 1934 | } |
| 1935 | |
| 1936 | #[gpui::test] |
| 1937 | fn filled_group_input_keeps_text_editing_directions(cx: &mut TestAppContext) { |
| 1938 | let (cx, state) = input_visual_harness(cx, true); |
| 1939 | cx.update(|window, cx| { |
| 1940 | state |
| 1941 | .update(cx, |state, cx| { |
| 1942 | state.set_input_value("first", "Freeform answer", window, cx) |
| 1943 | }) |
| 1944 | .unwrap(); |
| 1945 | }); |
| 1946 | focus_input(cx, &state, "first"); |
| 1947 | |
| 1948 | simulate_key(cx, "down", false, false); |
| 1949 | cx.update(|window, cx| { |
| 1950 | let state = state.read(cx); |
| 1951 | assert!(state.is_current_input_focused(window)); |
| 1952 | assert_eq!( |
| 1953 | state |
| 1954 | .answer("first") |
| 1955 | .unwrap() |
| 1956 | .freeform() |
| 1957 | .map(SharedString::as_ref), |
| 1958 | Some("Freeform answer") |
| 1959 | ); |
| 1960 | assert!(state.answer("first").unwrap().choices().is_empty()); |
| 1961 | }); |
| 1962 | |
| 1963 | simulate_key(cx, "up", false, false); |
| 1964 | cx.update(|window, cx| { |
| 1965 | let state = state.read(cx); |
| 1966 | assert!(state.is_current_input_focused(window)); |
| 1967 | assert_eq!( |
| 1968 | state |
| 1969 | .answer("first") |
| 1970 | .unwrap() |
| 1971 | .freeform() |
| 1972 | .map(SharedString::as_ref), |
| 1973 | Some("Freeform answer") |
| 1974 | ); |
| 1975 | assert!(state.answer("first").unwrap().choices().is_empty()); |
| 1976 | }); |
| 1977 | } |
| 1978 | |
| 1979 | /// A single-answer item, a multiple-answer item, then a last single-answer |
| 1980 | /// item with a freeform input; each choice sits in a wrapper the test can |
| 1981 | /// click by its debug selector. |
| 1982 | struct ChooseHarness { |
| 1983 | state: Entity<QuestionnaireState>, |
| 1984 | submits: usize, |
| 1985 | _subscription: gpui::Subscription, |
| 1986 | } |
| 1987 | |
| 1988 | impl Render for ChooseHarness { |
| 1989 | fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement { |
| 1990 | let state = &self.state; |
| 1991 | let choice = |item: &'static str, value: &'static str| { |
| 1992 | div() |
| 1993 | .id(SharedString::from(format!("choice-{item}-{value}"))) |
| 1994 | .debug_selector(move || format!("choice-{item}-{value}")) |
| 1995 | .child(QuestionnaireChoice::new(state, item, value)) |
| 1996 | }; |
| 1997 | Questionnaire::new(state) |
| 1998 | .size(px(480.)) |
| 1999 | .child( |
| 2000 | QuestionnaireItem::new(state, "first").child( |
| 2001 | QuestionnaireChoices::new(state, "first") |
| 2002 | .child(choice("first", "alpha")) |
| 2003 | .child(choice("first", "beta")), |
| 2004 | ), |
| 2005 | ) |
| 2006 | .child( |
| 2007 | QuestionnaireItem::new(state, "second").child( |
| 2008 | QuestionnaireChoices::new(state, "second") |
| 2009 | .child(choice("second", "x")) |
| 2010 | .child(choice("second", "y")), |
| 2011 | ), |
| 2012 | ) |
| 2013 | .child( |
| 2014 | QuestionnaireItem::new(state, "third") |
| 2015 | .child( |
| 2016 | QuestionnaireChoices::new(state, "third") |
| 2017 | .child(choice("third", "omega")), |
| 2018 | ) |
| 2019 | .child(QuestionnaireInput::new(state, "third")), |
| 2020 | ) |
| 2021 | } |
| 2022 | } |
| 2023 | |
| 2024 | fn choose_harness( |
| 2025 | cx: &mut TestAppContext, |
| 2026 | ) -> ( |
| 2027 | &mut VisualTestContext, |
| 2028 | Entity<ChooseHarness>, |
| 2029 | Entity<QuestionnaireState>, |
| 2030 | ) { |
| 2031 | cx.update(crate::init); |
| 2032 | let (view, cx) = cx.add_window_view(|window, cx| { |
| 2033 | let input = cx.new(|cx| crate::input::InputState::new(window, cx)); |
| 2034 | let state = cx.new(|cx| { |
| 2035 | QuestionnaireState::new( |
| 2036 | vec![ |
| 2037 | QuestionnaireItemDefinition::new("first", "First").with_choices([ |
| 2038 | QuestionnaireChoiceDefinition::new("alpha", "Alpha"), |
| 2039 | QuestionnaireChoiceDefinition::new("beta", "Beta"), |
| 2040 | ]), |
| 2041 | QuestionnaireItemDefinition::new("second", "Second") |
| 2042 | .with_multiple(true) |
| 2043 | .with_choices([ |
| 2044 | QuestionnaireChoiceDefinition::new("x", "X"), |
| 2045 | QuestionnaireChoiceDefinition::new("y", "Y"), |
| 2046 | ]), |
| 2047 | QuestionnaireItemDefinition::new("third", "Third") |
| 2048 | .with_choice(QuestionnaireChoiceDefinition::new("omega", "Omega")) |
| 2049 | .with_input(QuestionnaireInputDefinition::new(input, "Other")), |
| 2050 | ], |
| 2051 | cx, |
| 2052 | ) |
| 2053 | .unwrap() |
| 2054 | .with_shortcuts(QuestionnaireShortcutMode::Numbers) |
| 2055 | }); |
| 2056 | let subscription = cx.subscribe(&state, |this: &mut ChooseHarness, _, event, _| { |
| 2057 | if matches!(event, QuestionnaireEvent::Submit(_)) { |
| 2058 | this.submits += 1; |
| 2059 | } |
| 2060 | }); |
| 2061 | ChooseHarness { |
| 2062 | state, |
| 2063 | submits: 0, |
| 2064 | _subscription: subscription, |
| 2065 | } |
| 2066 | }); |
| 2067 | cx.update(|window, cx| window.draw(cx).clear(cx)); |
| 2068 | let state = cx.update(|_, cx| view.read(cx).state.clone()); |
| 2069 | cx.update(|window, cx| { |
| 2070 | let focus_handle = state.read(cx).focus_handle().clone(); |
| 2071 | focus_handle.focus(window, cx); |
| 2072 | }); |
| 2073 | (cx, view, state) |
| 2074 | } |
| 2075 | |
| 2076 | /// Past the hold a newly chosen single answer keeps before confirming. |
| 2077 | fn wait_out_hold(cx: &mut VisualTestContext) { |
| 2078 | cx.executor() |
| 2079 | .advance_clock(std::time::Duration::from_millis(200)); |
| 2080 | cx.run_until_parked(); |
| 2081 | cx.update(|window, cx| window.draw(cx).clear(cx)); |
| 2082 | } |
| 2083 | |
| 2084 | fn click_choice(cx: &mut VisualTestContext, item: &str, value: &str) { |
| 2085 | cx.update(|window, cx| window.draw(cx).clear(cx)); |
| 2086 | let selector = Box::leak(format!("choice-{item}-{value}").into_boxed_str()); |
| 2087 | let bounds = cx.debug_bounds(selector).expect("choice rendered"); |
| 2088 | cx.simulate_click(bounds.center(), gpui::Modifiers::default()); |
| 2089 | } |
| 2090 | |
| 2091 | fn press(cx: &mut VisualTestContext, key: &str) { |
| 2092 | let keystroke = Keystroke::parse(key).unwrap(); |
| 2093 | cx.simulate_event(KeyDownEvent { |
| 2094 | keystroke: keystroke.clone(), |
| 2095 | is_held: false, |
| 2096 | prefer_character_input: false, |
| 2097 | }); |
| 2098 | cx.simulate_event(gpui::KeyUpEvent { keystroke }); |
| 2099 | } |
| 2100 | |
| 2101 | fn focus_choice( |
| 2102 | cx: &mut VisualTestContext, |
| 2103 | state: &Entity<QuestionnaireState>, |
| 2104 | item: &str, |
| 2105 | value: &str, |
| 2106 | ) { |
| 2107 | cx.update(|window, cx| { |
| 2108 | let focus_handle = state |
| 2109 | .read(cx) |
| 2110 | .choice_focus_handle(item, value) |
| 2111 | .unwrap() |
| 2112 | .clone(); |
| 2113 | focus_handle.focus(window, cx); |
| 2114 | }); |
| 2115 | } |
| 2116 | |
| 2117 | fn current(cx: &mut VisualTestContext, state: &Entity<QuestionnaireState>) -> SharedString { |
| 2118 | cx.update(|_, cx| state.read(cx).current_item().unwrap().clone()) |
| 2119 | } |
| 2120 | |
| 2121 | fn choices( |
| 2122 | cx: &mut VisualTestContext, |
| 2123 | state: &Entity<QuestionnaireState>, |
| 2124 | item: &str, |
| 2125 | ) -> Vec<SharedString> { |
| 2126 | cx.update(|_, cx| state.read(cx).answer(item).unwrap().choices().to_vec()) |
| 2127 | } |
| 2128 | |
| 2129 | #[gpui::test] |
| 2130 | fn clicking_a_single_choice_shows_it_then_advances(cx: &mut TestAppContext) { |
| 2131 | let (cx, view, state) = choose_harness(cx); |
| 2132 | |
| 2133 | click_choice(cx, "first", "alpha"); |
| 2134 | cx.run_until_parked(); |
| 2135 | assert_eq!( |
| 2136 | choices(cx, &state, "first"), |
| 2137 | vec![SharedString::from("alpha")] |
| 2138 | ); |
| 2139 | assert_eq!(current(cx, &state), "first", "the selection paints first"); |
| 2140 | wait_out_hold(cx); |
| 2141 | assert_eq!(current(cx, &state), "second"); |
| 2142 | |
| 2143 | click_choice(cx, "second", "x"); |
| 2144 | wait_out_hold(cx); |
| 2145 | assert_eq!(current(cx, &state), "second", "a checkbox only toggles"); |
| 2146 | assert_eq!(choices(cx, &state, "second"), vec![SharedString::from("x")]); |
| 2147 | |
| 2148 | cx.update(|window, cx| state.update(cx, |state, cx| state.go_next(window, cx))); |
| 2149 | focus_input(cx, &state, "third"); |
| 2150 | cx.simulate_input("draft"); |
| 2151 | wait_out_hold(cx); |
| 2152 | assert_eq!(current(cx, &state), "third", "typing never advances"); |
| 2153 | |
| 2154 | click_choice(cx, "third", "omega"); |
| 2155 | click_choice(cx, "third", "omega"); |
| 2156 | wait_out_hold(cx); |
| 2157 | assert_eq!( |
| 2158 | cx.update(|_, cx| view.read(cx).submits), |
| 2159 | 1, |
| 2160 | "a double click on the last item submits once" |
| 2161 | ); |
| 2162 | |
| 2163 | // Choosing the selected answer again after going back confirms it. |
| 2164 | cx.update(|window, cx| { |
| 2165 | state |
| 2166 | .update(cx, |state, cx| state.set_current_item("first", window, cx)) |
| 2167 | .unwrap(); |
| 2168 | }); |
| 2169 | click_choice(cx, "first", "alpha"); |
| 2170 | assert_eq!(current(cx, &state), "second"); |
| 2171 | } |
| 2172 | |
| 2173 | #[gpui::test] |
| 2174 | fn shortcuts_and_space_advance_while_arrows_only_move(cx: &mut TestAppContext) { |
| 2175 | let (cx, _, state) = choose_harness(cx); |
| 2176 | |
| 2177 | focus_choice(cx, &state, "first", "alpha"); |
| 2178 | press(cx, "down"); |
| 2179 | wait_out_hold(cx); |
| 2180 | assert_eq!( |
| 2181 | choices(cx, &state, "first"), |
| 2182 | vec![SharedString::from("beta")] |
| 2183 | ); |
| 2184 | assert_eq!( |
| 2185 | current(cx, &state), |
| 2186 | "first", |
| 2187 | "an arrow moves the selection only" |
| 2188 | ); |
| 2189 | |
| 2190 | simulate_key(cx, "1", false, true); |
| 2191 | cx.run_until_parked(); |
| 2192 | assert_eq!( |
| 2193 | choices(cx, &state, "first"), |
| 2194 | vec![SharedString::from("alpha")] |
| 2195 | ); |
| 2196 | assert_eq!(current(cx, &state), "first"); |
| 2197 | wait_out_hold(cx); |
| 2198 | assert_eq!(current(cx, &state), "second"); |
| 2199 | |
| 2200 | simulate_key(cx, "2", false, true); |
| 2201 | wait_out_hold(cx); |
| 2202 | assert_eq!( |
| 2203 | current(cx, &state), |
| 2204 | "second", |
| 2205 | "a multiple-answer shortcut toggles" |
| 2206 | ); |
| 2207 | assert_eq!(choices(cx, &state, "second"), vec![SharedString::from("y")]); |
| 2208 | |
| 2209 | cx.update(|window, cx| state.update(cx, |state, cx| state.go_previous(window, cx))); |
| 2210 | focus_choice(cx, &state, "first", "beta"); |
| 2211 | press(cx, "space"); |
| 2212 | wait_out_hold(cx); |
| 2213 | assert_eq!( |
| 2214 | choices(cx, &state, "first"), |
| 2215 | vec![SharedString::from("beta")] |
| 2216 | ); |
| 2217 | assert_eq!(current(cx, &state), "second", "Space chooses and advances"); |
| 2218 | } |
| 2219 | |
| 2220 | #[test] |
| 2221 | fn invalid_error_projects_alert_role() { |
| 2222 | let error = questionnaire_error_root("questionnaire-error-test".into()).into_element(); |
| 2223 | assert_eq!(error.a11y_role(), Some(Role::Alert)); |
| 2224 | } |
| 2225 | } |