From cc906c83cdf896d94b7ccf91258466714be631f6 Mon Sep 17 00:00:00 2001 From: Nick Senger Date: Wed, 8 Nov 2023 19:12:53 -0800 Subject: feat: quad shadows --- core/src/element.rs | 1 + core/src/lib.rs | 2 ++ core/src/renderer.rs | 5 ++++- core/src/shadow.rs | 15 +++++++++++++++ 4 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 core/src/shadow.rs (limited to 'core') diff --git a/core/src/element.rs b/core/src/element.rs index 8b510218..b264ad77 100644 --- a/core/src/element.rs +++ b/core/src/element.rs @@ -540,6 +540,7 @@ where border_color: color, border_width: 1.0, border_radius: 0.0.into(), + shadow: Default::default() }, Color::TRANSPARENT, ); diff --git a/core/src/lib.rs b/core/src/lib.rs index 864df6e6..b326d0f3 100644 --- a/core/src/lib.rs +++ b/core/src/lib.rs @@ -27,6 +27,7 @@ pub mod layout; pub mod mouse; pub mod overlay; pub mod renderer; +pub mod shadow; pub mod svg; pub mod text; pub mod time; @@ -70,6 +71,7 @@ pub use pixels::Pixels; pub use point::Point; pub use rectangle::Rectangle; pub use renderer::Renderer; +pub use shadow::Shadow; pub use shell::Shell; pub use size::Size; pub use text::Text; diff --git a/core/src/renderer.rs b/core/src/renderer.rs index 1b327e56..481048b0 100644 --- a/core/src/renderer.rs +++ b/core/src/renderer.rs @@ -5,7 +5,7 @@ mod null; #[cfg(debug_assertions)] pub use null::Null; -use crate::{Background, BorderRadius, Color, Rectangle, Vector}; +use crate::{Background, BorderRadius, Color, Rectangle, Shadow, Vector}; /// A component that can be used by widgets to draw themselves on a screen. pub trait Renderer: Sized { @@ -45,6 +45,9 @@ pub struct Quad { /// The border color of the [`Quad`]. pub border_color: Color, + + /// The shadow of the [`Quad`]. + pub shadow: Shadow, } /// The styling attributes of a [`Renderer`]. diff --git a/core/src/shadow.rs b/core/src/shadow.rs new file mode 100644 index 00000000..238ea36a --- /dev/null +++ b/core/src/shadow.rs @@ -0,0 +1,15 @@ +//! Shadow +use crate::{Color, Vector}; + +/// A shadow +#[derive(Debug, Clone, Copy, PartialEq, Default)] +pub struct Shadow { + /// The color of the shadow + pub color: Color, + + /// The offset of the shadow + pub offset: Vector, + + /// The blur_radius of the shadow + pub blur_radius: f32, +} -- cgit From 83902921a3065e8dadfaca23c2e03dd770d93780 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sat, 20 Jan 2024 12:02:04 +0100 Subject: Run `cargo fmt` --- core/src/element.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'core') diff --git a/core/src/element.rs b/core/src/element.rs index b264ad77..a39919d3 100644 --- a/core/src/element.rs +++ b/core/src/element.rs @@ -540,7 +540,7 @@ where border_color: color, border_width: 1.0, border_radius: 0.0.into(), - shadow: Default::default() + shadow: Default::default(), }, Color::TRANSPARENT, ); -- cgit From b7b457a575cdd103915994f640c50262ce30a7c5 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sat, 20 Jan 2024 12:11:18 +0100 Subject: Make `shadow` optional in `renderer::Quad` --- core/src/renderer.rs | 16 ++++++++++++++-- core/src/shadow.rs | 4 ++-- 2 files changed, 16 insertions(+), 4 deletions(-) (limited to 'core') diff --git a/core/src/renderer.rs b/core/src/renderer.rs index 481048b0..1ca62559 100644 --- a/core/src/renderer.rs +++ b/core/src/renderer.rs @@ -5,7 +5,7 @@ mod null; #[cfg(debug_assertions)] pub use null::Null; -use crate::{Background, BorderRadius, Color, Rectangle, Shadow, Vector}; +use crate::{Background, BorderRadius, Color, Rectangle, Shadow, Size, Vector}; /// A component that can be used by widgets to draw themselves on a screen. pub trait Renderer: Sized { @@ -47,7 +47,19 @@ pub struct Quad { pub border_color: Color, /// The shadow of the [`Quad`]. - pub shadow: Shadow, + pub shadow: Option, +} + +impl Default for Quad { + fn default() -> Self { + Self { + bounds: Rectangle::with_size(Size::ZERO), + border_radius: 0.0.into(), + border_width: 0.0, + border_color: Color::TRANSPARENT, + shadow: None, + } + } } /// The styling attributes of a [`Renderer`]. diff --git a/core/src/shadow.rs b/core/src/shadow.rs index 238ea36a..de8ce1c3 100644 --- a/core/src/shadow.rs +++ b/core/src/shadow.rs @@ -2,7 +2,7 @@ use crate::{Color, Vector}; /// A shadow -#[derive(Debug, Clone, Copy, PartialEq, Default)] +#[derive(Debug, Clone, Copy, PartialEq)] pub struct Shadow { /// The color of the shadow pub color: Color, @@ -10,6 +10,6 @@ pub struct Shadow { /// The offset of the shadow pub offset: Vector, - /// The blur_radius of the shadow + /// The blur radius of the shadow pub blur_radius: f32, } -- cgit From 370b2f6df799c948188d3949e34112258b2a8498 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sat, 20 Jan 2024 12:25:07 +0100 Subject: Use `Default` implementation of `renderer::Quad` --- core/src/element.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'core') diff --git a/core/src/element.rs b/core/src/element.rs index a39919d3..e57ad777 100644 --- a/core/src/element.rs +++ b/core/src/element.rs @@ -539,8 +539,7 @@ where bounds: layout.bounds(), border_color: color, border_width: 1.0, - border_radius: 0.0.into(), - shadow: Default::default(), + ..renderer::Quad::default() }, Color::TRANSPARENT, ); -- cgit From 25f182f933ea6b7c112c8f9a450a98dc9b9eebdd Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sat, 20 Jan 2024 13:29:25 +0100 Subject: Introduce `Border` struct analogous to `Shadow` --- core/src/border.rs | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++ core/src/element.rs | 10 +++++++--- core/src/lib.rs | 6 +++--- core/src/renderer.rs | 22 +++++++-------------- core/src/shadow.rs | 11 +++++------ 5 files changed, 76 insertions(+), 27 deletions(-) create mode 100644 core/src/border.rs (limited to 'core') diff --git a/core/src/border.rs b/core/src/border.rs new file mode 100644 index 00000000..21823341 --- /dev/null +++ b/core/src/border.rs @@ -0,0 +1,54 @@ +//! Draw lines around containers. +use crate::Color; + +/// A border. +#[derive(Debug, Clone, Copy, PartialEq, Default)] +pub struct Border { + /// The color of the border. + pub color: Color, + + /// The width of the border. + pub width: f32, + + /// The radius of the border. + pub radius: Radius, +} + +impl Border { + /// Creates a new default [`Border`] with the given [`Radius`]. + pub fn with_radius(radius: impl Into) -> Self { + Self { + radius: radius.into(), + ..Self::default() + } + } +} + +/// The border radii for the corners of a graphics primitive in the order: +/// top-left, top-right, bottom-right, bottom-left. +#[derive(Debug, Clone, Copy, PartialEq, Default)] +pub struct Radius([f32; 4]); + +impl From for Radius { + fn from(w: f32) -> Self { + Self([w; 4]) + } +} + +impl From for Radius { + fn from(w: u8) -> Self { + Self([f32::from(w); 4]) + } +} + +impl From<[f32; 4]> for Radius { + fn from(radi: [f32; 4]) -> Self { + Self(radi) + } +} + +impl From for [f32; 4] { + fn from(radi: Radius) -> Self { + radi.0 + } +} diff --git a/core/src/element.rs b/core/src/element.rs index e57ad777..4d4bfa36 100644 --- a/core/src/element.rs +++ b/core/src/element.rs @@ -6,7 +6,8 @@ use crate::renderer; use crate::widget; use crate::widget::tree::{self, Tree}; use crate::{ - Clipboard, Color, Layout, Length, Rectangle, Shell, Size, Vector, Widget, + Border, Clipboard, Color, Layout, Length, Rectangle, Shell, Size, Vector, + Widget, }; use std::any::Any; @@ -537,8 +538,11 @@ where renderer.fill_quad( renderer::Quad { bounds: layout.bounds(), - border_color: color, - border_width: 1.0, + border: Border { + color, + width: 1.0, + ..Border::default() + }, ..renderer::Quad::default() }, Color::TRANSPARENT, diff --git a/core/src/lib.rs b/core/src/lib.rs index b326d0f3..bbc973f0 100644 --- a/core/src/lib.rs +++ b/core/src/lib.rs @@ -17,6 +17,7 @@ rustdoc::broken_intra_doc_links )] pub mod alignment; +pub mod border; pub mod clipboard; pub mod event; pub mod font; @@ -27,7 +28,6 @@ pub mod layout; pub mod mouse; pub mod overlay; pub mod renderer; -pub mod shadow; pub mod svg; pub mod text; pub mod time; @@ -37,7 +37,6 @@ pub mod window; mod angle; mod background; -mod border_radius; mod color; mod content_fit; mod element; @@ -47,6 +46,7 @@ mod padding; mod pixels; mod point; mod rectangle; +mod shadow; mod shell; mod size; mod vector; @@ -54,7 +54,7 @@ mod vector; pub use alignment::Alignment; pub use angle::{Degrees, Radians}; pub use background::Background; -pub use border_radius::BorderRadius; +pub use border::Border; pub use clipboard::Clipboard; pub use color::Color; pub use content_fit::ContentFit; diff --git a/core/src/renderer.rs b/core/src/renderer.rs index 1ca62559..a2a66aa8 100644 --- a/core/src/renderer.rs +++ b/core/src/renderer.rs @@ -5,7 +5,7 @@ mod null; #[cfg(debug_assertions)] pub use null::Null; -use crate::{Background, BorderRadius, Color, Rectangle, Shadow, Size, Vector}; +use crate::{Background, Border, Color, Rectangle, Shadow, Size, Vector}; /// A component that can be used by widgets to draw themselves on a screen. pub trait Renderer: Sized { @@ -37,27 +37,19 @@ pub struct Quad { /// The bounds of the [`Quad`]. pub bounds: Rectangle, - /// The border radius of the [`Quad`]. - pub border_radius: BorderRadius, + /// The [`Border`] of the [`Quad`]. + pub border: Border, - /// The border width of the [`Quad`]. - pub border_width: f32, - - /// The border color of the [`Quad`]. - pub border_color: Color, - - /// The shadow of the [`Quad`]. - pub shadow: Option, + /// The [`Shadow`] of the [`Quad`]. + pub shadow: Shadow, } impl Default for Quad { fn default() -> Self { Self { bounds: Rectangle::with_size(Size::ZERO), - border_radius: 0.0.into(), - border_width: 0.0, - border_color: Color::TRANSPARENT, - shadow: None, + border: Border::default(), + shadow: Shadow::default(), } } } diff --git a/core/src/shadow.rs b/core/src/shadow.rs index de8ce1c3..803101ed 100644 --- a/core/src/shadow.rs +++ b/core/src/shadow.rs @@ -1,15 +1,14 @@ -//! Shadow use crate::{Color, Vector}; -/// A shadow -#[derive(Debug, Clone, Copy, PartialEq)] +/// A shadow. +#[derive(Debug, Clone, Copy, PartialEq, Default)] pub struct Shadow { - /// The color of the shadow + /// The color of the shadow. pub color: Color, - /// The offset of the shadow + /// The offset of the shadow. pub offset: Vector, - /// The blur radius of the shadow + /// The blur radius of the shadow. pub blur_radius: f32, } -- cgit