use std::{sync::Arc, time::Duration};
use crate::{
AccessibilityPopover, AccessibilityRole, AnchorAlign, AnchorPlacement, AnchorPlacementHandle,
AnchorSide, AsyncViewContext, Color, Element, ElementId, EventContext, FocusHandle,
MAX_WINDOW_LOGICAL_COORDINATE, MAX_WINDOW_LOGICAL_DIMENSION, Point, PopoverAnchor,
PopoverConstraintAdjustment, PopoverGravity, PopoverOptions, Rect, ResolvedAnchorPlacement,
Size, StateAccessor, Task, View, ViewContext, WindowBackgroundAppearance, WindowCommandError,
WindowHandle, WindowOptions, anchor_placement, button, div,
};
pub const MAX_POPOVER_HOVER_DELAY: Duration = Duration::from_secs(10);
pub const DEFAULT_POPOVER_HOVER_DELAY: Duration = Duration::from_millis(300);
pub const MAX_POPOVER_SIDE_OFFSET: f32 = 256.0;
pub const MAX_POPOVER_ALIGN_OFFSET: f32 = 4_096.0;
pub const MAX_POPOVER_COLLISION_PADDING: f32 = 512.0;
pub const MAX_POPOVER_ARROW_SIZE: f32 = 256.0;
const MAX_POPOVER_ANCHOR_GAP: f32 = MAX_POPOVER_SIDE_OFFSET;
const MAX_POPOVER_VIEWPORT_MARGIN: f32 = MAX_POPOVER_COLLISION_PADDING;
const DEFAULT_POPOVER_ANCHOR_GAP: f32 = 6.0;
const DEFAULT_POPOVER_VIEWPORT_MARGIN: f32 = 8.0;
const POPOVER_POSITIONER_ID_TAG: u64 = 0x847d_5a0f_0f9b_31e7;
const POPOVER_BACKDROP_ID_TAG: u64 = 0x663e_a690_8d7f_c442;
const POPOVER_TITLE_ID_TAG: u64 = 0x23ce_95af_9dc3_481b;
const POPOVER_DESCRIPTION_ID_TAG: u64 = 0xa935_070d_46db_78c1;
const POPOVER_CLOSE_ID_TAG: u64 = 0xd55f_271c_bbd9_e6a4;
const POPOVER_ARROW_ID_TAG: u64 = 0x1f2c_6ad8_5e07_9b3d;
const POPOVER_VIEWPORT_ID_TAG: u64 = 0x74b0_e319_c68a_20f5;
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub enum PopoverKind {
#[default]
Dialog,
Menu,
ListBox,
Tree,
Grid,
}
impl PopoverKind {
const fn accessibility_role(self) -> AccessibilityRole {
match self {
Self::Dialog => AccessibilityRole::Dialog,
Self::Menu => AccessibilityRole::Menu,
Self::ListBox => AccessibilityRole::ListBox,
Self::Tree => AccessibilityRole::Tree,
Self::Grid => AccessibilityRole::Grid,
}
}
const fn accessibility_popover(self) -> AccessibilityPopover {
match self {
Self::Dialog => AccessibilityPopover::Dialog,
Self::Menu => AccessibilityPopover::Menu,
Self::ListBox => AccessibilityPopover::ListBox,
Self::Tree => AccessibilityPopover::Tree,
Self::Grid => AccessibilityPopover::Grid,
}
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
#[must_use = "a Popover descriptor has no effect until one of its parts is mounted"]
pub struct Popover {
trigger_id: ElementId,
surface_id: ElementId,
open: bool,
kind: PopoverKind,
placement: AnchorPlacement,
anchor: PopoverAnchorSource,
anchor_gap: f32,
align_offset: f32,
viewport_margin: f32,
sticky: bool,
modal: bool,
arrow_size: f32,
arrow_padding: f32,
resolved: Option<ResolvedAnchorPlacement>,
initial_focus: Option<FocusHandle>,
dismiss_on_escape: bool,
dismiss_on_pointer_outside: bool,
}
#[derive(Clone, Copy, Debug, Default, PartialEq)]
enum PopoverAnchorSource {
#[default]
Trigger,
Element(ElementId),
Point(Point),
}
impl Popover {
pub fn new(
trigger_id: impl Into<ElementId>,
surface_id: impl Into<ElementId>,
open: bool,
) -> Self {
Self {
trigger_id: trigger_id.into(),
surface_id: surface_id.into(),
open,
kind: PopoverKind::Dialog,
placement: AnchorPlacement::BottomStart,
anchor: PopoverAnchorSource::Trigger,
anchor_gap: DEFAULT_POPOVER_ANCHOR_GAP,
align_offset: 0.0,
viewport_margin: DEFAULT_POPOVER_VIEWPORT_MARGIN,
sticky: true,
modal: false,
arrow_size: 0.0,
arrow_padding: 0.0,
resolved: None,
initial_focus: None,
dismiss_on_escape: true,
dismiss_on_pointer_outside: true,
}
}
pub const fn open(mut self, open: bool) -> Self {
self.open = open;
self
}
pub const fn kind(mut self, kind: PopoverKind) -> Self {
self.kind = kind;
self
}
pub const fn placement(mut self, placement: AnchorPlacement) -> Self {
self.placement = placement;
self
}
pub const fn side(self, side: AnchorSide) -> Self {
self.placement(anchor_placement(side, AnchorAlign::of(self.placement)))
}
pub const fn align(self, align: AnchorAlign) -> Self {
self.placement(anchor_placement(AnchorSide::of(self.placement), align))
}
pub fn side_offset(self, offset: f32) -> Self {
self.anchor_gap(offset)
}
pub fn align_offset(mut self, offset: f32) -> Self {
self.align_offset = finite_clamped(
offset,
-MAX_POPOVER_ALIGN_OFFSET,
MAX_POPOVER_ALIGN_OFFSET,
0.0,
);
self
}
pub fn collision_padding(self, padding: f32) -> Self {
self.viewport_margin(padding)
}
pub const fn sticky(mut self, sticky: bool) -> Self {
self.sticky = sticky;
self
}
pub fn anchor_element(mut self, anchor: impl Into<ElementId>) -> Self {
self.anchor = PopoverAnchorSource::Element(anchor.into());
self
}
pub fn anchor_point(mut self, anchor: Point) -> Self {
self.anchor = PopoverAnchorSource::Point(Point::new(
finite_clamped(
anchor.x,
-MAX_WINDOW_LOGICAL_COORDINATE,
MAX_WINDOW_LOGICAL_COORDINATE,
0.0,
),
finite_clamped(
anchor.y,
-MAX_WINDOW_LOGICAL_COORDINATE,
MAX_WINDOW_LOGICAL_COORDINATE,
0.0,
),
));
self
}
pub const fn anchor_trigger(mut self) -> Self {
self.anchor = PopoverAnchorSource::Trigger;
self
}
pub const fn modal(mut self, modal: bool) -> Self {
self.modal = modal;
self
}
pub const fn is_modal(self) -> bool {
self.modal
}
pub fn arrow_size(mut self, size: f32) -> Self {
self.arrow_size = finite_clamped(size, 0.0, MAX_POPOVER_ARROW_SIZE, 0.0);
self
}
pub fn arrow_padding(mut self, padding: f32) -> Self {
self.arrow_padding = finite_clamped(padding, 0.0, MAX_POPOVER_ARROW_SIZE, 0.0);
self
}
pub fn track_placement(mut self, placement: &AnchorPlacementHandle) -> Self {
self.resolved = placement.resolved();
self
}
pub const fn resolved_placement(mut self, resolved: Option<ResolvedAnchorPlacement>) -> Self {
self.resolved = resolved;
self
}
pub fn resolved_side(self) -> AnchorSide {
AnchorSide::of(self.resolved_anchor_placement())
}
pub fn resolved_align(self) -> AnchorAlign {
AnchorAlign::of(self.resolved_anchor_placement())
}
pub fn resolved_anchor_placement(self) -> AnchorPlacement {
self.resolved
.map_or(self.placement, |resolved| resolved.placement)
}
pub fn state(self) -> PopoverPartState {
let measured = self.resolved.unwrap_or_default();
let reported = self.resolved.is_some();
PopoverPartState {
open: self.open,
modal: self.modal,
side: self.resolved_side(),
align: self.resolved_align(),
anchor_hidden: reported && measured.anchor_hidden,
anchor_width: if reported { measured.anchor.width } else { 0.0 },
anchor_height: if reported {
measured.anchor.height
} else {
0.0
},
available_width: if reported {
measured.available.width
} else {
0.0
},
available_height: if reported {
measured.available.height
} else {
0.0
},
}
}
pub fn anchor_gap(mut self, gap: f32) -> Self {
self.anchor_gap =
finite_clamped(gap, 0.0, MAX_POPOVER_ANCHOR_GAP, DEFAULT_POPOVER_ANCHOR_GAP);
self
}
pub fn viewport_margin(mut self, margin: f32) -> Self {
self.viewport_margin = finite_clamped(
margin,
0.0,
MAX_POPOVER_VIEWPORT_MARGIN,
DEFAULT_POPOVER_VIEWPORT_MARGIN,
);
self
}
pub fn initial_focus(mut self, focus: impl Into<ElementId>) -> Self {
self.initial_focus = Some(FocusHandle::new(focus));
self
}
pub const fn dismiss_on_escape(mut self, dismiss: bool) -> Self {
self.dismiss_on_escape = dismiss;
self
}
pub const fn dismiss_on_pointer_outside(mut self, dismiss: bool) -> Self {
self.dismiss_on_pointer_outside = dismiss;
self
}
pub const fn is_open(self) -> bool {
self.open
}
pub const fn trigger_id(self) -> ElementId {
self.trigger_id
}
pub const fn surface_id(self) -> ElementId {
self.surface_id
}
pub const fn popover_id(self) -> ElementId {
self.surface_id
}
pub fn positioner_id(self) -> ElementId {
derived_popover_id(self.surface_id, self.trigger_id, POPOVER_POSITIONER_ID_TAG)
}
pub fn backdrop_id(self) -> ElementId {
derived_popover_id(self.surface_id, self.trigger_id, POPOVER_BACKDROP_ID_TAG)
}
pub fn title_id(self) -> ElementId {
derived_popover_id(self.surface_id, self.trigger_id, POPOVER_TITLE_ID_TAG)
}
pub fn description_id(self) -> ElementId {
derived_popover_id(self.surface_id, self.trigger_id, POPOVER_DESCRIPTION_ID_TAG)
}
pub fn close_id(self) -> ElementId {
derived_popover_id(self.surface_id, self.trigger_id, POPOVER_CLOSE_ID_TAG)
}
pub fn arrow_id(self) -> ElementId {
derived_popover_id(self.surface_id, self.trigger_id, POPOVER_ARROW_ID_TAG)
}
pub fn viewport_id(self) -> ElementId {
derived_popover_id(self.surface_id, self.trigger_id, POPOVER_VIEWPORT_ID_TAG)
}
pub fn trigger_focus(self) -> FocusHandle {
FocusHandle::new(self.trigger_id)
}
pub fn surface_focus(self) -> FocusHandle {
FocusHandle::new(self.surface_id)
}
pub fn popover_focus(self) -> FocusHandle {
self.surface_focus()
}
pub fn focus_surface(self, cx: &mut EventContext) {
cx.focus(self.initial_focus.unwrap_or_else(|| self.popover_focus()));
}
pub fn root(self) -> Element {
div()
}
pub fn root_with(self, root: Element) -> Element {
root
}
pub fn trigger_with(self, trigger: Element) -> Element {
let trigger = trigger
.id(self.trigger_id)
.focusable()
.accessibility_role(AccessibilityRole::Button)
.user_select_none()
.app_region_no_drag()
.cursor_default()
.accessibility_expanded(self.open)
.accessibility_has_popover(self.kind.accessibility_popover());
if self.open {
trigger.accessibility_controls(self.surface_id)
} else {
trigger
}
}
pub fn trigger(self) -> Element {
self.trigger_with(button())
}
pub fn positioner_with(self, positioner: Element) -> Element {
self.anchored(positioner.id(self.positioner_id()))
.app_region_no_drag()
.cursor_default()
}
pub fn positioner(self) -> Element {
self.positioner_with(crate::div())
}
pub fn tracked_positioner_with(
self,
positioner: Element,
placement: &AnchorPlacementHandle,
) -> Element {
self.positioner_with(positioner.report_anchor_placement(placement.clone()))
}
pub fn tracked_positioner(self, placement: &AnchorPlacementHandle) -> Element {
self.tracked_positioner_with(crate::div(), placement)
}
pub fn portal_with(self, portal: Element) -> Element {
self.positioner_with(portal)
}
pub fn portal(self) -> Element {
self.portal_with(crate::div())
}
fn anchored(self, element: Element) -> Element {
let element = match self.anchor {
PopoverAnchorSource::Trigger => element.anchor_to(self.trigger_id, self.placement),
PopoverAnchorSource::Element(anchor) => element.anchor_to(anchor, self.placement),
PopoverAnchorSource::Point(anchor) => element.anchor_at(anchor, self.placement),
};
element
.anchor_gap(self.anchor_gap)
.anchor_align_offset(self.align_offset)
.viewport_margin(self.viewport_margin)
.anchor_sticky(self.sticky)
}
pub fn popup_with(self, popover: Element) -> Element {
let mut popover = popover
.id(self.surface_id)
.restore_focus_to(self.trigger_focus())
.track_focus(self.popover_focus())
.accessibility_role(self.kind.accessibility_role())
.accessibility_labelled_by(self.title_id())
.accessibility_described_by(self.description_id())
.block_pointer()
.app_region_no_drag()
.cursor_default();
if self.dismiss_on_escape {
popover = popover.dismiss_on_escape();
}
if self.dismiss_on_pointer_outside {
popover = popover.dismiss_on_pointer_outside();
}
if self.modal {
popover = popover.focus_trap();
}
popover
}
pub fn popup(self) -> Element {
self.popup_with(crate::div())
}
pub fn surface_with(self, surface: Element) -> Element {
self.anchored(self.popup_with(surface))
}
pub fn tracked_surface_with(
self,
surface: Element,
placement: &AnchorPlacementHandle,
) -> Element {
self.surface_with(surface.report_anchor_placement(placement.clone()))
}
pub fn tracked_surface(self, placement: &AnchorPlacementHandle) -> Element {
self.tracked_surface_with(crate::div(), placement)
}
pub fn surface(self) -> Element {
self.surface_with(div())
}
pub fn arrow_with(self, arrow: Element) -> Element {
let arrow = arrow
.id(self.arrow_id())
.absolute()
.accessibility_hidden(true)
.app_region_no_drag();
let side = self.resolved_side();
let offset = self.arrow_cross_offset(side);
match (side, offset) {
(AnchorSide::Bottom, Some(offset)) => arrow.top(0.0).left(offset),
(AnchorSide::Bottom, None) => arrow.top(0.0),
(AnchorSide::Top, Some(offset)) => arrow.bottom(0.0).left(offset),
(AnchorSide::Top, None) => arrow.bottom(0.0),
(AnchorSide::Right, Some(offset)) => arrow.left(0.0).top(offset),
(AnchorSide::Right, None) => arrow.left(0.0),
(AnchorSide::Left, Some(offset)) => arrow.right(0.0).top(offset),
(AnchorSide::Left, None) => arrow.right(0.0),
}
}
pub fn arrow(self) -> Element {
self.arrow_with(crate::div())
}
fn arrow_cross_offset(self, side: AnchorSide) -> Option<f32> {
let resolved = self.resolved?;
let (anchor_center, popup_origin, popup_extent) = if side.is_vertical() {
(
resolved.anchor.x + resolved.anchor.width * 0.5,
resolved.bounds.x,
resolved.bounds.width,
)
} else {
(
resolved.anchor.y + resolved.anchor.height * 0.5,
resolved.bounds.y,
resolved.bounds.height,
)
};
let leading = anchor_center - popup_origin - self.arrow_size * 0.5;
let last = popup_extent - self.arrow_size - self.arrow_padding;
if !leading.is_finite() || !last.is_finite() {
return None;
}
Some(leading.clamp(self.arrow_padding, last.max(self.arrow_padding)))
}
pub fn viewport_with(self, viewport: Element) -> Element {
viewport
.id(self.viewport_id())
.overflow_y_scroll()
.app_region_no_drag()
}
pub fn viewport(self) -> Element {
self.viewport_with(crate::div())
}
pub fn backdrop_with(self, backdrop: Element) -> Element {
backdrop
.id(self.backdrop_id())
.overlay()
.inset_0()
.size_full()
.app_region_no_drag()
.cursor_default()
.accessibility_hidden(true)
}
pub fn backdrop(self) -> Element {
self.backdrop_with(crate::div())
}
pub fn title_with(self, title: Element) -> Element {
title.id(self.title_id())
}
pub fn title(self) -> Element {
self.title_with(crate::div())
}
pub fn description_with(self, description: Element) -> Element {
description.id(self.description_id())
}
pub fn description(self) -> Element {
self.description_with(crate::div())
}
pub fn close_with(self, label: impl Into<Arc<str>>, close: Element) -> Element {
close
.id(self.close_id())
.clickable()
.cursor_default()
.accessibility_role(AccessibilityRole::Button)
.accessibility_label(label)
.app_region_no_drag()
.user_select_none()
}
pub fn close(self, label: impl Into<Arc<str>>) -> Element {
self.close_with(label, crate::button())
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
enum PopoverHoverPart {
Trigger,
Popup,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
enum PopoverHoverPhase {
Open,
Close,
}
#[derive(Debug)]
pub struct PopoverHoverState {
open: bool,
delay: Duration,
close_delay: Duration,
trigger_hovered: bool,
popup_hovered: bool,
hoverable_popup: bool,
pending: Option<PopoverHoverPhase>,
generation: u64,
task: Option<Task<()>>,
}
impl Default for PopoverHoverState {
fn default() -> Self {
Self::new()
}
}
impl PopoverHoverState {
pub fn new() -> Self {
Self {
open: false,
delay: DEFAULT_POPOVER_HOVER_DELAY,
close_delay: Duration::ZERO,
trigger_hovered: false,
popup_hovered: false,
hoverable_popup: true,
pending: None,
generation: 0,
task: None,
}
}
pub fn delay(mut self, delay: Duration) -> Self {
self.delay = delay.min(MAX_POPOVER_HOVER_DELAY);
self
}
pub fn close_delay(mut self, delay: Duration) -> Self {
self.close_delay = delay.min(MAX_POPOVER_HOVER_DELAY);
self
}
pub const fn hoverable_popup(mut self, hoverable: bool) -> Self {
self.hoverable_popup = hoverable;
self
}
pub const fn is_open(&self) -> bool {
self.open
}
pub const fn is_pending(&self) -> bool {
self.pending.is_some()
}
pub const fn is_hovered(&self) -> bool {
self.trigger_hovered || (self.hoverable_popup && self.popup_hovered)
}
pub fn open_now(&mut self) -> bool {
self.cancel_pending();
!std::mem::replace(&mut self.open, true)
}
pub fn close_now(&mut self) -> bool {
self.cancel_pending();
std::mem::replace(&mut self.open, false)
}
pub fn toggle(&mut self) -> bool {
if self.open {
self.close_now()
} else {
self.open_now()
}
}
pub fn cancel_pending(&mut self) {
self.generation = self.generation.wrapping_add(1);
self.pending = None;
if let Some(task) = self.task.take() {
task.cancel();
}
}
pub fn trigger_with<V: 'static>(
&self,
cx: &mut ViewContext<'_, V>,
popover: Popover,
access: fn(&mut V) -> &mut Self,
trigger: Element,
) -> Element {
self.trigger_with_accessor(cx, popover, StateAccessor::from(access), trigger)
}
pub fn trigger<V: 'static>(
&self,
cx: &mut ViewContext<'_, V>,
popover: Popover,
access: fn(&mut V) -> &mut Self,
) -> Element {
self.trigger_with(cx, popover, access, crate::button())
}
pub fn trigger_with_accessor<V: 'static>(
&self,
cx: &mut ViewContext<'_, V>,
popover: Popover,
access: StateAccessor<V, Self>,
trigger: Element,
) -> Element {
let hovered =
self.hover_listener(cx, popover.trigger_id(), PopoverHoverPart::Trigger, access);
popover.trigger_with(trigger).on_hover(hovered)
}
pub fn popup_with<V: 'static>(
&self,
cx: &mut ViewContext<'_, V>,
popover: Popover,
access: fn(&mut V) -> &mut Self,
popup: Element,
) -> Element {
self.popup_with_accessor(cx, popover, StateAccessor::from(access), popup)
}
pub fn popup<V: 'static>(
&self,
cx: &mut ViewContext<'_, V>,
popover: Popover,
access: fn(&mut V) -> &mut Self,
) -> Element {
self.popup_with(cx, popover, access, crate::div())
}
pub fn popup_with_accessor<V: 'static>(
&self,
cx: &mut ViewContext<'_, V>,
popover: Popover,
access: StateAccessor<V, Self>,
popup: Element,
) -> Element {
let hovered =
self.hover_listener(cx, popover.popover_id(), PopoverHoverPart::Popup, access);
popover.popup_with(popup).on_hover(hovered)
}
fn hover_listener<V: 'static>(
&self,
cx: &mut ViewContext<'_, V>,
id: ElementId,
part: PopoverHoverPart,
access: StateAccessor<V, Self>,
) -> crate::HoverListener<V> {
cx.hover_listener(id, move |view, hovered, cx| {
let access = access.clone();
let hovered = *hovered;
access
.clone()
.get(view)
.record_hover(part, hovered, access, cx);
})
}
fn record_hover<V: 'static>(
&mut self,
part: PopoverHoverPart,
hovered: bool,
access: StateAccessor<V, Self>,
cx: &mut EventContext,
) {
match part {
PopoverHoverPart::Trigger => self.trigger_hovered = hovered,
PopoverHoverPart::Popup => self.popup_hovered = hovered,
}
let wanted = if self.is_hovered() {
PopoverHoverPhase::Open
} else {
PopoverHoverPhase::Close
};
let already = match wanted {
PopoverHoverPhase::Open => self.open,
PopoverHoverPhase::Close => !self.open,
};
if already {
if self.pending.is_some() {
self.cancel_pending();
}
return;
}
if self.pending == Some(wanted) {
return;
}
let delay = match wanted {
PopoverHoverPhase::Open => self.delay,
PopoverHoverPhase::Close => self.close_delay,
};
self.schedule(wanted, delay, access, cx);
}
fn schedule<V: 'static>(
&mut self,
phase: PopoverHoverPhase,
delay: Duration,
access: StateAccessor<V, Self>,
cx: &mut EventContext,
) {
self.cancel_pending();
if delay.is_zero() {
if self.apply(phase) {
cx.invalidate();
}
return;
}
self.pending = Some(phase);
let generation = self.generation;
let spawned = cx.spawn::<V, _, _, _>(move |task_cx: AsyncViewContext<V>| async move {
if task_cx.sleep(delay).await.is_err() {
return;
}
let _ = task_cx
.update(move |view, cx| {
let state = access.get(view);
if state.generation != generation || state.pending != Some(phase) {
return;
}
state.pending = None;
state.task = None;
if state.apply(phase) {
cx.invalidate();
}
})
.await;
});
match spawned {
Ok(task) => self.task = Some(task),
Err(_) => {
self.pending = None;
if self.apply(phase) {
cx.invalidate();
}
}
}
}
fn apply(&mut self, phase: PopoverHoverPhase) -> bool {
let open = phase == PopoverHoverPhase::Open;
std::mem::replace(&mut self.open, open) != open
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct PopoverPartState {
pub open: bool,
pub modal: bool,
pub side: AnchorSide,
pub align: AnchorAlign,
pub anchor_hidden: bool,
pub anchor_width: f32,
pub anchor_height: f32,
pub available_width: f32,
pub available_height: f32,
}
impl PopoverPartState {
pub fn is_measured(self) -> bool {
self.anchor_width > 0.0
|| self.anchor_height > 0.0
|| self.available_width > 0.0
|| self.available_height > 0.0
}
}
fn derived_popover_id(parent: ElementId, avoid: ElementId, tag: u64) -> ElementId {
let mut hash = parent.as_u64() ^ tag;
hash ^= hash >> 30;
hash = hash.wrapping_mul(0xbf58_476d_1ce4_e5b9);
hash ^= hash >> 27;
hash = hash.wrapping_mul(0x94d0_49bb_1331_11eb);
hash ^= hash >> 31;
for _ in 0..5 {
if hash != 0 && hash != parent.as_u64() && hash != avoid.as_u64() && hash != u64::MAX {
return ElementId::new(hash);
}
hash = hash.wrapping_add(tag | 1);
}
unreachable!("five distinct candidates cannot all match four reserved popover IDs")
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct SystemPopover {
size: Size,
placement: AnchorPlacement,
gap: f32,
viewport_margin: f32,
offset: Point,
constraints: PopoverConstraintAdjustment,
dismiss_on_escape: bool,
dismiss_on_pointer_outside: bool,
grab: bool,
accepts_key_focus: bool,
}
impl SystemPopover {
pub fn new(width: f32, height: f32) -> Self {
Self {
size: Size::new(
finite_clamped(width, 1.0, MAX_WINDOW_LOGICAL_DIMENSION, 240.0),
finite_clamped(height, 1.0, MAX_WINDOW_LOGICAL_DIMENSION, 180.0),
),
placement: AnchorPlacement::BottomStart,
gap: DEFAULT_POPOVER_ANCHOR_GAP,
viewport_margin: DEFAULT_POPOVER_VIEWPORT_MARGIN,
offset: Point::ZERO,
constraints: PopoverConstraintAdjustment::FIT,
dismiss_on_escape: true,
dismiss_on_pointer_outside: true,
grab: true,
accepts_key_focus: true,
}
}
pub const fn size(self) -> Size {
self.size
}
pub const fn placement(mut self, placement: AnchorPlacement) -> Self {
self.placement = placement;
self
}
pub fn gap(mut self, gap: f32) -> Self {
self.gap = finite_clamped(gap, 0.0, 512.0, 0.0);
self
}
pub fn viewport_margin(mut self, margin: f32) -> Self {
self.viewport_margin = finite_clamped(
margin,
0.0,
MAX_POPOVER_VIEWPORT_MARGIN,
DEFAULT_POPOVER_VIEWPORT_MARGIN,
);
self
}
pub const fn dismiss_on_escape(mut self, dismiss: bool) -> Self {
self.dismiss_on_escape = dismiss;
self
}
pub const fn dismiss_on_pointer_outside(mut self, dismiss: bool) -> Self {
self.dismiss_on_pointer_outside = dismiss;
self
}
pub fn offset(mut self, x: f32, y: f32) -> Self {
self.offset = Point::new(
finite_clamped(
x,
-MAX_WINDOW_LOGICAL_COORDINATE,
MAX_WINDOW_LOGICAL_COORDINATE,
0.0,
),
finite_clamped(
y,
-MAX_WINDOW_LOGICAL_COORDINATE,
MAX_WINDOW_LOGICAL_COORDINATE,
0.0,
),
);
self
}
pub const fn constraint_adjustment(mut self, constraints: PopoverConstraintAdjustment) -> Self {
self.constraints = constraints;
self
}
pub const fn grab(mut self, grab: bool) -> Self {
self.grab = grab;
self.dismiss_on_escape = grab;
self.dismiss_on_pointer_outside = grab;
if grab {
self.accepts_key_focus = true;
}
self
}
pub const fn accepts_key_focus(mut self, accepts_key_focus: bool) -> Self {
self.accepts_key_focus = accepts_key_focus;
if !accepts_key_focus {
self.grab = false;
self.dismiss_on_escape = false;
self.dismiss_on_pointer_outside = false;
}
self
}
pub fn popover_options(self) -> PopoverOptions {
let (anchor, gravity) = native_popover_placement(self.placement);
let mut offset = self.offset;
match self.placement {
AnchorPlacement::TopStart | AnchorPlacement::Top | AnchorPlacement::TopEnd => {
offset.y -= self.gap;
}
AnchorPlacement::BottomStart | AnchorPlacement::Bottom | AnchorPlacement::BottomEnd => {
offset.y += self.gap;
}
AnchorPlacement::LeftStart | AnchorPlacement::Left | AnchorPlacement::LeftEnd => {
offset.x -= self.gap;
}
AnchorPlacement::RightStart | AnchorPlacement::Right | AnchorPlacement::RightEnd => {
offset.x += self.gap;
}
}
PopoverOptions::new(Rect::ZERO)
.anchor(anchor)
.gravity(gravity)
.constraint_adjustment(self.constraints)
.offset(offset.x, offset.y)
.grab(self.grab)
.accepts_key_focus(self.accepts_key_focus)
.viewport_margin(self.viewport_margin)
.dismiss_on_escape(self.dismiss_on_escape)
.dismiss_on_pointer_outside(self.dismiss_on_pointer_outside)
}
pub fn window_options(self, title: impl Into<String>) -> WindowOptions {
WindowOptions::new(title)
.size(self.size.width, self.size.height)
.background(Color::TRANSPARENT)
.window_background(WindowBackgroundAppearance::Transparent)
.system_popover(self.popover_options())
}
pub fn open<V: View>(
self,
cx: &mut EventContext,
anchor: impl Into<ElementId>,
title: impl Into<String>,
view: V,
) -> Result<WindowHandle, WindowCommandError> {
cx.open_system_popover(anchor, self.window_options(title), view)
}
}
impl Default for SystemPopover {
fn default() -> Self {
Self::new(240.0, 180.0)
}
}
fn native_popover_placement(placement: AnchorPlacement) -> (PopoverAnchor, PopoverGravity) {
match placement {
AnchorPlacement::TopStart => (PopoverAnchor::TopLeft, PopoverGravity::TopRight),
AnchorPlacement::Top => (PopoverAnchor::Top, PopoverGravity::Top),
AnchorPlacement::TopEnd => (PopoverAnchor::TopRight, PopoverGravity::TopLeft),
AnchorPlacement::BottomStart => (PopoverAnchor::BottomLeft, PopoverGravity::BottomRight),
AnchorPlacement::Bottom => (PopoverAnchor::Bottom, PopoverGravity::Bottom),
AnchorPlacement::BottomEnd => (PopoverAnchor::BottomRight, PopoverGravity::BottomLeft),
AnchorPlacement::LeftStart => (PopoverAnchor::TopLeft, PopoverGravity::BottomLeft),
AnchorPlacement::Left => (PopoverAnchor::Left, PopoverGravity::Left),
AnchorPlacement::LeftEnd => (PopoverAnchor::BottomLeft, PopoverGravity::TopLeft),
AnchorPlacement::RightStart => (PopoverAnchor::TopRight, PopoverGravity::BottomRight),
AnchorPlacement::Right => (PopoverAnchor::Right, PopoverGravity::Right),
AnchorPlacement::RightEnd => (PopoverAnchor::BottomRight, PopoverGravity::TopRight),
}
}
fn finite_clamped(value: f32, minimum: f32, maximum: f32, fallback: f32) -> f32 {
if value.is_finite() {
value.clamp(minimum, maximum)
} else {
fallback.clamp(minimum, maximum)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::element::AnchorTarget;
use crate::{
AppRegion, Application, Event, IntoElement, TestAppContext, View, ViewContext,
WindowBounds, WindowKind, WindowOptions,
};
use taffy::style_helpers::length;
#[test]
fn descriptor_builds_paired_controlled_elements_without_retained_component_state() {
assert!(std::mem::size_of::<Popover>() <= 128);
let closed = Popover::new("trigger", "surface", false).kind(PopoverKind::Menu);
let closed_trigger = closed.trigger();
assert_eq!(closed_trigger.explicit_id, Some("trigger".into()));
assert_eq!(closed_trigger.accessibility.expanded, Some(false));
assert_eq!(closed_trigger.accessibility.relations.controls(), None);
assert_eq!(
closed_trigger.accessibility.has_popover,
Some(AccessibilityPopover::Menu)
);
assert_eq!(closed_trigger.app_region, Some(AppRegion::NoDrag));
assert_eq!(closed_trigger.cursor_style, Some(crate::CursorStyle::Arrow));
let open = Popover::new("trigger", "surface", true)
.kind(PopoverKind::Menu)
.placement(AnchorPlacement::TopEnd);
let trigger = open.trigger();
assert_eq!(trigger.accessibility.expanded, Some(true));
assert_eq!(
trigger.accessibility.relations.controls(),
Some("surface".into())
);
let surface = open.surface();
assert_eq!(surface.explicit_id, Some("surface".into()));
assert_eq!(surface.accessibility.role, AccessibilityRole::Menu);
assert_eq!(surface.dismiss_policy, crate::element::DismissPolicy::BOTH);
assert!(surface.blocks_pointer);
assert!(surface.focusable);
assert_eq!(surface.restore_focus, Some(open.trigger_focus()));
assert_eq!(surface.app_region, Some(AppRegion::NoDrag));
assert!(surface.portal);
assert_eq!(surface.visual.background, None);
assert_eq!(surface.visual.border_color, None);
assert_eq!(surface.visual.shadows, None);
let anchor = surface.anchor.expect("popover anchor");
assert_eq!(anchor.target, AnchorTarget::Element("trigger".into()));
assert_eq!(anchor.placement, AnchorPlacement::TopEnd);
assert_eq!(anchor.gap, DEFAULT_POPOVER_ANCHOR_GAP);
assert_eq!(anchor.viewport_margin, DEFAULT_POPOVER_VIEWPORT_MARGIN);
}
#[test]
fn split_parts_add_exact_behavior_without_appearance_tokens() {
let popover = Popover::new("trigger", "surface", true)
.kind(PopoverKind::ListBox)
.placement(AnchorPlacement::BottomEnd)
.anchor_gap(12.0)
.viewport_margin(20.0)
.initial_focus("first-option")
.dismiss_on_escape(false);
let trigger = popover.trigger_with(crate::div());
assert_eq!(trigger.explicit_id, Some("trigger".into()));
assert_eq!(trigger.accessibility.role, AccessibilityRole::Button);
assert!(trigger.focusable);
assert_eq!(trigger.visual.background, None);
assert_eq!(trigger.visual.border_color, None);
assert_eq!(trigger.cursor_style, Some(crate::CursorStyle::Arrow));
let positioner = popover.positioner_with(crate::div());
assert_eq!(positioner.explicit_id, Some(popover.positioner_id()));
assert!(positioner.portal);
assert!(positioner.blocks_pointer);
assert_eq!(positioner.visual.background, None);
assert_eq!(positioner.visual.border_color, None);
let anchor = positioner.anchor.expect("popover positioner anchor");
assert_eq!(anchor.target, AnchorTarget::Element("trigger".into()));
assert_eq!(anchor.placement, AnchorPlacement::BottomEnd);
assert_eq!(anchor.gap, 12.0);
assert_eq!(anchor.viewport_margin, 20.0);
let surface = popover.popup_with(crate::div());
assert_eq!(surface.explicit_id, Some("surface".into()));
assert_eq!(surface.accessibility.role, AccessibilityRole::ListBox);
assert!(!surface.portal);
assert!(!surface.dismiss_policy.on_escape());
assert!(surface.dismiss_policy.on_pointer_outside());
assert!(surface.blocks_pointer);
assert_eq!(surface.restore_focus, Some(popover.trigger_focus()));
assert_eq!(
surface.accessibility.relations.labelled_by(),
Some(popover.title_id())
);
assert_eq!(
surface.accessibility.relations.described_by(),
Some(popover.description_id())
);
assert_eq!(surface.visual.background, None);
assert_eq!(surface.visual.border_color, None);
assert_eq!(surface.visual.shadows, None);
assert_eq!(surface.visual.radius, 0.0);
let title = popover.title_with(crate::text("Visible title"));
let description = popover.description_with(crate::text("Visible description"));
let close = popover.close_with("Close popover", crate::div());
let backdrop = popover.backdrop_with(crate::div());
assert_eq!(title.explicit_id, Some(popover.title_id()));
assert_eq!(description.explicit_id, Some(popover.description_id()));
assert_eq!(close.explicit_id, Some(popover.close_id()));
assert!(close.clickable);
assert_eq!(close.accessibility.role, AccessibilityRole::Button);
assert_eq!(close.accessibility.label.as_deref(), Some("Close popover"));
assert_eq!(close.visual.background, None);
assert_eq!(backdrop.explicit_id, Some(popover.backdrop_id()));
assert!(backdrop.portal);
assert!(backdrop.blocks_pointer);
assert!(backdrop.accessibility.hidden);
}
#[test]
fn structural_geometry_is_bounded_and_dismissal_paths_are_independent() {
let popover = Popover::new("trigger", "surface", true)
.anchor_gap(-4.0)
.viewport_margin(f32::NAN)
.dismiss_on_escape(false)
.dismiss_on_pointer_outside(false);
let positioner = popover.positioner_with(crate::div());
let anchor = positioner.anchor.expect("popover positioner anchor");
assert_eq!(anchor.gap, 0.0);
assert_eq!(anchor.viewport_margin, DEFAULT_POPOVER_VIEWPORT_MARGIN);
let popover = popover.popup_with(crate::div());
assert!(popover.dismiss_policy.is_empty());
assert!(popover.blocks_pointer);
}
#[test]
fn derived_part_ids_are_stable_distinct_and_avoid_the_declared_pair() {
let popover = Popover::new(41_u64, 42_u64, true);
let ids = [
popover.positioner_id(),
popover.backdrop_id(),
popover.title_id(),
popover.description_id(),
popover.close_id(),
];
for (index, id) in ids.iter().enumerate() {
assert_ne!(*id, popover.trigger_id());
assert_ne!(*id, popover.surface_id());
assert_ne!(*id, ElementId::new(0));
assert_ne!(*id, ElementId::new(u64::MAX));
assert!(!ids[..index].contains(id));
}
assert_eq!(
popover.positioner_id(),
Popover::new(41_u64, 42_u64, false).positioner_id()
);
}
#[test]
fn system_descriptor_maps_placement_and_builds_a_transparent_popover_host() {
let descriptor = SystemPopover::new(320.0, 200.0)
.placement(AnchorPlacement::TopEnd)
.gap(8.0)
.viewport_margin(12.0)
.offset(3.0, 4.0)
.dismiss_on_escape(false);
assert_eq!(descriptor.size(), Size::new(320.0, 200.0));
let popover = descriptor.popover_options();
assert_eq!(popover.anchor_rect, Rect::ZERO);
assert_eq!(popover.anchor, PopoverAnchor::TopRight);
assert_eq!(popover.gravity, PopoverGravity::TopLeft);
assert_eq!(popover.offset, Point::new(3.0, -4.0));
assert_eq!(
popover.constraint_adjustment,
PopoverConstraintAdjustment::FIT
);
assert_eq!(popover.viewport_margin, 12.0);
assert!(!popover.dismiss_on_escape);
assert!(popover.dismiss_on_pointer_outside);
assert!(popover.grab);
assert!(popover.accepts_key_focus);
let options = descriptor.window_options("Unstyled popover");
assert_eq!(options.kind, WindowKind::SystemPopover);
assert_eq!(options.size, Size::new(320.0, 200.0));
assert_eq!(options.background, Color::TRANSPARENT);
assert_eq!(
options.window_background,
WindowBackgroundAppearance::Transparent
);
assert!(options.focus);
assert_eq!(options.popover, Some(popover));
let never_key = descriptor.accepts_key_focus(false).popover_options();
assert!(!never_key.grab);
assert!(!never_key.accepts_key_focus);
}
#[derive(Default)]
struct SystemPopoverLauncher {
popover: Option<WindowHandle>,
}
struct SystemPopoverSurface;
impl View for SystemPopoverSurface {
fn render(&mut self, _cx: &mut ViewContext<'_, Self>) -> impl IntoElement {
div().size_full()
}
}
impl View for SystemPopoverLauncher {
fn render(&mut self, cx: &mut ViewContext<'_, Self>) -> impl IntoElement {
let open = cx.listener("anchor", |view, cx| {
view.popover = Some(
SystemPopover::new(180.0, 120.0)
.gap(6.0)
.open(cx, "anchor", "System popover", SystemPopoverSurface)
.expect("a mounted window can open a system popover"),
);
});
div()
.size_full()
.relative()
.child(
button()
.id("anchor")
.absolute()
.left(280.0)
.top(180.0)
.w(40.0)
.h(30.0)
.on_click(open),
)
.child(button().id("other").child("Other"))
}
}
#[test]
fn system_popover_uses_retained_trigger_bounds_and_may_cross_the_parent_edge() {
let parent_bounds = Rect::new(100.0, 100.0, 320.0, 240.0);
let (mut cx, launcher) = Application::new()
.into_test_context(
WindowOptions::new("Anchor parent")
.window_bounds(WindowBounds::Windowed(parent_bounds))
.without_minimum_size(),
SystemPopoverLauncher::default(),
)
.unwrap();
let parent = launcher.window_handle();
cx.click(parent, "anchor").unwrap();
let popover = cx
.read(launcher, |view| view.popover)
.unwrap()
.expect("listener retained the popover handle");
let popover_state = cx.window_state(popover).unwrap();
let popover_bounds = popover_state.bounds.bounds();
assert_eq!(popover_state.kind, WindowKind::SystemPopover);
assert_eq!(popover_bounds, Rect::new(380.0, 316.0, 180.0, 120.0));
assert!(popover_bounds.right() > parent_bounds.right());
assert!(popover_bounds.bottom() > parent_bounds.bottom());
assert_eq!(
popover_bounds.intersection(cx.primary_display().unwrap().visible_bounds()),
Some(popover_bounds)
);
let renders = cx.render_count(popover).unwrap();
cx.run_until_idle().unwrap();
assert_eq!(cx.render_count(popover).unwrap(), renders);
cx.focus(parent, "other").unwrap();
assert_eq!(cx.focused(parent).unwrap(), Some("other".into()));
cx.update(launcher, |_view, cx| cx.close_window_handle(popover))
.unwrap();
assert!(!cx.is_window_open(popover));
assert_eq!(cx.focused(parent).unwrap(), Some("anchor".into()));
cx.click(parent, "anchor").unwrap();
let owned_popover = cx
.read(launcher, |view| view.popover)
.unwrap()
.expect("the anchor can reopen its system popover");
assert!(cx.is_window_open(owned_popover));
cx.update(launcher, |_view, cx| cx.close_window()).unwrap();
assert!(cx.windows().is_empty());
}
#[derive(Default)]
struct PopoverView {
open: bool,
chosen: bool,
}
impl View for PopoverView {
fn event(&mut self, event: &Event, cx: &mut EventContext) {
if *event == Event::Dismiss("surface".into()) {
self.open = false;
cx.invalidate();
}
}
fn render(&mut self, cx: &mut ViewContext<'_, Self>) -> impl IntoElement {
let popover = Popover::new("trigger", "surface", self.open).initial_focus("choose");
let toggle = cx.listener(popover.trigger_id(), move |view, cx| {
view.open = !view.open;
if view.open {
popover.focus_surface(cx);
}
cx.invalidate();
});
let choose = cx.listener("choose", move |view, cx| {
view.chosen = true;
view.open = false;
cx.invalidate();
});
let mut root = crate::div().children([
popover.trigger().on_click(toggle).child("Open"),
crate::button().id("after").child("After"),
]);
if popover.is_open() {
root = root.child(
popover.positioner_with(
crate::div().child(
popover
.popup_with(crate::div())
.accessibility_label("Actions")
.child(
crate::button()
.id("choose")
.on_click(choose)
.child("Choose"),
),
),
),
);
}
root
}
}
#[test]
fn controlled_open_focus_dismiss_and_idle_paths_use_the_existing_runtime() {
let (mut cx, view) = TestAppContext::new(PopoverView::default()).unwrap();
let window = view.window_handle();
assert!(!cx.contains_element(window, "surface").unwrap());
cx.click(window, "trigger").unwrap();
assert!(cx.read(view, |view| view.open).unwrap());
assert!(cx.contains_element(window, "surface").unwrap());
assert_eq!(cx.focused(window).unwrap(), Some("choose".into()));
let trigger_bounds = cx.element_bounds(window, "trigger").unwrap();
let positioner_bounds = cx
.element_bounds(
window,
Popover::new("trigger", "surface", true).positioner_id(),
)
.unwrap();
let popover_bounds = cx.element_bounds(window, "surface").unwrap();
assert_eq!(positioner_bounds, popover_bounds);
assert_eq!(positioner_bounds.x, DEFAULT_POPOVER_VIEWPORT_MARGIN);
assert!(positioner_bounds.x > trigger_bounds.x);
assert_eq!(
positioner_bounds.y,
trigger_bounds.bottom() + DEFAULT_POPOVER_ANCHOR_GAP
);
cx.simulate_keystrokes(window, "escape").unwrap();
assert!(!cx.read(view, |view| view.open).unwrap());
assert!(!cx.contains_element(window, "surface").unwrap());
assert_eq!(cx.focused(window).unwrap(), Some("trigger".into()));
cx.click(window, "trigger").unwrap();
cx.update(view, |view, cx| {
view.open = !view.open;
view.open = !view.open;
cx.invalidate();
})
.unwrap();
assert!(cx.contains_element(window, "surface").unwrap());
assert_eq!(cx.focused(window).unwrap(), Some("choose".into()));
cx.update(view, |view, cx| {
view.open = false;
cx.invalidate();
})
.unwrap();
assert!(!cx.contains_element(window, "surface").unwrap());
assert_eq!(cx.focused(window).unwrap(), Some("trigger".into()));
cx.click(window, "trigger").unwrap();
cx.click(window, "choose").unwrap();
assert!(cx.read(view, |view| view.chosen).unwrap());
assert!(!cx.read(view, |view| view.open).unwrap());
assert_eq!(cx.focused(window).unwrap(), Some("trigger".into()));
let renders = cx.render_count(window).unwrap();
cx.run_until_idle().unwrap();
assert_eq!(cx.render_count(window).unwrap(), renders);
}
#[test]
fn base_ui_positioner_props_map_to_bounded_anchored_geometry() {
let popover = Popover::new("trigger", "popup", true)
.side(AnchorSide::Left)
.align(AnchorAlign::End)
.side_offset(14.0)
.align_offset(-9.0)
.collision_padding(24.0)
.sticky(false)
.modal(true);
assert_eq!(
popover.resolved_anchor_placement(),
AnchorPlacement::LeftEnd
);
assert!(popover.is_modal());
let positioner = popover.positioner_with(div());
let anchor = positioner.anchor.expect("positioner anchor");
assert_eq!(anchor.target, AnchorTarget::Element("trigger".into()));
assert_eq!(anchor.placement, AnchorPlacement::LeftEnd);
assert_eq!(anchor.gap, 14.0);
assert_eq!(anchor.align_offset, -9.0);
assert_eq!(anchor.viewport_margin, 24.0);
assert!(!anchor.sticky);
assert_eq!(positioner.visual.background, None);
assert_eq!(positioner.visual.border_color, None);
assert!(!positioner.reports_anchor_placement());
assert_eq!(
popover.portal_with(div()).explicit_id,
Some(popover.positioner_id())
);
let modal_popup = popover.popup_with(div());
assert!(modal_popup.focus_trap);
assert!(
!Popover::new("trigger", "popup", true)
.popup_with(div())
.focus_trap
);
assert_eq!(
Popover::new("t", "p", true).side_offset(3.0),
Popover::new("t", "p", true).anchor_gap(3.0)
);
assert_eq!(
Popover::new("t", "p", true).collision_padding(3.0),
Popover::new("t", "p", true).viewport_margin(3.0)
);
let clamped = Popover::new("trigger", "popup", true)
.side_offset(1.0e9)
.align_offset(f32::NAN)
.collision_padding(-5.0)
.arrow_size(1.0e9)
.arrow_padding(f32::NAN);
let anchor = clamped.positioner_with(div()).anchor.expect("anchor");
assert_eq!(anchor.gap, MAX_POPOVER_SIDE_OFFSET);
assert_eq!(anchor.align_offset, 0.0);
assert_eq!(anchor.viewport_margin, 0.0);
let broken = Popover::new("trigger", "popup", true)
.side_offset(f32::INFINITY)
.collision_padding(f32::NAN);
let anchor = broken.positioner_with(div()).anchor.expect("anchor");
assert_eq!(anchor.gap, DEFAULT_POPOVER_ANCHOR_GAP);
assert_eq!(anchor.viewport_margin, DEFAULT_POPOVER_VIEWPORT_MARGIN);
assert_eq!(clamped.arrow_size, MAX_POPOVER_ARROW_SIZE);
assert_eq!(clamped.arrow_padding, 0.0);
let against_row = popover.anchor_element("row-3");
let anchor = against_row.positioner_with(div()).anchor.expect("anchor");
assert_eq!(anchor.target, AnchorTarget::Element("row-3".into()));
assert_eq!(
against_row.trigger().accessibility.relations.controls(),
Some("popup".into())
);
let at_point = popover.anchor_point(Point::new(40.0, 50.0));
let anchor = at_point.positioner_with(div()).anchor.expect("anchor");
assert_eq!(anchor.target, AnchorTarget::Point(Point::new(40.0, 50.0)));
assert_eq!(anchor.gap, 14.0);
assert_eq!(
at_point
.anchor_trigger()
.positioner_with(div())
.anchor
.expect("anchor")
.target,
AnchorTarget::Element("trigger".into())
);
let viewport = popover.viewport_with(div());
assert_eq!(viewport.explicit_id, Some(popover.viewport_id()));
assert_eq!(viewport.visual.background, None);
assert_ne!(popover.viewport_id(), popover.arrow_id());
assert_ne!(popover.viewport_id(), popover.positioner_id());
assert_ne!(popover.arrow_id(), popover.positioner_id());
let handle = AnchorPlacementHandle::new();
assert!(
popover
.tracked_positioner_with(div(), &handle)
.reports_anchor_placement()
);
assert!(
popover
.tracked_surface_with(div(), &handle)
.reports_anchor_placement()
);
}
#[test]
fn the_arrow_and_state_snapshot_follow_the_resolved_placement_not_the_preference() {
let preferred = Popover::new("trigger", "popup", true)
.side(AnchorSide::Bottom)
.arrow_size(10.0)
.arrow_padding(4.0);
assert_eq!(preferred.resolved_side(), AnchorSide::Bottom);
assert_eq!(
preferred.state(),
PopoverPartState {
open: true,
modal: false,
side: AnchorSide::Bottom,
align: AnchorAlign::Start,
anchor_hidden: false,
anchor_width: 0.0,
anchor_height: 0.0,
available_width: 0.0,
available_height: 0.0,
}
);
assert!(!preferred.state().is_measured());
let unmeasured = preferred.arrow_with(div());
assert_eq!(unmeasured.layout.inset.top, length(0.0));
assert!(unmeasured.accessibility.hidden);
let flipped = preferred.resolved_placement(Some(ResolvedAnchorPlacement {
placement: AnchorPlacement::TopStart,
anchor: Rect::new(100.0, 560.0, 40.0, 30.0),
bounds: Rect::new(100.0, 354.0, 200.0, 200.0),
available: Size::new(944.0, 546.0),
anchor_hidden: false,
}));
assert_eq!(flipped.resolved_side(), AnchorSide::Top);
assert_eq!(flipped.resolved_align(), AnchorAlign::Start);
let arrow = flipped.arrow_with(div());
assert_eq!(arrow.explicit_id, Some(flipped.arrow_id()));
assert_eq!(arrow.layout.inset.bottom, length(0.0));
assert_eq!(arrow.layout.inset.left, length(15.0));
let state = flipped.state();
assert_eq!(state.side, AnchorSide::Top);
assert_eq!(state.anchor_width, 40.0);
assert_eq!(state.anchor_height, 30.0);
assert_eq!(state.available_width, 944.0);
assert_eq!(state.available_height, 546.0);
assert!(state.is_measured());
assert!(!state.anchor_hidden);
let corner = flipped.resolved_placement(Some(ResolvedAnchorPlacement {
placement: AnchorPlacement::TopStart,
anchor: Rect::new(0.0, 560.0, 4.0, 30.0),
bounds: Rect::new(100.0, 354.0, 200.0, 200.0),
available: Size::new(944.0, 546.0),
anchor_hidden: true,
}));
assert_eq!(corner.arrow_with(div()).layout.inset.left, length(4.0));
assert!(corner.state().anchor_hidden);
let leftward = preferred.resolved_placement(Some(ResolvedAnchorPlacement {
placement: AnchorPlacement::Left,
anchor: Rect::new(400.0, 100.0, 40.0, 60.0),
bounds: Rect::new(180.0, 90.0, 200.0, 80.0),
available: Size::new(392.0, 624.0),
anchor_hidden: false,
}));
let arrow = leftward.arrow_with(div());
assert_eq!(arrow.layout.inset.right, length(0.0));
assert_eq!(arrow.layout.inset.top, length(35.0));
}
#[derive(Default)]
struct FlipView {
open: bool,
placement: AnchorPlacementHandle,
}
impl View for FlipView {
fn event(&mut self, event: &Event, cx: &mut EventContext) {
if *event == Event::Dismiss("popup".into()) {
self.open = false;
cx.invalidate();
}
}
fn render(&mut self, cx: &mut ViewContext<'_, Self>) -> impl IntoElement {
let popover = Popover::new("trigger", "popup", self.open)
.side(AnchorSide::Bottom)
.align(AnchorAlign::Start)
.side_offset(6.0)
.arrow_size(10.0)
.arrow_padding(4.0)
.track_placement(&self.placement);
let toggle = cx.listener("trigger", |view: &mut Self, cx: &mut EventContext| {
view.open = !view.open;
cx.invalidate();
});
let mut root = div().size_full().relative().child(
popover
.trigger()
.absolute()
.left(100.0)
.top(560.0)
.w(40.0)
.h(30.0)
.on_click(toggle),
);
if popover.is_open() {
root = root.child(
popover.tracked_positioner_with(
div().child(
popover
.popup_with(div())
.relative()
.w(200.0)
.h(200.0)
.accessibility_label("Actions")
.child(popover.arrow_with(div().w(10.0).h(6.0))),
),
&self.placement,
),
);
}
root
}
}
#[test]
fn a_flipped_popover_reports_its_real_placement_and_repositions_the_arrow() {
let (mut cx, view) = TestAppContext::new(FlipView::default()).unwrap();
let window = view.window_handle();
assert_eq!(
cx.read(view, |view| view.placement.resolved()).unwrap(),
None
);
cx.click(window, "trigger").unwrap();
let popup_bounds = cx.element_bounds(window, "popup").unwrap();
cx.run_until_idle().unwrap();
let resolved = cx
.read(view, |view| view.placement.resolved())
.unwrap()
.expect("a painted anchored surface publishes its resolved placement");
assert_eq!(resolved.placement, AnchorPlacement::TopStart);
assert_eq!(resolved.side(), AnchorSide::Top);
assert_eq!(resolved.align(), AnchorAlign::Start);
assert_eq!(resolved.anchor, Rect::new(100.0, 560.0, 40.0, 30.0));
assert_eq!(resolved.bounds, popup_bounds);
assert_eq!(resolved.available.height, 560.0 - 8.0 - 6.0);
assert!(!resolved.anchor_hidden);
assert!(popup_bounds.bottom() <= 560.0);
let arrow_bounds = cx
.element_bounds(window, Popover::new("trigger", "popup", true).arrow_id())
.unwrap();
assert_eq!(arrow_bounds.bottom(), popup_bounds.bottom());
assert_eq!(arrow_bounds.x, popup_bounds.x + 15.0);
let renders = cx.render_count(window).unwrap();
cx.run_until_idle().unwrap();
assert_eq!(cx.render_count(window).unwrap(), renders);
cx.simulate_keystrokes(window, "escape").unwrap();
assert!(!cx.read(view, |view| view.open).unwrap());
cx.update(view, |view, _cx| view.placement.clear()).unwrap();
assert_eq!(
cx.read(view, |view| view.placement.resolved()).unwrap(),
None
);
}
struct HoverPopoverView {
hover: PopoverHoverState,
}
impl Default for HoverPopoverView {
fn default() -> Self {
Self {
hover: PopoverHoverState::new()
.delay(Duration::from_millis(200))
.close_delay(Duration::from_millis(100)),
}
}
}
impl View for HoverPopoverView {
fn render(&mut self, cx: &mut ViewContext<'_, Self>) -> impl IntoElement {
let popover = Popover::new("trigger", "popup", self.hover.is_open());
let trigger = self.hover.trigger_with(
cx,
popover,
|view| &mut view.hover,
div().absolute().left(0.0).top(0.0).w(60.0).h(20.0),
);
let mut root = div().size_full().relative().child(trigger);
if popover.is_open() {
let popup = self.hover.popup_with(
cx,
popover,
|view| &mut view.hover,
div().w(100.0).h(60.0),
);
root = root.child(popover.positioner_with(div().child(popup)));
}
root
}
}
#[test]
fn hover_opening_uses_exact_one_shot_deadlines_and_survives_the_gap_to_the_popup() {
let (mut cx, view) = TestAppContext::new(HoverPopoverView::default()).unwrap();
let window = view.window_handle();
assert!(!cx.contains_element(window, "popup").unwrap());
cx.visual(window)
.unwrap()
.move_pointer(Point::new(10.0, 10.0))
.unwrap();
assert!(cx.read(view, |view| view.hover.is_pending()).unwrap());
assert!(!cx.read(view, |view| view.hover.is_open()).unwrap());
cx.advance_time(Duration::from_millis(199)).unwrap();
assert!(!cx.read(view, |view| view.hover.is_open()).unwrap());
cx.advance_time(Duration::from_millis(1)).unwrap();
assert!(cx.read(view, |view| view.hover.is_open()).unwrap());
assert!(!cx.read(view, |view| view.hover.is_pending()).unwrap());
assert!(cx.contains_element(window, "popup").unwrap());
let renders = cx.render_count(window).unwrap();
cx.run_until_idle().unwrap();
assert_eq!(cx.render_count(window).unwrap(), renders);
cx.visual(window)
.unwrap()
.move_pointer(Point::new(600.0, 400.0))
.unwrap();
assert!(cx.read(view, |view| view.hover.is_pending()).unwrap());
cx.visual(window)
.unwrap()
.move_pointer(Point::new(40.0, 50.0))
.unwrap();
assert!(!cx.read(view, |view| view.hover.is_pending()).unwrap());
cx.advance_time(Duration::from_millis(500)).unwrap();
assert!(cx.read(view, |view| view.hover.is_open()).unwrap());
cx.visual(window)
.unwrap()
.move_pointer(Point::new(600.0, 400.0))
.unwrap();
cx.advance_time(Duration::from_millis(99)).unwrap();
assert!(cx.read(view, |view| view.hover.is_open()).unwrap());
cx.advance_time(Duration::from_millis(1)).unwrap();
assert!(!cx.read(view, |view| view.hover.is_open()).unwrap());
assert!(!cx.contains_element(window, "popup").unwrap());
let renders = cx.render_count(window).unwrap();
cx.run_until_idle().unwrap();
assert_eq!(cx.render_count(window).unwrap(), renders);
}
#[test]
fn hover_state_defaults_are_bounded_and_immediate_changes_cancel_deadlines() {
let state = PopoverHoverState::new();
assert!(!state.is_open());
assert!(!state.is_pending());
assert_eq!(state.delay, DEFAULT_POPOVER_HOVER_DELAY);
assert_eq!(state.close_delay, Duration::ZERO);
assert_eq!(
PopoverHoverState::new()
.delay(Duration::from_secs(3_600))
.delay,
MAX_POPOVER_HOVER_DELAY
);
assert_eq!(
PopoverHoverState::new()
.close_delay(Duration::from_secs(3_600))
.close_delay,
MAX_POPOVER_HOVER_DELAY
);
let mut state = PopoverHoverState::new().hoverable_popup(false);
assert!(state.open_now());
assert!(!state.open_now());
assert!(state.is_open());
assert!(state.toggle());
assert!(!state.is_open());
assert!(state.toggle());
assert!(state.close_now());
assert!(!state.close_now());
state.popup_hovered = true;
assert!(!state.is_hovered());
}
}