From 06802cb0a0b78304c6f8da207200b467f41a3bbb Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Wed, 27 Apr 2022 10:05:09 +0200 Subject: [PATCH] Add Rounding::at_least and Rounding::at_most --- egui/src/widgets/color_picker.rs | 7 +------ epaint/src/shape.rs | 22 ++++++++++++++++++++++ epaint/src/tessellator.rs | 8 +------- 3 files changed, 24 insertions(+), 13 deletions(-) diff --git a/egui/src/widgets/color_picker.rs b/egui/src/widgets/color_picker.rs index c87807b9..014b4099 100644 --- a/egui/src/widgets/color_picker.rs +++ b/egui/src/widgets/color_picker.rs @@ -90,13 +90,8 @@ fn color_button(ui: &mut Ui, color: Color32, open: bool) -> Response { ui.painter().rect_filled(left_half, 0.0, color); ui.painter().rect_filled(right_half, 0.0, color.to_opaque()); - let rounding = Rounding { - nw: visuals.rounding.nw.at_most(2.0), - ne: visuals.rounding.ne.at_most(2.0), - sw: visuals.rounding.sw.at_most(2.0), - se: visuals.rounding.se.at_most(2.0), - }; + let rounding = visuals.rounding.at_most(2.0); ui.painter() .rect_stroke(rect, rounding, (2.0, visuals.bg_fill)); // fill is intentional, because default style has no border } diff --git a/epaint/src/shape.rs b/epaint/src/shape.rs index 4b3612bd..8660e5df 100644 --- a/epaint/src/shape.rs +++ b/epaint/src/shape.rs @@ -535,6 +535,28 @@ impl Rounding { pub fn is_same(&self) -> bool { self.nw == self.ne && self.nw == self.sw && self.nw == self.se } + + /// Make sure each corner has a rounding of at least this. + #[inline] + pub fn at_least(&self, min: f32) -> Self { + Self { + nw: self.nw.max(min), + ne: self.ne.max(min), + sw: self.sw.max(min), + se: self.se.max(min), + } + } + + /// Make sure each corner has a rounding of at most this. + #[inline] + pub fn at_most(&self, max: f32) -> Self { + Self { + nw: self.nw.min(max), + ne: self.ne.min(max), + sw: self.sw.min(max), + se: self.se.min(max), + } + } } // ---------------------------------------------------------------------------- diff --git a/epaint/src/tessellator.rs b/epaint/src/tessellator.rs index 66317a58..980abee5 100644 --- a/epaint/src/tessellator.rs +++ b/epaint/src/tessellator.rs @@ -257,13 +257,7 @@ pub mod path { let half_width = rect.width() * 0.5; let half_height = rect.height() * 0.5; let max_cr = half_width.min(half_height); - - Rounding { - nw: rounding.nw.at_most(max_cr).at_least(0.0), - ne: rounding.ne.at_most(max_cr).at_least(0.0), - sw: rounding.sw.at_most(max_cr).at_least(0.0), - se: rounding.se.at_most(max_cr).at_least(0.0), - } + rounding.at_most(max_cr).at_least(0.0) } }