floating_ui/floating_ui/utils/
mod.rs1use crate::{
2 utils_::{key_in, object_utils, string_union, StringUnion},
3 Error, Result,
4};
5
6string_union!(Alignment {
7 "start" => Start,
8 "end" => End
9});
10
11string_union!(Side {
12 "top" => Top,
13 "bottom" => Bottom,
14 "left" => Left,
15 "right" => Right
16});
17
18mod aligned_placement;
19pub use aligned_placement::AlignmentPlacement;
20
21mod placement;
22pub use placement::Placement;
23
24string_union!(Strategy {
25 "absolute" => Absolute,
26 "fixed" => Fixed
27});
28
29string_union!(Axis {
30 "x" => X,
31 "y" => Y
32});
33
34key_in!(Coords, Axis { X, Y } => f64);
35
36string_union!(Length {
37 "width" => Width,
38 "height" => Height
39});
40
41key_in!(Dimensions, Length { Width, Height } => f64);
42
43mod side_object;
44pub use side_object::{PartialSideObject, SideObject};
45
46mod rect;
47pub use rect::Rect;
48
49mod padding;
50pub use padding::Padding;
51
52mod client_rect_object;
53pub use client_rect_object::ClientRectObject;
54use wasm_bindgen::JsValue;
55
56#[derive(Clone)]
57pub struct ElementRects {
58 pub reference: Rect,
59 pub floating: Rect,
60}
61
62impl TryFrom<JsValue> for ElementRects {
63 type Error = Error;
64
65 fn try_from(value: JsValue) -> Result<Self> {
66 let obj = js_sys::Object::from(value);
67
68 let reference = object_utils::get_js_value(&obj, "reference")?.try_into()?;
69 let floating = object_utils::get_js_value(&obj, "floating")?.try_into()?;
70
71 Ok(Self {
72 reference,
73 floating,
74 })
75 }
76}
77
78impl TryInto<JsValue> for ElementRects {
79 type Error = Error;
80
81 fn try_into(self) -> Result<JsValue> {
82 let obj = js_sys::Object::new();
83
84 object_utils::set_js_value(&obj, "reference", &self.reference.try_into()?)?;
85 object_utils::set_js_value(&obj, "floating", &self.floating.try_into()?)?;
86
87 Ok(obj.into())
88 }
89}