Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add box_shadow spread, h_offset, and v_offset #108

Merged
merged 2 commits into from
Sep 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 65 additions & 2 deletions src/style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,21 @@
pub struct BoxShadow {
pub blur_radius: f64,
pub color: Color,
pub spread: f64,
pub h_offset: f64,
pub v_offset: f64,
}

impl Default for BoxShadow {
fn default() -> Self {
Self {
blur_radius: 0.0,
color: Color::BLACK,
spread: 0.0,
h_offset: 0.0,
v_offset: 0.0,
}
}

Check warning on line 93 in src/style.rs

View check run for this annotation

Codecov / codecov/patch

src/style.rs#L85-L93

Added lines #L85 - L93 were not covered by tests
}

/// The value for a [`Style`] property
Expand Down Expand Up @@ -615,7 +630,7 @@

self.box_shadow = Some(BoxShadow {
blur_radius,
color: Color::BLACK,
..Default::default()

Check warning on line 633 in src/style.rs

View check run for this annotation

Codecov / codecov/patch

src/style.rs#L633

Added line #L633 was not covered by tests
})
.into();
self
Expand All @@ -630,8 +645,56 @@
}

self.box_shadow = Some(BoxShadow {
blur_radius: 0.0,
color,
..Default::default()
})
.into();
self
}

Check warning on line 653 in src/style.rs

View check run for this annotation

Codecov / codecov/patch

src/style.rs#L649-L653

Added lines #L649 - L653 were not covered by tests

pub fn box_shadow_spread(mut self, spread: f64) -> Self {
if let Some(box_shadow) = self.box_shadow.as_mut() {
if let Some(box_shadow) = box_shadow.as_mut() {
box_shadow.spread = spread;
return self;
}
}

Check warning on line 661 in src/style.rs

View check run for this annotation

Codecov / codecov/patch

src/style.rs#L656-L661

Added lines #L656 - L661 were not covered by tests

self.box_shadow = Some(BoxShadow {
spread,
..Default::default()
})
.into();
self
}

Check warning on line 669 in src/style.rs

View check run for this annotation

Codecov / codecov/patch

src/style.rs#L663-L669

Added lines #L663 - L669 were not covered by tests

pub fn box_shadow_h_offset(mut self, h_offset: f64) -> Self {
if let Some(box_shadow) = self.box_shadow.as_mut() {
if let Some(box_shadow) = box_shadow.as_mut() {
box_shadow.h_offset = h_offset;
return self;
}
}

Check warning on line 677 in src/style.rs

View check run for this annotation

Codecov / codecov/patch

src/style.rs#L672-L677

Added lines #L672 - L677 were not covered by tests

self.box_shadow = Some(BoxShadow {
h_offset,
..Default::default()
})
.into();
self
}

Check warning on line 685 in src/style.rs

View check run for this annotation

Codecov / codecov/patch

src/style.rs#L679-L685

Added lines #L679 - L685 were not covered by tests

pub fn box_shadow_v_offset(mut self, v_offset: f64) -> Self {
if let Some(box_shadow) = self.box_shadow.as_mut() {
if let Some(box_shadow) = box_shadow.as_mut() {
box_shadow.v_offset = v_offset;
return self;
}
}

Check warning on line 693 in src/style.rs

View check run for this annotation

Codecov / codecov/patch

src/style.rs#L688-L693

Added lines #L688 - L693 were not covered by tests

self.box_shadow = Some(BoxShadow {
v_offset,
..Default::default()

Check warning on line 697 in src/style.rs

View check run for this annotation

Codecov / codecov/patch

src/style.rs#L695-L697

Added lines #L695 - L697 were not covered by tests
})
.into();
self
Expand Down
36 changes: 23 additions & 13 deletions src/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@

use bitflags::bitflags;
use floem_renderer::Renderer;
use kurbo::{Affine, Circle, Line, Point, Rect, Size};
use kurbo::{Affine, Circle, Insets, Line, Point, Rect, RoundedRect, Size};
use taffy::prelude::Node;

use crate::{
Expand Down Expand Up @@ -795,24 +795,16 @@
};
cx.fill(&circle, bg, 0.0);
} else {
let rect = rect.to_rounded_rect(radius as f64);
if let Some(shadow) = style.box_shadow.as_ref() {
if shadow.blur_radius > 0.0 {
cx.fill(&rect, shadow.color, shadow.blur_radius);
}
}
paint_box_shadow(cx, style, rect, Some(radius as f64));

Check warning on line 798 in src/view.rs

View check run for this annotation

Codecov / codecov/patch

src/view.rs#L798

Added line #L798 was not covered by tests
let bg = match style.background {
Some(color) => color,
None => return,
};
cx.fill(&rect, bg, 0.0);
let rounded_rect = rect.to_rounded_rect(radius as f64);
cx.fill(&rounded_rect, bg, 0.0);

Check warning on line 804 in src/view.rs

View check run for this annotation

Codecov / codecov/patch

src/view.rs#L803-L804

Added lines #L803 - L804 were not covered by tests
}
} else {
if let Some(shadow) = style.box_shadow.as_ref() {
if shadow.blur_radius > 0.0 {
cx.fill(&size.to_rect(), shadow.color, shadow.blur_radius);
}
}
paint_box_shadow(cx, style, size.to_rect(), None);

Check warning on line 807 in src/view.rs

View check run for this annotation

Codecov / codecov/patch

src/view.rs#L807

Added line #L807 was not covered by tests
let bg = match style.background {
Some(color) => color,
None => return,
Expand All @@ -821,6 +813,24 @@
}
}

fn paint_box_shadow(cx: &mut PaintCx, style: &ComputedStyle, rect: Rect, rect_radius: Option<f64>) {
if let Some(shadow) = style.box_shadow.as_ref() {
let inset = Insets::new(
-shadow.h_offset / 2.0,
-shadow.v_offset / 2.0,
shadow.h_offset / 2.0,
shadow.v_offset / 2.0,
);
let rect = rect.inflate(shadow.spread, shadow.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);
} else {
cx.fill(&rect, shadow.color, shadow.blur_radius);
}
}
}

Check warning on line 832 in src/view.rs

View check run for this annotation

Codecov / codecov/patch

src/view.rs#L817-L832

Added lines #L817 - L832 were not covered by tests

fn paint_outline(cx: &mut PaintCx, style: &ComputedStyle, size: Size) {
if style.outline == 0. {
// TODO: we should warn! when outline is < 0
Expand Down
Loading