Skip to content

Commit

Permalink
Make box shadow take percent
Browse files Browse the repository at this point in the history
  • Loading branch information
jrmoulton committed Nov 8, 2023
1 parent a32abd4 commit 5de38b9
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 24 deletions.
32 changes: 16 additions & 16 deletions src/style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -897,21 +897,21 @@ pub enum CursorStyle {

#[derive(Debug, Clone, Copy, PartialEq)]
pub struct BoxShadow {
pub blur_radius: f64,
pub blur_radius: PxPct,
pub color: Color,
pub spread: f64,
pub h_offset: f64,
pub v_offset: f64,
pub spread: PxPct,
pub h_offset: PxPct,
pub v_offset: PxPct,
}

impl Default for BoxShadow {
fn default() -> Self {
Self {
blur_radius: 0.0,
blur_radius: PxPct::Px(0.),
color: Color::BLACK,
spread: 0.0,
h_offset: 0.0,
v_offset: 0.0,
spread: PxPct::Px(0.),
h_offset: PxPct::Px(0.),
v_offset: PxPct::Px(0.),
}
}
}
Expand Down Expand Up @@ -1436,9 +1436,9 @@ impl Style {
self.set_style_value(Background, color.into().map(Some))
}

pub fn box_shadow_blur(self, blur_radius: f64) -> Self {
pub fn box_shadow_blur(self, blur_radius: impl Into<PxPct>) -> Self {
let mut value = self.get(BoxShadowProp).unwrap_or_default();
value.blur_radius = blur_radius;
value.blur_radius = blur_radius.into();
self.set(BoxShadowProp, Some(value))
}

Expand All @@ -1448,21 +1448,21 @@ impl Style {
self.set(BoxShadowProp, Some(value))
}

pub fn box_shadow_spread(self, spread: f64) -> Self {
pub fn box_shadow_spread(self, spread: impl Into<PxPct>) -> Self {
let mut value = self.get(BoxShadowProp).unwrap_or_default();
value.spread = spread;
value.spread = spread.into();
self.set(BoxShadowProp, Some(value))
}

pub fn box_shadow_h_offset(self, h_offset: f64) -> Self {
pub fn box_shadow_h_offset(self, h_offset: impl Into<PxPct>) -> Self {
let mut value = self.get(BoxShadowProp).unwrap_or_default();
value.h_offset = h_offset;
value.h_offset = h_offset.into();
self.set(BoxShadowProp, Some(value))
}

pub fn box_shadow_v_offset(self, v_offset: f64) -> Self {
pub fn box_shadow_v_offset(self, v_offset: impl Into<PxPct>) -> Self {
let mut value = self.get(BoxShadowProp).unwrap_or_default();
value.v_offset = v_offset;
value.v_offset = v_offset.into();
self.set(BoxShadowProp, Some(value))
}

Expand Down
33 changes: 25 additions & 8 deletions src/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,18 +282,35 @@ pub(crate) fn paint_bg(

fn paint_box_shadow(cx: &mut PaintCx, style: &Style, rect: Rect, rect_radius: Option<f64>) {
if let Some(shadow) = style.get(BoxShadowProp).as_ref() {
let min = rect.size().min_side();
let h_offset = match shadow.h_offset {
crate::unit::PxPct::Px(px) => px,
crate::unit::PxPct::Pct(pct) => min * (pct / 100.),
};
let v_offset = match shadow.v_offset {
crate::unit::PxPct::Px(px) => px,
crate::unit::PxPct::Pct(pct) => min * (pct / 100.),
};
let spread = match shadow.spread {
crate::unit::PxPct::Px(px) => px,
crate::unit::PxPct::Pct(pct) => min * (pct / 100.),
};
let blur_radius = match shadow.blur_radius {
crate::unit::PxPct::Px(px) => px,
crate::unit::PxPct::Pct(pct) => min * (pct / 100.),
};
let inset = Insets::new(
-shadow.h_offset / 2.0,
-shadow.v_offset / 2.0,
shadow.h_offset / 2.0,
shadow.v_offset / 2.0,
-h_offset / 2.0,
-v_offset / 2.0,
h_offset / 2.0,
v_offset / 2.0,
);
let rect = rect.inflate(shadow.spread, shadow.spread).inset(inset);
let rect = rect.inflate(spread, spread).inset(inset);
if let Some(radii) = rect_radius {
let rounded_rect = RoundedRect::from_rect(rect, radii + shadow.spread);
cx.fill(&rounded_rect, shadow.color, shadow.blur_radius);
let rounded_rect = RoundedRect::from_rect(rect, radii + spread);
cx.fill(&rounded_rect, shadow.color, blur_radius);
} else {
cx.fill(&rect, shadow.color, shadow.blur_radius);
cx.fill(&rect, shadow.color, blur_radius);
}
}
}
Expand Down

0 comments on commit 5de38b9

Please sign in to comment.