From e3a923ff3b49817d94a371b0d0246751a599d104 Mon Sep 17 00:00:00 2001 From: Robbert van der Helm Date: Tue, 28 Feb 2023 18:50:15 +0100 Subject: [PATCH] Avoid restoring window sizes with ViziaState Unless specified otherwise. This avoids some bugs where old, now incorrect sizes are being recalled. --- BREAKING_CHANGES.md | 7 ++++++ nih_plug_vizia/src/lib.rs | 24 ++++++++++++++++--- plugins/crisp/src/editor.rs | 2 +- plugins/diopser/src/editor.rs | 2 +- plugins/examples/gain_gui_vizia/src/editor.rs | 2 +- plugins/spectral_compressor/src/editor.rs | 2 +- 6 files changed, 32 insertions(+), 7 deletions(-) diff --git a/BREAKING_CHANGES.md b/BREAKING_CHANGES.md index e2962b43..60b575d5 100644 --- a/BREAKING_CHANGES.md +++ b/BREAKING_CHANGES.md @@ -6,6 +6,13 @@ new and what's changed, this document lists all breaking changes in reverse chronological order. If a new feature did not require any changes to existing code then it will not be listed here. +## [2023-02-28] + +- `ViziaState::from_size()` now takes a third boolean argument to control + whether the window's size is persisted or not. This avoids a potential bug + where an old window size is recalled after the plugin's GUI's size has changed + in an update to the plugin. + ## [2023-02-20] - The way audio IO layouts are configured has changed completely to align better diff --git a/nih_plug_vizia/src/lib.rs b/nih_plug_vizia/src/lib.rs index 54cc81c3..6dd94bcf 100644 --- a/nih_plug_vizia/src/lib.rs +++ b/nih_plug_vizia/src/lib.rs @@ -95,11 +95,17 @@ pub struct ViziaState { /// Whether the editor's window is currently open. #[serde(skip)] open: AtomicBool, + + /// Whether the size should be saved. If the window's size is always scaled uniformly, then this + /// is not needed and can only result in problems. + should_save_size: bool, } impl<'a> PersistentField<'a, ViziaState> for Arc { fn set(&self, new_value: ViziaState) { - self.size.store(new_value.size.load()); + if self.should_save_size { + self.size.store(new_value.size.load()); + } self.scale_factor.store(new_value.scale_factor.load()); } @@ -114,22 +120,34 @@ impl<'a> PersistentField<'a, ViziaState> for Arc { impl ViziaState { /// Initialize the GUI's state. This value can be passed to [`create_vizia_editor()`]. The /// window size is in logical pixels, so before it is multiplied by the DPI scaling factor. - pub fn from_size(width: u32, height: u32) -> Arc { + /// + /// Setting `should_save_size` to `false` may be useful when the size is supposed to be fixed + /// and only the scaling factor changes. This allows the object to be persisted in a `Params` + /// object without accidentally restoring old sizes after the window's logical size has changed + /// in a plugin update. + pub fn from_size(width: u32, height: u32, should_save_size: bool) -> Arc { Arc::new(ViziaState { size: AtomicCell::new((width, height)), scale_factor: AtomicCell::new(1.0), open: AtomicBool::new(false), + should_save_size, }) } /// The same as [`from_size()`][Self::from_size()], but with a separate initial scale factor. /// This scale factor gets applied on top of any HiDPI scaling, and it can be modified at /// runtime by changing `cx.user_scale_factor`. - pub fn from_size_with_scale(width: u32, height: u32, scale_factor: f64) -> Arc { + pub fn from_size_with_scale( + width: u32, + height: u32, + scale_factor: f64, + should_save_size: bool, + ) -> Arc { Arc::new(ViziaState { size: AtomicCell::new((width, height)), scale_factor: AtomicCell::new(scale_factor), open: AtomicBool::new(false), + should_save_size, }) } diff --git a/plugins/crisp/src/editor.rs b/plugins/crisp/src/editor.rs index ca793dfd..19fa39c9 100644 --- a/plugins/crisp/src/editor.rs +++ b/plugins/crisp/src/editor.rs @@ -31,7 +31,7 @@ impl Model for Data {} // Makes sense to also define this here, makes it a bit easier to keep track of pub(crate) fn default_state() -> Arc { - ViziaState::from_size(400, 390) + ViziaState::from_size(400, 390, false) } pub(crate) fn create( diff --git a/plugins/diopser/src/editor.rs b/plugins/diopser/src/editor.rs index 3edf844b..f47cc33c 100644 --- a/plugins/diopser/src/editor.rs +++ b/plugins/diopser/src/editor.rs @@ -60,7 +60,7 @@ impl Model for Data {} // Makes sense to also define this here, makes it a bit easier to keep track of pub(crate) fn default_state() -> Arc { - ViziaState::from_size(EDITOR_WIDTH, EDITOR_HEIGHT) + ViziaState::from_size(EDITOR_WIDTH, EDITOR_HEIGHT, false) } pub(crate) fn create(editor_data: Data, editor_state: Arc) -> Option> { diff --git a/plugins/examples/gain_gui_vizia/src/editor.rs b/plugins/examples/gain_gui_vizia/src/editor.rs index 69049911..3bbf7c74 100644 --- a/plugins/examples/gain_gui_vizia/src/editor.rs +++ b/plugins/examples/gain_gui_vizia/src/editor.rs @@ -19,7 +19,7 @@ impl Model for Data {} // Makes sense to also define this here, makes it a bit easier to keep track of pub(crate) fn default_state() -> Arc { - ViziaState::from_size(200, 150) + ViziaState::from_size(200, 150, false) } pub(crate) fn create( diff --git a/plugins/spectral_compressor/src/editor.rs b/plugins/spectral_compressor/src/editor.rs index 61e2cf4b..c312ac80 100644 --- a/plugins/spectral_compressor/src/editor.rs +++ b/plugins/spectral_compressor/src/editor.rs @@ -36,7 +36,7 @@ impl Model for Data {} // Makes sense to also define this here, makes it a bit easier to keep track of pub(crate) fn default_state() -> Arc { - ViziaState::from_size(680, 535) + ViziaState::from_size(680, 535, false) } pub(crate) fn create(