1
0
mirror of https://github.com/emilk/egui.git synced 2026-09-01 14:20:04 -04:00

Store Margin using i8 to reduce its size (#5567)

Adds `Marginf` to fill the previous niche.

This is all in a pursuit to shrink the sizes of often-used structs, to
improve performance (less cache misses, less memcpy:s, etc).

* On the path towards https://github.com/emilk/egui/issues/4019
This commit is contained in:
Emil Ernerfeldt
2025-01-02 16:05:52 +01:00
committed by GitHub
parent aeea70d9e7
commit d58d13781d
12 changed files with 449 additions and 107 deletions

View File

@@ -4,7 +4,7 @@ use crate::{
epaint, layers::ShapeIdx, InnerResponse, Response, Sense, Style, Ui, UiBuilder, UiKind,
UiStackInfo,
};
use epaint::{Color32, Margin, Rect, Rounding, Shadow, Shape, Stroke};
use epaint::{Color32, Margin, Marginf, Rect, Rounding, Shadow, Shape, Stroke};
/// Add a background, frame and/or margin to a rectangular background of a [`Ui`].
///
@@ -73,6 +73,18 @@ pub struct Frame {
pub stroke: Stroke,
}
#[test]
fn frame_size() {
assert_eq!(
std::mem::size_of::<Frame>(), 44,
"Frame changed size! If it shrank - good! Update this test. If it grew - bad! Try to find a way to avoid it."
);
assert!(
std::mem::size_of::<Frame>() <= 64,
"Frame is getting way too big!"
);
}
impl Frame {
pub fn none() -> Self {
Self::default()
@@ -81,7 +93,7 @@ impl Frame {
/// For when you want to group a few widgets together within a frame.
pub fn group(style: &Style) -> Self {
Self {
inner_margin: Margin::same(6.0), // same and symmetric looks best in corners when nesting groups
inner_margin: Margin::same(6), // same and symmetric looks best in corners when nesting groups
rounding: style.visuals.widgets.noninteractive.rounding,
stroke: style.visuals.widgets.noninteractive.bg_stroke,
..Default::default()
@@ -90,7 +102,7 @@ impl Frame {
pub fn side_top_panel(style: &Style) -> Self {
Self {
inner_margin: Margin::symmetric(8.0, 2.0),
inner_margin: Margin::symmetric(8, 2),
fill: style.visuals.panel_fill,
..Default::default()
}
@@ -98,7 +110,7 @@ impl Frame {
pub fn central_panel(style: &Style) -> Self {
Self {
inner_margin: Margin::same(8.0),
inner_margin: Margin::same(8),
fill: style.visuals.panel_fill,
..Default::default()
}
@@ -143,7 +155,7 @@ impl Frame {
/// and in dark mode this will be very dark.
pub fn canvas(style: &Style) -> Self {
Self {
inner_margin: Margin::same(2.0),
inner_margin: Margin::same(2),
rounding: style.visuals.widgets.noninteractive.rounding,
fill: style.visuals.extreme_bg_color,
stroke: style.visuals.window_stroke(),
@@ -213,10 +225,10 @@ impl Frame {
}
impl Frame {
/// inner margin plus outer margin.
/// Inner margin plus outer margin.
#[inline]
pub fn total_margin(&self) -> Margin {
self.inner_margin + self.outer_margin
pub fn total_margin(&self) -> Marginf {
Marginf::from(self.inner_margin) + Marginf::from(self.outer_margin)
}
}

View File

@@ -486,7 +486,7 @@ impl<'open> Window<'open> {
// Calculate roughly how much larger the window size is compared to the inner rect
let (title_bar_height, title_content_spacing) = if with_title_bar {
let style = ctx.style();
let spacing = window_margin.top + window_margin.bottom;
let spacing = window_margin.sum().y;
let height = ctx.fonts(|f| title.font_height(f, &style)) + spacing;
let half_height = (height / 2.0).round() as _;
window_frame.rounding.ne = window_frame.rounding.ne.clamp(0, half_height);

View File

@@ -1238,8 +1238,8 @@ impl Default for Spacing {
fn default() -> Self {
Self {
item_spacing: vec2(8.0, 3.0),
window_margin: Margin::same(6.0),
menu_margin: Margin::same(6.0),
window_margin: Margin::same(6),
menu_margin: Margin::same(6),
button_padding: vec2(4.0, 1.0),
indent: 18.0, // match checkbox/radio-button with `button_padding.x + icon_width + icon_spacing`
interact_size: vec2(40.0, 18.0),
@@ -2371,9 +2371,17 @@ impl Widget for &mut Margin {
// Apply the checkbox:
if same {
*self = Margin::same((self.left + self.right + self.top + self.bottom) / 4.0);
} else if self.is_same() {
self.right *= 1.00001; // prevent collapsing into sameness
*self =
Margin::from((self.leftf() + self.rightf() + self.topf() + self.bottomf()) / 4.0);
} else {
// Make sure it is not same:
if self.is_same() {
if self.right == i8::MAX {
self.right = i8::MAX - 1;
} else {
self.right += 1;
}
}
}
response

View File

@@ -126,7 +126,7 @@ impl<'t> TextEdit<'t> {
layouter: None,
password: false,
frame: true,
margin: Margin::symmetric(4.0, 2.0),
margin: Margin::symmetric(4, 2),
multiline: true,
interactive: true,
desired_width: None,