Skip to content

Commit

Permalink
Replace uses of RangeInclusive<f32> with emath::Rangef (#3221)
Browse files Browse the repository at this point in the history
* Replace uses of `RangeInclusive<f32>` with `emath::Rangef`

* Fix doc-test
  • Loading branch information
emilk authored Aug 10, 2023
1 parent 4c3b380 commit 8cdffc4
Show file tree
Hide file tree
Showing 14 changed files with 213 additions and 161 deletions.
71 changes: 36 additions & 35 deletions crates/egui/src/containers/panel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
//!
//! Add your [`Window`]:s after any top-level panels.

use std::ops::RangeInclusive;

use crate::*;

/// State regarding panels.
Expand Down Expand Up @@ -99,7 +97,7 @@ pub struct SidePanel {
resizable: bool,
show_separator_line: bool,
default_width: f32,
width_range: RangeInclusive<f32>,
width_range: Rangef,
}

impl SidePanel {
Expand All @@ -122,7 +120,7 @@ impl SidePanel {
resizable: true,
show_separator_line: true,
default_width: 200.0,
width_range: 96.0..=f32::INFINITY,
width_range: Rangef::new(96.0, f32::INFINITY),
}
}

Expand Down Expand Up @@ -153,34 +151,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.width_range = Rangef::new(
self.width_range.min.at_most(default_width),
self.width_range.max.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().at_least(min_width);
self.width_range = Rangef::new(min_width, self.width_range.max.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().at_most(max_width)..=max_width;
self.width_range = Rangef::new(self.width_range.min.at_most(max_width), max_width);
self
}

/// The allowable width range for the panel.
pub fn width_range(mut self, width_range: RangeInclusive<f32>) -> Self {
self.default_width = clamp_to_range(self.default_width, width_range.clone());
pub fn width_range(mut self, width_range: impl Into<Rangef>) -> Self {
let width_range = width_range.into();
self.default_width = clamp_to_range(self.default_width, width_range);
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.width_range = Rangef::point(width);
self
}

Expand Down Expand Up @@ -224,7 +225,7 @@ impl SidePanel {
if let Some(state) = PanelState::load(ui.ctx(), id) {
width = state.rect.width();
}
width = clamp_to_range(width, width_range.clone()).at_most(available_rect.width());
width = clamp_to_range(width, width_range).at_most(available_rect.width());
side.set_rect_width(&mut panel_rect, width);
ui.ctx().check_for_id_clash(id, panel_rect, "SidePanel");
}
Expand All @@ -241,7 +242,7 @@ impl SidePanel {

let resize_x = side.opposite().side_x(panel_rect);
let mouse_over_resize_line = we_are_on_top
&& panel_rect.y_range().contains(&pointer.y)
&& panel_rect.y_range().contains(pointer.y)
&& (resize_x - pointer.x).abs()
<= ui.style().interaction.resize_grab_radius_side;

Expand All @@ -253,8 +254,7 @@ impl SidePanel {
is_resizing = ui.memory(|mem| mem.is_being_dragged(resize_id));
if is_resizing {
let width = (pointer.x - side.side_x(panel_rect)).abs();
let width =
clamp_to_range(width, width_range.clone()).at_most(available_rect.width());
let width = clamp_to_range(width, width_range).at_most(available_rect.width());
side.set_rect_width(&mut panel_rect, width);
}

Expand All @@ -273,7 +273,7 @@ impl SidePanel {
let frame = frame.unwrap_or_else(|| Frame::side_top_panel(ui.style()));
let inner_response = frame.show(&mut panel_ui, |ui| {
ui.set_min_height(ui.max_rect().height()); // Make sure the frame fills the full height
ui.set_min_width(*width_range.start());
ui.set_min_width(width_range.min);
add_contents(ui)
});

Expand Down Expand Up @@ -544,7 +544,7 @@ pub struct TopBottomPanel {
resizable: bool,
show_separator_line: bool,
default_height: Option<f32>,
height_range: RangeInclusive<f32>,
height_range: Rangef,
}

impl TopBottomPanel {
Expand All @@ -567,7 +567,7 @@ impl TopBottomPanel {
resizable: false,
show_separator_line: true,
default_height: None,
height_range: 20.0..=f32::INFINITY,
height_range: Rangef::new(20.0, f32::INFINITY),
}
}

Expand Down Expand Up @@ -599,36 +599,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.height_range = Rangef::new(
self.height_range.min.at_most(default_height),
self.height_range.max.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().at_least(min_height);
self.height_range = Rangef::new(min_height, self.height_range.max.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().at_most(max_height)..=max_height;
self.height_range = Rangef::new(self.height_range.min.at_most(max_height), max_height);
self
}

/// The allowable height range for the panel.
pub fn height_range(mut self, height_range: RangeInclusive<f32>) -> Self {
pub fn height_range(mut self, height_range: impl Into<Rangef>) -> Self {
let height_range = height_range.into();
self.default_height = self
.default_height
.map(|default_height| clamp_to_range(default_height, height_range.clone()));
.map(|default_height| clamp_to_range(default_height, height_range));
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.height_range = Rangef::point(height);
self
}

Expand Down Expand Up @@ -673,7 +676,7 @@ impl TopBottomPanel {
} else {
default_height.unwrap_or_else(|| ui.style().spacing.interact_size.y)
};
height = clamp_to_range(height, height_range.clone()).at_most(available_rect.height());
height = clamp_to_range(height, height_range).at_most(available_rect.height());
side.set_rect_height(&mut panel_rect, height);
ui.ctx()
.check_for_id_clash(id, panel_rect, "TopBottomPanel");
Expand All @@ -692,7 +695,7 @@ impl TopBottomPanel {

let resize_y = side.opposite().side_y(panel_rect);
let mouse_over_resize_line = we_are_on_top
&& panel_rect.x_range().contains(&pointer.x)
&& panel_rect.x_range().contains(pointer.x)
&& (resize_y - pointer.y).abs()
<= ui.style().interaction.resize_grab_radius_side;

Expand All @@ -704,8 +707,8 @@ impl TopBottomPanel {
is_resizing = ui.memory(|mem| mem.interaction.drag_id == Some(resize_id));
if is_resizing {
let height = (pointer.y - side.side_y(panel_rect)).abs();
let height = clamp_to_range(height, height_range.clone())
.at_most(available_rect.height());
let height =
clamp_to_range(height, height_range).at_most(available_rect.height());
side.set_rect_height(&mut panel_rect, height);
}

Expand All @@ -724,7 +727,7 @@ impl TopBottomPanel {
let frame = frame.unwrap_or_else(|| Frame::side_top_panel(ui.style()));
let inner_response = frame.show(&mut panel_ui, |ui| {
ui.set_min_width(ui.max_rect().width()); // Make the frame fill full width
ui.set_min_height(*height_range.start());
ui.set_min_height(height_range.min);
add_contents(ui)
});

Expand Down Expand Up @@ -1056,9 +1059,7 @@ impl CentralPanel {
}
}

fn clamp_to_range(x: f32, range: RangeInclusive<f32>) -> f32 {
x.clamp(
range.start().min(*range.end()),
range.start().max(*range.end()),
)
fn clamp_to_range(x: f32, range: Rangef) -> f32 {
let range = range.as_positive();
x.clamp(range.min, range.max)
}
3 changes: 1 addition & 2 deletions crates/egui/src/containers/scroll_area.rs
Original file line number Diff line number Diff line change
Expand Up @@ -640,8 +640,7 @@ impl Prepared {
let min = content_ui.min_rect().min[d];
let clip_rect = content_ui.clip_rect();
let visible_range = min..=min + clip_rect.size()[d];
let start = *scroll.start();
let end = *scroll.end();
let (start, end) = (scroll.min, scroll.max);
let clip_start = clip_rect.min[d];
let clip_end = clip_rect.max[d];
let mut spacing = ui.spacing().item_spacing[d];
Expand Down
4 changes: 1 addition & 3 deletions crates/egui/src/frame_state.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::ops::RangeInclusive;

use crate::{id::IdSet, *};

#[derive(Clone, Copy, Debug)]
Expand Down Expand Up @@ -46,7 +44,7 @@ pub(crate) struct FrameState {
pub(crate) scroll_delta: Vec2, // TODO(emilk): move to `InputState` ?

/// horizontal, vertical
pub(crate) scroll_target: [Option<(RangeInclusive<f32>, Option<Align>)>; 2],
pub(crate) scroll_target: [Option<(Rangef, Option<Align>)>; 2],

#[cfg(feature = "accesskit")]
pub(crate) accesskit_state: Option<AccessKitFrameState>,
Expand Down
4 changes: 3 additions & 1 deletion crates/egui/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,9 @@ pub use epaint::emath;
#[cfg(feature = "color-hex")]
pub use ecolor::hex_color;
pub use ecolor::{Color32, Rgba};
pub use emath::{lerp, pos2, remap, remap_clamp, vec2, Align, Align2, NumExt, Pos2, Rect, Vec2};
pub use emath::{
lerp, pos2, remap, remap_clamp, vec2, Align, Align2, NumExt, Pos2, Rangef, Rect, Vec2,
};
pub use epaint::{
mutex,
text::{FontData, FontDefinitions, FontFamily, FontId, FontTweak},
Expand Down
7 changes: 3 additions & 4 deletions crates/egui/src/painter.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use std::ops::RangeInclusive;
use std::sync::Arc;

use crate::{
emath::{Align2, Pos2, Rect, Vec2},
emath::{Align2, Pos2, Rangef, Rect, Vec2},
layers::{LayerId, PaintList, ShapeIdx},
Color32, Context, FontId,
};
Expand Down Expand Up @@ -263,12 +262,12 @@ impl Painter {
}

/// Paints a horizontal line.
pub fn hline(&self, x: RangeInclusive<f32>, y: f32, stroke: impl Into<Stroke>) {
pub fn hline(&self, x: impl Into<Rangef>, y: f32, stroke: impl Into<Stroke>) {
self.add(Shape::hline(x, y, stroke));
}

/// Paints a vertical line.
pub fn vline(&self, x: f32, y: RangeInclusive<f32>, stroke: impl Into<Stroke>) {
pub fn vline(&self, x: f32, y: impl Into<Rangef>, stroke: impl Into<Stroke>) {
self.add(Shape::vline(x, y, stroke));
}

Expand Down
20 changes: 11 additions & 9 deletions crates/egui/src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -517,15 +517,17 @@ impl Ui {
}

/// `ui.set_width_range(min..=max);` is equivalent to `ui.set_min_width(min); ui.set_max_width(max);`.
pub fn set_width_range(&mut self, width: std::ops::RangeInclusive<f32>) {
self.set_min_width(*width.start());
self.set_max_width(*width.end());
pub fn set_width_range(&mut self, width: impl Into<Rangef>) {
let width = width.into();
self.set_min_width(width.min);
self.set_max_width(width.max);
}

/// `ui.set_height_range(min..=max);` is equivalent to `ui.set_min_height(min); ui.set_max_height(max);`.
pub fn set_height_range(&mut self, height: std::ops::RangeInclusive<f32>) {
self.set_min_height(*height.start());
self.set_max_height(*height.end());
pub fn set_height_range(&mut self, height: impl Into<Rangef>) {
let height = height.into();
self.set_min_height(height.min);
self.set_max_height(height.max);
}

/// Set both the minimum and maximum width.
Expand Down Expand Up @@ -978,7 +980,7 @@ impl Ui {
/// ```
pub fn scroll_to_rect(&self, rect: Rect, align: Option<Align>) {
for d in 0..2 {
let range = rect.min[d]..=rect.max[d];
let range = Rangef::new(rect.min[d], rect.max[d]);
self.ctx()
.frame_state_mut(|state| state.scroll_target[d] = Some((range, align)));
}
Expand Down Expand Up @@ -1008,9 +1010,9 @@ impl Ui {
pub fn scroll_to_cursor(&self, align: Option<Align>) {
let target = self.next_widget_position();
for d in 0..2 {
let target = target[d];
let target = Rangef::point(target[d]);
self.ctx()
.frame_state_mut(|state| state.scroll_target[d] = Some((target..=target, align)));
.frame_state_mut(|state| state.scroll_target[d] = Some((target, align)));
}
}

Expand Down
Loading

0 comments on commit 8cdffc4

Please sign in to comment.