1use std::{cell::RefCell, rc::Rc};
24
25use arc_motion::{
26 ArcPresence, ArcPresenceConfig, BlurFallback, CubicBezier, arc_spring, is_reduced_motion,
27 motion_now,
28};
29use arc_tokens::{ActiveArcTheme as _, Radii, Shadows, Spacing, Typography, oklab_mix};
30use gpui_kit::{
31 AnyElement, App, Bounds, ElementId, FocusHandle, Hsla, IntoElement, KeyDownEvent, MouseButton,
32 MouseDownEvent, ParentElement, PathBuilder, Pixels, RenderOnce, Role, SharedString, Styled,
33 TestSupportExt as _, Window, base::StyledExt as _, canvas, deferred, div, point, prelude::*,
34 px,
35};
36
37pub const DIALOG_MAX_WIDTH: f32 = 440.0;
39pub const DIALOG_VIEWPORT_MARGIN: f32 = 32.0;
41
42type OpenCallback = Rc<dyn Fn(&bool, &mut Window, &mut App)>;
43
44pub fn dialog_card_y_offset(is_open: bool, spring_y: f32, presence_t: f32, reduced: bool) -> f32 {
49 if reduced {
50 return 0.0;
51 }
52 if is_open {
53 spring_y
54 } else {
55 let exit_progress = (1.0 - presence_t).clamp(0.0, 1.0);
56 spring_y + (4.0 - spring_y) * exit_progress
57 }
58}
59
60pub fn swap_text_sample(elapsed_secs: f64, reduced: bool) -> (f32, f32, f32, f32) {
63 if reduced || elapsed_secs >= 0.22 {
64 return (0.0, -5.0, 1.0, 0.0);
65 }
66 let out_t = (elapsed_secs / 0.16).clamp(0.0, 1.0) as f32;
67 let out_eased = CubicBezier::new(0.2, 0.0, 0.0, 1.0).sample(out_t);
68 let in_t = (elapsed_secs / 0.22).clamp(0.0, 1.0) as f32;
69 let in_eased = CubicBezier::new(0.16, 1.0, 0.3, 1.0).sample(in_t);
70 let old_blur = BlurFallback::evaluate(px(out_eased * 4.0), reduced);
71 let new_blur = BlurFallback::evaluate(px((1.0 - in_eased) * 8.0), reduced);
72 let old_alpha = ((1.0 - out_eased) * old_blur.opacity_multiplier).clamp(0.0, 1.0);
73 let old_y = -5.0 * out_eased;
74 let new_alpha = (in_eased * new_blur.opacity_multiplier).clamp(0.0, 1.0);
75 let new_y = 5.0 * (1.0 - in_eased);
76 (old_alpha, old_y, new_alpha, new_y)
77}
78
79fn paint_close_x(cx: f32, cy: f32, scale: f32, color: Hsla, window: &mut Window) {
80 let r = 4.2 * scale;
81 for (x1, y1, x2, y2) in [
82 (cx - r, cy - r, cx + r, cy + r),
83 (cx + r, cy - r, cx - r, cy + r),
84 ] {
85 let mut b = PathBuilder::stroke(px(1.75));
86 b.move_to(point(px(x1), px(y1)));
87 b.line_to(point(px(x2), px(y2)));
88 if let Ok(path) = b.build() {
89 window.paint_path(path, color);
90 }
91 }
92}
93
94#[derive(Clone)]
95struct DialogInternal {
96 open: bool,
97 last_open_changed_at: f64,
98 close_hovered: bool,
99 close_pressed: bool,
100 prev_title: Option<SharedString>,
101 last_title: SharedString,
102 title_changed_at: f64,
103 prev_desc: Option<SharedString>,
104 last_desc: SharedString,
105 desc_changed_at: f64,
106 trigger_bounds: Rc<RefCell<Option<Bounds<Pixels>>>>,
107 card_bounds: Rc<RefCell<Option<Bounds<Pixels>>>>,
108 focus_handle: FocusHandle,
109}
110
111impl DialogInternal {
112 fn new(
113 default_open: bool,
114 title: SharedString,
115 desc: SharedString,
116 now: f64,
117 focus_handle: FocusHandle,
118 ) -> Self {
119 Self {
120 open: default_open,
121 last_open_changed_at: now - 10.0,
122 close_hovered: false,
123 close_pressed: false,
124 prev_title: None,
125 last_title: title,
126 title_changed_at: now - 10.0,
127 prev_desc: None,
128 last_desc: desc,
129 desc_changed_at: now - 10.0,
130 trigger_bounds: Rc::new(RefCell::new(None)),
131 card_bounds: Rc::new(RefCell::new(None)),
132 focus_handle,
133 }
134 }
135}
136
137#[derive(IntoElement)]
140pub struct ArcDialog {
141 id: ElementId,
142 title: SharedString,
143 description: Option<SharedString>,
144 trigger: Option<AnyElement>,
145 body: Option<AnyElement>,
146 open: Option<bool>,
147 default_open: bool,
148 content_only: bool,
149 max_width: Pixels,
150 on_open_change: Option<OpenCallback>,
151}
152
153impl ArcDialog {
154 pub fn new(id: impl Into<ElementId>, title: impl Into<SharedString>) -> Self {
155 Self {
156 id: id.into(),
157 title: title.into(),
158 description: None,
159 trigger: None,
160 body: None,
161 open: None,
162 default_open: false,
163 content_only: false,
164 max_width: px(DIALOG_MAX_WIDTH),
165 on_open_change: None,
166 }
167 }
168
169 pub fn description(mut self, description: impl Into<SharedString>) -> Self {
170 self.description = Some(description.into());
171 self
172 }
173
174 pub fn trigger(mut self, trigger: impl IntoElement) -> Self {
176 self.trigger = Some(trigger.into_any_element());
177 self
178 }
179
180 pub fn content(mut self, body: impl IntoElement) -> Self {
182 self.body = Some(body.into_any_element());
183 self
184 }
185
186 pub fn content_only(mut self, content_only: bool) -> Self {
189 self.content_only = content_only;
190 self
191 }
192
193 pub fn max_width(mut self, max_width: Pixels) -> Self {
195 if f32::from(max_width).is_finite() && max_width > px(0.0) {
196 self.max_width = max_width;
197 }
198 self
199 }
200
201 pub fn open(mut self, open: bool) -> Self {
202 self.open = Some(open);
203 self
204 }
205
206 pub fn default_open(mut self, default_open: bool) -> Self {
207 self.default_open = default_open;
208 self
209 }
210
211 pub fn on_open_change(
212 mut self,
213 handler: impl Fn(&bool, &mut Window, &mut App) + 'static,
214 ) -> Self {
215 self.on_open_change = Some(Rc::new(handler));
216 self
217 }
218}
219
220#[allow(clippy::too_many_arguments)]
221fn render_swap_slot(
222 old_text: Option<SharedString>,
223 new_text: SharedString,
224 elapsed: f64,
225 reduced: bool,
226 line_height_px: f32,
227 text_size: Pixels,
228 font_medium: bool,
229 color: Hsla,
230) -> AnyElement {
231 let (old_alpha, old_y, new_alpha, new_y) = swap_text_sample(elapsed, reduced);
232 let swapping = old_text.is_some() && old_alpha > 0.01;
233
234 if !swapping {
235 return div()
236 .line_height(px(line_height_px))
237 .text_size(text_size)
238 .when(font_medium, |d| d.font_medium())
239 .text_color(color)
240 .child(new_text)
241 .into_any_element();
242 }
243
244 div()
245 .relative()
246 .min_h(px(line_height_px))
247 .child(
248 div()
249 .line_height(px(line_height_px))
250 .text_size(text_size)
251 .when(font_medium, |d| d.font_medium())
252 .text_color(color)
253 .opacity(new_alpha)
254 .mt(px(new_y))
255 .child(new_text),
256 )
257 .when_some(old_text, |d, old| {
258 d.child(
259 div()
260 .absolute()
261 .top(px(old_y))
262 .left_0()
263 .right_0()
264 .line_height(px(line_height_px))
265 .text_size(text_size)
266 .when(font_medium, |d| d.font_medium())
267 .text_color(color)
268 .opacity(old_alpha)
269 .child(old),
270 )
271 })
272 .into_any_element()
273}
274
275impl RenderOnce for ArcDialog {
276 fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
277 let theme = cx.arc_theme().clone();
278 let colors = theme.colors;
279 let motion = theme.motion;
280 let reduced = is_reduced_motion(cx);
281 let now = motion_now(cx);
282
283 let desc_str = self.description.clone().unwrap_or_default();
284 let state_id = ElementId::NamedChild(self.id.clone().into(), "__arc-dialog-state".into());
285 let title_init = self.title.clone();
286 let desc_init = desc_str.clone();
287 let internal = window.use_keyed_state(state_id, cx, |_, cx| {
288 DialogInternal::new(
289 self.default_open,
290 title_init,
291 desc_init,
292 now,
293 cx.focus_handle(),
294 )
295 });
296
297 {
299 let snap = internal.read(cx).clone();
300 let is_open_now = self.open.unwrap_or(snap.open);
301 if snap.last_title != self.title {
302 let prev = snap.last_title.clone();
303 let next = self.title.clone();
304 internal.update(cx, |s, _| {
305 if is_open_now {
306 s.prev_title = Some(prev);
307 s.title_changed_at = now;
308 }
309 s.last_title = next;
310 });
311 }
312 if snap.last_desc != desc_str {
313 let prev = snap.last_desc.clone();
314 let next = desc_str.clone();
315 internal.update(cx, |s, _| {
316 if is_open_now && !prev.is_empty() {
317 s.prev_desc = Some(prev);
318 s.desc_changed_at = now;
319 }
320 s.last_desc = next;
321 });
322 }
323 }
324
325 let snap = internal.read(cx).clone();
326 let is_open = self.open.unwrap_or(snap.open);
327
328 let overlay_presence = ArcPresence::new((self.id.clone(), "dialog-overlay"), is_open)
329 .animate_initial(false)
330 .config(ArcPresenceConfig {
331 enter_duration: motion.duration.standard,
332 exit_duration: motion.duration.fast,
333 enter_ease: motion.ease.enter,
334 exit_ease: motion.ease.standard,
335 fade_on_reduced_motion: true,
336 })
337 .sample(window, cx);
338
339 let card_presence = ArcPresence::new((self.id.clone(), "dialog-card"), is_open)
340 .animate_initial(false)
341 .config(ArcPresenceConfig {
342 enter_duration: motion.duration.fast,
343 exit_duration: motion.duration.fast,
344 enter_ease: motion.ease.enter,
345 exit_ease: motion.ease.standard,
346 fade_on_reduced_motion: true,
347 })
348 .sample(window, cx);
349
350 let spring_y = if reduced {
351 0.0
352 } else {
353 arc_spring(
354 (self.id.clone(), "dialog-y"),
355 if is_open { 0.0 } else { 8.0 },
356 motion.spring.smooth,
357 window,
358 cx,
359 )
360 };
361 let y_offset = dialog_card_y_offset(is_open, spring_y, card_presence.progress, reduced);
362
363 let title_elapsed = (now - snap.title_changed_at).max(0.0);
364 let desc_elapsed = (now - snap.desc_changed_at).max(0.0);
365 if is_open && !reduced && (title_elapsed < 0.23 || desc_elapsed < 0.23) {
366 window.request_animation_frame();
367 }
368
369 let trigger_bounds_ref = snap.trigger_bounds.clone();
370 let trigger_probe = canvas(
371 move |rect, _, _| {
372 *trigger_bounds_ref.borrow_mut() = Some(rect);
373 },
374 |_, _, _, _| {},
375 )
376 .absolute()
377 .inset_0();
378
379 let internal_trig = internal.clone();
380 let on_open_trig = self.on_open_change.clone();
381 let focus_trig = snap.focus_handle.clone();
382 let mut root = div()
383 .id(self.id.clone())
384 .test_support()
385 .relative()
386 .flex()
387 .child(trigger_probe);
388
389 if let Some(trig) = self.trigger {
390 root = root.child(
391 div()
392 .id(ElementId::NamedChild(
393 self.id.clone().into(),
394 "trigger".into(),
395 ))
396 .test_support()
397 .role(Role::Button)
398 .aria_label("Open dialog")
399 .relative()
400 .flex()
401 .cursor_pointer()
402 .on_mouse_down(MouseButton::Left, move |_, window, cx| {
403 let t = motion_now(cx);
404 internal_trig.update(cx, |s, _| {
405 s.open = true;
406 s.last_open_changed_at = t;
407 });
408 focus_trig.focus(window, cx);
409 if let Some(h) = &on_open_trig {
410 h(&true, window, cx);
411 }
412 window.refresh();
413 })
414 .child(trig),
415 );
416 }
417
418 if !overlay_presence.should_render() && !card_presence.should_render() {
419 return root;
420 }
421
422 if is_open && !snap.focus_handle.contains_focused(window, cx) {
424 snap.focus_handle.focus(window, cx);
425 }
426
427 let card_bounds_ref = snap.card_bounds.clone();
430 let card_bounds_out = snap.card_bounds.clone();
431 let trig_bounds_out = snap.trigger_bounds.clone();
432 let internal_out = internal.clone();
433 let on_open_out = self.on_open_change.clone();
434 let opened_at = snap.last_open_changed_at;
435 let card_probe = canvas(
436 move |rect, _, _| {
437 *card_bounds_ref.borrow_mut() = Some(rect);
438 },
439 move |_, _, window, _| {
440 if is_open {
441 let c_ref = card_bounds_out.clone();
442 let t_ref = trig_bounds_out.clone();
443 let st = internal_out.clone();
444 let cb = on_open_out.clone();
445 window.on_mouse_event(move |event: &MouseDownEvent, phase, window, cx| {
446 if !phase.bubble() {
447 return;
448 }
449 let t_now = motion_now(cx);
450 if (t_now - opened_at).abs() < 0.001 {
451 return;
452 }
453 let in_card = c_ref.borrow().is_some_and(|b| b.contains(&event.position));
454 let in_trig = t_ref.borrow().is_some_and(|b| b.contains(&event.position));
455 if !in_card && !in_trig {
456 st.update(cx, |s, _| {
457 s.open = false;
458 s.last_open_changed_at = t_now;
459 });
460 if let Some(h) = &cb {
461 h(&false, window, cx);
462 }
463 window.refresh();
464 }
465 });
466 }
467 },
468 )
469 .absolute()
470 .inset_0();
471
472 let close_bg = if snap.close_hovered || snap.close_pressed {
474 colors.surface_muted
475 } else {
476 oklab_mix(colors.surface_muted, Hsla::transparent_black(), 0.5)
477 };
478 let close_fg = if snap.close_hovered || snap.close_pressed {
479 colors.foreground
480 } else {
481 colors.text_secondary
482 };
483 let close_scale = if snap.close_pressed && !reduced {
484 0.92
485 } else {
486 1.0
487 };
488 let internal_close_hover = internal.clone();
489 let internal_close_press = internal.clone();
490 let internal_close_up = internal.clone();
491 let on_open_close = self.on_open_change.clone();
492
493 let close_btn = div()
494 .id(ElementId::NamedChild(
495 self.id.clone().into(),
496 "close".into(),
497 ))
498 .test_support()
499 .role(Role::Button)
500 .aria_label("Close dialog")
501 .flex_none()
502 .w(Spacing::S8)
503 .h(Spacing::S8)
504 .rounded(Radii::CONTROL)
505 .border_1()
506 .border_color(colors.border)
507 .bg(close_bg)
508 .cursor_pointer()
509 .on_hover(move |hovered, window, cx| {
510 internal_close_hover.update(cx, |s, _| {
511 s.close_hovered = *hovered;
512 if !*hovered {
513 s.close_pressed = false;
514 }
515 });
516 window.refresh();
517 })
518 .on_mouse_down(MouseButton::Left, move |_, window, cx| {
519 cx.stop_propagation();
520 internal_close_press.update(cx, |s, _| {
521 s.close_pressed = true;
522 });
523 window.refresh();
524 })
525 .on_mouse_up(MouseButton::Left, move |_, window, cx| {
526 cx.stop_propagation();
527 let t = motion_now(cx);
528 internal_close_up.update(cx, |s, _| {
529 s.close_pressed = false;
530 s.open = false;
531 s.last_open_changed_at = t;
532 });
533 if let Some(h) = &on_open_close {
534 h(&false, window, cx);
535 }
536 window.refresh();
537 })
538 .child(
539 canvas(
540 |_, _, _| {},
541 move |rect, _, window, _| {
542 let cx = f32::from(rect.origin.x) + f32::from(rect.size.width) * 0.5;
543 let cy = f32::from(rect.origin.y) + f32::from(rect.size.height) * 0.5;
544 paint_close_x(cx, cy, close_scale, close_fg, window);
545 },
546 )
547 .size_full(),
548 );
549
550 let title_slot = render_swap_slot(
551 snap.prev_title.clone(),
552 self.title.clone(),
553 title_elapsed,
554 reduced,
555 24.0,
556 Typography::TEXT_LG,
557 true,
558 colors.foreground,
559 );
560
561 let desc_slot = self.description.as_ref().map(|d| {
562 render_swap_slot(
563 snap.prev_desc.clone(),
564 d.clone(),
565 desc_elapsed,
566 reduced,
567 21.0,
568 Typography::TEXT_SM,
569 false,
570 colors.text_secondary,
571 )
572 });
573
574 let header = div()
575 .w_full()
576 .flex()
577 .items_start()
578 .justify_between()
579 .gap(Spacing::S5)
580 .p(Spacing::S6)
581 .border_b_1()
582 .border_color(colors.border)
583 .child(
584 div()
585 .flex_1()
586 .flex()
587 .flex_col()
588 .gap(Spacing::S2)
589 .child(title_slot)
590 .when_some(desc_slot, |d, desc| d.child(desc)),
591 )
592 .child(close_btn);
593
594 let body = div()
595 .w_full()
596 .when(!self.content_only, |d| d.p(Spacing::S6))
597 .text_size(Typography::TEXT_SM)
598 .text_color(colors.foreground)
599 .when_some(self.body, |d, b| d.child(b));
600
601 let viewport = window.viewport_size();
602 let avail_w = (f32::from(viewport.width) - DIALOG_VIEWPORT_MARGIN).max(0.0);
603 let card_w = avail_w.min(f32::from(self.max_width));
604
605 let internal_key = internal.clone();
606 let on_open_key = self.on_open_change.clone();
607 let card = div()
608 .id(ElementId::NamedChild(
609 self.id.clone().into(),
610 "content".into(),
611 ))
612 .role(Role::Dialog)
613 .aria_label(self.title.clone())
614 .test_support()
615 .track_focus(&snap.focus_handle)
616 .relative()
617 .w(px(card_w))
618 .mt(px(y_offset))
619 .when(!self.content_only, |d| {
620 d.rounded(Radii::SURFACE)
621 .border_1()
622 .border_color(colors.border)
623 .bg(colors.surface_raised)
624 .shadow(Shadows::floating(theme.mode))
625 })
626 .text_color(colors.foreground)
627 .opacity(card_presence.progress.clamp(0.0, 1.0))
628 .on_mouse_down(MouseButton::Left, |_, _, cx| {
629 cx.stop_propagation();
630 })
631 .on_key_down(move |event: &KeyDownEvent, window, cx| {
632 if event.keystroke.key.as_str() == "escape" {
633 cx.stop_propagation();
634 let t = motion_now(cx);
635 internal_key.update(cx, |s, _| {
636 s.open = false;
637 s.last_open_changed_at = t;
638 });
639 if let Some(h) = &on_open_key {
640 h(&false, window, cx);
641 }
642 window.refresh();
643 }
644 })
645 .child(card_probe)
646 .when(!self.content_only, |d| d.child(header))
647 .child(body);
648
649 let mut backdrop_color = gpui_kit::hsla(0.0, 0.0, 0.04, 0.46);
651 backdrop_color.a *= overlay_presence.progress.clamp(0.0, 1.0);
652
653 let (rel_left, rel_top) = snap
654 .trigger_bounds
655 .borrow()
656 .map(|b| (-f32::from(b.origin.x), -f32::from(b.origin.y)))
657 .unwrap_or((0.0, 0.0));
658
659 let modal_layer = div()
660 .id(ElementId::NamedChild(
661 self.id.clone().into(),
662 "backdrop".into(),
663 ))
664 .test_support()
665 .absolute()
666 .left(px(rel_left))
667 .top(px(rel_top))
668 .w(viewport.width)
669 .h(viewport.height)
670 .bg(backdrop_color)
671 .flex()
672 .items_center()
673 .justify_center()
674 .p(Spacing::S4)
675 .when(is_open, |layer| {
677 layer.on_scroll(|_, _, cx| cx.stop_propagation())
678 })
679 .child(card);
680
681 root.child(deferred(modal_layer).with_priority(30))
682 }
683}
684
685#[cfg(test)]
686mod tests {
687 use std::{cell::Cell, rc::Rc};
688
689 use arc_tokens::{Accent, ThemeMode, install_arc_theme};
690 use gpui_kit::{
691 Context, IntoElement, MouseButton, ParentElement, Render, Role, SharedString, Styled,
692 TestAppContext, Window, div, px,
693 test::{TestEventExt as _, TestQueryExt as _, TestWindowExt as _},
694 };
695
696 use super::*;
697
698 struct DialogHarness {
699 open: bool,
700 title: SharedString,
701 close_count: Rc<Cell<usize>>,
702 }
703
704 impl Render for DialogHarness {
705 fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
706 let count = self.close_count.clone();
707 div().size_full().p(px(24.0)).child(
708 ArcDialog::new("test-dialog", self.title.clone())
709 .description("Confirm workspace archive")
710 .open(self.open)
711 .on_open_change(cx.listener(move |this, next: &bool, _, cx| {
712 this.open = *next;
713 if !*next {
714 count.set(count.get() + 1);
715 }
716 cx.notify();
717 }))
718 .trigger(div().w(px(100.0)).h(px(36.0)).child("Open dialog"))
719 .content(div().child("Dialog body content")),
720 )
721 }
722 }
723
724 #[gpui_kit::test]
725 fn modal_consumes_wheel_after_inner_list_and_unlocks_after_close(cx: &mut TestAppContext) {
726 use gpui_kit::{
727 InputEvent as _, ScrollDelta, ScrollEvent, ScrollHandle, TouchPhase, point, size,
728 };
729 struct ScrollDialog {
730 open: bool,
731 document: ScrollHandle,
732 list: ScrollHandle,
733 }
734 impl Render for ScrollDialog {
735 fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
736 div()
737 .id("modal-scroll-document")
738 .size_full()
739 .overflow_y_scroll()
740 .track_scroll(&self.document)
741 .child(
742 ArcDialog::new("scroll-dialog", "Scroll dialog")
743 .open(self.open)
744 .trigger(div().w(px(120.0)).h(px(36.0)).child("Open"))
745 .on_open_change(cx.listener(|this, open: &bool, _, cx| {
746 this.open = *open;
747 cx.notify();
748 }))
749 .content(
750 div()
751 .id("modal-scroll-list")
752 .test_support()
753 .h(px(120.0))
754 .overflow_y_scroll()
755 .track_scroll(&self.list)
756 .child(div().h(px(900.0)).flex_none()),
757 ),
758 )
759 .child(div().h(px(1800.0)).flex_none())
760 }
761 }
762 cx.update(|cx| {
763 gpui_kit::init(cx);
764 install_arc_theme(ThemeMode::Light, Accent::Violet, cx);
765 arc_motion::set_reduced_motion(arc_motion::ReducedMotionChoice::Reduce, cx);
766 });
767 let document = ScrollHandle::new();
768 let list = ScrollHandle::new();
769 let (_, cx) = cx.add_window_view(|_, _| ScrollDialog {
770 open: false,
771 document: document.clone(),
772 list: list.clone(),
773 });
774 cx.simulate_resize(size(px(800.0), px(700.0)));
775 cx.update(|w, cx| {
776 w.render_frame(cx);
777 w.click(
778 ElementId::NamedChild(ElementId::from("scroll-dialog").into(), "trigger".into()),
779 cx,
780 );
781 w.render_frame(cx);
782 });
783 cx.executor()
784 .advance_clock(std::time::Duration::from_millis(400));
785 cx.update(|w, cx| {
786 w.render_frame(cx);
787 for (index, delta) in [-60.0, -2000.0, -60.0, 60.0, 2000.0, 60.0]
788 .into_iter()
789 .enumerate()
790 {
791 let position = w.find("modal-scroll-list").bounds().center();
792 w.pointer_move(position, None, cx);
793 w.dispatch_event(
794 ScrollEvent {
795 position,
796 delta: ScrollDelta::Pixels(point(px(0.0), px(delta))),
797 modifiers: Default::default(),
798 touch_phase: TouchPhase::Moved,
799 source: Default::default(),
800 }
801 .to_platform_input(),
802 cx,
803 );
804 w.render_frame(cx);
805 assert_eq!(document.offset().y, px(0.0));
806 if index == 0 {
807 assert!(list.offset().y < px(0.0), "内层列表必须仍能滚动");
808 }
809 if index >= 4 {
810 assert_eq!(list.offset().y, px(0.0));
811 }
812 }
813 w.press("escape", cx);
814 });
815 cx.executor()
816 .advance_clock(std::time::Duration::from_millis(400));
817 cx.update(|w, cx| {
818 w.render_frame(cx);
819 let position = point(px(400.0), px(300.0));
820 w.pointer_move(position, None, cx);
821 w.dispatch_event(
822 ScrollEvent {
823 position,
824 delta: ScrollDelta::Pixels(point(px(0.0), px(-60.0))),
825 modifiers: Default::default(),
826 touch_phase: TouchPhase::Moved,
827 source: Default::default(),
828 }
829 .to_platform_input(),
830 cx,
831 );
832 w.render_frame(cx);
833 assert!(document.offset().y < px(0.0));
834 });
835 }
836
837 #[test]
838 fn card_offset_and_swap_text_curves_follow_spec() {
839 assert!((dialog_card_y_offset(true, 8.0, 0.0, false) - 8.0).abs() < 1e-4);
840 assert!((dialog_card_y_offset(true, 0.0, 1.0, false) - 0.0).abs() < 1e-4);
841 assert!((dialog_card_y_offset(false, 0.0, 0.0, false) - 4.0).abs() < 1e-4);
842 assert_eq!(dialog_card_y_offset(true, 6.0, 0.5, true), 0.0);
843
844 let (old_a0, _, new_a0, new_y0) = swap_text_sample(0.0, false);
845 assert!(old_a0 > 0.95);
846 assert!(new_a0 < 0.05);
847 assert!(new_y0 > 4.0);
848
849 let (old_a1, _, new_a1, new_y1) = swap_text_sample(0.25, false);
850 assert_eq!(old_a1, 0.0);
851 assert_eq!(new_a1, 1.0);
852 assert_eq!(new_y1, 0.0);
853 }
854
855 #[gpui_kit::test]
856 fn dialog_opens_on_trigger_and_closes_on_close_button_or_outside_click(
857 cx: &mut TestAppContext,
858 ) {
859 cx.update(|cx| {
860 gpui_kit::init(cx);
861 install_arc_theme(ThemeMode::Light, Accent::Violet, cx);
862 });
863 let close_count = Rc::new(Cell::new(0));
864 let (_view, cx) = cx.add_window_view(|_, _| DialogHarness {
865 open: false,
866 title: "Archive project".into(),
867 close_count: close_count.clone(),
868 });
869
870 cx.update(|window, cx| {
871 window.render_frame(cx);
872
873 let trig = window.find_by_label("Open dialog");
874 assert_eq!(trig.role(), Some(Role::Button));
875 let tpos = trig.bounds().center();
876 window.pointer_down(tpos, MouseButton::Left, cx);
877 window.pointer_up(tpos, MouseButton::Left, cx);
878 window.render_frame(cx);
879
880 let modal = window.find_by_label("Archive project");
881 assert_eq!(modal.role(), Some(Role::Dialog));
882
883 let close_btn = window.find_by_label("Close dialog");
885 let cpos = close_btn.bounds().center();
886 window.pointer_down(cpos, MouseButton::Left, cx);
887 window.pointer_up(cpos, MouseButton::Left, cx);
888 window.render_frame(cx);
889 assert_eq!(close_count.get(), 1);
890 });
891 }
892}