use waterui_core::layout::{Alignment, HorizontalAlignment, VerticalAlignment};
macro_rules! token {
($(#[$meta:meta])* $name:ident => $($target:ident),+) => {
$(#[$meta])*
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct $name;
$(
impl From<$name> for $target {
fn from(_: $name) -> Self {
Self::$name
}
}
)+
};
}
token!(
Leading => HorizontalAlignment, Alignment
);
token!(
Trailing => HorizontalAlignment, Alignment
);
token!(
Center => HorizontalAlignment, VerticalAlignment, Alignment
);
token!(
Top => VerticalAlignment, Alignment
);
token!(
Bottom => VerticalAlignment, Alignment
);
token!(
FirstBaseline => VerticalAlignment
);
token!(
LastBaseline => VerticalAlignment
);
token!(
TopLeading => Alignment
);
token!(
TopTrailing => Alignment
);
token!(
BottomLeading => Alignment
);
token!(
BottomTrailing => Alignment
);
macro_rules! horizontal_alignment_methods {
() => {
#[must_use]
pub fn leading(self) -> Self {
self.alignment($crate::alignment::Leading)
}
#[must_use]
pub fn centered(self) -> Self {
self.alignment($crate::alignment::Center)
}
#[must_use]
pub fn trailing(self) -> Self {
self.alignment($crate::alignment::Trailing)
}
};
}
macro_rules! vertical_alignment_methods {
() => {
#[must_use]
pub fn top(self) -> Self {
self.alignment($crate::alignment::Top)
}
#[must_use]
pub fn centered(self) -> Self {
self.alignment($crate::alignment::Center)
}
#[must_use]
pub fn bottom(self) -> Self {
self.alignment($crate::alignment::Bottom)
}
#[must_use]
pub fn first_baseline(self) -> Self {
self.alignment($crate::alignment::FirstBaseline)
}
#[must_use]
pub fn last_baseline(self) -> Self {
self.alignment($crate::alignment::LastBaseline)
}
};
}
macro_rules! two_dimensional_alignment_methods {
() => {
#[must_use]
pub fn top_leading(self) -> Self {
self.alignment($crate::alignment::TopLeading)
}
#[must_use]
pub fn top(self) -> Self {
self.alignment($crate::alignment::Top)
}
#[must_use]
pub fn top_trailing(self) -> Self {
self.alignment($crate::alignment::TopTrailing)
}
#[must_use]
pub fn leading(self) -> Self {
self.alignment($crate::alignment::Leading)
}
#[must_use]
pub fn centered(self) -> Self {
self.alignment($crate::alignment::Center)
}
#[must_use]
pub fn trailing(self) -> Self {
self.alignment($crate::alignment::Trailing)
}
#[must_use]
pub fn bottom_leading(self) -> Self {
self.alignment($crate::alignment::BottomLeading)
}
#[must_use]
pub fn bottom(self) -> Self {
self.alignment($crate::alignment::Bottom)
}
#[must_use]
pub fn bottom_trailing(self) -> Self {
self.alignment($crate::alignment::BottomTrailing)
}
};
}
pub(crate) use {
horizontal_alignment_methods, two_dimensional_alignment_methods, vertical_alignment_methods,
};
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn tokens_convert_to_the_positions_they_name() {
assert_eq!(
HorizontalAlignment::from(Leading),
HorizontalAlignment::Leading
);
assert_eq!(VerticalAlignment::from(Bottom), VerticalAlignment::Bottom);
assert_eq!(Alignment::from(Center), Alignment::Center);
assert_eq!(Alignment::from(Leading), Alignment::Leading);
assert_eq!(Alignment::from(BottomTrailing), Alignment::BottomTrailing);
}
}