use crate::{TypeStyle, generated::*};
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum Direction {
#[default]
Ltr,
Rtl,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum WidthClass {
Compact,
Medium,
Expanded,
Large,
ExtraLarge,
}
impl WidthClass {
pub fn from_width(width: f32) -> Self {
if !width.is_finite() {
return Self::Compact;
}
if width >= MD_SYS_LAYOUT_WINDOW_WIDTH_EXTRA_LARGE {
Self::ExtraLarge
} else if width >= MD_SYS_LAYOUT_WINDOW_WIDTH_LARGE {
Self::Large
} else if width >= MD_SYS_LAYOUT_WINDOW_WIDTH_EXPANDED {
Self::Expanded
} else if width >= MD_SYS_LAYOUT_WINDOW_WIDTH_MEDIUM {
Self::Medium
} else {
Self::Compact
}
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum Density {
Compact,
#[default]
Standard,
Comfortable,
}
impl Density {
pub fn visual_size(self, baseline: f32) -> f32 {
let delta = match self {
Self::Compact => MD_SYS_LAYOUT_DENSITY_COMPACT,
Self::Standard => MD_SYS_LAYOUT_DENSITY_STANDARD,
Self::Comfortable => MD_SYS_LAYOUT_DENSITY_COMFORTABLE,
};
if !baseline.is_finite() || baseline <= 0.0 {
return 0.0;
}
(baseline + delta).max(0.0)
}
pub fn interactive_size(self, baseline: f32) -> f32 {
self.visual_size(baseline)
.max(MD_SYS_LAYOUT_MINIMUM_INTERACTIVE_SIZE)
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct LayoutPolicy {
pub density: Density,
pub direction: Direction,
}
impl LayoutPolicy {
pub fn scale_text(style: TypeStyle, font_scale: f32) -> TypeStyle {
if !font_scale.is_finite() || font_scale <= 0.0 {
return style;
}
let scaled = TypeStyle {
size: style.size * font_scale,
line_height: style.line_height * font_scale,
tracking: style.tracking * font_scale,
..style
};
if [scaled.size, scaled.line_height, scaled.tracking]
.into_iter()
.all(f32::is_finite)
&& scaled.size > 0.0
&& scaled.line_height > 0.0
{
scaled
} else {
style
}
}
pub const fn horizontal<T: Copy>(self, start: T, end: T) -> (T, T) {
match self.direction {
Direction::Ltr => (start, end),
Direction::Rtl => (end, start),
}
}
}
pub const fn non_negative_f32(value: f32) -> f32 {
if !value.is_finite() || value <= 0.0 {
0.0
} else {
value
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct LogicalEdges {
pub top: f32,
pub bottom: f32,
pub start: f32,
pub end: f32,
}
impl LogicalEdges {
pub const ZERO: Self = Self {
top: 0.0,
bottom: 0.0,
start: 0.0,
end: 0.0,
};
pub const fn all(value: f32) -> Self {
let value = non_negative_f32(value);
Self {
top: value,
bottom: value,
start: value,
end: value,
}
}
pub const fn symmetric(horizontal: f32, vertical: f32) -> Self {
let horizontal = non_negative_f32(horizontal);
let vertical = non_negative_f32(vertical);
Self {
top: vertical,
bottom: vertical,
start: horizontal,
end: horizontal,
}
}
pub const fn new(top: f32, end: f32, bottom: f32, start: f32) -> Self {
Self {
top: non_negative_f32(top),
bottom: non_negative_f32(bottom),
start: non_negative_f32(start),
end: non_negative_f32(end),
}
}
pub const fn top(mut self, value: f32) -> Self {
self.top = non_negative_f32(value);
self
}
pub const fn bottom(mut self, value: f32) -> Self {
self.bottom = non_negative_f32(value);
self
}
pub const fn start(mut self, value: f32) -> Self {
self.start = non_negative_f32(value);
self
}
pub const fn end(mut self, value: f32) -> Self {
self.end = non_negative_f32(value);
self
}
pub const fn horizontal(mut self, value: f32) -> Self {
let value = non_negative_f32(value);
self.start = value;
self.end = value;
self
}
pub const fn vertical(mut self, value: f32) -> Self {
let value = non_negative_f32(value);
self.top = value;
self.bottom = value;
self
}
pub const fn sanitize(self) -> Self {
Self {
top: non_negative_f32(self.top),
bottom: non_negative_f32(self.bottom),
start: non_negative_f32(self.start),
end: non_negative_f32(self.end),
}
}
pub const fn resolve(self, direction: Direction) -> [f32; 4] {
let clean = self.sanitize();
let (left, right) = match direction {
Direction::Ltr => (clean.start, clean.end),
Direction::Rtl => (clean.end, clean.start),
};
[clean.top, right, clean.bottom, left]
}
}