Struct kas_core::layout::AlignPair

source ·
pub struct AlignPair {
    pub horiz: Align,
    pub vert: Align,
}
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§

Default on both axes

Center on both axes

Stretch on both axes

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)
    }

Extract one component, based on a direction

Set one component of self, based on a direction

Examples found in repository?
src/layout/visitor.rs (line 499)
497
498
499
500
501
    fn apply_align(&mut self, axis: AxisInfo, hints: AlignHints) -> AxisInfo {
        let axis = axis.with_align_hints(hints);
        self.align.set_component(axis, axis.align_or_default());
        axis
    }
More examples
Hide additional 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)
    }

Construct a rect of size ideal within rect using the given alignment

Examples found in repository?
src/layout/visitor.rs (line 505)
504
505
506
    fn aligned_rect(&self, rect: Rect) -> Rect {
        self.align.aligned_rect(self.size, rect)
    }
More examples
Hide additional 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§

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
Returns the “default value” for a type. Read more
Converts to this type from the input type.
Converts to this type from the input type.
Feeds this value into the given Hasher. Read more
Feeds a slice of this type into the given Hasher. Read more
This method tests for self and other values to be equal, and is used by ==.
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more
Cast from Self to T Read more
Try converting from Self to T Read more
Try approximate conversion from Self to T Read more
Cast approximately from Self to T Read more
Cast to integer, truncating Read more
Cast to the nearest integer Read more
Cast the floor to an integer Read more
Cast the ceiling to an integer Read more
Try converting to integer with truncation Read more
Try converting to the nearest integer Read more
Try converting the floor to an integer Read more
Try convert the ceiling to an integer Read more
Compare self to key and return true if they are equal.

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.