1
0
mirror of https://github.com/emilk/egui.git synced 2026-08-29 04:40:03 -04:00

Shrink size of Shadow by using i8/u8 instead of f32 (#5568)

* Part of https://github.com/emilk/egui/issues/4019
This commit is contained in:
Emil Ernerfeldt
2025-01-02 16:22:44 +01:00
committed by GitHub
parent d58d13781d
commit ee4ab08c8a
4 changed files with 47 additions and 33 deletions

View File

@@ -5,33 +5,41 @@ use crate::{Color32, Marginf, Rect, RectShape, Rounding, Vec2};
/// Can be used for a rectangular shadow with a soft penumbra.
///
/// Very similar to a box-shadow in CSS.
#[derive(Clone, Copy, Debug, Default, PartialEq)]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct Shadow {
/// Move the shadow by this much.
///
/// For instance, a value of `[1.0, 2.0]` will move the shadow 1 point to the right and 2 points down,
/// causing a drop-shadow effect.
pub offset: Vec2,
pub offset: [i8; 2],
/// The width of the blur, i.e. the width of the fuzzy penumbra.
///
/// A value of 0.0 means a sharp shadow.
pub blur: f32,
/// A value of 0 means a sharp shadow.
pub blur: u8,
/// Expand the shadow in all directions by this much.
pub spread: f32,
pub spread: u8,
/// Color of the opaque center of the shadow.
pub color: Color32,
}
#[test]
fn shadow_size() {
assert_eq!(
std::mem::size_of::<Shadow>(), 8,
"Shadow changed size! If it shrank - good! Update this test. If it grew - bad! Try to find a way to avoid it."
);
}
impl Shadow {
/// No shadow at all.
pub const NONE: Self = Self {
offset: Vec2::ZERO,
blur: 0.0,
spread: 0.0,
offset: [0, 0],
blur: 0,
spread: 0,
color: Color32::TRANSPARENT,
};
@@ -45,11 +53,14 @@ impl Shadow {
spread,
color,
} = *self;
let [offset_x, offset_y] = offset;
let rect = rect.translate(offset).expand(spread);
let rounding = rounding.into() + Rounding::from(spread.abs());
let rect = rect
.translate(Vec2::new(offset_x as _, offset_y as _))
.expand(spread as _);
let rounding = rounding.into() + Rounding::from(spread);
RectShape::filled(rect, rounding, color).with_blur_width(blur)
RectShape::filled(rect, rounding, color).with_blur_width(blur as _)
}
/// How much larger than the parent rect are we in each direction?
@@ -60,11 +71,14 @@ impl Shadow {
spread,
color: _,
} = *self;
let spread = spread as f32;
let blur = blur as f32;
let [offset_x, offset_y] = offset;
Marginf {
left: spread + 0.5 * blur - offset.x,
right: spread + 0.5 * blur + offset.x,
top: spread + 0.5 * blur - offset.y,
bottom: spread + 0.5 * blur + offset.y,
left: spread + 0.5 * blur - offset_x as f32,
right: spread + 0.5 * blur + offset_x as f32,
top: spread + 0.5 * blur - offset_y as f32,
bottom: spread + 0.5 * blur + offset_y as f32,
}
}
}