Crate nih_plug_iced

source ·
Expand description

iced editor support for NIH plug.

This integration requires you to pass your parameters to your editor object through the IcedEditor::InitializationFlags, and to add a message type for your editor to handle parmater updates. This is a minimal example:

use nih_plug_iced::*;

pub(crate) fn default_state() -> Arc<IcedState> {
    IcedState::from_size(200, 150)
}

pub(crate) fn create(
    params: Arc<FooParams>,
    editor_state: Arc<IcedState>,
) -> Option<Box<dyn Editor>> {
    create_iced_editor::<Foo>(editor_state, params)
}

struct FooEditor {
    params: Arc<FooParams>,
    context: Arc<dyn GuiContext>,

    foo_slider_state: nih_widgets::param_slider::State,
}

#[derive(Debug, Clone, Copy)]
enum Message {
    /// Update a parameter's value.
    ParamUpdate(nih_widgets::ParamMessage),
}

impl IcedEditor for FooEditor {
    type Executor = executor::Default;
    type Message = Message;
    type InitializationFlags = Arc<FooParams>;

    fn new(
        params: Self::InitializationFlags,
        context: Arc<dyn GuiContext>,
    ) -> (Self, Command<Self::Message>) {
        let editor = FooEditor {
            params,
            context,

            foo_slider_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<Self::Message> {
        match message {
            Message::ParamUpdate(message) => self.handle_param_message(message),
        }

        Command::none()
    }

    fn view(&mut self) -> Element<'_, Self::Message> {
        Column::new()
            .align_items(Alignment::Center)
            .push(
                Text::new("Foo")
                    .height(20.into())
                    .width(Length::Fill)
                    .horizontal_alignment(alignment::Horizontal::Center)
                    .vertical_alignment(alignment::Vertical::Center),
            )
            .push(
                nih_widgets::ParamSlider::new(
                    &mut self.foo_slider_state,
                    &self.params.foo,
                    self.context.as_ref(),
                )
                .map(Message::ParamUpdate),
            )
            .into()
    }
}

Re-exports

  • pub use iced_baseview::*;

Modules

  • Binary assets for use with nih_plug_iced.
  • Widgets and utilities for making widgets to integrate iced with NIH-plug.

Structs

Traits

  • A plugin editor using iced. This wraps around [Application] with the only change being that the usual new() function now additionally takes a Arc<dyn GuiContext> that the editor can store to interact with the parameters. The editor should have a Arc<impl Params> as part of their InitializationFlags so it can read the current parameter values. See [Application] for more information.

Functions

  • Create an Editor instance using iced. The rough idea is that you implement IcedEditor, which is roughly analogous to iced’s regular [Application] trait except that it receives the GuiContext alongside its initialization flags so it can update the parameter values. The IcedState passed to this function contains the GUI’s intitial size, and this is kept in sync whenever the GUI gets resized. You can also use this to know if the GUI is open, so you can avoid performing potentially expensive calculations while the GUI is not open. If you want this size to be persisted when restoring a plugin instance, then you can store it in a #[persist = "key"] field on your parameters struct.