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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
use floating_ui_utils::{
    get_alignment, get_side, get_side_axis, Alignment, Axis, Coords, Placement, Side,
};
use serde::{Deserialize, Serialize};

use crate::{
    middleware::{ArrowData, ARROW_NAME},
    types::{
        Derivable, DerivableFn, Middleware, MiddlewareReturn, MiddlewareState,
        MiddlewareWithOptions,
    },
};

fn convert_value_to_coords<Element: Clone, Window: Clone>(
    state: MiddlewareState<Element, Window>,
    options: &OffsetOptions,
) -> Coords {
    let MiddlewareState {
        placement,
        platform,
        elements,
        ..
    } = state;

    let rtl = platform.is_rtl(elements.floating).unwrap_or(false);
    let side = get_side(placement);
    let alignment = get_alignment(placement);
    let is_vertical = get_side_axis(placement) == Axis::Y;
    let main_axis_multi = match side {
        Side::Left | Side::Top => -1.0,
        Side::Right | Side::Bottom => 1.0,
    };
    let cross_axis_multi = match rtl && is_vertical {
        true => -1.0,
        false => 1.0,
    };

    let (main_axis, mut cross_axis, alignment_axis): (f64, f64, Option<f64>) = match options {
        OffsetOptions::Value(value) => (*value, 0.0, None),
        OffsetOptions::Values(values) => (
            values.main_axis.unwrap_or(0.0),
            values.cross_axis.unwrap_or(0.0),
            values.alignment_axis,
        ),
    };

    if let Some(alignment) = alignment {
        if let Some(alignment_axis) = alignment_axis {
            cross_axis = match alignment {
                Alignment::Start => alignment_axis,
                Alignment::End => alignment_axis * -1.0,
            };
        }
    }

    match is_vertical {
        true => Coords {
            x: cross_axis * cross_axis_multi,
            y: main_axis * main_axis_multi,
        },
        false => Coords {
            x: main_axis * main_axis_multi,
            y: cross_axis * cross_axis_multi,
        },
    }
}

/// Name of the [`Offset`] middleware.
pub const OFFSET_NAME: &str = "offset";

/// Axes configuration for [`OffsetOptions`].
#[derive(Clone, Default, Debug)]
pub struct OffsetOptionsValues {
    /// The axis that runs along the side of the floating element. Represents the distance (gutter or margin) between the reference and floating element.
    ///
    /// Defaults to `0`.
    pub main_axis: Option<f64>,

    /// The axis that runs along the alignment of the floating element. Represents the skidding between the reference and floating element.
    ///
    /// Defaults to `0`.
    pub cross_axis: Option<f64>,

    /// The same axis as [`cross_axis`][`Self::cross_axis`] but applies only to aligned placements and inverts the [`End`][`floating_ui_utils::Alignment::End`] alignment.
    /// When set to a number, it overrides the [`cross_axis`][`Self::cross_axis`] value.
    ///
    /// A positive number will move the floating element in the direction of the opposite edge to the one that is aligned, while a negative number the reverse.
    ///
    /// Defaults to [`Option::None`].
    pub alignment_axis: Option<f64>,
}

impl OffsetOptionsValues {
    /// Set `main_axis` option.
    pub fn main_axis(mut self, value: f64) -> Self {
        self.main_axis = Some(value);
        self
    }

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

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

/// Options for [`Offset`] middleware.
///
/// A number (shorthand for [`main_axis`][`OffsetOptionsValues::main_axis`] or distance) or an axes configuration ([`OffsetOptionsValues`]).
#[derive(Clone, Debug)]
pub enum OffsetOptions {
    Value(f64),
    Values(OffsetOptionsValues),
}

impl Default for OffsetOptions {
    fn default() -> Self {
        OffsetOptions::Value(0.0)
    }
}

/// Data stored by [`Offset`] middleware.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct OffsetData {
    pub diff_coords: Coords,
    pub placement: Placement,
}

/// Modifies the placement by translating the floating element along the specified axes.
///
/// See <https://floating-ui.com/docs/offset> for the original documentation.
pub struct Offset<'a, Element: Clone, Window: Clone> {
    options: Derivable<'a, Element, Window, OffsetOptions>,
}

impl<'a, Element: Clone, Window: Clone> Offset<'a, Element, Window> {
    /// Constructs a new instance of this middleware.
    pub fn new(options: OffsetOptions) -> Self {
        Offset {
            options: options.into(),
        }
    }

    /// Constructs a new instance of this middleware with derivable options.
    pub fn new_derivable(options: Derivable<'a, Element, Window, OffsetOptions>) -> Self {
        Offset { options }
    }

    /// Constructs a new instance of this middleware with derivable options function.
    pub fn new_derivable_fn(options: DerivableFn<'a, Element, Window, OffsetOptions>) -> Self {
        Offset {
            options: options.into(),
        }
    }
}

impl<'a, Element: Clone, Window: Clone> Clone for Offset<'a, Element, Window> {
    fn clone(&self) -> Self {
        Self {
            options: self.options.clone(),
        }
    }
}

impl<'a, Element: Clone, Window: Clone> Middleware<Element, Window>
    for Offset<'a, Element, Window>
{
    fn name(&self) -> &'static str {
        OFFSET_NAME
    }

    fn compute(&self, state: MiddlewareState<Element, Window>) -> MiddlewareReturn {
        let options = self.options.evaluate(state.clone());

        let MiddlewareState {
            x,
            y,
            placement,
            middleware_data,
            ..
        } = state;

        let data: Option<OffsetData> = middleware_data.get_as(self.name());

        let diff_coords = convert_value_to_coords(state, &options);

        // If the placement is the same and the arrow caused an alignment offset then we don't need to change the positioning coordinates.
        if let Some(data_placement) = data.map(|data| data.placement) {
            if placement == data_placement {
                let arrow_data: Option<ArrowData> = middleware_data.get_as(ARROW_NAME);
                if arrow_data.map_or(false, |arrow_data| arrow_data.alignment_offset.is_some()) {
                    return MiddlewareReturn {
                        x: None,
                        y: None,
                        data: None,
                        reset: None,
                    };
                }
            }
        }

        MiddlewareReturn {
            x: Some(x + diff_coords.x),
            y: Some(y + diff_coords.y),
            data: Some(
                serde_json::to_value(OffsetData {
                    diff_coords,
                    placement,
                })
                .expect("Data should be valid JSON."),
            ),
            reset: None,
        }
    }
}

impl<'a, Element: Clone, Window: Clone> MiddlewareWithOptions<Element, Window, OffsetOptions>
    for Offset<'a, Element, Window>
{
    fn options(&self) -> &Derivable<Element, Window, OffsetOptions> {
        &self.options
    }
}