From 0abfcb6045ee8185cc5e4b983f0c4f2327063066 Mon Sep 17 00:00:00 2001 From: Robbert van der Helm Date: Thu, 2 Feb 2023 20:03:48 +0100 Subject: [PATCH] Add a ParamButton name override modifier --- nih_plug_vizia/src/widgets/param_button.rs | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/nih_plug_vizia/src/widgets/param_button.rs b/nih_plug_vizia/src/widgets/param_button.rs index 6dc2bbd1..333ae340 100644 --- a/nih_plug_vizia/src/widgets/param_button.rs +++ b/nih_plug_vizia/src/widgets/param_button.rs @@ -16,6 +16,9 @@ pub struct ParamButton { // These fields are set through modifiers: /// Whether or not to listen to scroll events for changing the parameter's value in steps. use_scroll_wheel: bool, + /// A specific label to use instead of displaying the parameter's value. + label_override: Option, + /// The number of (fractional) scrolled lines that have not yet been turned into parameter /// change events. This is needed to support trackpads with smooth scrolling. scrolled_lines: f32, @@ -39,12 +42,19 @@ impl ParamButton { param_base: ParamWidgetBase::new(cx, params.clone(), params_to_param), use_scroll_wheel: true, + label_override: None, + scrolled_lines: 0.0, } .build( cx, ParamWidgetBase::build_view(params.clone(), params_to_param, move |cx, param_data| { - Label::new(cx, param_data.param().name()); + Binding::new(cx, Self::label_override, move |cx, label_override| { + match label_override.get(cx) { + Some(label_override) => Label::new(cx, &label_override), + None => Label::new(cx, param_data.param().name()), + }; + }) }), ) // We'll add the `:checked` pseudoclass when the button is pressed @@ -116,6 +126,10 @@ pub trait ParamButtonExt { /// Change the colors scheme for a bypass button. This simply adds the `bypass` class. fn for_bypass(self) -> Self; + + /// Change the label used for the button. If this is not set, then the parameter's name will be + /// used. + fn with_label(self, value: impl Into) -> Self; } impl ParamButtonExt for Handle<'_, ParamButton> { @@ -126,4 +140,10 @@ impl ParamButtonExt for Handle<'_, ParamButton> { fn for_bypass(self) -> Self { self.class("bypass") } + + fn with_label(self, value: impl Into) -> Self { + self.modify(|param_button: &mut ParamButton| { + param_button.label_override = Some(value.into()) + }) + } }