use crate::{
color::{self, Color2},
coord::{Coord, Vec2},
};
#[derive(Debug, Clone, Copy, PartialEq, Eq,Hash)]
pub struct Style<C = Color2>
where
C: color::Updater,
{
pub left_margin: Coord,
pub right_margin: Coord,
pub top_margin: Coord,
pub bottom_margin: Coord,
pub min_width: Coord,
pub max_width: Coord,
pub min_height: Coord,
pub max_height: Coord,
pub align_numer: Coord,
pub align_denom: Coord,
pub colors: C,
}
impl Default for Style {
fn default() -> Self {
Self::with_colors(Color2::default())
}
}
impl<C> Style<C>
where
C: color::Updater,
{
pub fn with_colors(colors: C) -> Self {
Self {
left_margin: 0,
right_margin: 0,
top_margin: 0,
bottom_margin: 0,
min_width: 0,
max_width: Coord::max_value(),
min_height: 0,
max_height: Coord::max_value(),
align_numer: 0,
align_denom: 1,
colors,
}
}
pub fn colors<D>(self, colors: D) -> Style<D>
where
D: color::Updater,
{
Style {
left_margin: self.left_margin,
right_margin: self.right_margin,
top_margin: self.top_margin,
bottom_margin: self.bottom_margin,
min_width: self.min_width,
max_width: self.max_width,
min_height: self.min_height,
max_height: self.max_height,
align_numer: self.align_numer,
align_denom: self.align_denom,
colors,
}
}
pub fn left_margin(self, left_margin: Coord) -> Self {
Self { left_margin, ..self }
}
pub fn right_margin(self, right_margin: Coord) -> Self {
Self { right_margin, ..self }
}
pub fn top_margin(self, top_margin: Coord) -> Self {
Self { top_margin, ..self }
}
pub fn bottom_margin(self, bottom_margin: Coord) -> Self {
Self { bottom_margin, ..self }
}
pub fn min_width(self, min_width: Coord) -> Self {
Self { min_width, ..self }
}
pub fn max_width(self, max_width: Coord) -> Self {
Self { max_width, ..self }
}
pub fn min_height(self, min_height: Coord) -> Self {
Self { min_height, ..self }
}
pub fn max_height(self, max_height: Coord) -> Self {
Self { max_height, ..self }
}
pub fn align(self, align_numer: Coord, align_denom: Coord) -> Self {
Self { align_numer, align_denom, ..self }
}
pub fn make_margin_below(&self) -> Vec2 {
Vec2 { x: self.left_margin, y: self.top_margin }
}
pub fn make_margin_above(&self) -> Vec2 {
Vec2 { x: self.right_margin, y: self.bottom_margin }
}
pub fn make_min_size(&self) -> Vec2 {
Vec2 { x: self.min_width, y: self.min_height }
}
pub fn make_max_size(&self) -> Vec2 {
Vec2 { x: self.max_width, y: self.max_height }
}
pub fn make_size(&self, screen_size: Vec2) -> Vec2 {
Vec2 {
y: screen_size
.y
.saturating_sub(self.make_margin_below().y)
.saturating_sub(self.make_margin_above().y)
.min(self.make_max_size().y),
x: screen_size
.x
.saturating_sub(self.make_margin_below().x)
.saturating_sub(self.make_margin_above().x)
.min(self.make_max_size().x),
}
}
}