Persist the editor states for all plugins

This commit is contained in:
Robbert van der Helm
2022-07-13 23:16:29 +02:00
parent b6720383a3
commit 0469bdf806
5 changed files with 44 additions and 19 deletions

View File

@@ -6,7 +6,6 @@ use std::sync::Arc;
/// This is mostly identical to the gain example, minus some fluff, and with a GUI.
struct Gain {
params: Arc<GainParams>,
editor_state: Arc<EguiState>,
/// Needed to normalize the peak meter's response based on the sample rate.
peak_meter_decay_weight: f32,
@@ -20,6 +19,11 @@ struct Gain {
#[derive(Params)]
struct GainParams {
/// The editor state, saved together with the parameter state so the custom scaling can be
/// restored.
#[persist = "editor-state"]
editor_state: Arc<EguiState>,
#[id = "gain"]
pub gain: FloatParam,
@@ -32,7 +36,6 @@ impl Default for Gain {
fn default() -> Self {
Self {
params: Arc::new(GainParams::default()),
editor_state: EguiState::from_size(300, 180),
peak_meter_decay_weight: 1.0,
peak_meter: Arc::new(AtomicF32::new(util::MINUS_INFINITY_DB)),
@@ -43,6 +46,8 @@ impl Default for Gain {
impl Default for GainParams {
fn default() -> Self {
Self {
editor_state: EguiState::from_size(300, 180),
gain: FloatParam::new(
"Gain",
0.0,
@@ -80,7 +85,7 @@ impl Plugin for Gain {
let params = self.params.clone();
let peak_meter = self.peak_meter.clone();
create_egui_editor(
self.editor_state.clone(),
self.params.editor_state.clone(),
(),
move |egui_ctx, setter, _state| {
egui::CentralPanel::default().show(egui_ctx, |ui| {
@@ -170,7 +175,7 @@ impl Plugin for Gain {
// To save resources, a plugin can (and probably should!) only perform expensive
// calculations that are only displayed on the GUI while the GUI is open
if self.editor_state.is_open() {
if self.params.editor_state.is_open() {
amplitude = (amplitude / num_samples as f32).abs();
let current_peak_meter = self.peak_meter.load(std::sync::atomic::Ordering::Relaxed);
let new_peak_meter = if amplitude > current_peak_meter {