use crate::core::{Property, UsesProperty, Widget};
use crate::kurbo::{Axis, Insets, Point, Rect, RoundedRect, Size, Vec2};
use crate::layout::Length;
use crate::properties::CornerRadius;
impl<W: Widget> UsesProperty<BorderWidth> for W {}
#[expect(missing_docs, reason = "field names are self-descriptive")]
#[derive(Default, Clone, Copy, Debug, PartialEq)]
pub struct BorderWidth {
pub width: Length,
}
impl Property for BorderWidth {
fn static_default() -> &'static Self {
static DEFAULT: BorderWidth = BorderWidth {
width: Length::ZERO,
};
&DEFAULT
}
}
impl BorderWidth {
pub const fn all(width: Length) -> Self {
Self { width }
}
pub fn length(&self, _axis: Axis) -> Length {
self.width.saturating_add(self.width)
}
pub fn size_up(&self, size: Size) -> Size {
let border_width = self.width.get();
let width = size.width + border_width * 2.;
let height = size.height + border_width * 2.;
Size::new(width, height)
}
pub fn size_down(&self, size: Size) -> Size {
let border_width = self.width.get();
let width = (size.width - border_width * 2.).max(0.);
let height = (size.height - border_width * 2.).max(0.);
Size::new(width, height)
}
pub fn insets_up(&self, insets: Insets) -> Insets {
let border_width = self.width.get();
Insets {
x0: insets.x0 + border_width,
y0: insets.y0 + border_width,
x1: insets.x1 + border_width,
y1: insets.y1 + border_width,
}
}
pub fn baseline_up(&self, baseline: f64) -> f64 {
baseline + self.width.get()
}
pub fn baseline_down(&self, baseline: f64) -> f64 {
baseline - self.width.get()
}
pub fn origin_down(&self, origin: Point) -> Point {
let border_width = self.width.get();
origin + Vec2::new(border_width, border_width)
}
pub fn bg_rect(&self, border_box: Rect, border_radius: &CornerRadius) -> RoundedRect {
let border_width = self.width.get();
border_box
.inset(-border_width)
.to_rounded_rect(border_radius.radius.saturating_sub(self.width).get())
}
pub fn border_rect(&self, border_box: Rect, border_radius: &CornerRadius) -> RoundedRect {
let border_width = self.width.get();
border_box
.inset(-border_width / 2.0)
.to_rounded_rect(border_radius.radius.get())
}
}