Browse Source

SlidSlider will now show the value display by default.

Turn off with slider.show_value(false)

Closes https://github.com/emilk/egui/issues/183
dancing-strings-experiment
Emil Ernerfeldt 4 years ago
parent
commit
9a546ff97a
  1. 1
      CHANGELOG.md
  2. 54
      egui/src/widgets/slider.rs

1
CHANGELOG.md

@ -18,6 +18,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
* Improve the positioning of tooltips. * Improve the positioning of tooltips.
* Only show tooltips if mouse is still. * Only show tooltips if mouse is still.
* `Slider` will now show the value display by default, unless turned off with `.show_value(false)`.
## 0.9.0 - 2021-02-07 - Light Mode and much more ## 0.9.0 - 2021-02-07 - Light Mode and much more

54
egui/src/widgets/slider.rs

@ -47,6 +47,15 @@ struct SliderSpec {
/// If you want to clamp incoming and outgoing values, use [`Slider::clamp_to_range`]. /// 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. /// The range can include any numbers, and go from low-to-high or from high-to-low.
///
/// The slider consists of three parts: a horizontal slider, a value display, and an optional text.
/// The user can click the value display to edit its value. It can be turned off with `.show_value(false)`.
///
/// ```
/// # let ui = &mut egui::Ui::__test();
/// # let mut my_f32: f32 = 0.0;
/// ui.add(egui::Slider::f32(&mut my_f32, 0.0..=100.0).text("My value"));
/// ```
#[must_use = "You should put this widget in an ui with `ui.add(widget);`"] #[must_use = "You should put this widget in an ui with `ui.add(widget);`"]
pub struct Slider<'a> { pub struct Slider<'a> {
get_set_value: GetSetValue<'a>, get_set_value: GetSetValue<'a>,
@ -54,8 +63,8 @@ pub struct Slider<'a> {
spec: SliderSpec, spec: SliderSpec,
clamp_to_range: bool, clamp_to_range: bool,
smart_aim: bool, smart_aim: bool,
// TODO: label: Option<Label> show_value: bool,
text: Option<String>, text: String,
text_color: Option<Color32>, text_color: Option<Color32>,
min_decimals: usize, min_decimals: usize,
max_decimals: Option<usize>, max_decimals: Option<usize>,
@ -76,7 +85,8 @@ impl<'a> Slider<'a> {
}, },
clamp_to_range: false, clamp_to_range: false,
smart_aim: true, smart_aim: true,
text: None, show_value: true,
text: Default::default(),
text_color: None, text_color: None,
min_decimals: 0, min_decimals: 0,
max_decimals: None, max_decimals: None,
@ -142,8 +152,16 @@ impl<'a> Slider<'a> {
.integer() .integer()
} }
/// Control wether or not the slider shows the current value.
/// Default: `true`.
pub fn show_value(mut self, show_value: bool) -> Self {
self.show_value = show_value;
self
}
/// Show a text next to the slider (e.g. explaining what the slider controls).
pub fn text(mut self, text: impl Into<String>) -> Self { pub fn text(mut self, text: impl Into<String>) -> Self {
self.text = Some(text.into()); self.text = text.into();
self self
} }
@ -335,9 +353,9 @@ impl<'a> Slider<'a> {
} }
fn label_ui(&mut self, ui: &mut Ui) { fn label_ui(&mut self, ui: &mut Ui) {
if let Some(label_text) = self.text.as_deref() { if !self.text.is_empty() {
let text_color = self.text_color.unwrap_or_else(|| ui.visuals().text_color()); let text_color = self.text_color.unwrap_or_else(|| ui.visuals().text_color());
ui.add(Label::new(label_text).wrap(false).text_color(text_color)); ui.add(Label::new(&self.text).wrap(false).text_color(text_color));
} }
} }
@ -417,21 +435,21 @@ impl<'a> Widget for Slider<'a> {
let font = &ui.fonts()[text_style]; let font = &ui.fonts()[text_style];
let height = font.row_height().at_least(ui.spacing().interact_size.y); let height = font.row_height().at_least(ui.spacing().interact_size.y);
if self.text.is_some() { let inner_response = ui.horizontal(|ui| {
ui.horizontal(|ui| { let slider_response = self.allocate_slider_space(ui, height);
let slider_response = self.allocate_slider_space(ui, height); self.slider_ui(ui, &slider_response);
self.slider_ui(ui, &slider_response);
if self.show_value {
let x_range = x_range(&slider_response.rect); let x_range = x_range(&slider_response.rect);
self.value_ui(ui, x_range); self.value_ui(ui, x_range);
}
if !self.text.is_empty() {
self.label_ui(ui); self.label_ui(ui);
slider_response }
}) slider_response
.inner });
} else { inner_response.inner | inner_response.response
let response = self.allocate_slider_space(ui, height);
self.slider_ui(ui, &response);
response
}
} }
} }

Loading…
Cancel
Save