From 6d57a24f35c160829c17ed1259b7817891987a26 Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Mon, 25 Jan 2021 19:55:08 +0100 Subject: [PATCH] Add Slider::clamp_to_range(bool) --- CHANGELOG.md | 1 + egui/src/widgets/slider.rs | 25 ++++++++++++++++++++++++- egui_demo_lib/src/apps/demo/sliders.rs | 13 +++++++++++++ 3 files changed, 38 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f5f48dc9..3a7ec564 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). * Add support for secondary and middle mouse buttons. * `egui::popup::popup_below_widget`: show a popup area below another widget. +* Add `Slider::clamp_to_range(bool)`: if set, clamp the incoming and outgoing values to the slider range. ### Changed 🔧 diff --git a/egui/src/widgets/slider.rs b/egui/src/widgets/slider.rs index 32b02d05..7d9bf2e1 100644 --- a/egui/src/widgets/slider.rs +++ b/egui/src/widgets/slider.rs @@ -36,12 +36,19 @@ struct SliderSpec { } /// Control a number by a horizontal slider. +/// +/// The slider range defines the values you get when pulling the slider to the far edges. +/// By default, the slider can still show values outside this range, +/// and still allows users to enter values outside the range by clicking the slider value and editing it. +/// If you want to clamp incoming and outgoing values, use [`Slider::clamp_to_range`]. +/// /// The range can include any numbers, and go from low-to-high or from high-to-low. #[must_use = "You should put this widget in an ui with `ui.add(widget);`"] pub struct Slider<'a> { get_set_value: GetSetValue<'a>, range: RangeInclusive, spec: SliderSpec, + clamp_to_range: bool, smart_aim: bool, // TODO: label: Option