1
0
Fork 0

Add build closure parameter to create_egui_editor

This commit is contained in:
Robbert van der Helm 2022-10-20 14:03:55 +02:00
parent a9b1dd61fd
commit eed5a62abb
3 changed files with 12 additions and 2 deletions

View file

@ -8,6 +8,8 @@ code then it will not be listed here.
## [2022-10-20] ## [2022-10-20]
- The `create_egui_editor()` function from `nih_plug_egui` now also takes a
build closure to apply initialization logic to the egui context.
- The `nih_plug::param` module has been renamed to `nih_plug::params`. Code that - The `nih_plug::param` module has been renamed to `nih_plug::params`. Code that
only uses the prelude module doesn't need to be changed. only uses the prelude module doesn't need to be changed.
- Some items have been moved out of `nih_plug::param::internals`. The main - Some items have been moved out of `nih_plug::param::internals`. The main

View file

@ -35,18 +35,21 @@ pub mod widgets;
/// field on your parameters struct. /// field on your parameters struct.
/// ///
/// See [`EguiState::from_size()`]. /// See [`EguiState::from_size()`].
pub fn create_egui_editor<T, U>( pub fn create_egui_editor<T, B, U>(
egui_state: Arc<EguiState>, egui_state: Arc<EguiState>,
user_state: T, user_state: T,
build: B,
update: U, update: U,
) -> Option<Box<dyn Editor>> ) -> Option<Box<dyn Editor>>
where where
T: 'static + Send + Sync, T: 'static + Send + Sync,
B: Fn(&Context, &mut T) + 'static + Send + Sync,
U: Fn(&Context, &ParamSetter, &mut T) + 'static + Send + Sync, U: Fn(&Context, &ParamSetter, &mut T) + 'static + Send + Sync,
{ {
Some(Box::new(EguiEditor { Some(Box::new(EguiEditor {
egui_state, egui_state,
user_state: Arc::new(RwLock::new(user_state)), user_state: Arc::new(RwLock::new(user_state)),
build: Arc::new(build),
update: Arc::new(update), update: Arc::new(update),
// TODO: We can't get the size of the window when baseview does its own scaling, so if the // TODO: We can't get the size of the window when baseview does its own scaling, so if the
@ -110,6 +113,9 @@ struct EguiEditor<T> {
egui_state: Arc<EguiState>, egui_state: Arc<EguiState>,
/// The plugin's state. This is kept in between editor openenings. /// The plugin's state. This is kept in between editor openenings.
user_state: Arc<RwLock<T>>, user_state: Arc<RwLock<T>>,
/// The user's build function. Applied once at the start of the application.
build: Arc<dyn Fn(&Context, &mut T) + 'static + Send + Sync>,
/// The user's update function. /// The user's update function.
update: Arc<dyn Fn(&Context, &ParamSetter, &mut T) + 'static + Send + Sync>, update: Arc<dyn Fn(&Context, &ParamSetter, &mut T) + 'static + Send + Sync>,
@ -127,6 +133,7 @@ where
parent: ParentWindowHandle, parent: ParentWindowHandle,
context: Arc<dyn GuiContext>, context: Arc<dyn GuiContext>,
) -> Box<dyn std::any::Any + Send + Sync> { ) -> Box<dyn std::any::Any + Send + Sync> {
let build = self.build.clone();
let update = self.update.clone(); let update = self.update.clone();
let state = self.user_state.clone(); let state = self.user_state.clone();
@ -161,7 +168,7 @@ where
}), }),
}, },
state, state,
|_, _, _| {}, move |egui_ctx, _queue, state| build(egui_ctx, &mut state.write()),
move |egui_ctx, _queue, state| { move |egui_ctx, _queue, state| {
let setter = ParamSetter::new(context.as_ref()); let setter = ParamSetter::new(context.as_ref());

View file

@ -93,6 +93,7 @@ impl Plugin for Gain {
create_egui_editor( create_egui_editor(
self.params.editor_state.clone(), self.params.editor_state.clone(),
(), (),
|_, _| {},
move |egui_ctx, setter, _state| { move |egui_ctx, setter, _state| {
egui::CentralPanel::default().show(egui_ctx, |ui| { egui::CentralPanel::default().show(egui_ctx, |ui| {
// NOTE: See `plugins/diopser/src/editor.rs` for an example using the generic UI widget // NOTE: See `plugins/diopser/src/editor.rs` for an example using the generic UI widget