Expand description
Provides alignment information on both axes along with ideal size
Note that the ideal
size detail is only used for non-stretch alignment.
Fields§
§horiz: Align
§vert: Align
Implementations§
source§impl AlignPair
impl AlignPair
sourcepub const fn new(horiz: Align, vert: Align) -> Self
pub const fn new(horiz: Align, vert: Align) -> Self
Construct with horiz. and vert. alignment
Examples found in repository?
src/layout/align.rs (line 87)
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
pub fn complete(&self, horiz: Align, vert: Align) -> AlignPair {
AlignPair::new(self.horiz.unwrap_or(horiz), self.vert.unwrap_or(vert))
}
}
/// Provides alignment information on both axes along with ideal size
///
/// Note that the `ideal` size detail is only used for non-stretch alignment.
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, Hash)]
pub struct AlignPair {
pub horiz: Align,
pub vert: Align,
}
impl AlignPair {
/// Default on both axes
pub const DEFAULT: AlignPair = AlignPair::new(Align::Default, Align::Default);
/// Center on both axes
pub const CENTER: AlignPair = AlignPair::new(Align::Center, Align::Center);
/// Stretch on both axes
pub const STRETCH: AlignPair = AlignPair::new(Align::Stretch, Align::Stretch);
/// Construct with horiz. and vert. alignment
pub const fn new(horiz: Align, vert: Align) -> Self {
Self { horiz, vert }
}
/// Extract one component, based on a direction
#[inline]
pub fn extract<D: Directional>(self, dir: D) -> Align {
match dir.is_vertical() {
false => self.horiz,
true => self.vert,
}
}
/// Set one component of self, based on a direction
#[inline]
pub fn set_component<D: Directional>(&mut self, dir: D, align: Align) {
match dir.is_vertical() {
false => self.horiz = align,
true => self.vert = align,
}
}
/// Construct a rect of size `ideal` within `rect` using the given alignment
pub fn aligned_rect(&self, ideal: Size, rect: Rect) -> Rect {
let mut pos = rect.pos;
let mut size = rect.size;
if ideal.0 < size.0 && self.horiz != Align::Stretch {
pos.0 += match self.horiz {
Align::Center => (size.0 - ideal.0) / 2,
Align::BR => size.0 - ideal.0,
Align::Default | Align::TL | Align::Stretch => 0,
};
size.0 = ideal.0;
}
if ideal.1 < size.1 && self.vert != Align::Stretch {
pos.1 += match self.vert {
Align::Center => (size.1 - ideal.1) / 2,
Align::BR => size.1 - ideal.1,
Align::Default | Align::TL | Align::Stretch => 0,
};
size.1 = ideal.1;
}
Rect { pos, size }
}
}
impl From<(Align, Align)> for AlignPair {
#[inline]
fn from(p: (Align, Align)) -> Self {
AlignPair::new(p.0, p.1)
}
sourcepub fn extract<D: Directional>(self, dir: D) -> Align
pub fn extract<D: Directional>(self, dir: D) -> Align
Extract one component, based on a direction
sourcepub fn set_component<D: Directional>(&mut self, dir: D, align: Align)
pub fn set_component<D: Directional>(&mut self, dir: D, align: Align)
Set one component of self, based on a direction
Examples found in repository?
More examples
src/layout/size_types.rs (line 264)
253 254 255 256 257 258 259 260 261 262 263 264 265 266
pub fn size_rules(&mut self, mgr: SizeMgr, axis: AxisInfo) -> SizeRules {
let margins = mgr.margins(self.margins).extract(axis);
let scale_factor = mgr.scale_factor();
let min = self
.size
.to_physical(scale_factor * self.min_factor)
.extract(axis);
let ideal = self
.size
.to_physical(scale_factor * self.ideal_factor)
.extract(axis);
self.align.set_component(axis, axis.align_or_center());
SizeRules::new(min, ideal, margins, self.stretch)
}
sourcepub fn aligned_rect(&self, ideal: Size, rect: Rect) -> Rect
pub fn aligned_rect(&self, ideal: Size, rect: Rect) -> Rect
Construct a rect of size ideal
within rect
using the given alignment
Examples found in repository?
More examples
src/layout/size_types.rs (line 291)
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
pub fn align_rect(&mut self, rect: Rect, scale_factor: f32) -> Rect {
let mut size = rect.size;
if self.stretch == Stretch::None {
let ideal = self.size.to_physical(scale_factor * self.ideal_factor);
size = size.min(ideal);
}
if self.fix_aspect {
let logical_size = Vec2::from(self.size);
let Vec2(rw, rh) = Vec2::conv(size) / logical_size;
// Use smaller ratio, if any is finite
if rw < rh {
size.1 = i32::conv_nearest(rw * logical_size.1);
} else if rh < rw {
size.0 = i32::conv_nearest(rh * logical_size.0);
}
}
self.align.aligned_rect(size, rect)
}
Trait Implementations§
source§impl PartialEq<AlignPair> for AlignPair
impl PartialEq<AlignPair> for AlignPair
impl Copy for AlignPair
impl Eq for AlignPair
impl StructuralEq for AlignPair
impl StructuralPartialEq for AlignPair
Auto Trait Implementations§
impl RefUnwindSafe for AlignPair
impl Send for AlignPair
impl Sync for AlignPair
impl Unpin for AlignPair
impl UnwindSafe for AlignPair
Blanket Implementations§
source§impl<S, T> CastApprox<T> for Swhere
T: ConvApprox<S>,
impl<S, T> CastApprox<T> for Swhere
T: ConvApprox<S>,
source§fn try_cast_approx(self) -> Result<T, Error>
fn try_cast_approx(self) -> Result<T, Error>
source§fn cast_approx(self) -> T
fn cast_approx(self) -> T
source§impl<S, T> CastFloat<T> for Swhere
T: ConvFloat<S>,
impl<S, T> CastFloat<T> for Swhere
T: ConvFloat<S>,
source§fn cast_trunc(self) -> T
fn cast_trunc(self) -> T
Cast to integer, truncating Read more
source§fn cast_nearest(self) -> T
fn cast_nearest(self) -> T
Cast to the nearest integer Read more
source§fn cast_floor(self) -> T
fn cast_floor(self) -> T
Cast the floor to an integer Read more
source§impl<Q, K> Equivalent<K> for Qwhere
Q: Eq + ?Sized,
K: Borrow<Q> + ?Sized,
impl<Q, K> Equivalent<K> for Qwhere
Q: Eq + ?Sized,
K: Borrow<Q> + ?Sized,
source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
Compare self to
key
and return true
if they are equal.