From 9b9799eb35ee368e34eacb7e6af02f3e3edfccab Mon Sep 17 00:00:00 2001 From: Robbert van der Helm Date: Sat, 5 Mar 2022 19:39:39 +0100 Subject: [PATCH] Allow overriding slider width --- nih_plug_egui/src/widgets/generic_ui.rs | 3 ++- nih_plug_egui/src/widgets/param_slider.rs | 29 ++++++++++++++--------- 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/nih_plug_egui/src/widgets/generic_ui.rs b/nih_plug_egui/src/widgets/generic_ui.rs index c6635260..164e074c 100644 --- a/nih_plug_egui/src/widgets/generic_ui.rs +++ b/nih_plug_egui/src/widgets/generic_ui.rs @@ -60,6 +60,7 @@ pub fn create( impl ParamWidget for GenericSlider { fn add_widget(&self, ui: &mut Ui, param: &P, setter: &ParamSetter) { - ui.add(ParamSlider::for_param(param, setter)); + // Make these sliders a bit wider, else they look a bit odd + ui.add(ParamSlider::for_param(param, setter).with_width(100.0)); } } diff --git a/nih_plug_egui/src/widgets/param_slider.rs b/nih_plug_egui/src/widgets/param_slider.rs index d65b25f2..2086aec7 100644 --- a/nih_plug_egui/src/widgets/param_slider.rs +++ b/nih_plug_egui/src/widgets/param_slider.rs @@ -27,6 +27,7 @@ pub struct ParamSlider<'a, P: Param> { setter: &'a ParamSetter<'a>, draw_value: bool, + slider_width: Option, } impl<'a, P: Param> ParamSlider<'a, P> { @@ -36,7 +37,9 @@ impl<'a, P: Param> ParamSlider<'a, P> { Self { param, setter, + draw_value: true, + slider_width: None, } } @@ -46,6 +49,12 @@ impl<'a, P: Param> ParamSlider<'a, P> { self } + /// Set a custom width for the slider. + pub fn with_width(mut self, width: f32) -> Self { + self.slider_width = Some(width); + self + } + fn normalized_value(&self) -> f32 { self.param.normalized_value() } @@ -212,26 +221,24 @@ impl<'a, P: Param> ParamSlider<'a, P> { impl Widget for ParamSlider<'_, P> { fn ui(self, ui: &mut Ui) -> Response { + let slider_width = self + .slider_width + .unwrap_or_else(|| ui.spacing().slider_width); + ui.horizontal(|ui| { // Allocate space, but add some padding on the top and bottom to make it look a bit slimmer. let height = ui .text_style_height(&TextStyle::Body) - .max(ui.spacing().interact_size.y); - let slider_height = ui.painter().round_to_pixel(height * 0.65); + .max(ui.spacing().interact_size.y * 0.8); + let slider_height = ui.painter().round_to_pixel(height * 0.8); let response = ui .vertical(|ui| { - ui.allocate_space(vec2( - ui.spacing().slider_width, - (height - slider_height) / 2.0, - )); + ui.allocate_space(vec2(slider_width, (height - slider_height) / 2.0)); let response = ui.allocate_response( - vec2(ui.spacing().slider_width, slider_height), + vec2(slider_width, slider_height), Sense::click_and_drag(), ); - ui.allocate_space(vec2( - ui.spacing().slider_width, - (height - slider_height) / 2.0, - )); + ui.allocate_space(vec2(slider_width, (height - slider_height) / 2.0)); response }) .inner;