Skip to content

Commit e127a71

Browse files
Add conversion utilities
1 parent 89a5833 commit e127a71

1 file changed

Lines changed: 90 additions & 56 deletions

File tree

packages/utils/src/lib.rs

Lines changed: 90 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,15 @@ impl Side {
3333
Side::Left => Side::Right,
3434
}
3535
}
36+
37+
pub fn axis(&self) -> Axis {
38+
match self {
39+
Side::Top => Axis::Y,
40+
Side::Right => Axis::X,
41+
Side::Bottom => Axis::Y,
42+
Side::Left => Axis::X,
43+
}
44+
}
3645
}
3746

3847
#[derive(Copy, Clone, Debug, PartialEq)]
@@ -97,6 +106,59 @@ impl Placement {
97106
Placement::LeftEnd => Side::Left,
98107
}
99108
}
109+
110+
pub fn opposite(&self) -> Placement {
111+
match self {
112+
Placement::Top => Placement::Bottom,
113+
Placement::TopStart => Placement::BottomStart,
114+
Placement::TopEnd => Placement::BottomEnd,
115+
Placement::Right => Placement::Left,
116+
Placement::RightStart => Placement::LeftStart,
117+
Placement::RightEnd => Placement::LeftEnd,
118+
Placement::Bottom => Placement::Top,
119+
Placement::BottomStart => Placement::TopStart,
120+
Placement::BottomEnd => Placement::TopEnd,
121+
Placement::Left => Placement::Right,
122+
Placement::LeftStart => Placement::RightStart,
123+
Placement::LeftEnd => Placement::RightEnd,
124+
}
125+
}
126+
127+
pub fn opposite_alignment(&self) -> Placement {
128+
match self {
129+
Placement::Top => Placement::Top,
130+
Placement::TopStart => Placement::TopEnd,
131+
Placement::TopEnd => Placement::TopStart,
132+
Placement::Right => Placement::Right,
133+
Placement::RightStart => Placement::RightEnd,
134+
Placement::RightEnd => Placement::RightStart,
135+
Placement::Bottom => Placement::Bottom,
136+
Placement::BottomStart => Placement::BottomEnd,
137+
Placement::BottomEnd => Placement::BottomStart,
138+
Placement::Left => Placement::Left,
139+
Placement::LeftStart => Placement::LeftEnd,
140+
Placement::LeftEnd => Placement::LeftStart,
141+
}
142+
}
143+
}
144+
145+
impl From<(Side, Option<Alignment>)> for Placement {
146+
fn from(value: (Side, Option<Alignment>)) -> Self {
147+
match value {
148+
(Side::Top, None) => Placement::Top,
149+
(Side::Top, Some(Alignment::Start)) => Placement::TopStart,
150+
(Side::Top, Some(Alignment::End)) => Placement::TopEnd,
151+
(Side::Right, None) => Placement::Right,
152+
(Side::Right, Some(Alignment::Start)) => Placement::RightStart,
153+
(Side::Right, Some(Alignment::End)) => Placement::RightEnd,
154+
(Side::Bottom, None) => Placement::Bottom,
155+
(Side::Bottom, Some(Alignment::Start)) => Placement::BottomStart,
156+
(Side::Bottom, Some(Alignment::End)) => Placement::BottomEnd,
157+
(Side::Left, None) => Placement::Left,
158+
(Side::Left, Some(Alignment::Start)) => Placement::LeftStart,
159+
(Side::Left, Some(Alignment::End)) => Placement::LeftEnd,
160+
}
161+
}
100162
}
101163

102164
#[derive(Copy, Clone, Debug, PartialEq)]
@@ -112,6 +174,13 @@ pub enum Axis {
112174
}
113175

114176
impl Axis {
177+
pub fn opposite(&self) -> Axis {
178+
match self {
179+
Axis::X => Axis::Y,
180+
Axis::Y => Axis::X,
181+
}
182+
}
183+
115184
pub fn length(&self) -> Length {
116185
match self {
117186
Axis::X => Length::Width,
@@ -243,6 +312,21 @@ pub struct ClientRectObject {
243312
pub left: f64,
244313
}
245314

315+
impl From<Rect> for ClientRectObject {
316+
fn from(value: Rect) -> Self {
317+
ClientRectObject {
318+
x: value.x,
319+
y: value.y,
320+
width: value.width,
321+
height: value.height,
322+
top: value.y,
323+
right: value.x + value.width,
324+
bottom: value.y + value.height,
325+
left: value.x,
326+
}
327+
}
328+
}
329+
246330
cfg_if::cfg_if! {
247331
if #[cfg(feature = "dom")] {
248332
impl ClientRectObject {
@@ -501,43 +585,19 @@ pub fn get_alignment(placement: Placement) -> Option<Alignment> {
501585
}
502586

503587
pub fn get_placement(side: Side, alignment: Option<Alignment>) -> Placement {
504-
match (side, alignment) {
505-
(Side::Top, None) => Placement::Top,
506-
(Side::Top, Some(Alignment::Start)) => Placement::TopStart,
507-
(Side::Top, Some(Alignment::End)) => Placement::TopEnd,
508-
(Side::Right, None) => Placement::Right,
509-
(Side::Right, Some(Alignment::Start)) => Placement::RightStart,
510-
(Side::Right, Some(Alignment::End)) => Placement::RightEnd,
511-
(Side::Bottom, None) => Placement::Bottom,
512-
(Side::Bottom, Some(Alignment::Start)) => Placement::BottomStart,
513-
(Side::Bottom, Some(Alignment::End)) => Placement::BottomEnd,
514-
(Side::Left, None) => Placement::Left,
515-
(Side::Left, Some(Alignment::Start)) => Placement::LeftStart,
516-
(Side::Left, Some(Alignment::End)) => Placement::LeftEnd,
517-
}
588+
(side, alignment).into()
518589
}
519590

520591
pub fn get_opposite_axis(axis: Axis) -> Axis {
521-
match axis {
522-
Axis::X => Axis::Y,
523-
Axis::Y => Axis::X,
524-
}
592+
axis.opposite()
525593
}
526594

527595
pub fn get_axis_length(axis: Axis) -> Length {
528-
match axis {
529-
Axis::X => Length::Width,
530-
Axis::Y => Length::Height,
531-
}
596+
axis.length()
532597
}
533598

534599
pub fn get_side_axis(placement: Placement) -> Axis {
535-
match get_side(placement) {
536-
Side::Top => Axis::Y,
537-
Side::Right => Axis::X,
538-
Side::Bottom => Axis::Y,
539-
Side::Left => Axis::X,
540-
}
600+
placement.side().axis()
541601
}
542602

543603
pub fn get_alignment_axis(placement: Placement) -> Axis {
@@ -584,20 +644,7 @@ pub fn get_expanded_placements(placement: Placement) -> Vec<Placement> {
584644
}
585645

586646
pub fn get_opposite_alignment_placement(placement: Placement) -> Placement {
587-
match placement {
588-
Placement::Top => Placement::Top,
589-
Placement::TopStart => Placement::TopEnd,
590-
Placement::TopEnd => Placement::TopStart,
591-
Placement::Right => Placement::Right,
592-
Placement::RightStart => Placement::RightEnd,
593-
Placement::RightEnd => Placement::RightStart,
594-
Placement::Bottom => Placement::Bottom,
595-
Placement::BottomStart => Placement::BottomEnd,
596-
Placement::BottomEnd => Placement::BottomStart,
597-
Placement::Left => Placement::Left,
598-
Placement::LeftStart => Placement::LeftEnd,
599-
Placement::LeftEnd => Placement::LeftStart,
600-
}
647+
placement.opposite_alignment()
601648
}
602649

603650
pub fn get_side_list(side: Side, is_start: bool, rtl: Option<bool>) -> Vec<Side> {
@@ -655,20 +702,7 @@ pub fn get_opposite_axis_placements(
655702
}
656703

657704
pub fn get_opposite_placement(placement: Placement) -> Placement {
658-
match placement {
659-
Placement::Top => Placement::Bottom,
660-
Placement::TopStart => Placement::BottomStart,
661-
Placement::TopEnd => Placement::BottomEnd,
662-
Placement::Right => Placement::Left,
663-
Placement::RightStart => Placement::LeftStart,
664-
Placement::RightEnd => Placement::LeftEnd,
665-
Placement::Bottom => Placement::Top,
666-
Placement::BottomStart => Placement::TopStart,
667-
Placement::BottomEnd => Placement::TopEnd,
668-
Placement::Left => Placement::Right,
669-
Placement::LeftStart => Placement::RightStart,
670-
Placement::LeftEnd => Placement::RightEnd,
671-
}
705+
placement.opposite()
672706
}
673707

674708
pub fn expand_padding_object(padding: PartialSideObject) -> SideObject {

0 commit comments

Comments
 (0)