From 7d42788be21efcc7bab1562b182dc9dae26ee0be Mon Sep 17 00:00:00 2001 From: Robbert van der Helm Date: Fri, 11 Nov 2022 19:23:29 +0100 Subject: [PATCH] Add back a ParamWidgetBase::view with explicit cx The version that returns a callback is nice, but it's a bit awkward when dealing with multiple parameters. --- nih_plug_vizia/src/widgets/param_base.rs | 62 +++++++++++++++--------- 1 file changed, 38 insertions(+), 24 deletions(-) diff --git a/nih_plug_vizia/src/widgets/param_base.rs b/nih_plug_vizia/src/widgets/param_base.rs index 5cf2669f..0b73ad11 100644 --- a/nih_plug_vizia/src/widgets/param_base.rs +++ b/nih_plug_vizia/src/widgets/param_base.rs @@ -117,8 +117,43 @@ impl ParamWidgetBase { /// Create a view using the a parameter's data. This is not tied to a particular /// [`ParamWidgetBase`] instance, but it allows you to easily create lenses for the parameter's /// values and access static parameter data. - /// - /// This can be used directly as an argument to [`View::build()`]. + pub fn view( + cx: &mut Context, + params: L, + params_to_param: FMap, + content: F, + ) -> R + where + L: Lens + Clone, + Params: 'static, + P: Param + 'static, + FMap: Fn(&Params) -> &P + Copy + 'static, + F: FnOnce(&mut Context, ParamWidgetData) -> R, + { + // We'll provide the raw `&P` to the callbacks to make creating parameter widgets more + // convenient. + // SAFETY: This &P won't outlive this function, and in the context of NIH-plug &P will + // outlive the editor + let param: &P = unsafe { + &*params + .clone() + .map(move |params| params_to_param(params) as *const P) + .get(cx) + }; + + // The widget can use this to access data parameter data and to create lenses for working + // with the parameter's values + let param_data = ParamWidgetData { + param, + params, + params_to_param, + }; + + content(cx, param_data) + } + + /// A shorthand for [`view()`][Self::view()] that can be used directly as an argument to + /// [`View::build()`]. pub fn build_view( params: L, params_to_param: FMap, @@ -131,28 +166,7 @@ impl ParamWidgetBase { FMap: Fn(&Params) -> &P + Copy + 'static, F: FnOnce(&mut Context, ParamWidgetData) -> R, { - move |cx| { - // We'll provide the raw `&P` to the callbacks to make creating parameter widgets more - // convenient. - // SAFETY: This &P won't outlive this function, and in the context of NIH-plug &P will - // outlive the editor - let param: &P = unsafe { - &*params - .clone() - .map(move |params| params_to_param(params) as *const P) - .get(cx) - }; - - // The widget can use this to access data parameter data and to create lenses for working - // with the parameter's values - let param_data = ParamWidgetData { - param, - params, - params_to_param, - }; - - content(cx, param_data) - } + move |cx| Self::view(cx, params, params_to_param, content) } /// Convenience function for using [`ParamWidgetData::make_lens()`]. Whenever possible,