From da96fcacd319f52849bf5cf2f13885fb22d69748 Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Fri, 28 Oct 2022 10:04:54 +0200 Subject: [PATCH] Improve panel sizing API --- crates/egui/src/containers/panel.rs | 38 ++++++++++++++++++++++++----- 1 file changed, 32 insertions(+), 6 deletions(-) diff --git a/crates/egui/src/containers/panel.rs b/crates/egui/src/containers/panel.rs index b0fbe2ec..233ceae4 100644 --- a/crates/egui/src/containers/panel.rs +++ b/crates/egui/src/containers/panel.rs @@ -137,25 +137,37 @@ impl SidePanel { /// The initial wrapping width of the [`SidePanel`]. pub fn default_width(mut self, default_width: f32) -> Self { self.default_width = default_width; + self.width_range = self.width_range.start().at_most(default_width) + ..=self.width_range.end().at_least(default_width); self } + /// Minimum width of the panel. pub fn min_width(mut self, min_width: f32) -> Self { - self.width_range = min_width..=(*self.width_range.end()); + self.width_range = min_width..=self.width_range.end().at_least(min_width); self } + /// Maximum width of the panel. pub fn max_width(mut self, max_width: f32) -> Self { - self.width_range = (*self.width_range.start())..=max_width; + self.width_range = self.width_range.start().at_most(max_width)..=max_width; self } - /// The allowable width range for resizable panels. + /// The allowable width range for the panel. pub fn width_range(mut self, width_range: RangeInclusive) -> Self { + self.default_width = clamp_to_range(self.default_width, width_range.clone()); self.width_range = width_range; self } + /// Enforce this exact width. + pub fn exact_width(mut self, width: f32) -> Self { + self.default_width = width; + self.width_range = width..=width; + self + } + /// Change the background color, margins, etc. pub fn frame(mut self, frame: Frame) -> Self { self.frame = Some(frame); @@ -420,25 +432,39 @@ impl TopBottomPanel { /// Defaults to [`style::Spacing::interact_size`].y. pub fn default_height(mut self, default_height: f32) -> Self { self.default_height = Some(default_height); + self.height_range = self.height_range.start().at_most(default_height) + ..=self.height_range.end().at_least(default_height); self } + /// Minimum height of the panel. pub fn min_height(mut self, min_height: f32) -> Self { - self.height_range = min_height..=(*self.height_range.end()); + self.height_range = min_height..=self.height_range.end().at_least(min_height); self } + /// Maximum height of the panel. pub fn max_height(mut self, max_height: f32) -> Self { - self.height_range = (*self.height_range.start())..=max_height; + self.height_range = self.height_range.start().at_most(max_height)..=max_height; self } - /// The allowable height range for resizable panels. + /// The allowable height range for the panel. pub fn height_range(mut self, height_range: RangeInclusive) -> Self { + self.default_height = self + .default_height + .map(|default_height| clamp_to_range(default_height, height_range.clone())); self.height_range = height_range; self } + /// Enforce this exact height. + pub fn exact_height(mut self, height: f32) -> Self { + self.default_height = Some(height); + self.height_range = height..=height; + self + } + /// Change the background color, margins, etc. pub fn frame(mut self, frame: Frame) -> Self { self.frame = Some(frame);