Introduce helper methods for alignment for all widgets
This commit is contained in:
parent
be06060117
commit
f9dd5cbb09
45 changed files with 380 additions and 282 deletions
|
|
@ -1,5 +1,30 @@
|
|||
//! Align and position widgets.
|
||||
|
||||
/// Returns a value representing center alignment.
|
||||
pub const fn center() -> Alignment {
|
||||
Alignment::Center
|
||||
}
|
||||
|
||||
/// Returns a value representing left alignment.
|
||||
pub const fn left() -> Horizontal {
|
||||
Horizontal::Left
|
||||
}
|
||||
|
||||
/// Returns a value representing right alignment.
|
||||
pub const fn right() -> Horizontal {
|
||||
Horizontal::Right
|
||||
}
|
||||
|
||||
/// Returns a value representing top alignment.
|
||||
pub const fn top() -> Vertical {
|
||||
Vertical::Top
|
||||
}
|
||||
|
||||
/// Returns a value representing bottom alignment.
|
||||
pub const fn bottom() -> Vertical {
|
||||
Vertical::Bottom
|
||||
}
|
||||
|
||||
/// Alignment on the axis of a container.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
pub enum Alignment {
|
||||
|
|
@ -46,6 +71,16 @@ pub enum Horizontal {
|
|||
Right,
|
||||
}
|
||||
|
||||
impl From<Alignment> for Horizontal {
|
||||
fn from(alignment: Alignment) -> Self {
|
||||
match alignment {
|
||||
Alignment::Start => Self::Left,
|
||||
Alignment::Center => Self::Center,
|
||||
Alignment::End => Self::Right,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The vertical [`Alignment`] of some resource.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
pub enum Vertical {
|
||||
|
|
@ -58,3 +93,13 @@ pub enum Vertical {
|
|||
/// Align bottom
|
||||
Bottom,
|
||||
}
|
||||
|
||||
impl From<Alignment> for Vertical {
|
||||
fn from(alignment: Alignment) -> Self {
|
||||
match alignment {
|
||||
Alignment::Start => Self::Top,
|
||||
Alignment::Center => Self::Center,
|
||||
Alignment::End => Self::Bottom,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/// (e.g. `impl Into<Pixels>`) and, since `Pixels` implements `From` both for
|
||||
/// `f32` and `u16`, you should be able to provide both integers and float
|
||||
/// literals as needed.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, PartialOrd)]
|
||||
#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Default)]
|
||||
pub struct Pixels(pub f32);
|
||||
|
||||
impl From<f32> for Pixels {
|
||||
|
|
@ -27,6 +27,30 @@ impl From<Pixels> for f32 {
|
|||
}
|
||||
}
|
||||
|
||||
impl std::ops::Add for Pixels {
|
||||
type Output = Pixels;
|
||||
|
||||
fn add(self, rhs: Self) -> Self {
|
||||
Pixels(self.0 + rhs.0)
|
||||
}
|
||||
}
|
||||
|
||||
impl std::ops::Add<f32> for Pixels {
|
||||
type Output = Pixels;
|
||||
|
||||
fn add(self, rhs: f32) -> Self {
|
||||
Pixels(self.0 + rhs)
|
||||
}
|
||||
}
|
||||
|
||||
impl std::ops::Mul for Pixels {
|
||||
type Output = Pixels;
|
||||
|
||||
fn mul(self, rhs: Self) -> Self {
|
||||
Pixels(self.0 * rhs.0)
|
||||
}
|
||||
}
|
||||
|
||||
impl std::ops::Mul<f32> for Pixels {
|
||||
type Output = Pixels;
|
||||
|
||||
|
|
|
|||
|
|
@ -86,21 +86,56 @@ where
|
|||
self
|
||||
}
|
||||
|
||||
/// Centers the [`Text`], both horizontally and vertically.
|
||||
pub fn center(self) -> Self {
|
||||
self.center_x().center_y()
|
||||
}
|
||||
|
||||
/// Centers the [`Text`] horizontally.
|
||||
pub fn center_x(self) -> Self {
|
||||
self.align_x(alignment::center())
|
||||
}
|
||||
|
||||
/// Aligns the [`Text`] to the left, the default.
|
||||
pub fn align_left(self) -> Self {
|
||||
self.align_x(alignment::left())
|
||||
}
|
||||
|
||||
/// Aligns the [`Text`] to the right.
|
||||
pub fn align_right(self) -> Self {
|
||||
self.align_x(alignment::right())
|
||||
}
|
||||
|
||||
/// Centers the [`Text`] vertically.
|
||||
pub fn center_y(self) -> Self {
|
||||
self.align_y(alignment::center())
|
||||
}
|
||||
|
||||
/// Aligns the [`Text`] to the top, the default.
|
||||
pub fn align_top(self) -> Self {
|
||||
self.align_y(alignment::top())
|
||||
}
|
||||
|
||||
/// Aligns the [`Text`] to the bottom.
|
||||
pub fn align_bottom(self) -> Self {
|
||||
self.align_y(alignment::bottom())
|
||||
}
|
||||
|
||||
/// Sets the [`alignment::Horizontal`] of the [`Text`].
|
||||
pub fn horizontal_alignment(
|
||||
pub fn align_x(
|
||||
mut self,
|
||||
alignment: alignment::Horizontal,
|
||||
alignment: impl Into<alignment::Horizontal>,
|
||||
) -> Self {
|
||||
self.horizontal_alignment = alignment;
|
||||
self.horizontal_alignment = alignment.into();
|
||||
self
|
||||
}
|
||||
|
||||
/// Sets the [`alignment::Vertical`] of the [`Text`].
|
||||
pub fn vertical_alignment(
|
||||
pub fn align_y(
|
||||
mut self,
|
||||
alignment: alignment::Vertical,
|
||||
alignment: impl Into<alignment::Vertical>,
|
||||
) -> Self {
|
||||
self.vertical_alignment = alignment;
|
||||
self.vertical_alignment = alignment.into();
|
||||
self
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue