1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
use floating_ui_utils::{
    get_padding_object, rect_to_client_rect, Coords, ElementOrVirtual, OwnedElementOrWindow,
    Padding, Rect, SideObject,
};

use crate::types::{
    Boundary, ConvertOffsetParentRelativeRectToViewportRelativeRectArgs, ElementContext, Elements,
    GetClippingRectArgs, MiddlewareState, RootBoundary,
};

/// Options for [`detect_overflow`].
#[derive(Clone, Debug)]
pub struct DetectOverflowOptions<Element> {
    /// The clipping element(s) or area in which overflow will be checked.
    ///
    /// Defaults to [`Boundary::ClippingAncestors`].
    pub boundary: Option<Boundary<Element>>,

    /// The root clipping area in which overflow will be checked.
    ///
    /// Defaults to [`RootBoundary::Viewport`].
    pub root_boundary: Option<RootBoundary>,

    /// The element in which overflow is being checked relative to a boundary.
    ///
    /// Defaults to [`ElementContext::Floating`].
    pub element_context: Option<ElementContext>,

    /// Whether to check for overflow using the alternate element's boundary (only when [`boundary`][`Self::boundary`] is [`Boundary::ClippingAncestors`]).
    ///
    /// Defaults to `false`.
    pub alt_boundary: Option<bool>,

    /// Virtual padding for the resolved overflow detection offsets.
    ///
    /// Defaults to `0` on all sides.
    pub padding: Option<Padding>,
}

impl<Element> DetectOverflowOptions<Element> {
    /// Set `boundary` option.
    pub fn boundary(mut self, value: Boundary<Element>) -> Self {
        self.boundary = Some(value);
        self
    }

    /// Set `root_boundary` option.
    pub fn root_boundary(mut self, value: RootBoundary) -> Self {
        self.root_boundary = Some(value);
        self
    }

    /// Set `element_context` option.
    pub fn element_context(mut self, value: ElementContext) -> Self {
        self.element_context = Some(value);
        self
    }

    /// Set `alt_boundary` option.
    pub fn alt_boundary(mut self, value: bool) -> Self {
        self.alt_boundary = Some(value);
        self
    }

    /// Set `padding` option.
    pub fn padding(mut self, value: Padding) -> Self {
        self.padding = Some(value);
        self
    }
}

impl<Element> Default for DetectOverflowOptions<Element> {
    fn default() -> Self {
        Self {
            boundary: Default::default(),
            root_boundary: Default::default(),
            element_context: Default::default(),
            alt_boundary: Default::default(),
            padding: Default::default(),
        }
    }
}

/// Resolves with an object of overflow side offsets that determine how much the element is overflowing a given clipping boundary on each side.
/// - positive = overflowing the boundary by that number of pixels
/// - negative = how many pixels left before it will overflow
/// - `0` = lies flush with the boundary
///
/// See <https://floating-ui.com/docs/detectOverflow> for the original documentation.
pub fn detect_overflow<Element: Clone, Window: Clone>(
    state: MiddlewareState<Element, Window>,
    options: DetectOverflowOptions<Element>,
) -> SideObject {
    let MiddlewareState {
        x,
        y,
        platform,
        rects,
        elements,
        strategy,
        ..
    } = state;

    let boundary = options.boundary.unwrap_or(Boundary::ClippingAncestors);
    let root_boundary = options.root_boundary.unwrap_or(RootBoundary::Viewport);
    let element_context = options.element_context.unwrap_or(ElementContext::Floating);
    let alt_boundary = options.alt_boundary.unwrap_or(false);
    let padding = options.padding.unwrap_or(Padding::All(0.0));

    let padding_object = get_padding_object(padding);
    let alt_context = match element_context {
        ElementContext::Reference => ElementContext::Floating,
        ElementContext::Floating => ElementContext::Reference,
    };
    let element = match alt_boundary {
        true => elements.get_element_context(alt_context),
        false => elements.get_element_context(element_context),
    };

    let document_element = platform.get_document_element(elements.floating);
    let context_element: Option<Element>;

    let element = match element {
        ElementOrVirtual::Element(element) => element,
        ElementOrVirtual::VirtualElement(virtual_element) => {
            context_element = virtual_element.context_element();

            context_element
                .as_ref()
                .or(document_element.as_ref())
                .expect("Element should exist.")
        }
    };

    let clipping_client_rect =
        rect_to_client_rect(platform.get_clipping_rect(GetClippingRectArgs {
            element,
            boundary,
            root_boundary,
            strategy,
        }));

    let rect = match element_context {
        ElementContext::Reference => rects.reference.clone(),
        ElementContext::Floating => Rect {
            x,
            y,
            width: rects.floating.width,
            height: rects.floating.height,
        },
    };

    let offset_parent = platform.get_offset_parent(elements.floating);
    let offset_scale = match offset_parent.as_ref() {
        Some(offset_parent) => match offset_parent {
            OwnedElementOrWindow::Element(element) => {
                platform.get_scale(element).unwrap_or(Coords::new(1.0))
            }
            OwnedElementOrWindow::Window(_) => Coords::new(1.0),
        },
        None => Coords::new(1.0),
    };

    let element_client_rect = rect_to_client_rect(
        platform
            .convert_offset_parent_relative_rect_to_viewport_relative_rect(
                ConvertOffsetParentRelativeRectToViewportRelativeRectArgs {
                    elements: Some(Elements {
                        reference: elements.reference,
                        floating: elements.floating,
                    }),
                    rect: rect.clone(),
                    offset_parent: offset_parent
                        .as_ref()
                        .map(|offset_parent| offset_parent.into()),
                    strategy,
                },
            )
            .unwrap_or(rect),
    );

    SideObject {
        top: (clipping_client_rect.top - element_client_rect.top + padding_object.top)
            / offset_scale.y,
        right: (element_client_rect.right - clipping_client_rect.right + padding_object.right)
            / offset_scale.x,
        bottom: (element_client_rect.bottom - clipping_client_rect.bottom + padding_object.bottom)
            / offset_scale.y,
        left: (clipping_client_rect.left - element_client_rect.left + padding_object.left)
            / offset_scale.x,
    }
}