| 1 | use super::*; |
| 2 | use crate::input::{InputState, TextareaState}; |
| 3 | use gpui::{AppContext as _, TestAppContext}; |
| 4 | |
| 5 | #[test] |
| 6 | fn validation_takes_precedence_over_focus_and_remains_visible_when_disabled() { |
| 7 | for mode in [crate::ThemeMode::Light, crate::ThemeMode::Dark] { |
| 8 | let theme = crate::Theme { |
| 9 | mode, |
| 10 | ..Default::default() |
| 11 | }; |
| 12 | let focused = GroupAppearance::new(&theme, true, false, false); |
| 13 | assert_eq!(focused.border, theme.ring); |
| 14 | assert_eq!(focused.ring, Some(theme.ring.opacity(0.5))); |
| 15 | |
| 16 | let disabled = GroupAppearance::new(&theme, true, true, false); |
| 17 | assert_eq!(disabled.border, theme.input); |
| 18 | assert_eq!(disabled.ring, None); |
| 19 | |
| 20 | for focused in [false, true] { |
| 21 | for disabled in [false, true] { |
| 22 | let invalid = GroupAppearance::new(&theme, focused, disabled, true); |
| 23 | assert_eq!(invalid.border, theme.danger); |
| 24 | assert_eq!( |
| 25 | invalid.ring, |
| 26 | Some( |
| 27 | theme |
| 28 | .danger |
| 29 | .opacity(if theme.is_dark() { 0.4 } else { 0.2 }) |
| 30 | ) |
| 31 | ); |
| 32 | } |
| 33 | } |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | #[gpui::test] |
| 38 | fn test_input_group_builder(cx: &mut TestAppContext) { |
| 39 | cx.update(crate::init); |
| 40 | cx.add_window(|window, cx| { |
| 41 | let input = cx.new(|cx| InputState::new(window, cx)); |
| 42 | let textarea = cx.new(|cx| TextareaState::new(window, cx)); |
| 43 | let group = InputGroup::new("group") |
| 44 | .input(InputGroupInput::new(&input).aria_label("Replaced input")) |
| 45 | .input( |
| 46 | InputGroupTextarea::new(&textarea) |
| 47 | .aria_label("Message") |
| 48 | .readonly(true), |
| 49 | ) |
| 50 | .disabled(true) |
| 51 | .invalid(true) |
| 52 | .small() |
| 53 | .addon( |
| 54 | InputGroupAddon::new("footer") |
| 55 | .align(InputGroupAddonAlignment::BlockEnd) |
| 56 | .child(InputGroupText::new().child("Help")) |
| 57 | .child(InputGroupButton::new("send").primary().label("Send")), |
| 58 | ); |
| 59 | assert_eq!( |
| 60 | group.control.as_ref().unwrap().state().entity_id(), |
| 61 | textarea.entity_id() |
| 62 | ); |
| 63 | assert!(group.disabled && group.invalid); |
| 64 | assert_eq!(group.size, Size::Small); |
| 65 | assert_eq!(group.addons[0].children.len(), 2); |
| 66 | assert_eq!( |
| 67 | group.addons[0].alignment, |
| 68 | InputGroupAddonAlignment::BlockEnd |
| 69 | ); |
| 70 | |
| 71 | let button = InputGroupButton::new("icon") |
| 72 | .icon(crate::IconName::Copy) |
| 73 | .small(); |
| 74 | assert_eq!(button.size, Size::Small); |
| 75 | assert!(button.button.is_icon_only()); |
| 76 | assert!(matches!(button.button.variant(), ButtonVariant::Ghost)); |
| 77 | gpui::Empty |
| 78 | }); |
| 79 | } |
| 80 | |
| 81 | #[cfg(feature = "test-support")] |
| 82 | mod interaction { |
| 83 | use super::*; |
| 84 | use crate::{Root, WindowExt as _}; |
| 85 | use gpui::{ |
| 86 | ClipboardEntry, ClipboardItem, Context, Entity, EntityInputHandler as _, FocusHandle, |
| 87 | InputEvent as _, LongPressEvent, Modifiers, Render, TouchPhase, VisualTestContext, point, |
| 88 | px, |
| 89 | }; |
| 90 | use gpui_base::test_support::{ElementSnapshot, find}; |
| 91 | use std::rc::Rc; |
| 92 | |
| 93 | struct Probe { |
| 94 | input: Entity<InputState>, |
| 95 | textarea: Entity<TextareaState>, |
| 96 | other: FocusHandle, |
| 97 | multiline: bool, |
| 98 | disabled: bool, |
| 99 | readonly: bool, |
| 100 | invalid: bool, |
| 101 | clicks: usize, |
| 102 | control_style: StyleRefinement, |
| 103 | width: gpui::DefiniteLength, |
| 104 | paste_handler: Option<Rc<dyn Fn(&ClipboardItem, &mut Window, &mut App) -> bool>>, |
| 105 | } |
| 106 | |
| 107 | impl Render for Probe { |
| 108 | fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> impl IntoElement { |
| 109 | div().p_4().child(div().track_focus(&self.other)).child( |
| 110 | InputGroup::new("group") |
| 111 | .w(self.width) |
| 112 | .disabled(self.disabled) |
| 113 | .readonly(self.readonly) |
| 114 | .invalid(self.invalid) |
| 115 | .map(|group| { |
| 116 | if self.multiline { |
| 117 | group.input( |
| 118 | InputGroupTextarea::new(&self.textarea) |
| 119 | .aria_label("Message") |
| 120 | .when_some(self.paste_handler.clone(), |input, handler| { |
| 121 | input.on_paste(move |item, window, cx| { |
| 122 | handler(item, window, cx) |
| 123 | }) |
| 124 | }) |
| 125 | .refine_style(&self.control_style), |
| 126 | ) |
| 127 | } else { |
| 128 | group.input( |
| 129 | InputGroupInput::new(&self.input) |
| 130 | .aria_label("Address") |
| 131 | .when_some(self.paste_handler.clone(), |input, handler| { |
| 132 | input.on_paste(move |item, window, cx| { |
| 133 | handler(item, window, cx) |
| 134 | }) |
| 135 | }) |
| 136 | .refine_style(&self.control_style), |
| 137 | ) |
| 138 | } |
| 139 | }) |
| 140 | // Deliberately supply parts in reverse visual order. |
| 141 | .addon( |
| 142 | InputGroupAddon::new("end") |
| 143 | .align(InputGroupAddonAlignment::InlineEnd) |
| 144 | .child(InputGroupButton::new("action").label("Run").on_click( |
| 145 | cx.listener(|this, _, window, cx| { |
| 146 | this.clicks += 1; |
| 147 | this.other.focus(window, cx); |
| 148 | cx.notify(); |
| 149 | }), |
| 150 | )), |
| 151 | ) |
| 152 | .addon( |
| 153 | InputGroupAddon::new("start") |
| 154 | .child(InputGroupText::new().child("https://")), |
| 155 | ) |
| 156 | .when(self.multiline, |group| { |
| 157 | group |
| 158 | .addon( |
| 159 | InputGroupAddon::new("footer") |
| 160 | .align(InputGroupAddonAlignment::BlockEnd) |
| 161 | .child("Footer"), |
| 162 | ) |
| 163 | .addon( |
| 164 | InputGroupAddon::new("header") |
| 165 | .align(InputGroupAddonAlignment::BlockStart) |
| 166 | .child("Header"), |
| 167 | ) |
| 168 | }), |
| 169 | ) |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | fn mount(cx: &mut TestAppContext, multiline: bool) -> (Entity<Probe>, VisualTestContext) { |
| 174 | cx.update(crate::init); |
| 175 | let captured = Rc::new(std::cell::RefCell::new(None)); |
| 176 | let capture = captured.clone(); |
| 177 | let window = cx.add_window(move |window, cx| { |
| 178 | let probe = cx.new(|cx| Probe { |
| 179 | input: cx.new(|cx| InputState::new(window, cx)), |
| 180 | textarea: cx.new(|cx| TextareaState::new(window, cx).auto_grow(1, 6)), |
| 181 | other: cx.focus_handle(), |
| 182 | multiline, |
| 183 | disabled: false, |
| 184 | readonly: false, |
| 185 | invalid: false, |
| 186 | clicks: 0, |
| 187 | control_style: StyleRefinement::default(), |
| 188 | width: rems(20.).into(), |
| 189 | paste_handler: None, |
| 190 | }); |
| 191 | *capture.borrow_mut() = Some(probe.clone()); |
| 192 | Root::new(probe, window, cx) |
| 193 | }); |
| 194 | let probe = captured.borrow().clone().unwrap(); |
| 195 | let mut cx = VisualTestContext::from_window(window.into(), cx); |
| 196 | draw(&mut cx); |
| 197 | (probe, cx) |
| 198 | } |
| 199 | |
| 200 | fn draw(cx: &mut VisualTestContext) { |
| 201 | cx.run_until_parked(); |
| 202 | cx.update(|window, cx| window.draw(cx).clear(cx)); |
| 203 | } |
| 204 | |
| 205 | fn snapshot(cx: &mut VisualTestContext, id: impl Into<ElementId>) -> ElementSnapshot { |
| 206 | let id = id.into(); |
| 207 | cx.update(|window, _| find(window, &[], &id).unwrap_or_else(|| panic!("missing {id:?}"))) |
| 208 | } |
| 209 | |
| 210 | fn click(cx: &mut VisualTestContext, id: &'static str) { |
| 211 | let bounds = snapshot(cx, id).bounds(); |
| 212 | cx.simulate_click(bounds.center(), Modifiers::default()); |
| 213 | draw(cx); |
| 214 | } |
| 215 | |
| 216 | #[gpui::test] |
| 217 | fn paste_hooks_receive_payloads_and_respect_editability(cx: &mut TestAppContext) { |
| 218 | for multiline in [false, true] { |
| 219 | let (probe, mut cx) = mount(cx, multiline); |
| 220 | let state: crate::input::state::TextInputState = probe.read_with(&cx, |probe, _| { |
| 221 | if multiline { |
| 222 | probe.textarea.clone().into() |
| 223 | } else { |
| 224 | probe.input.clone().into() |
| 225 | } |
| 226 | }); |
| 227 | let received = Rc::new(std::cell::RefCell::new(Vec::new())); |
| 228 | probe.update(&mut cx, |probe, cx| { |
| 229 | let received = received.clone(); |
| 230 | probe.paste_handler = Some(Rc::new(move |item, _, _| { |
| 231 | received.borrow_mut().push(item.clone()); |
| 232 | item.entries().iter().any(|entry| { |
| 233 | matches!( |
| 234 | entry, |
| 235 | ClipboardEntry::Image(_) | ClipboardEntry::ExternalPaths(_) |
| 236 | ) |
| 237 | }) |
| 238 | })); |
| 239 | cx.notify(); |
| 240 | }); |
| 241 | draw(&mut cx); |
| 242 | cx.update(|window, cx| state.focus(window, cx)); |
| 243 | draw(&mut cx); |
| 244 | let payloads = [ |
| 245 | ClipboardItem { |
| 246 | entries: vec![ |
| 247 | ClipboardEntry::Image(gpui::Image::empty()), |
| 248 | ClipboardEntry::String(gpui::ClipboardString::new("image caption".into())), |
| 249 | ], |
| 250 | }, |
| 251 | ClipboardItem { |
| 252 | entries: vec![ClipboardEntry::ExternalPaths(gpui::ExternalPaths( |
| 253 | vec!["attachment.png".into()].into(), |
| 254 | ))], |
| 255 | }, |
| 256 | ClipboardItem::new_string("text".into()), |
| 257 | ]; |
| 258 | for (ix, payload) in payloads.iter().enumerate() { |
| 259 | cx.update(|window, cx| { |
| 260 | cx.write_to_clipboard(payload.clone()); |
| 261 | window.dispatch_action(Box::new(crate::input::Paste), cx); |
| 262 | }); |
| 263 | draw(&mut cx); |
| 264 | assert_eq!(&*received.borrow(), &payloads[..=ix]); |
| 265 | assert_eq!( |
| 266 | cx.update(|_, cx| state.text(cx).to_string()), |
| 267 | if ix < 2 { "" } else { "text" } |
| 268 | ); |
| 269 | } |
| 270 | for (disabled, readonly) in [(true, false), (false, true)] { |
| 271 | probe.update(&mut cx, |probe, cx| { |
| 272 | probe.disabled = disabled; |
| 273 | probe.readonly = readonly; |
| 274 | cx.notify(); |
| 275 | }); |
| 276 | draw(&mut cx); |
| 277 | cx.update(|window, cx| window.dispatch_action(Box::new(crate::input::Paste), cx)); |
| 278 | draw(&mut cx); |
| 279 | assert_eq!(received.borrow().len(), 3); |
| 280 | assert_eq!(cx.update(|_, cx| state.text(cx).to_string()), "text"); |
| 281 | } |
| 282 | probe.update(&mut cx, |probe, cx| { |
| 283 | probe.readonly = false; |
| 284 | probe.paste_handler = None; |
| 285 | cx.notify(); |
| 286 | }); |
| 287 | draw(&mut cx); |
| 288 | cx.update(|window, cx| window.dispatch_action(Box::new(crate::input::Paste), cx)); |
| 289 | draw(&mut cx); |
| 290 | assert_eq!(received.borrow().len(), 3); |
| 291 | assert_eq!(cx.update(|_, cx| state.text(cx).to_string()), "texttext"); |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | #[gpui::test] |
| 296 | fn long_press_shows_edit_menu_and_copy_keeps_the_text(cx: &mut TestAppContext) { |
| 297 | for multiline in [false, true] { |
| 298 | let (probe, mut cx) = mount(cx, multiline); |
| 299 | cx.update(|window, cx| { |
| 300 | if multiline { |
| 301 | let state = probe.read(cx).textarea.clone(); |
| 302 | state.update(cx, |state, cx| { |
| 303 | state.set_value("quick select value", window, cx) |
| 304 | }); |
| 305 | } else { |
| 306 | let state = probe.read(cx).input.clone(); |
| 307 | state.update(cx, |state, cx| { |
| 308 | state.set_value("quick select value", window, cx) |
| 309 | }); |
| 310 | } |
| 311 | }); |
| 312 | draw(&mut cx); |
| 313 | let bounds = probe |
| 314 | .read_with(&cx, |probe, cx| { |
| 315 | if multiline { |
| 316 | probe.textarea.read(cx).range_to_bounds(&(0..1)) |
| 317 | } else { |
| 318 | probe.input.read(cx).range_to_bounds(&(0..1)) |
| 319 | } |
| 320 | }) |
| 321 | .unwrap(); |
| 322 | let position = bounds.center(); |
| 323 | for phase in [TouchPhase::Started, TouchPhase::Ended] { |
| 324 | cx.update(|window, cx| { |
| 325 | window.dispatch_event( |
| 326 | LongPressEvent { |
| 327 | phase, |
| 328 | start_position: position, |
| 329 | position, |
| 330 | } |
| 331 | .to_platform_input(), |
| 332 | cx, |
| 333 | ); |
| 334 | }); |
| 335 | draw(&mut cx); |
| 336 | } |
| 337 | for command in ["Cut", "Copy", "Paste", "Select All"] { |
| 338 | assert!(snapshot(&mut cx, command).visible()); |
| 339 | } |
| 340 | click(&mut cx, "Copy"); |
| 341 | assert_eq!( |
| 342 | cx.update(|_, cx| cx.read_from_clipboard().and_then(|item| item.text())), |
| 343 | Some("quick".into()) |
| 344 | ); |
| 345 | assert!(cx.update(|window, _| find(window, &[], &"Copy".into()).is_none())); |
| 346 | assert_eq!( |
| 347 | probe.read_with(&cx, |probe, cx| { |
| 348 | if multiline { |
| 349 | probe.textarea.read(cx).value() |
| 350 | } else { |
| 351 | probe.input.read(cx).value() |
| 352 | } |
| 353 | }), |
| 354 | "quick select value" |
| 355 | ); |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | #[gpui::test] |
| 360 | fn editor_overrides_preserve_caret_selection_and_ime_geometry(cx: &mut TestAppContext) { |
| 361 | let (probe, mut cx) = mount(cx, false); |
| 362 | let state = probe.read_with(&cx, |probe, _| probe.input.clone()); |
| 363 | click(&mut cx, "start"); |
| 364 | cx.simulate_input("Ada 中文"); |
| 365 | probe.update(&mut cx, |probe, cx| { |
| 366 | probe.control_style = StyleRefinement::default().px(px(24.)).py_0().text_lg(); |
| 367 | cx.notify(); |
| 368 | }); |
| 369 | draw(&mut cx); |
| 370 | let frame = snapshot(&mut cx, ("input", state.entity_id())).bounds(); |
| 371 | let text = state.read_with(&cx, |state, _| state.text_bounds().unwrap()); |
| 372 | assert!((text.left() - frame.left() - px(24.)).abs() <= px(1.)); |
| 373 | cx.simulate_click( |
| 374 | point(text.left() + px(0.5), text.center().y), |
| 375 | Modifiers::default(), |
| 376 | ); |
| 377 | draw(&mut cx); |
| 378 | assert_eq!(state.read_with(&cx, |state, _| state.cursor()), 0); |
| 379 | let range = state.read_with(&cx, |state, _| state.range_to_bounds(&(4..7)).unwrap()); |
| 380 | let ime = cx.update(|window, cx| { |
| 381 | state.update(cx, |state, cx| { |
| 382 | state.bounds_for_range(4..5, text, window, cx).unwrap() |
| 383 | }) |
| 384 | }); |
| 385 | assert_eq!( |
| 386 | range, ime, |
| 387 | "UTF-16 IME bounds must match the actual glyph bounds" |
| 388 | ); |
| 389 | state.update(&mut cx, |state, cx| state.set_selected_range(4..7, cx)); |
| 390 | cx.simulate_input("文"); |
| 391 | draw(&mut cx); |
| 392 | assert_eq!(state.read_with(&cx, |state, _| state.value()), "Ada 文文"); |
| 393 | } |
| 394 | |
| 395 | #[gpui::test] |
| 396 | fn textarea_keeps_scrolling_native_between_block_addons(cx: &mut TestAppContext) { |
| 397 | let (probe, mut cx) = mount(cx, true); |
| 398 | let state = probe.read_with(&cx, |probe, _| probe.textarea.clone()); |
| 399 | click(&mut cx, "header"); |
| 400 | cx.simulate_input("One\nTwo\nThree\nFour\nFive\nSix\nSeven\nEight"); |
| 401 | draw(&mut cx); |
| 402 | assert!(state.read_with(&cx, |state, _| state.scroll_offset().y < px(0.))); |
| 403 | let (caret, viewport) = state.read_with(&cx, |state, _| { |
| 404 | // cursor_layout keeps y in content coordinates; the native painter |
| 405 | // applies the vertical scroll offset when painting the caret. |
| 406 | let mut caret = state.cursor_layout().unwrap().0; |
| 407 | caret.origin.y += state.scroll_offset().y; |
| 408 | (caret, state.input_bounds()) |
| 409 | }); |
| 410 | assert!( |
| 411 | caret.top() >= viewport.top() - px(1.) && caret.bottom() <= viewport.bottom() + px(1.), |
| 412 | "caret {caret:?}, viewport {viewport:?}" |
| 413 | ); |
| 414 | cx.update(|window, cx| { |
| 415 | window.dispatch_action(Box::new(crate::input::SelectAll), cx); |
| 416 | window.dispatch_action(Box::new(crate::input::Copy), cx); |
| 417 | }); |
| 418 | assert_eq!( |
| 419 | cx.update(|_, cx| cx.read_from_clipboard().and_then(|item| item.text())), |
| 420 | Some("One\nTwo\nThree\nFour\nFive\nSix\nSeven\nEight".into()) |
| 421 | ); |
| 422 | } |
| 423 | |
| 424 | #[gpui::test] |
| 425 | fn addon_focus_editing_and_button_actions_use_the_retained_state(cx: &mut TestAppContext) { |
| 426 | let (probe, mut cx) = mount(cx, false); |
| 427 | let state = probe.read_with(&cx, |probe, _| probe.input.clone()); |
| 428 | click(&mut cx, "start"); |
| 429 | cx.simulate_input("Ada 中文"); |
| 430 | draw(&mut cx); |
| 431 | assert_eq!(state.read_with(&cx, |state, _| state.value()), "Ada 中文"); |
| 432 | let control = snapshot(&mut cx, ("input", state.entity_id())); |
| 433 | assert_eq!(control.role(), Some(Role::TextInput)); |
| 434 | assert_eq!(control.label(), Some("Address")); |
| 435 | assert_eq!(control.value(), Some("Ada 中文")); |
| 436 | assert_eq!(control.focused(), Some(true)); |
| 437 | assert_eq!(snapshot(&mut cx, "group").role(), Some(Role::Group)); |
| 438 | assert_eq!( |
| 439 | cx.update(|window, cx| window.focused_input(cx)), |
| 440 | Some(state.clone().into()) |
| 441 | ); |
| 442 | |
| 443 | click(&mut cx, "action"); |
| 444 | assert_eq!(probe.read_with(&cx, |probe, _| probe.clicks), 1); |
| 445 | assert!(cx.update(|window, cx| probe.read(cx).other.is_focused(window))); |
| 446 | assert_eq!(state.read_with(&cx, |state, _| state.value()), "Ada 中文"); |
| 447 | |
| 448 | let before = snapshot(&mut cx, "start").bounds(); |
| 449 | let after = snapshot(&mut cx, "end").bounds(); |
| 450 | assert!(before.right() <= control.bounds().left()); |
| 451 | assert!(control.bounds().right() <= after.left()); |
| 452 | } |
| 453 | |
| 454 | #[gpui::test] |
| 455 | fn disabled_blocks_actions_and_edits_but_readonly_keeps_copy_and_actions( |
| 456 | cx: &mut TestAppContext, |
| 457 | ) { |
| 458 | let (probe, mut cx) = mount(cx, false); |
| 459 | let state = probe.read_with(&cx, |probe, _| probe.input.clone()); |
| 460 | click(&mut cx, "start"); |
| 461 | cx.simulate_input("Retained value"); |
| 462 | probe.update(&mut cx, |probe, cx| { |
| 463 | probe.disabled = true; |
| 464 | cx.notify(); |
| 465 | }); |
| 466 | draw(&mut cx); |
| 467 | click(&mut cx, "action"); |
| 468 | click(&mut cx, "start"); |
| 469 | cx.simulate_input("blocked"); |
| 470 | draw(&mut cx); |
| 471 | assert_eq!(probe.read_with(&cx, |probe, _| probe.clicks), 0); |
| 472 | assert_eq!( |
| 473 | state.read_with(&cx, |state, _| state.value()), |
| 474 | "Retained value" |
| 475 | ); |
| 476 | assert!(state.read_with(&cx, |state, _| state.presentation().is_disabled())); |
| 477 | |
| 478 | probe.update(&mut cx, |probe, cx| { |
| 479 | probe.disabled = false; |
| 480 | probe.readonly = true; |
| 481 | cx.notify(); |
| 482 | }); |
| 483 | draw(&mut cx); |
| 484 | click(&mut cx, "start"); |
| 485 | cx.simulate_input("blocked"); |
| 486 | cx.update(|window, cx| { |
| 487 | window.dispatch_action(Box::new(crate::input::SelectAll), cx); |
| 488 | window.dispatch_action(Box::new(crate::input::Copy), cx); |
| 489 | }); |
| 490 | draw(&mut cx); |
| 491 | assert_eq!( |
| 492 | state.read_with(&cx, |state, _| state.value()), |
| 493 | "Retained value" |
| 494 | ); |
| 495 | assert_eq!( |
| 496 | cx.update(|_, cx| cx.read_from_clipboard().and_then(|item| item.text())), |
| 497 | Some("Retained value".into()) |
| 498 | ); |
| 499 | click(&mut cx, "action"); |
| 500 | assert_eq!(probe.read_with(&cx, |probe, _| probe.clicks), 1); |
| 501 | |
| 502 | probe.update(&mut cx, |probe, cx| { |
| 503 | probe.readonly = false; |
| 504 | probe.invalid = true; |
| 505 | cx.notify(); |
| 506 | }); |
| 507 | draw(&mut cx); |
| 508 | click(&mut cx, "start"); |
| 509 | cx.simulate_input("Editable error"); |
| 510 | draw(&mut cx); |
| 511 | assert_eq!( |
| 512 | state.read_with(&cx, |state, _| state.value()), |
| 513 | "Editable error" |
| 514 | ); |
| 515 | } |
| 516 | |
| 517 | #[gpui::test] |
| 518 | fn textarea_grows_between_block_addons_and_retains_native_focus(cx: &mut TestAppContext) { |
| 519 | let (probe, mut cx) = mount(cx, true); |
| 520 | let state = probe.read_with(&cx, |probe, _| probe.textarea.clone()); |
| 521 | let initial_footer = snapshot(&mut cx, "footer").bounds(); |
| 522 | click(&mut cx, "header"); |
| 523 | cx.simulate_input("One\nTwo\nThree\nFour\nFive"); |
| 524 | draw(&mut cx); |
| 525 | let control = snapshot(&mut cx, ("input", state.entity_id())); |
| 526 | let header = snapshot(&mut cx, "header").bounds(); |
| 527 | let footer = snapshot(&mut cx, "footer").bounds(); |
| 528 | assert_eq!(control.role(), Some(Role::MultilineTextInput)); |
| 529 | assert_eq!(control.focused(), Some(true)); |
| 530 | assert!(header.bottom() <= control.bounds().top()); |
| 531 | assert!(control.bounds().bottom() <= footer.top()); |
| 532 | assert!( |
| 533 | footer.top() > initial_footer.top(), |
| 534 | "textarea must grow before its toolbar" |
| 535 | ); |
| 536 | assert_eq!( |
| 537 | state.read_with(&cx, |state, _| state.value()), |
| 538 | "One\nTwo\nThree\nFour\nFive" |
| 539 | ); |
| 540 | |
| 541 | // A narrow viewport must still give the editor usable space between addons. |
| 542 | cx.update(|window, cx| { |
| 543 | window.set_rem_size(px(20.)); |
| 544 | window.draw(cx).clear(cx); |
| 545 | }); |
| 546 | let bounds = snapshot(&mut cx, ("input", state.entity_id())).bounds(); |
| 547 | assert!(bounds.size.width > px(0.)); |
| 548 | assert!(bounds.bottom() <= snapshot(&mut cx, "footer").bounds().top()); |
| 549 | } |
| 550 | |
| 551 | struct PopupProbe { |
| 552 | input: Entity<InputState>, |
| 553 | changes: Rc<std::cell::RefCell<Vec<(&'static str, bool)>>>, |
| 554 | clicks: Rc<std::cell::Cell<usize>>, |
| 555 | } |
| 556 | |
| 557 | impl Render for PopupProbe { |
| 558 | fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement { |
| 559 | use crate::{ |
| 560 | menu::{DropdownMenu as _, PopupMenuItem}, |
| 561 | popover::Popover, |
| 562 | }; |
| 563 | let menu_changes = self.changes.clone(); |
| 564 | let popover_changes = self.changes.clone(); |
| 565 | let enabled_clicks = self.clicks.clone(); |
| 566 | let disabled_clicks = self.clicks.clone(); |
| 567 | v_flex() |
| 568 | .p_4() |
| 569 | .child( |
| 570 | InputGroup::new("popup-group") |
| 571 | .w_96() |
| 572 | .input(InputGroupInput::new(&self.input)) |
| 573 | .addon( |
| 574 | InputGroupAddon::new("popup-actions") |
| 575 | .align(InputGroupAddonAlignment::BlockEnd) |
| 576 | .child(InputGroupButton::new("first").label("First").on_click( |
| 577 | move |_, _, _| enabled_clicks.set(enabled_clicks.get() + 1), |
| 578 | )) |
| 579 | .child(div().id("between").test_support().child("then")) |
| 580 | .children([ |
| 581 | InputGroupButton::new("last") |
| 582 | .label("Last") |
| 583 | .into_any_element(), |
| 584 | InputGroupButton::new("menu-trigger") |
| 585 | .label("Menu") |
| 586 | .dropdown_menu(|menu, _, _| { |
| 587 | menu.item(PopupMenuItem::new("Action")) |
| 588 | }) |
| 589 | .on_open_change(move |open, _, _| { |
| 590 | menu_changes.borrow_mut().push(("menu", *open)); |
| 591 | }) |
| 592 | .into_any_element(), |
| 593 | Popover::new("group-popover") |
| 594 | .trigger( |
| 595 | InputGroupButton::new("popover-trigger") |
| 596 | .label("Details"), |
| 597 | ) |
| 598 | .child("Details content") |
| 599 | .on_open_change(move |open, _, _| { |
| 600 | popover_changes.borrow_mut().push(("popover", *open)); |
| 601 | }) |
| 602 | .into_any_element(), |
| 603 | ]), |
| 604 | ), |
| 605 | ) |
| 606 | .child( |
| 607 | // Exercise inherited disabled state without the group's capture |
| 608 | // handlers: the native button itself must reject activation. |
| 609 | InputGroupAddon::new("disabled-addon") |
| 610 | .child( |
| 611 | InputGroupButton::new("disabled-action") |
| 612 | .label("Disabled") |
| 613 | .on_click(move |_, _, _| { |
| 614 | disabled_clicks.set(disabled_clicks.get() + 1) |
| 615 | }), |
| 616 | ) |
| 617 | .render_in_group( |
| 618 | GroupPresentation { |
| 619 | disabled: true, |
| 620 | ..Default::default() |
| 621 | }, |
| 622 | window, |
| 623 | cx, |
| 624 | ), |
| 625 | ) |
| 626 | } |
| 627 | } |
| 628 | |
| 629 | #[gpui::test] |
| 630 | fn ordered_children_and_native_button_popup_composition(cx: &mut TestAppContext) { |
| 631 | cx.update(crate::init); |
| 632 | let changes = Rc::new(std::cell::RefCell::new(Vec::new())); |
| 633 | let clicks = Rc::new(std::cell::Cell::new(0)); |
| 634 | let window = cx.add_window({ |
| 635 | let changes = changes.clone(); |
| 636 | let clicks = clicks.clone(); |
| 637 | move |window, cx| { |
| 638 | let probe = cx.new(|cx| PopupProbe { |
| 639 | input: cx.new(|cx| InputState::new(window, cx)), |
| 640 | changes, |
| 641 | clicks, |
| 642 | }); |
| 643 | Root::new(probe, window, cx) |
| 644 | } |
| 645 | }); |
| 646 | let mut cx = VisualTestContext::from_window(window.into(), cx); |
| 647 | draw(&mut cx); |
| 648 | let first = snapshot(&mut cx, "first").bounds(); |
| 649 | let between = snapshot(&mut cx, "between").bounds(); |
| 650 | let last = snapshot(&mut cx, "last").bounds(); |
| 651 | assert!(first.right() <= between.left() && between.right() <= last.left()); |
| 652 | click(&mut cx, "first"); |
| 653 | assert_eq!(clicks.get(), 1); |
| 654 | click(&mut cx, "disabled-action"); |
| 655 | assert_eq!(clicks.get(), 1); |
| 656 | |
| 657 | click(&mut cx, "menu-trigger"); |
| 658 | assert_eq!(&*changes.borrow(), &[("menu", true)]); |
| 659 | cx.simulate_keystrokes("escape"); |
| 660 | draw(&mut cx); |
| 661 | assert_eq!(&*changes.borrow(), &[("menu", true), ("menu", false)]); |
| 662 | |
| 663 | click(&mut cx, "popover-trigger"); |
| 664 | assert_eq!(changes.borrow().last(), Some(&("popover", true))); |
| 665 | cx.simulate_keystrokes("escape"); |
| 666 | draw(&mut cx); |
| 667 | assert_eq!(changes.borrow().last(), Some(&("popover", false))); |
| 668 | } |
| 669 | } |