From aa3e6282b72383e069ad0baad5d7ad0a79f75134 Mon Sep 17 00:00:00 2001 From: Robbert van der Helm Date: Wed, 16 Mar 2022 01:29:08 +0100 Subject: [PATCH] Use the new iced generic UI for Crisp --- plugins/crisp/Cargo.toml | 2 +- plugins/crisp/src/editor.rs | 90 ++++++++++++++++++++++++++++++++----- plugins/crisp/src/lib.rs | 4 +- 3 files changed, 81 insertions(+), 15 deletions(-) diff --git a/plugins/crisp/Cargo.toml b/plugins/crisp/Cargo.toml index 0d61fccd..87bc88d2 100644 --- a/plugins/crisp/Cargo.toml +++ b/plugins/crisp/Cargo.toml @@ -10,4 +10,4 @@ crate-type = ["cdylib"] [dependencies] nih_plug = { path = "../../", features = ["assert_process_allocs"] } -nih_plug_egui = { path = "../../nih_plug_egui" } +nih_plug_iced = { path = "../../nih_plug_iced" } diff --git a/plugins/crisp/src/editor.rs b/plugins/crisp/src/editor.rs index f1ae3c7a..740b6590 100644 --- a/plugins/crisp/src/editor.rs +++ b/plugins/crisp/src/editor.rs @@ -14,26 +14,92 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -use nih_plug::prelude::Editor; -use nih_plug_egui::widgets::generic_ui; -use nih_plug_egui::{create_egui_editor, egui, EguiState}; +use nih_plug::prelude::{Editor, GuiContext}; +use nih_plug_iced::widgets as nih_widgets; +use nih_plug_iced::widgets::generic_ui::GenericUi; +use nih_plug_iced::*; use std::pin::Pin; use std::sync::Arc; use crate::CrispParams; // Makes sense to also define this here, makes it a bit easier to keep track of -pub fn default_state() -> Arc { - EguiState::from_size(250, 350) +pub(crate) fn default_state() -> Arc { + // We have a scroll bar, so we should proudly show off that we have a scroll bar + IcedState::from_size(370, 330) } -pub fn create( +pub(crate) fn create( params: Pin>, - editor_state: Arc, + editor_state: Arc, ) -> Option> { - create_egui_editor(editor_state, (), move |egui_ctx, setter, _state| { - egui::CentralPanel::default().show(egui_ctx, |ui| { - generic_ui::create(ui, params.as_ref(), setter, generic_ui::GenericSlider); - }); - }) + create_iced_editor::(editor_state, params) +} + +struct CrispEditor { + params: Pin>, + context: Arc, + + generic_ui_state: nih_widgets::generic_ui::State, +} + +#[derive(Debug, Clone, Copy)] +enum Message { + /// Update a parameter's value. + ParamUpdate(nih_widgets::ParamMessage), +} + +impl IcedEditor for CrispEditor { + type Executor = executor::Default; + type Message = Message; + type InitializationFlags = Pin>; + + fn new( + params: Self::InitializationFlags, + context: Arc, + ) -> (Self, Command) { + let editor = CrispEditor { + params, + context, + + generic_ui_state: Default::default(), + }; + + (editor, Command::none()) + } + + fn context(&self) -> &dyn GuiContext { + self.context.as_ref() + } + + fn update( + &mut self, + _window: &mut WindowQueue, + message: Self::Message, + ) -> Command { + match message { + Message::ParamUpdate(message) => self.handle_param_message(message), + } + + Command::none() + } + + fn view(&mut self) -> Element<'_, Self::Message> { + GenericUi::new( + &mut self.generic_ui_state, + self.params.as_ref(), + self.context.as_ref(), + ) + .pad_scrollbar() + .map(Message::ParamUpdate) + } + + fn background_color(&self) -> nih_plug_iced::Color { + nih_plug_iced::Color { + r: 0.98, + g: 0.98, + b: 0.98, + a: 1.0, + } + } } diff --git a/plugins/crisp/src/lib.rs b/plugins/crisp/src/lib.rs index d7db7bd8..f8b99eb8 100644 --- a/plugins/crisp/src/lib.rs +++ b/plugins/crisp/src/lib.rs @@ -18,7 +18,7 @@ extern crate nih_plug; use nih_plug::prelude::*; -use nih_plug_egui::EguiState; +use nih_plug_iced::IcedState; use pcg::Pcg32iState; use std::pin::Pin; use std::sync::Arc; @@ -45,7 +45,7 @@ const MAX_FILTER_FREQUENCY: f32 = 22_000.0; /// since this effect just turns into literal noise at high frequencies. struct Crisp { params: Pin>, - editor_state: Arc, + editor_state: Arc, /// Needed for computing the filter coefficients. sample_rate: f32,