From 93c763be5d68c8f2a74a23d3417eab33905a9a96 Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Mon, 14 Aug 2023 13:44:07 +0200 Subject: [PATCH] Use `Margin` instead of a,b,c,d --- crates/egui/src/style.rs | 7 +++++++ crates/egui/src/widgets/plot/mod.rs | 30 ++++++++++++----------------- 2 files changed, 19 insertions(+), 18 deletions(-) diff --git a/crates/egui/src/style.rs b/crates/egui/src/style.rs index 4a8dba7c0..d8517fc91 100644 --- a/crates/egui/src/style.rs +++ b/crates/egui/src/style.rs @@ -338,6 +338,13 @@ pub struct Margin { } impl Margin { + pub const ZERO: Self = Self { + left: 0.0, + right: 0.0, + top: 0.0, + bottom: 0.0, + }; + #[inline] pub fn same(margin: f32) -> Self { Self { diff --git a/crates/egui/src/widgets/plot/mod.rs b/crates/egui/src/widgets/plot/mod.rs index 5f8018668..d03d632f9 100644 --- a/crates/egui/src/widgets/plot/mod.rs +++ b/crates/egui/src/widgets/plot/mod.rs @@ -748,9 +748,10 @@ impl Plot { // Next we want to create this layout. // Incides are only examples. // - // +-b-+---------x----------+ + + // left right + // +---+---------x----------+ + // | | x-Axis 3 | - // c +--------------------+ + // | +--------------------+ top // | | x-Axis 2 | // +-+-+--------------------+-+-+ // |y|y| |y|y| @@ -762,26 +763,22 @@ impl Plot { // |1|0| |2|3| // +-+-+--------------------+-+-+ // | x-Axis 0 | | - // +--------------------+ a + // +--------------------+ | bottom // | x-Axis 1 | | - // + +--------------------+-d-+ + // + +--------------------+---+ // let mut plot_rect: Rect = { - // find dimensions of axis labels - // for a, b, c, d meanings see picture - let mut a = 0.0; - let mut b = 0.0; - let mut c = 0.0; - let mut d = 0.0; + // Calcuclate the space needed for each axis labels. + let mut margin = Margin::ZERO; if show_axes.x { for cfg in &x_axes { match cfg.placement { axis::Placement::Default => { - a += cfg.thickness(); + margin.bottom += cfg.thickness(); } axis::Placement::Opposite => { - c += cfg.thickness(); + margin.top += cfg.thickness(); } } } @@ -790,20 +787,17 @@ impl Plot { for cfg in &y_axes { match cfg.placement { axis::Placement::Default => { - b += cfg.thickness(); + margin.left += cfg.thickness(); } axis::Placement::Opposite => { - d += cfg.thickness(); + margin.right += cfg.thickness(); } } } } // determine plot rectangle - Rect { - min: complete_rect.min + Vec2::new(b, c), - max: complete_rect.max - Vec2::new(d, a), - } + margin.shrink_rect(complete_rect) }; let mut x_axis_widgets = Vec::::new(); let mut y_axis_widgets = Vec::::new();