Skip to content

Commit 24d417b

Browse files
committed
Shrink size of Shadow by using i8/u8 instead of f32
* Part of #4019
1 parent d58d137 commit 24d417b

File tree

4 files changed

+47
-33
lines changed

4 files changed

+47
-33
lines changed

crates/egui/src/containers/frame.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ pub struct Frame {
7676
#[test]
7777
fn frame_size() {
7878
assert_eq!(
79-
std::mem::size_of::<Frame>(), 44,
79+
std::mem::size_of::<Frame>(), 32,
8080
"Frame changed size! If it shrank - good! Update this test. If it grew - bad! Try to find a way to avoid it."
8181
);
8282
assert!(

crates/egui/src/style.rs

+14-14
Original file line numberDiff line numberDiff line change
@@ -1293,9 +1293,9 @@ impl Visuals {
12931293

12941294
window_rounding: Rounding::same(6),
12951295
window_shadow: Shadow {
1296-
offset: vec2(10.0, 20.0),
1297-
blur: 15.0,
1298-
spread: 0.0,
1296+
offset: [10, 20],
1297+
blur: 15,
1298+
spread: 0,
12991299
color: Color32::from_black_alpha(96),
13001300
},
13011301
window_fill: Color32::from_gray(27),
@@ -1307,9 +1307,9 @@ impl Visuals {
13071307
panel_fill: Color32::from_gray(27),
13081308

13091309
popup_shadow: Shadow {
1310-
offset: vec2(6.0, 10.0),
1311-
blur: 8.0,
1312-
spread: 0.0,
1310+
offset: [6, 10],
1311+
blur: 8,
1312+
spread: 0,
13131313
color: Color32::from_black_alpha(96),
13141314
},
13151315

@@ -1349,9 +1349,9 @@ impl Visuals {
13491349
error_fg_color: Color32::from_rgb(255, 0, 0), // red
13501350

13511351
window_shadow: Shadow {
1352-
offset: vec2(10.0, 20.0),
1353-
blur: 15.0,
1354-
spread: 0.0,
1352+
offset: [10, 20],
1353+
blur: 15,
1354+
spread: 0,
13551355
color: Color32::from_black_alpha(25),
13561356
},
13571357
window_fill: Color32::from_gray(248),
@@ -1360,9 +1360,9 @@ impl Visuals {
13601360
panel_fill: Color32::from_gray(248),
13611361

13621362
popup_shadow: Shadow {
1363-
offset: vec2(6.0, 10.0),
1364-
blur: 8.0,
1365-
spread: 0.0,
1363+
offset: [6, 10],
1364+
blur: 8,
1365+
spread: 0,
13661366
color: Color32::from_black_alpha(25),
13671367
},
13681368

@@ -2456,13 +2456,13 @@ impl Widget for &mut Shadow {
24562456
ui.vertical(|ui| {
24572457
crate::Grid::new("shadow_ui").show(ui, |ui| {
24582458
ui.add(
2459-
DragValue::new(&mut offset.x)
2459+
DragValue::new(&mut offset[0])
24602460
.speed(1.0)
24612461
.range(-100.0..=100.0)
24622462
.prefix("x: "),
24632463
);
24642464
ui.add(
2465-
DragValue::new(&mut offset.y)
2465+
DragValue::new(&mut offset[1])
24662466
.speed(1.0)
24672467
.range(-100.0..=100.0)
24682468
.prefix("y: "),

crates/egui_demo_lib/src/demo/frame_demo.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ impl Default for FrameDemo {
1212
outer_margin: 24.0.into(),
1313
rounding: 14.0.into(),
1414
shadow: egui::Shadow {
15-
offset: [8.0, 12.0].into(),
16-
blur: 16.0,
17-
spread: 0.0,
15+
offset: [8, 12],
16+
blur: 16,
17+
spread: 0,
1818
color: egui::Color32::from_black_alpha(180),
1919
},
2020
fill: egui::Color32::from_rgba_unmultiplied(97, 0, 255, 128),

crates/epaint/src/shadow.rs

+29-15
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,41 @@ use crate::{Color32, Marginf, Rect, RectShape, Rounding, Vec2};
55
/// Can be used for a rectangular shadow with a soft penumbra.
66
///
77
/// Very similar to a box-shadow in CSS.
8-
#[derive(Clone, Copy, Debug, Default, PartialEq)]
8+
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
99
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
1010
pub struct Shadow {
1111
/// Move the shadow by this much.
1212
///
1313
/// For instance, a value of `[1.0, 2.0]` will move the shadow 1 point to the right and 2 points down,
1414
/// causing a drop-shadow effect.
15-
pub offset: Vec2,
15+
pub offset: [i8; 2],
1616

1717
/// The width of the blur, i.e. the width of the fuzzy penumbra.
1818
///
19-
/// A value of 0.0 means a sharp shadow.
20-
pub blur: f32,
19+
/// A value of 0 means a sharp shadow.
20+
pub blur: u8,
2121

2222
/// Expand the shadow in all directions by this much.
23-
pub spread: f32,
23+
pub spread: u8,
2424

2525
/// Color of the opaque center of the shadow.
2626
pub color: Color32,
2727
}
2828

29+
#[test]
30+
fn shadow_size() {
31+
assert_eq!(
32+
std::mem::size_of::<Shadow>(), 8,
33+
"Shadow changed size! If it shrank - good! Update this test. If it grew - bad! Try to find a way to avoid it."
34+
);
35+
}
36+
2937
impl Shadow {
3038
/// No shadow at all.
3139
pub const NONE: Self = Self {
32-
offset: Vec2::ZERO,
33-
blur: 0.0,
34-
spread: 0.0,
40+
offset: [0, 0],
41+
blur: 0,
42+
spread: 0,
3543
color: Color32::TRANSPARENT,
3644
};
3745

@@ -45,11 +53,14 @@ impl Shadow {
4553
spread,
4654
color,
4755
} = *self;
56+
let [offset_x, offset_y] = offset;
4857

49-
let rect = rect.translate(offset).expand(spread);
50-
let rounding = rounding.into() + Rounding::from(spread.abs());
58+
let rect = rect
59+
.translate(Vec2::new(offset_x as _, offset_y as _))
60+
.expand(spread as _);
61+
let rounding = rounding.into() + Rounding::from(spread);
5162

52-
RectShape::filled(rect, rounding, color).with_blur_width(blur)
63+
RectShape::filled(rect, rounding, color).with_blur_width(blur as _)
5364
}
5465

5566
/// How much larger than the parent rect are we in each direction?
@@ -60,11 +71,14 @@ impl Shadow {
6071
spread,
6172
color: _,
6273
} = *self;
74+
let spread = spread as f32;
75+
let blur = blur as f32;
76+
let [offset_x, offset_y] = offset;
6377
Marginf {
64-
left: spread + 0.5 * blur - offset.x,
65-
right: spread + 0.5 * blur + offset.x,
66-
top: spread + 0.5 * blur - offset.y,
67-
bottom: spread + 0.5 * blur + offset.y,
78+
left: spread + 0.5 * blur - offset_x as f32,
79+
right: spread + 0.5 * blur + offset_x as f32,
80+
top: spread + 0.5 * blur - offset_y as f32,
81+
bottom: spread + 0.5 * blur + offset_y as f32,
6882
}
6983
}
7084
}

0 commit comments

Comments
 (0)