1
0
Fork 0

Make Arc<IcedState> persistable

This commit is contained in:
Robbert van der Helm 2022-07-13 23:10:36 +02:00
parent ef1d56646a
commit b9c1a4e5e1
3 changed files with 24 additions and 2 deletions

1
Cargo.lock generated
View file

@ -2435,6 +2435,7 @@ dependencies = [
"iced_baseview", "iced_baseview",
"nih_plug", "nih_plug",
"nih_plug_assets", "nih_plug_assets",
"serde",
] ]
[[package]] [[package]]

View file

@ -62,3 +62,5 @@ crossbeam = "0.8"
# Upstream doesn't work with the current iced version, this branch also contains # Upstream doesn't work with the current iced version, this branch also contains
# additional features # additional features
iced_baseview = { git = "https://github.com/robbert-vdh/iced_baseview.git", branch = "feature/update-baseview", default_features = false } iced_baseview = { git = "https://github.com/robbert-vdh/iced_baseview.git", branch = "feature/update-baseview", default_features = false }
# To make the state persistable
serde = { version = "1.0", features = ["derive"] }

View file

@ -92,7 +92,9 @@
use baseview::{WindowOpenOptions, WindowScalePolicy}; use baseview::{WindowOpenOptions, WindowScalePolicy};
use crossbeam::atomic::AtomicCell; use crossbeam::atomic::AtomicCell;
use crossbeam::channel; use crossbeam::channel;
use nih_plug::param::internals::PersistentField;
use nih_plug::prelude::{Editor, GuiContext, ParentWindowHandle}; use nih_plug::prelude::{Editor, GuiContext, ParentWindowHandle};
use serde::{Deserialize, Serialize};
use std::fmt::Debug; use std::fmt::Debug;
use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc; use std::sync::Arc;
@ -229,13 +231,30 @@ pub trait IcedEditor: 'static + Send + Sync + Sized {
} }
} }
// TODO: Once we add resizing, we may want to be able to remember the GUI size. In that case we need /// State for an `nih_plug_iced` editor.
// to make this serializable (only restoring the size of course) so it can be persisted. #[derive(Serialize, Deserialize)]
pub struct IcedState { pub struct IcedState {
/// The window's size in logical pixels before applying `scale_factor`.
#[serde(with = "nih_plug::param::internals::serialize_atomic_cell")]
size: AtomicCell<(u32, u32)>, size: AtomicCell<(u32, u32)>,
/// Whether the editor's window is currently open.
#[serde(skip)]
open: AtomicBool, open: AtomicBool,
} }
impl<'a> PersistentField<'a, IcedState> for Arc<IcedState> {
fn set(&self, new_value: IcedState) {
self.size.store(new_value.size.load());
}
fn map<F, R>(&self, f: F) -> R
where
F: Fn(&IcedState) -> R,
{
f(self)
}
}
impl IcedState { impl IcedState {
/// Initialize the GUI's state. This value can be passed to [`create_iced_editor()`]. The window /// Initialize the GUI's state. This value can be passed to [`create_iced_editor()`]. The window
/// size is in logical pixels, so before it is multiplied by the DPI scaling factor. /// size is in logical pixels, so before it is multiplied by the DPI scaling factor.