1
0
Fork 0
nih-plug/plugins/diopser/src/editor.rs
Robbert van der Helm 083885a40c Rework Params trait API with Arc instead of Pin
This is a breaking change requiring a small change to plugin
implementations.

The reason why `Pin<&dyn Params>` was used was more as a hint to
indicate that the object must last for the plugin's lifetime, but `Pin`
doesn't enforce that. It also makes the APIs a lot more awkward.
Requiring the use of `Arc` fixes the following problems:

- When storing the params object in the wrapper, the `ParamPtr`s are
  guaranteed to be stable.
- This makes it possible to access the `Params` object without acquiring
  a lock on the plugin, this is very important for implementing
  plugin-side preset management.
- It enforces immutability on the `Params` object.
- And of course the API is much nicer without a bunch of unsafe code to
  work around Pin's limitations.
2022-04-07 15:31:46 +02:00

74 lines
2.3 KiB
Rust

// Diopser: a phase rotation plugin
// Copyright (C) 2021-2022 Robbert van der Helm
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
use nih_plug::prelude::Editor;
use nih_plug_vizia::vizia::*;
use nih_plug_vizia::widgets::*;
use nih_plug_vizia::{assets, create_vizia_editor, ViziaState};
use std::sync::Arc;
use crate::DiopserParams;
/// VIZIA uses points instead of pixels for text
const POINT_SCALE: f32 = 0.75;
#[derive(Lens)]
struct Data {
params: Arc<DiopserParams>,
}
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> {
ViziaState::from_size(380, 270)
}
pub(crate) fn create(
params: Arc<DiopserParams>,
editor_state: Arc<ViziaState>,
) -> Option<Box<dyn Editor>> {
create_vizia_editor(editor_state, move |cx| {
Data {
params: params.clone(),
}
.build(cx);
ResizeHandle::new(cx);
VStack::new(cx, |cx| {
Label::new(cx, "Diopser")
.font(assets::NOTO_SANS_THIN)
.font_size(40.0 * POINT_SCALE)
.height(Pixels(50.0))
.child_top(Stretch(1.0))
.child_bottom(Pixels(0.0))
// Make this more or less align with the parameters column
.right(Percentage(12.0));
// See the Crisp generic UI for an example using a ScrollView
GenericUi::new(cx, Data::params)
.width(Percentage(100.0))
.height(Auto)
.child_top(Pixels(5.0));
})
.width(Percentage(100.0))
.row_between(Pixels(0.0))
.child_left(Stretch(1.0))
.child_right(Stretch(1.0));
})
}