| 1 | use gpui::{ |
| 2 | Anchor, Animation, AnimationExt as _, AnyElement, App, Bounds, Context, Div, ElementId, |
| 3 | FocusHandle, InteractiveElement as _, IntoElement, MouseButton, ParentElement, PathBuilder, |
| 4 | Pixels, Point, RenderOnce, Stateful, StyleRefinement, Styled, Window, canvas, point, |
| 5 | prelude::FluentBuilder as _, px, |
| 6 | }; |
| 7 | use std::{cell::Cell, rc::Rc, time::Duration}; |
| 8 | |
| 9 | use crate::{ActiveTheme as _, ThemeStyled as _}; |
| 10 | use crate::{ |
| 11 | Selectable, StyledExt as _, |
| 12 | animation::ease_out_cubic, |
| 13 | styled::popover_ring, |
| 14 | v_flex, |
| 15 | }; |
| 16 | use gpui_base::Placement; |
| 17 | use gpui_base::Popover as BasePopover; |
| 18 | pub use gpui_base::PopoverState; |
| 19 | |
| 20 | pub(crate) fn init(_: &mut App) {} |
| 21 | |
| 22 | /// How long a dropdown takes to settle into place after it opens. |
| 23 | /// |
| 24 | /// This is shadcn/ui's figure: its popup surfaces carry `animate-in`, whose |
| 25 | /// duration is 150ms. |
| 26 | const DROPDOWN_ENTER_DURATION: Duration = Duration::from_millis(150); |
| 27 | |
| 28 | /// Where a dropdown starts out, relative to where it comes to rest. |
| 29 | /// |
| 30 | /// Negative is above, so the surface slides *down* out of the trigger's edge — |
| 31 | /// what shadcn/ui expresses as `data-[side=bottom]:slide-in-from-top-2`. Its |
| 32 | /// `2` is `0.5rem`, which is 8px at the default root size. |
| 33 | const DROPDOWN_ENTER_OFFSET: Pixels = px(-8.); |
| 34 | |
| 35 | fn dropdown_positioner(bounds: Bounds<Pixels>) -> gpui_base::Positioner { |
| 36 | gpui_base::Positioner::side(bounds) |
| 37 | .placement(gpui_base::Placement::Bottom) |
| 38 | .align(gpui_base::Align::Start) |
| 39 | .offset(crate::fluent::DROPDOWN_OFFSET) |
| 40 | .margin(px(8.)) |
| 41 | } |
| 42 | |
| 43 | /// Positions a dropdown surface under its trigger and animates it in. |
| 44 | /// |
| 45 | /// This is the shared open motion for Select, Combobox and DatePicker, modelled |
| 46 | /// on shadcn/ui: over 150ms the surface fades up from nothing while sliding the |
| 47 | /// last 8px out of the trigger's edge, on an ease-out curve so it decelerates |
| 48 | /// into place. |
| 49 | /// |
| 50 | /// `surface` must be the panel itself — the element carrying |
| 51 | /// [`ThemeStyled::popover_style`] — and not a wrapper around it. GPUI takes a |
| 52 | /// shadow's shape from the element it is set on, so a wrapper of a different |
| 53 | /// size would throw the shadow out of register with the panel. |
| 54 | /// |
| 55 | /// # Why the shadow is animated too |
| 56 | /// |
| 57 | /// GPUI has no group compositing: `opacity` multiplies into each primitive's |
| 58 | /// alpha separately rather than fading a composited subtree. A drop shadow is |
| 59 | /// painted as a full blurred rect *under* the element — the shader only cuts the |
| 60 | /// element out of `inset` shadows — so a translucent panel does not hide its own |
| 61 | /// shadow, and mid-fade the shadow shows straight through the panel as a dark |
| 62 | /// slab. Ramping the ink by the cube of the fade keeps it out of sight until the |
| 63 | /// panel is opaque enough to cover it, and still lands on the resting shadow |
| 64 | /// [`crate::elevation::flyout`] gives every other popup. |
| 65 | /// |
| 66 | /// # Departures from shadcn |
| 67 | /// |
| 68 | /// - shadcn also scales the surface up from 95% (`zoom-in-95`). GPUI has no |
| 69 | /// element transform — only images and SVGs take a `TransformationMatrix` — |
| 70 | /// so there is nothing to scale a subtree with, and the fade and slide carry |
| 71 | /// the motion on their own. |
| 72 | /// - There is no exit motion. A closing dropdown stops being rendered in the |
| 73 | /// same frame its state flips, so playing one would mean keeping the surface |
| 74 | /// mounted past the close, which is a change to how each of these components |
| 75 | /// tracks `open`. |
| 76 | /// - The slide always comes from above. [`gpui_base::Positioner`] resolves the |
| 77 | /// side the surface actually lands on during layout and does not report it |
| 78 | /// back, so a dropdown that flips above its trigger for want of room below |
| 79 | /// slides the opposite way — 8px over 150ms, in the rare case where it |
| 80 | /// happens. |
| 81 | /// |
| 82 | /// Reduced motion needs no handling here: GPUI's animation element adopts the |
| 83 | /// final value on the first frame when the system asks for it. |
| 84 | pub(crate) fn dropdown_popup( |
| 85 | id: impl Into<ElementId>, |
| 86 | bounds: Bounds<Pixels>, |
| 87 | surface: impl IntoElement + Styled + 'static, |
| 88 | _cx: &App, |
| 89 | ) -> gpui_base::Positioner { |
| 90 | let travel: f32 = DROPDOWN_ENTER_OFFSET.into(); |
| 91 | dropdown_positioner(bounds).child(surface.with_animation( |
| 92 | id, |
| 93 | Animation::new(DROPDOWN_ENTER_DURATION).with_easing(ease_out_cubic), |
| 94 | move |surface, delta| { |
| 95 | surface.top(px(travel * (1. - delta))).opacity(delta) |
| 96 | }, |
| 97 | )) |
| 98 | } |
| 99 | |
| 100 | /// A popover element that can be triggered by a button or any other element. |
| 101 | #[derive(IntoElement)] |
| 102 | pub struct Popover { |
| 103 | id: ElementId, |
| 104 | style: StyleRefinement, |
| 105 | anchor: Anchor, |
| 106 | offset: Option<Pixels>, |
| 107 | arrow: bool, |
| 108 | default_open: bool, |
| 109 | open: Option<bool>, |
| 110 | tracked_focus_handle: Option<FocusHandle>, |
| 111 | trigger: Option<Box<dyn FnOnce(bool, &Window, &App) -> AnyElement + 'static>>, |
| 112 | content: Option< |
| 113 | Rc< |
| 114 | dyn Fn(&mut PopoverState, &mut Window, &mut Context<PopoverState>) -> AnyElement |
| 115 | + 'static, |
| 116 | >, |
| 117 | >, |
| 118 | children: Vec<AnyElement>, |
| 119 | /// Style for the trigger container, the element laid out in the parent |
| 120 | /// and measured to anchor the popup. |
| 121 | trigger_style: Option<StyleRefinement>, |
| 122 | mouse_button: MouseButton, |
| 123 | appearance: bool, |
| 124 | overlay_closable: bool, |
| 125 | on_open_change: Option<Rc<dyn Fn(&bool, &mut Window, &mut App)>>, |
| 126 | } |
| 127 | |
| 128 | impl Popover { |
| 129 | /// Create a new Popover with `view` mode. |
| 130 | pub fn new(id: impl Into<ElementId>) -> Self { |
| 131 | Self { |
| 132 | id: id.into(), |
| 133 | style: StyleRefinement::default(), |
| 134 | anchor: Anchor::TopLeft, |
| 135 | offset: None, |
| 136 | arrow: false, |
| 137 | trigger: None, |
| 138 | trigger_style: None, |
| 139 | content: None, |
| 140 | tracked_focus_handle: None, |
| 141 | children: vec![], |
| 142 | mouse_button: MouseButton::Left, |
| 143 | appearance: true, |
| 144 | overlay_closable: true, |
| 145 | default_open: false, |
| 146 | open: None, |
| 147 | on_open_change: None, |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | /// Set the anchor corner of the popover, default is [`Anchor::TopLeft`]. |
| 152 | /// |
| 153 | /// This names the popover's own anchor, not a corner of the trigger. |
| 154 | /// `TopLeft` opens below the trigger, left-aligned; `BottomRight` opens |
| 155 | /// above it, right-aligned. Legacy anchoring clamps without flipping. |
| 156 | pub fn anchor(mut self, anchor: impl Into<Anchor>) -> Self { |
| 157 | self.anchor = anchor.into(); |
| 158 | self |
| 159 | } |
| 160 | |
| 161 | /// Gap from the trigger to the surface (or arrow tip), default 0.25rem. |
| 162 | /// Preserves the anchor and does not enable automatic flipping. |
| 163 | pub fn offset(mut self, offset: impl Into<Pixels>) -> Self { |
| 164 | self.offset = Some(offset.into()); |
| 165 | self |
| 166 | } |
| 167 | |
| 168 | /// Show an arrow pointing toward the trigger. Default is `false`. |
| 169 | /// Follows the anchor, with its base inset to avoid rounded corners. |
| 170 | /// Uses the surface background, falling back to the theme's popover color. |
| 171 | pub fn arrow(mut self, arrow: bool) -> Self { |
| 172 | self.arrow = arrow; |
| 173 | self |
| 174 | } |
| 175 | |
| 176 | /// Set the mouse button to trigger the popover, default is `MouseButton::Left`. |
| 177 | pub fn mouse_button(mut self, mouse_button: MouseButton) -> Self { |
| 178 | self.mouse_button = mouse_button; |
| 179 | self |
| 180 | } |
| 181 | |
| 182 | /// Set the trigger element of the popover. |
| 183 | pub fn trigger<T>(mut self, trigger: T) -> Self |
| 184 | where |
| 185 | T: Selectable + IntoElement + 'static, |
| 186 | { |
| 187 | self.trigger = Some(Box::new(|is_open, _, _| { |
| 188 | let open = trigger.is_open(); |
| 189 | trigger.open(open || is_open).into_any_element() |
| 190 | })); |
| 191 | self |
| 192 | } |
| 193 | |
| 194 | /// Set the default open state of the popover, default is `false`. |
| 195 | /// |
| 196 | /// This is only used to initialize the open state of the popover. |
| 197 | /// |
| 198 | /// And please note that if you use the `open` method, this value will be ignored. |
| 199 | pub fn default_open(mut self, open: bool) -> Self { |
| 200 | self.default_open = open; |
| 201 | self |
| 202 | } |
| 203 | |
| 204 | /// Force set the open state of the popover. |
| 205 | /// |
| 206 | /// If this is set, the popover will be controlled by this value. |
| 207 | /// |
| 208 | /// NOTE: You must be used in conjunction with `on_open_change` to handle state changes. |
| 209 | pub fn open(mut self, open: bool) -> Self { |
| 210 | self.open = Some(open); |
| 211 | self |
| 212 | } |
| 213 | |
| 214 | /// Add a callback to be called when the open state changes. |
| 215 | /// |
| 216 | /// The first `&bool` parameter is the **new open state**. |
| 217 | /// |
| 218 | /// This is useful when using the `open` method to control the popover state. |
| 219 | pub fn on_open_change<F>(mut self, callback: F) -> Self |
| 220 | where |
| 221 | F: Fn(&bool, &mut Window, &mut App) + 'static, |
| 222 | { |
| 223 | self.on_open_change = Some(Rc::new(callback)); |
| 224 | self |
| 225 | } |
| 226 | |
| 227 | /// Set the style for the trigger container. |
| 228 | /// |
| 229 | /// The container is the element laid out in the parent and measured to |
| 230 | /// anchor the popup, so this is where `w_full` or `flex_1` must go for the |
| 231 | /// trigger to fill its slot. |
| 232 | pub fn trigger_style(mut self, style: StyleRefinement) -> Self { |
| 233 | self.trigger_style = Some(style); |
| 234 | self |
| 235 | } |
| 236 | |
| 237 | /// Set whether clicking outside the popover will dismiss it, default is `true`. |
| 238 | pub fn overlay_closable(mut self, closable: bool) -> Self { |
| 239 | self.overlay_closable = closable; |
| 240 | self |
| 241 | } |
| 242 | |
| 243 | /// Set the content builder for content of the Popover. |
| 244 | /// |
| 245 | /// This callback will called every time on render the popover. |
| 246 | /// So, you should avoid creating new elements or entities in the content closure. |
| 247 | pub fn content<F, E>(mut self, content: F) -> Self |
| 248 | where |
| 249 | E: IntoElement, |
| 250 | F: Fn(&mut PopoverState, &mut Window, &mut Context<PopoverState>) -> E + 'static, |
| 251 | { |
| 252 | self.content = Some(Rc::new(move |state, window, cx| { |
| 253 | content(state, window, cx).into_any_element() |
| 254 | })); |
| 255 | self |
| 256 | } |
| 257 | |
| 258 | /// Set whether the popover no style, default is `false`. |
| 259 | /// |
| 260 | /// If no style: |
| 261 | /// |
| 262 | /// - The popover will not have a bg, border, shadow, or padding. |
| 263 | /// - The click out of the popover will not dismiss it. |
| 264 | pub fn appearance(mut self, appearance: bool) -> Self { |
| 265 | self.appearance = appearance; |
| 266 | self |
| 267 | } |
| 268 | |
| 269 | /// Bind the focus handle to receive focus when the popover is opened. |
| 270 | /// If you not set this, a new focus handle will be created for the popover to |
| 271 | /// |
| 272 | /// If popover is opened, the focus will be moved to the focus handle. |
| 273 | pub fn track_focus(mut self, handle: &FocusHandle) -> Self { |
| 274 | self.tracked_focus_handle = Some(handle.clone()); |
| 275 | self |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | impl ParentElement for Popover { |
| 280 | fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) { |
| 281 | self.children.extend(elements); |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | impl Styled for Popover { |
| 286 | fn style(&mut self) -> &mut StyleRefinement { |
| 287 | &mut self.style |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | impl Popover { |
| 292 | pub(crate) fn render_popover_content( |
| 293 | anchor: Anchor, |
| 294 | appearance: bool, |
| 295 | window: &mut Window, |
| 296 | cx: &mut App, |
| 297 | ) -> Stateful<Div> { |
| 298 | v_flex() |
| 299 | .id("content") |
| 300 | .occlude() |
| 301 | .tab_group() |
| 302 | .when(appearance, |this| this.popover_style(window, cx).p_3()) |
| 303 | .map(|this| match anchor { |
| 304 | Anchor::TopLeft | Anchor::TopCenter | Anchor::TopRight => this.top_1(), |
| 305 | Anchor::BottomLeft | Anchor::BottomCenter | Anchor::BottomRight => this.bottom_1(), |
| 306 | Anchor::LeftCenter | Anchor::RightCenter => this.top_1(), // Fallback for centered |
| 307 | }) |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | impl RenderOnce for Popover { |
| 312 | fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement { |
| 313 | let anchor = self.anchor; |
| 314 | let arrow_size = if self.arrow { |
| 315 | window.rem_size() * 0.375 |
| 316 | } else { |
| 317 | px(0.) |
| 318 | }; |
| 319 | let offset = self.offset.unwrap_or(window.rem_size() * 0.25) + arrow_size; |
| 320 | let resolved = Rc::new(Cell::new(None)); |
| 321 | let arrow_position = resolved.clone(); |
| 322 | let arrow = self.arrow; |
| 323 | let background = self |
| 324 | .style |
| 325 | .background |
| 326 | .as_ref() |
| 327 | .and_then(gpui::Fill::color) |
| 328 | .unwrap_or_else(|| cx.theme().popover.into()); |
| 329 | let ring = popover_ring(cx); |
| 330 | let radius = cx.theme().radius; |
| 331 | let appearance = self.appearance; |
| 332 | let style = self.style; |
| 333 | let children = self.children; |
| 334 | let content = self.content; |
| 335 | let trigger_style = self.trigger_style; |
| 336 | |
| 337 | BasePopover::new(self.id) |
| 338 | .anchor(self.anchor) |
| 339 | .offset(offset) |
| 340 | .on_position(move |position, trigger| resolved.set(Some((position, trigger)))) |
| 341 | .mouse_button(self.mouse_button) |
| 342 | .default_open(self.default_open) |
| 343 | .overlay_closable(self.overlay_closable) |
| 344 | .content(move |state, window, cx| { |
| 345 | v_flex() |
| 346 | .id("content") |
| 347 | .occlude() |
| 348 | .tab_group() |
| 349 | .when(appearance, |this| this.popover_style(window, cx).p_3()) |
| 350 | .when_some(content, |this, content| { |
| 351 | this.child((content)(state, window, cx)) |
| 352 | }) |
| 353 | .children(children) |
| 354 | .refine_style(&style) |
| 355 | .when(arrow, |this| { |
| 356 | this.child( |
| 357 | canvas( |
| 358 | |_, _, _| {}, |
| 359 | move |bounds, _, window, _| { |
| 360 | let Some((_, trigger)) = arrow_position.get() else { |
| 361 | return; |
| 362 | }; |
| 363 | let (side, target) = arrow_anchor(anchor, trigger); |
| 364 | let trigger = Bounds::new(target, gpui::size(px(0.), px(0.))); |
| 365 | let points = |
| 366 | arrow_points(bounds, trigger, side, arrow_size, radius); |
| 367 | let mut fill = PathBuilder::fill(); |
| 368 | fill.move_to(points[0]); |
| 369 | fill.line_to(points[1]); |
| 370 | fill.line_to(points[2]); |
| 371 | fill.close(); |
| 372 | if let Ok(path) = fill.build() { |
| 373 | window.paint_path(path, background); |
| 374 | } |
| 375 | // The triangle ends exactly at the surface edge. |
| 376 | // Cover the ring and the antialiased base on both |
| 377 | // sides of that edge before drawing its two slopes. |
| 378 | window.paint_quad(gpui::fill( |
| 379 | arrow_join_bounds(points, side, px(1.)), |
| 380 | background, |
| 381 | )); |
| 382 | if appearance { |
| 383 | let mut outline = PathBuilder::stroke(px(1.)); |
| 384 | outline.move_to(points[0]); |
| 385 | outline.line_to(points[1]); |
| 386 | outline.line_to(points[2]); |
| 387 | if let Ok(path) = outline.build() { |
| 388 | window.paint_path(path, ring); |
| 389 | } |
| 390 | } |
| 391 | }, |
| 392 | ) |
| 393 | .absolute() |
| 394 | .inset_0() |
| 395 | .size_full(), |
| 396 | ) |
| 397 | }) |
| 398 | }) |
| 399 | .when_some(self.trigger, |this, trigger| this.trigger_with(trigger)) |
| 400 | .when_some(trigger_style, |this, style| this.refine_style(&style)) |
| 401 | .when_some(self.open, |this, open| this.open(open)) |
| 402 | .when_some(self.tracked_focus_handle, |this, handle| { |
| 403 | this.track_focus(&handle) |
| 404 | }) |
| 405 | .when_some(self.on_open_change, |this, callback| { |
| 406 | this.on_open_change(move |open, window, cx| callback(open, window, cx)) |
| 407 | }) |
| 408 | .into_any_element() |
| 409 | } |
| 410 | } |
| 411 | |
| 412 | /// The arrow follows the named anchor instead of always aiming at trigger center. |
| 413 | fn arrow_anchor(anchor: Anchor, trigger: Bounds<Pixels>) -> (Placement, Point<Pixels>) { |
| 414 | match anchor { |
| 415 | Anchor::TopLeft => (Placement::Bottom, trigger.bottom_left()), |
| 416 | Anchor::TopCenter => (Placement::Bottom, trigger.bottom_center()), |
| 417 | Anchor::TopRight => (Placement::Bottom, trigger.bottom_right()), |
| 418 | Anchor::BottomLeft => (Placement::Top, trigger.origin), |
| 419 | Anchor::BottomCenter => (Placement::Top, trigger.top_center()), |
| 420 | Anchor::BottomRight => (Placement::Top, trigger.top_right()), |
| 421 | Anchor::LeftCenter => (Placement::Right, trigger.right_center()), |
| 422 | Anchor::RightCenter => (Placement::Left, trigger.left_center()), |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | /// Clamp the arrow base clear of rounded corners while aiming at the trigger. |
| 427 | fn arrow_points( |
| 428 | surface: Bounds<Pixels>, |
| 429 | trigger: Bounds<Pixels>, |
| 430 | side: Placement, |
| 431 | depth: Pixels, |
| 432 | radius: Pixels, |
| 433 | ) -> [Point<Pixels>; 3] { |
| 434 | let horizontal = side.is_horizontal(); |
| 435 | let (start, end, target) = if horizontal { |
| 436 | (surface.top(), surface.bottom(), trigger.center().y) |
| 437 | } else { |
| 438 | (surface.left(), surface.right(), trigger.center().x) |
| 439 | }; |
| 440 | let half = depth.min((end - start) * 0.5); |
| 441 | let inset = (radius + half).min((end - start) * 0.5); |
| 442 | let center = target.clamp(start + inset, end - inset); |
| 443 | match side { |
| 444 | Placement::Bottom => [ |
| 445 | point(center - half, surface.top()), |
| 446 | point(center, surface.top() - depth), |
| 447 | point(center + half, surface.top()), |
| 448 | ], |
| 449 | Placement::Top => [ |
| 450 | point(center - half, surface.bottom()), |
| 451 | point(center, surface.bottom() + depth), |
| 452 | point(center + half, surface.bottom()), |
| 453 | ], |
| 454 | Placement::Right => [ |
| 455 | point(surface.left(), center - half), |
| 456 | point(surface.left() - depth, center), |
| 457 | point(surface.left(), center + half), |
| 458 | ], |
| 459 | Placement::Left => [ |
| 460 | point(surface.right(), center - half), |
| 461 | point(surface.right() + depth, center), |
| 462 | point(surface.right(), center + half), |
| 463 | ], |
| 464 | } |
| 465 | } |
| 466 | |
| 467 | fn arrow_join_bounds( |
| 468 | points: [Point<Pixels>; 3], |
| 469 | side: Placement, |
| 470 | stroke: Pixels, |
| 471 | ) -> Bounds<Pixels> { |
| 472 | // Inset by the stroke width so the patch remains inside the triangle's |
| 473 | // slopes at the outer edge of the ring, including on very small surfaces. |
| 474 | if side.is_horizontal() { |
| 475 | let inset = stroke.min((points[2].y - points[0].y) * 0.5); |
| 476 | Bounds::from_corners( |
| 477 | point(points[0].x - stroke, points[0].y + inset), |
| 478 | point(points[2].x + stroke, points[2].y - inset), |
| 479 | ) |
| 480 | } else { |
| 481 | let inset = stroke.min((points[2].x - points[0].x) * 0.5); |
| 482 | Bounds::from_corners( |
| 483 | point(points[0].x + inset, points[0].y - stroke), |
| 484 | point(points[2].x - inset, points[2].y + stroke), |
| 485 | ) |
| 486 | } |
| 487 | } |
| 488 | |
| 489 | #[cfg(test)] |
| 490 | mod tests { |
| 491 | use super::*; |
| 492 | use crate::{button::Button, h_flex, theme::Theme}; |
| 493 | use gpui::{Bounds, Context, MouseButton, Point, Render, div, point, px, size}; |
| 494 | use gpui_base::Popup as BasePopup; |
| 495 | use std::{cell::RefCell, rc::Rc}; |
| 496 | |
| 497 | #[test] |
| 498 | fn test_popover_builder_chaining() { |
| 499 | let popover = Popover::new("test") |
| 500 | .anchor(Anchor::BottomCenter) |
| 501 | .mouse_button(MouseButton::Right) |
| 502 | .default_open(true) |
| 503 | .appearance(false) |
| 504 | .overlay_closable(false); |
| 505 | |
| 506 | assert_eq!(popover.anchor, Anchor::BottomCenter); |
| 507 | assert_eq!(popover.mouse_button, MouseButton::Right); |
| 508 | assert!(popover.default_open); |
| 509 | assert!(!popover.appearance); |
| 510 | assert!(!popover.overlay_closable); |
| 511 | } |
| 512 | |
| 513 | #[test] |
| 514 | fn test_resolved_corner_top_positions() { |
| 515 | use gpui::px; |
| 516 | |
| 517 | let bounds = Bounds { |
| 518 | origin: Point { |
| 519 | x: px(100.), |
| 520 | y: px(100.), |
| 521 | }, |
| 522 | size: gpui::Size { |
| 523 | width: px(200.), |
| 524 | height: px(50.), |
| 525 | }, |
| 526 | }; |
| 527 | |
| 528 | let pos = BasePopup::resolved_corner(Anchor::TopLeft, bounds); |
| 529 | assert_eq!(pos.x, px(100.)); |
| 530 | assert_eq!(pos.y, px(100.)); |
| 531 | |
| 532 | let pos = BasePopup::resolved_corner(Anchor::TopCenter, bounds); |
| 533 | assert_eq!(pos.x, px(200.)); |
| 534 | assert_eq!(pos.y, px(100.)); |
| 535 | |
| 536 | let pos = BasePopup::resolved_corner(Anchor::TopRight, bounds); |
| 537 | assert_eq!(pos.x, px(300.)); |
| 538 | assert_eq!(pos.y, px(100.)); |
| 539 | |
| 540 | let pos = BasePopup::resolved_corner(Anchor::BottomLeft, bounds); |
| 541 | assert_eq!(pos.x, px(100.)); |
| 542 | assert_eq!(pos.y, px(50.)); |
| 543 | |
| 544 | let pos = BasePopup::resolved_corner(Anchor::BottomCenter, bounds); |
| 545 | assert_eq!(pos.x, px(200.)); |
| 546 | assert_eq!(pos.y, px(50.)); |
| 547 | |
| 548 | let pos = BasePopup::resolved_corner(Anchor::BottomRight, bounds); |
| 549 | assert_eq!(pos.x, px(300.)); |
| 550 | assert_eq!(pos.y, px(50.)); |
| 551 | } |
| 552 | |
| 553 | struct PopoverHarness { |
| 554 | changes: Rc<RefCell<Vec<bool>>>, |
| 555 | } |
| 556 | |
| 557 | struct AnchorHarness { |
| 558 | anchor: Anchor, |
| 559 | offset: Option<Pixels>, |
| 560 | origin: Point<Pixels>, |
| 561 | arrow: bool, |
| 562 | } |
| 563 | |
| 564 | impl Render for AnchorHarness { |
| 565 | fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement { |
| 566 | div().size_full().child( |
| 567 | div() |
| 568 | .absolute() |
| 569 | .left(self.origin.x) |
| 570 | .top(self.origin.y) |
| 571 | .child( |
| 572 | Popover::new("positioned-popover") |
| 573 | .default_open(true) |
| 574 | .appearance(false) |
| 575 | .arrow(self.arrow) |
| 576 | .anchor(self.anchor) |
| 577 | .when_some(self.offset, |this, gap| this.offset(gap)) |
| 578 | .trigger(Button::new("positioned-trigger").size(px(40.))) |
| 579 | .child( |
| 580 | div() |
| 581 | .debug_selector(|| "positioned-content".into()) |
| 582 | .size(px(60.)), |
| 583 | ), |
| 584 | ), |
| 585 | ) |
| 586 | } |
| 587 | } |
| 588 | |
| 589 | #[gpui::test] |
| 590 | fn anchor_and_offset_position_the_surface_on_each_trigger_edge(cx: &mut gpui::TestAppContext) { |
| 591 | cx.update(crate::init); |
| 592 | let (view, window) = cx.add_window_view(|_, _| AnchorHarness { |
| 593 | anchor: Anchor::TopLeft, |
| 594 | offset: None, |
| 595 | origin: point(px(200.), px(200.)), |
| 596 | arrow: false, |
| 597 | }); |
| 598 | window.update(|window, cx| window.draw(cx).clear(cx)); |
| 599 | window.update(|window, cx| window.draw(cx).clear(cx)); |
| 600 | // Legacy TopLeft means below the trigger, including the default 0.25rem gap. |
| 601 | let legacy = window.debug_bounds("positioned-content").unwrap(); |
| 602 | assert_eq!(legacy.left(), px(200.)); |
| 603 | assert_eq!(legacy.top(), px(244.)); |
| 604 | |
| 605 | for (side, x, y) in [ |
| 606 | (Anchor::BottomLeft, 200., 128.), |
| 607 | (Anchor::BottomCenter, 190., 128.), |
| 608 | (Anchor::BottomRight, 180., 128.), |
| 609 | (Anchor::TopLeft, 200., 252.), |
| 610 | (Anchor::TopCenter, 190., 252.), |
| 611 | (Anchor::TopRight, 180., 252.), |
| 612 | (Anchor::RightCenter, 128., 190.), |
| 613 | (Anchor::LeftCenter, 252., 190.), |
| 614 | ] { |
| 615 | window.update(|window, cx| { |
| 616 | view.update(cx, |view, cx| { |
| 617 | view.anchor = side; |
| 618 | view.offset = Some(px(12.)); |
| 619 | cx.notify(); |
| 620 | }); |
| 621 | window.draw(cx).clear(cx); |
| 622 | }); |
| 623 | assert_eq!( |
| 624 | window.debug_bounds("positioned-content").unwrap().origin, |
| 625 | point(px(x), px(y)), |
| 626 | "{side:?}" |
| 627 | ); |
| 628 | } |
| 629 | // Current-frame trigger bounds must be used after the owner moves. |
| 630 | window.update(|window, cx| { |
| 631 | view.update(cx, |view, cx| { |
| 632 | view.origin = point(px(260.), px(240.)); |
| 633 | cx.notify(); |
| 634 | }); |
| 635 | window.draw(cx).clear(cx); |
| 636 | }); |
| 637 | assert_eq!( |
| 638 | window.debug_bounds("positioned-content").unwrap().origin, |
| 639 | point(px(312.), px(230.)) |
| 640 | ); |
| 641 | } |
| 642 | |
| 643 | struct TriggerStyleHarness { |
| 644 | styled: bool, |
| 645 | } |
| 646 | |
| 647 | impl Render for TriggerStyleHarness { |
| 648 | fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement { |
| 649 | // Keep the trigger clear of the popup's window margin so the |
| 650 | // content origin reflects only the trigger container's bounds. |
| 651 | div().size_full().child( |
| 652 | h_flex() |
| 653 | .absolute() |
| 654 | .left(px(100.)) |
| 655 | .top(px(100.)) |
| 656 | .w(px(200.)) |
| 657 | .child( |
| 658 | Popover::new("trigger-style-popover") |
| 659 | .default_open(true) |
| 660 | .appearance(false) |
| 661 | .offset(px(0.)) |
| 662 | .anchor(Anchor::TopRight) |
| 663 | .when(self.styled, |this| { |
| 664 | this.trigger_style(StyleRefinement::default().w_full()) |
| 665 | }) |
| 666 | .trigger(Button::new("styled-trigger").size(px(40.))) |
| 667 | .child( |
| 668 | div() |
| 669 | .debug_selector(|| "styled-content".into()) |
| 670 | .size(px(20.)), |
| 671 | ), |
| 672 | ), |
| 673 | ) |
| 674 | } |
| 675 | } |
| 676 | |
| 677 | /// `trigger_style` styles the container the parent lays out and the popup |
| 678 | /// is anchored to. Applied to anything nested inside it, `w_full` would |
| 679 | /// resolve against a container that only wraps its content. |
| 680 | #[gpui::test] |
| 681 | fn trigger_style_is_applied_to_the_trigger_container(cx: &mut gpui::TestAppContext) { |
| 682 | cx.update(crate::init); |
| 683 | let (view, window) = cx.add_window_view(|_, _| TriggerStyleHarness { styled: false }); |
| 684 | window.update(|window, cx| window.draw(cx).clear(cx)); |
| 685 | window.update(|window, cx| window.draw(cx).clear(cx)); |
| 686 | // Unstyled: the container wraps the 40px trigger, so the content's right |
| 687 | // edge meets the trigger's right edge at 140px. |
| 688 | assert_eq!( |
| 689 | window.debug_bounds("styled-content").unwrap().origin, |
| 690 | point(px(120.), px(140.)) |
| 691 | ); |
| 692 | |
| 693 | window.update(|window, cx| { |
| 694 | view.update(cx, |view, cx| { |
| 695 | view.styled = true; |
| 696 | cx.notify(); |
| 697 | }); |
| 698 | window.draw(cx).clear(cx); |
| 699 | }); |
| 700 | window.update(|window, cx| window.draw(cx).clear(cx)); |
| 701 | // `w_full` stretches the container across the 200px row, and the popup |
| 702 | // follows the container's right edge at 300px. |
| 703 | assert_eq!( |
| 704 | window.debug_bounds("styled-content").unwrap().origin, |
| 705 | point(px(280.), px(140.)) |
| 706 | ); |
| 707 | } |
| 708 | |
| 709 | #[gpui::test] |
| 710 | fn arrow_reserves_space_without_changing_anchor_alignment(cx: &mut gpui::TestAppContext) { |
| 711 | cx.update(crate::init); |
| 712 | let (view, window) = cx.add_window_view(|_, _| AnchorHarness { |
| 713 | anchor: Anchor::TopLeft, |
| 714 | offset: Some(px(12.)), |
| 715 | origin: point(px(200.), px(8.)), |
| 716 | arrow: true, |
| 717 | }); |
| 718 | window.update(|window, cx| window.draw(cx).clear(cx)); |
| 719 | window.update(|window, cx| window.draw(cx).clear(cx)); |
| 720 | // Bottom edge 48 + tip gap 12 + arrow depth 6. |
| 721 | assert_eq!( |
| 722 | window.debug_bounds("positioned-content").unwrap().origin, |
| 723 | point(px(200.), px(66.)) |
| 724 | ); |
| 725 | window.update(|window, cx| { |
| 726 | view.update(cx, |view, cx| { |
| 727 | view.anchor = Anchor::TopRight; |
| 728 | view.arrow = false; |
| 729 | cx.notify(); |
| 730 | }); |
| 731 | window.draw(cx).clear(cx); |
| 732 | }); |
| 733 | assert_eq!( |
| 734 | window.debug_bounds("positioned-content").unwrap().origin, |
| 735 | point(px(180.), px(60.)) |
| 736 | ); |
| 737 | } |
| 738 | |
| 739 | #[gpui::test] |
| 740 | fn anchor_does_not_flip_when_offset_or_arrow_is_enabled(cx: &mut gpui::TestAppContext) { |
| 741 | cx.update(crate::init); |
| 742 | let (_, window) = cx.add_window_view(|_, _| AnchorHarness { |
| 743 | anchor: Anchor::BottomCenter, |
| 744 | offset: Some(px(12.)), |
| 745 | origin: point(px(200.), px(8.)), |
| 746 | arrow: true, |
| 747 | }); |
| 748 | window.update(|window, cx| window.draw(cx).clear(cx)); |
| 749 | window.update(|window, cx| window.draw(cx).clear(cx)); |
| 750 | // Clamp to the window margin instead of flipping below the trigger. |
| 751 | assert_eq!( |
| 752 | window.debug_bounds("positioned-content").unwrap().top(), |
| 753 | px(8.) |
| 754 | ); |
| 755 | } |
| 756 | |
| 757 | #[test] |
| 758 | fn arrow_alignment_uses_the_anchor_instead_of_trigger_center() { |
| 759 | let trigger = Bounds::new(point(px(120.), px(120.)), size(px(40.), px(20.))); |
| 760 | for (anchor, side, target) in [ |
| 761 | ( |
| 762 | Anchor::TopLeft, |
| 763 | Placement::Bottom, |
| 764 | point(px(120.), px(140.)), |
| 765 | ), |
| 766 | ( |
| 767 | Anchor::TopCenter, |
| 768 | Placement::Bottom, |
| 769 | point(px(140.), px(140.)), |
| 770 | ), |
| 771 | ( |
| 772 | Anchor::TopRight, |
| 773 | Placement::Bottom, |
| 774 | point(px(160.), px(140.)), |
| 775 | ), |
| 776 | ( |
| 777 | Anchor::BottomLeft, |
| 778 | Placement::Top, |
| 779 | point(px(120.), px(120.)), |
| 780 | ), |
| 781 | ( |
| 782 | Anchor::BottomCenter, |
| 783 | Placement::Top, |
| 784 | point(px(140.), px(120.)), |
| 785 | ), |
| 786 | ( |
| 787 | Anchor::BottomRight, |
| 788 | Placement::Top, |
| 789 | point(px(160.), px(120.)), |
| 790 | ), |
| 791 | ( |
| 792 | Anchor::LeftCenter, |
| 793 | Placement::Right, |
| 794 | point(px(160.), px(130.)), |
| 795 | ), |
| 796 | ( |
| 797 | Anchor::RightCenter, |
| 798 | Placement::Left, |
| 799 | point(px(120.), px(130.)), |
| 800 | ), |
| 801 | ] { |
| 802 | assert_eq!(arrow_anchor(anchor, trigger), (side, target)); |
| 803 | } |
| 804 | } |
| 805 | |
| 806 | #[test] |
| 807 | fn arrows_point_toward_the_trigger_on_every_resolved_side() { |
| 808 | let surface = Bounds::new(point(px(100.), px(100.)), size(px(80.), px(60.))); |
| 809 | for (side, trigger, tip) in [ |
| 810 | ( |
| 811 | Placement::Bottom, |
| 812 | Bounds::new(point(px(120.), px(50.)), size(px(40.), px(20.))), |
| 813 | point(px(140.), px(94.)), |
| 814 | ), |
| 815 | ( |
| 816 | Placement::Top, |
| 817 | Bounds::new(point(px(120.), px(180.)), size(px(40.), px(20.))), |
| 818 | point(px(140.), px(166.)), |
| 819 | ), |
| 820 | ( |
| 821 | Placement::Right, |
| 822 | Bounds::new(point(px(40.), px(120.)), size(px(40.), px(20.))), |
| 823 | point(px(94.), px(130.)), |
| 824 | ), |
| 825 | ( |
| 826 | Placement::Left, |
| 827 | Bounds::new(point(px(200.), px(120.)), size(px(40.), px(20.))), |
| 828 | point(px(186.), px(130.)), |
| 829 | ), |
| 830 | ] { |
| 831 | assert_eq!(arrow_points(surface, trigger, side, px(6.), px(4.))[1], tip); |
| 832 | } |
| 833 | let clamped = arrow_points( |
| 834 | surface, |
| 835 | Bounds::new(point(px(0.), px(50.)), size(px(20.), px(20.))), |
| 836 | Placement::Bottom, |
| 837 | px(6.), |
| 838 | px(4.), |
| 839 | ); |
| 840 | assert_eq!(clamped[0], point(px(104.), px(100.))); |
| 841 | assert_eq!(clamped[1], point(px(110.), px(94.))); |
| 842 | } |
| 843 | |
| 844 | #[test] |
| 845 | fn arrow_join_covers_both_sides_of_the_surface_edge() { |
| 846 | let surface = Bounds::new(point(px(100.), px(100.)), size(px(80.), px(60.))); |
| 847 | let trigger = Bounds::new(point(px(120.), px(120.)), size(px(40.), px(20.))); |
| 848 | for (side, expected) in [ |
| 849 | ( |
| 850 | Placement::Bottom, |
| 851 | Bounds::new(point(px(135.), px(99.)), size(px(10.), px(2.))), |
| 852 | ), |
| 853 | ( |
| 854 | Placement::Top, |
| 855 | Bounds::new(point(px(135.), px(159.)), size(px(10.), px(2.))), |
| 856 | ), |
| 857 | ( |
| 858 | Placement::Right, |
| 859 | Bounds::new(point(px(99.), px(125.)), size(px(2.), px(10.))), |
| 860 | ), |
| 861 | ( |
| 862 | Placement::Left, |
| 863 | Bounds::new(point(px(179.), px(125.)), size(px(2.), px(10.))), |
| 864 | ), |
| 865 | ] { |
| 866 | let points = arrow_points(surface, trigger, side, px(6.), px(4.)); |
| 867 | assert_eq!(arrow_join_bounds(points, side, px(1.)), expected); |
| 868 | } |
| 869 | } |
| 870 | |
| 871 | impl Render for PopoverHarness { |
| 872 | fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement { |
| 873 | let changes = self.changes.clone(); |
| 874 | Popover::new("runtime-popover") |
| 875 | .trigger(Button::new("runtime-trigger").label("Open").size(px(100.))) |
| 876 | .content(|_, _, _| { |
| 877 | div() |
| 878 | .debug_selector(|| "runtime-popover-content".into()) |
| 879 | .size(px(40.)) |
| 880 | }) |
| 881 | .on_open_change(move |open, _, _| changes.borrow_mut().push(*open)) |
| 882 | } |
| 883 | } |
| 884 | |
| 885 | #[gpui::test] |
| 886 | fn pointer_open_and_outside_dismiss_use_the_base_popup_host(cx: &mut gpui::TestAppContext) { |
| 887 | cx.update(|cx| { |
| 888 | gpui_base::GlobalState::init(cx); |
| 889 | cx.set_global(Theme::default()); |
| 890 | init(cx); |
| 891 | }); |
| 892 | |
| 893 | let changes = Rc::new(RefCell::new(Vec::new())); |
| 894 | let (_, cx) = cx.add_window_view({ |
| 895 | let changes = changes.clone(); |
| 896 | move |_, _| PopoverHarness { changes } |
| 897 | }); |
| 898 | cx.update(|window, cx| window.draw(cx).clear(cx)); |
| 899 | |
| 900 | cx.simulate_click(point(px(20.), px(20.)), Default::default()); |
| 901 | cx.update(|window, cx| window.draw(cx).clear(cx)); |
| 902 | assert!(cx.debug_bounds("runtime-popover-content").is_some()); |
| 903 | |
| 904 | cx.simulate_click(point(px(300.), px(300.)), Default::default()); |
| 905 | cx.update(|window, cx| window.draw(cx).clear(cx)); |
| 906 | assert!(cx.debug_bounds("runtime-popover-content").is_none()); |
| 907 | // A change callback reports state transitions, not redundant dismissal |
| 908 | // requests. The base host may see both paths, but only the first closes. |
| 909 | assert_eq!(&*changes.borrow(), &[true, false]); |
| 910 | } |
| 911 | |
| 912 | struct DefaultOpenHarness; |
| 913 | |
| 914 | impl Render for DefaultOpenHarness { |
| 915 | fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement { |
| 916 | Popover::new("default-open-popover") |
| 917 | .default_open(true) |
| 918 | .trigger(Button::new("default-open-trigger").label("Open")) |
| 919 | .child( |
| 920 | div() |
| 921 | .debug_selector(|| "default-open-content".into()) |
| 922 | .size(px(40.)), |
| 923 | ) |
| 924 | } |
| 925 | } |
| 926 | |
| 927 | #[gpui::test] |
| 928 | fn default_open_is_forwarded_to_the_base_popover(cx: &mut gpui::TestAppContext) { |
| 929 | cx.update(|cx| { |
| 930 | gpui_base::GlobalState::init(cx); |
| 931 | cx.set_global(Theme::default()); |
| 932 | init(cx); |
| 933 | }); |
| 934 | let (_, cx) = cx.add_window_view(|_, _| DefaultOpenHarness); |
| 935 | cx.update(|window, cx| window.draw(cx).clear(cx)); |
| 936 | cx.update(|window, cx| window.draw(cx).clear(cx)); |
| 937 | assert!(cx.debug_bounds("default-open-content").is_some()); |
| 938 | } |
| 939 | |
| 940 | struct Harness { |
| 941 | open: bool, |
| 942 | } |
| 943 | |
| 944 | impl Render for Harness { |
| 945 | fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> impl IntoElement { |
| 946 | div().size_full().when(self.open, |this| { |
| 947 | this.child(dropdown_popup( |
| 948 | "dropdown", |
| 949 | Bounds::new(point(px(0.), px(100.)), size(px(120.), px(30.))), |
| 950 | div().debug_selector(|| "surface".into()).size(px(50.)), |
| 951 | cx, |
| 952 | )) |
| 953 | }) |
| 954 | } |
| 955 | } |
| 956 | |
| 957 | /// A dropdown that reused one animation key across opens would play its |
| 958 | /// enter motion the first time and then appear already settled on every |
| 959 | /// open after that. That is invisible in any single frame and easy to |
| 960 | /// reintroduce by giving the animation a constant id, so it is pinned here. |
| 961 | #[gpui::test] |
| 962 | fn the_enter_motion_starts_over_every_time_the_dropdown_opens(cx: &mut gpui::TestAppContext) { |
| 963 | cx.update(crate::init); |
| 964 | let (view, window) = cx.add_window_view(|_, _| Harness { open: true }); |
| 965 | |
| 966 | window.update(|window, cx| window.draw(cx).clear(cx)); |
| 967 | let opening = window.debug_bounds("surface").unwrap().origin; |
| 968 | |
| 969 | // The animation runs off the wall clock, so settling is waited out |
| 970 | // rather than stepped. Several times the duration leaves room for a |
| 971 | // loaded machine. |
| 972 | std::thread::sleep(DROPDOWN_ENTER_DURATION * 4); |
| 973 | window.update(|window, cx| window.draw(cx).clear(cx)); |
| 974 | let settled = window.debug_bounds("surface").unwrap().origin; |
| 975 | |
| 976 | assert!( |
| 977 | opening.y < settled.y, |
| 978 | "the surface should slide down into place, from {opening:?} to {settled:?}", |
| 979 | ); |
| 980 | |
| 981 | for open in [false, true] { |
| 982 | window.update(|window, cx| { |
| 983 | view.update(cx, |this, cx| { |
| 984 | this.open = open; |
| 985 | cx.notify(); |
| 986 | }); |
| 987 | window.draw(cx).clear(cx); |
| 988 | }); |
| 989 | } |
| 990 | |
| 991 | let reopening = window.debug_bounds("surface").unwrap().origin; |
| 992 | assert!( |
| 993 | reopening.y < settled.y, |
| 994 | "reopening should start the motion over at {opening:?} rather than showing a \ |
| 995 | settled surface, but the first frame was already at {reopening:?}", |
| 996 | ); |
| 997 | } |
| 998 | } |