refactor: improve margin and scale factor on builder

This commit is contained in:
drendog 2025-11-30 06:26:14 +01:00
parent 791ceaa34e
commit dc676d606e
Signed by: dwenya
GPG key ID: 8DD77074645332D0
3 changed files with 63 additions and 12 deletions

View file

@ -149,13 +149,8 @@ impl LayerShika<HasComponent> {
} }
#[must_use] #[must_use]
pub const fn with_margin(mut self, top: i32, right: i32, bottom: i32, left: i32) -> Self { pub fn with_margin(mut self, margin: impl Into<Margins>) -> Self {
self.config.margin = Margins { self.config.margin = margin.into();
top,
right,
bottom,
left,
};
self self
} }
@ -172,14 +167,15 @@ impl LayerShika<HasComponent> {
} }
#[must_use] #[must_use]
pub fn with_namespace(mut self, namespace: String) -> Self { pub fn with_namespace(mut self, namespace: impl Into<String>) -> Self {
self.config.namespace = namespace; self.config.namespace = namespace.into();
self self
} }
pub fn with_scale_factor(mut self, scale_factor: f32) -> Result<Self> { #[must_use]
self.config.scale_factor = ScaleFactor::new(scale_factor)?; pub fn with_scale_factor(mut self, sf: impl TryInto<ScaleFactor, Error = DomainError>) -> Self {
Ok(self) self.config.scale_factor = sf.try_into().unwrap_or_default();
self
} }
#[must_use] #[must_use]

View file

@ -174,6 +174,14 @@ impl Default for ScaleFactor {
} }
} }
impl TryFrom<f32> for ScaleFactor {
type Error = DomainError;
fn try_from(factor: f32) -> Result<Self, Self::Error> {
Self::new(factor)
}
}
#[derive(Debug, Clone, Copy, PartialEq)] #[derive(Debug, Clone, Copy, PartialEq)]
pub struct LogicalPosition { pub struct LogicalPosition {
x: f32, x: f32,

View file

@ -5,3 +5,50 @@ pub struct Margins {
pub bottom: i32, pub bottom: i32,
pub left: i32, pub left: i32,
} }
impl Margins {
pub const fn all(px: i32) -> Self {
Self {
top: px,
right: px,
bottom: px,
left: px,
}
}
pub const fn symmetric(vertical: i32, horizontal: i32) -> Self {
Self {
top: vertical,
right: horizontal,
bottom: vertical,
left: horizontal,
}
}
pub const fn new(top: i32, right: i32, bottom: i32, left: i32) -> Self {
Self {
top,
right,
bottom,
left,
}
}
}
impl From<i32> for Margins {
fn from(px: i32) -> Self {
Self::all(px)
}
}
impl From<(i32, i32)> for Margins {
fn from((vertical, horizontal): (i32, i32)) -> Self {
Self::symmetric(vertical, horizontal)
}
}
impl From<(i32, i32, i32, i32)> for Margins {
fn from((top, right, bottom, left): (i32, i32, i32, i32)) -> Self {
Self::new(top, right, bottom, left)
}
}