From f812374ad077364996893e8a3a44ce1d48d0aa8b Mon Sep 17 00:00:00 2001 From: Robbert van der Helm Date: Sat, 23 Jul 2022 18:50:35 +0200 Subject: [PATCH] Make it easier to reuse generic UI widget drawing --- nih_plug_vizia/src/widgets/generic_ui.rs | 56 +++++++++++++----------- 1 file changed, 30 insertions(+), 26 deletions(-) diff --git a/nih_plug_vizia/src/widgets/generic_ui.rs b/nih_plug_vizia/src/widgets/generic_ui.rs index fa2667c3..57d85595 100644 --- a/nih_plug_vizia/src/widgets/generic_ui.rs +++ b/nih_plug_vizia/src/widgets/generic_ui.rs @@ -36,32 +36,7 @@ impl GenericUi { // Align this on the right Label::new(cx, unsafe { param_ptr.name() }).class("label"); - // TODO: Come up with a less hacky way to iterate over the mapping like this while - // keeping the clean interface on the `ParamSlider` widget - let dummy = StaticLens::new(&()); - unsafe { - match param_ptr { - ParamPtr::FloatParam(p) => ParamSlider::new(cx, dummy, move |_| &*p), - ParamPtr::IntParam(p) => ParamSlider::new(cx, dummy, move |_| &*p), - ParamPtr::BoolParam(p) => ParamSlider::new(cx, dummy, move |_| &*p), - ParamPtr::EnumParam(p) => ParamSlider::new(cx, dummy, move |_| &*p), - } - } - .set_style(match unsafe { param_ptr.step_count() } { - // This looks nice for boolean values, but it's too crowded for anything beyond - // that without making the widget wider - Some(step_count) if step_count <= 1 => { - ParamSliderStyle::CurrentStepLabeled { even: true } - } - Some(step_count) if step_count <= 2 => { - ParamSliderStyle::CurrentStep { even: true } - } - Some(_) => ParamSliderStyle::FromLeft, - // This is already the default, but continuous parameters should be drawn from - // the center if the default is also centered, or from the left if it is not - None => ParamSliderStyle::Centered, - }) - .class("widget"); + Self::draw_widget(cx, param_ptr); }) .class("row"); }) @@ -94,6 +69,35 @@ impl GenericUi { } }) } + + /// The standard widget drawing function. This can be used together with `.new_custom()` to only + /// draw the labels differently. + pub fn draw_widget(cx: &mut Context, param_ptr: ParamPtr) { + // TODO: Come up with a less hacky way to iterate over the mapping like this while + // keeping the clean interface on the `ParamSlider` widget + let dummy = StaticLens::new(&()); + unsafe { + match param_ptr { + ParamPtr::FloatParam(p) => ParamSlider::new(cx, dummy, move |_| &*p), + ParamPtr::IntParam(p) => ParamSlider::new(cx, dummy, move |_| &*p), + ParamPtr::BoolParam(p) => ParamSlider::new(cx, dummy, move |_| &*p), + ParamPtr::EnumParam(p) => ParamSlider::new(cx, dummy, move |_| &*p), + } + } + .set_style(match unsafe { param_ptr.step_count() } { + // This looks nice for boolean values, but it's too crowded for anything beyond + // that without making the widget wider + Some(step_count) if step_count <= 1 => { + ParamSliderStyle::CurrentStepLabeled { even: true } + } + Some(step_count) if step_count <= 2 => ParamSliderStyle::CurrentStep { even: true }, + Some(_) => ParamSliderStyle::FromLeft, + // This is already the default, but continuous parameters should be drawn from + // the center if the default is also centered, or from the left if it is not + None => ParamSliderStyle::Centered, + }) + .class("widget"); + } } impl View for GenericUi {