1
0
Fork 0

Allow overriding slider width

This commit is contained in:
Robbert van der Helm 2022-03-05 19:39:39 +01:00
parent cab6be5167
commit 9b9799eb35
2 changed files with 20 additions and 12 deletions

View file

@ -60,6 +60,7 @@ pub fn create(
impl ParamWidget for GenericSlider { impl ParamWidget for GenericSlider {
fn add_widget<P: Param>(&self, ui: &mut Ui, param: &P, setter: &ParamSetter) { fn add_widget<P: Param>(&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));
} }
} }

View file

@ -27,6 +27,7 @@ pub struct ParamSlider<'a, P: Param> {
setter: &'a ParamSetter<'a>, setter: &'a ParamSetter<'a>,
draw_value: bool, draw_value: bool,
slider_width: Option<f32>,
} }
impl<'a, P: Param> ParamSlider<'a, P> { impl<'a, P: Param> ParamSlider<'a, P> {
@ -36,7 +37,9 @@ impl<'a, P: Param> ParamSlider<'a, P> {
Self { Self {
param, param,
setter, setter,
draw_value: true, draw_value: true,
slider_width: None,
} }
} }
@ -46,6 +49,12 @@ impl<'a, P: Param> ParamSlider<'a, P> {
self 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 { fn normalized_value(&self) -> f32 {
self.param.normalized_value() self.param.normalized_value()
} }
@ -212,26 +221,24 @@ impl<'a, P: Param> ParamSlider<'a, P> {
impl<P: Param> Widget for ParamSlider<'_, P> { impl<P: Param> Widget for ParamSlider<'_, P> {
fn ui(self, ui: &mut Ui) -> Response { fn ui(self, ui: &mut Ui) -> Response {
let slider_width = self
.slider_width
.unwrap_or_else(|| ui.spacing().slider_width);
ui.horizontal(|ui| { ui.horizontal(|ui| {
// Allocate space, but add some padding on the top and bottom to make it look a bit slimmer. // Allocate space, but add some padding on the top and bottom to make it look a bit slimmer.
let height = ui let height = ui
.text_style_height(&TextStyle::Body) .text_style_height(&TextStyle::Body)
.max(ui.spacing().interact_size.y); .max(ui.spacing().interact_size.y * 0.8);
let slider_height = ui.painter().round_to_pixel(height * 0.65); let slider_height = ui.painter().round_to_pixel(height * 0.8);
let response = ui let response = ui
.vertical(|ui| { .vertical(|ui| {
ui.allocate_space(vec2( ui.allocate_space(vec2(slider_width, (height - slider_height) / 2.0));
ui.spacing().slider_width,
(height - slider_height) / 2.0,
));
let response = ui.allocate_response( let response = ui.allocate_response(
vec2(ui.spacing().slider_width, slider_height), vec2(slider_width, slider_height),
Sense::click_and_drag(), Sense::click_and_drag(),
); );
ui.allocate_space(vec2( ui.allocate_space(vec2(slider_width, (height - slider_height) / 2.0));
ui.spacing().slider_width,
(height - slider_height) / 2.0,
));
response response
}) })
.inner; .inner;