2022-02-06 10:22:33 +11:00
|
|
|
//! [egui](https://github.com/emilk/egui) editor support for NIH plug.
|
|
|
|
//!
|
2022-02-09 09:25:56 +11:00
|
|
|
//! TODO: Proper usage example, for now check out the gain_gui example
|
2022-02-06 10:22:33 +11:00
|
|
|
|
2022-02-08 07:58:59 +11:00
|
|
|
use baseview::gl::GlConfig;
|
2022-02-06 10:22:33 +11:00
|
|
|
use baseview::{Size, WindowHandle, WindowOpenOptions, WindowScalePolicy};
|
2022-02-09 06:38:10 +11:00
|
|
|
use crossbeam::atomic::AtomicCell;
|
2022-02-06 10:22:33 +11:00
|
|
|
use egui::CtxRef;
|
2022-02-08 07:58:59 +11:00
|
|
|
use egui_baseview::EguiWindow;
|
2022-02-07 03:12:57 +11:00
|
|
|
use nih_plug::{Editor, ParamSetter, ParentWindowHandle};
|
|
|
|
use parking_lot::RwLock;
|
2022-02-09 06:16:39 +11:00
|
|
|
use std::sync::atomic::{AtomicBool, Ordering};
|
2022-02-06 10:22:33 +11:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
|
|
/// Re-export for convenience.
|
2022-02-06 10:54:13 +11:00
|
|
|
pub use egui;
|
2022-02-06 10:22:33 +11:00
|
|
|
|
2022-02-09 09:25:56 +11:00
|
|
|
pub mod widgets;
|
|
|
|
|
2022-02-09 06:16:39 +11:00
|
|
|
/// Create an [Editor] instance using an [::egui] GUI. Using the user state parameter is optional,
|
|
|
|
/// but it can be useful for keeping track of some temporary GUI-only settings. See the `gui_gain`
|
|
|
|
/// example for more information on how to use this. The [EguiState] 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]` field on your parameters struct.
|
|
|
|
///
|
|
|
|
/// See [EguiState::from_size()].
|
2022-02-06 10:22:33 +11:00
|
|
|
//
|
|
|
|
// TODO: DPI scaling, this needs to be implemented on the framework level
|
2022-02-07 00:02:55 +11:00
|
|
|
pub fn create_egui_editor<T, U>(
|
2022-02-09 06:16:39 +11:00
|
|
|
egui_state: Arc<EguiState>,
|
|
|
|
user_state: T,
|
2022-02-07 03:12:57 +11:00
|
|
|
update: U,
|
2022-02-07 00:02:55 +11:00
|
|
|
) -> Option<Box<dyn Editor>>
|
2022-02-06 10:22:33 +11:00
|
|
|
where
|
2022-02-07 03:12:57 +11:00
|
|
|
T: 'static + Send + Sync,
|
|
|
|
U: Fn(&CtxRef, &ParamSetter, &mut T) + 'static + Send + Sync,
|
2022-02-06 10:22:33 +11:00
|
|
|
{
|
2022-02-07 03:12:57 +11:00
|
|
|
Some(Box::new(EguiEditor {
|
2022-02-09 06:16:39 +11:00
|
|
|
egui_state,
|
|
|
|
user_state: Arc::new(RwLock::new(user_state)),
|
2022-02-07 03:12:57 +11:00
|
|
|
update: Arc::new(update),
|
|
|
|
}))
|
2022-02-06 10:22:33 +11:00
|
|
|
}
|
|
|
|
|
2022-02-09 06:16:39 +11:00
|
|
|
// TODO: Once we add resizing, we may want to be able to remember the GUI size. In that case we need
|
|
|
|
// to make this serializable (only restoring the size of course) so it can be persisted.
|
|
|
|
pub struct EguiState {
|
|
|
|
size: AtomicCell<(u32, u32)>,
|
|
|
|
open: AtomicBool,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl EguiState {
|
|
|
|
/// Initialize the GUI's state. This is passed to [create_egui_editor()].
|
|
|
|
pub fn from_size(width: u32, height: u32) -> Arc<EguiState> {
|
|
|
|
Arc::new(EguiState {
|
|
|
|
size: AtomicCell::new((width, height)),
|
|
|
|
open: AtomicBool::new(false),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Return a `(width, height)` pair for the current size of the GUI.
|
|
|
|
pub fn size(&self) -> (u32, u32) {
|
|
|
|
self.size.load()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Whether the GUI is currently visible.
|
2022-02-09 06:33:08 +11:00
|
|
|
// Called `is_open()` instead of `open()` to avoid the ambiguity.
|
|
|
|
pub fn is_open(&self) -> bool {
|
2022-02-09 06:16:39 +11:00
|
|
|
self.open.load(Ordering::Acquire)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-06 10:22:33 +11:00
|
|
|
/// An [Editor] implementation that calls an egui draw loop.
|
2022-02-07 03:12:57 +11:00
|
|
|
struct EguiEditor<T> {
|
2022-02-09 06:16:39 +11:00
|
|
|
egui_state: Arc<EguiState>,
|
2022-02-07 03:12:57 +11:00
|
|
|
/// The plugin's state. This is kept in between editor openenings.
|
2022-02-09 06:16:39 +11:00
|
|
|
user_state: Arc<RwLock<T>>,
|
2022-02-07 03:12:57 +11:00
|
|
|
update: Arc<dyn Fn(&CtxRef, &ParamSetter, &mut T) + 'static + Send + Sync>,
|
2022-02-06 10:22:33 +11:00
|
|
|
}
|
|
|
|
|
2022-02-07 03:12:57 +11:00
|
|
|
impl<T> Editor for EguiEditor<T>
|
|
|
|
where
|
|
|
|
T: 'static + Send + Sync,
|
|
|
|
{
|
|
|
|
fn spawn(
|
|
|
|
&self,
|
|
|
|
parent: ParentWindowHandle,
|
|
|
|
context: Arc<dyn nih_plug::GuiContext>,
|
|
|
|
) -> Box<dyn std::any::Any> {
|
|
|
|
let update = self.update.clone();
|
2022-02-09 06:16:39 +11:00
|
|
|
let state = self.user_state.clone();
|
2022-02-07 03:12:57 +11:00
|
|
|
|
2022-02-09 06:16:39 +11:00
|
|
|
let (width, height) = self.egui_state.size();
|
2022-02-07 03:12:57 +11:00
|
|
|
let window = EguiWindow::open_parented(
|
|
|
|
&parent,
|
2022-02-08 07:58:59 +11:00
|
|
|
WindowOpenOptions {
|
|
|
|
title: String::from("egui window"),
|
|
|
|
size: Size::new(width as f64, height as f64),
|
|
|
|
// TODO: What happens when we use the system scale factor here? I'd assume this
|
|
|
|
// would work everywhere, even if the window may be tiny in some cases.
|
|
|
|
scale: WindowScalePolicy::ScaleFactor(1.0),
|
|
|
|
gl_config: Some(GlConfig {
|
2022-02-07 03:12:57 +11:00
|
|
|
version: (3, 2),
|
|
|
|
red_bits: 8,
|
|
|
|
blue_bits: 8,
|
|
|
|
green_bits: 8,
|
2022-02-08 07:58:59 +11:00
|
|
|
alpha_bits: 8,
|
2022-02-07 03:12:57 +11:00
|
|
|
depth_bits: 24,
|
|
|
|
stencil_bits: 8,
|
|
|
|
samples: None,
|
|
|
|
srgb: true,
|
|
|
|
double_buffer: true,
|
|
|
|
vsync: true,
|
|
|
|
..Default::default()
|
2022-02-08 07:58:59 +11:00
|
|
|
}),
|
2022-02-07 03:12:57 +11:00
|
|
|
},
|
|
|
|
state,
|
|
|
|
|_, _, _| {},
|
|
|
|
move |egui_ctx, queue, state| {
|
|
|
|
let setter = ParamSetter::new(context.as_ref());
|
|
|
|
|
|
|
|
// For now, just always redraw. Most plugin GUIs have meters, and those almost always
|
|
|
|
// need a redraw. Later we can try to be a bit more sophisticated about this. Without
|
|
|
|
// this we would also have a blank GUI when it gets first opened because most DAWs open
|
|
|
|
// their GUI while the window is still unmapped.
|
|
|
|
// TODO: Are there other useful parts of this queue we could pass to thep lugin?
|
|
|
|
queue.request_repaint();
|
|
|
|
(update)(egui_ctx, &setter, &mut state.write());
|
|
|
|
},
|
2022-02-08 07:58:59 +11:00
|
|
|
)
|
|
|
|
.expect("We provided an OpenGL config, did we not?");
|
2022-02-07 03:12:57 +11:00
|
|
|
|
2022-02-09 06:16:39 +11:00
|
|
|
self.egui_state.open.store(true, Ordering::Release);
|
|
|
|
Box::new(EguiEditorHandle {
|
|
|
|
egui_state: self.egui_state.clone(),
|
|
|
|
window,
|
|
|
|
})
|
2022-02-07 03:12:57 +11:00
|
|
|
}
|
2022-02-06 10:22:33 +11:00
|
|
|
|
|
|
|
fn size(&self) -> (u32, u32) {
|
2022-02-09 06:16:39 +11:00
|
|
|
self.egui_state.size()
|
2022-02-06 10:22:33 +11:00
|
|
|
}
|
|
|
|
}
|
2022-02-07 01:08:57 +11:00
|
|
|
|
2022-02-07 03:12:57 +11:00
|
|
|
/// The window handle used for [EguiEditor].
|
|
|
|
struct EguiEditorHandle {
|
2022-02-09 06:16:39 +11:00
|
|
|
egui_state: Arc<EguiState>,
|
2022-02-07 03:12:57 +11:00
|
|
|
window: WindowHandle,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The window handle enum stored within 'WindowHandle' contains raw pointers. Is there a way around
|
|
|
|
/// having this requirement?
|
|
|
|
unsafe impl Send for EguiEditorHandle {}
|
|
|
|
unsafe impl Sync for EguiEditorHandle {}
|
|
|
|
|
|
|
|
impl Drop for EguiEditorHandle {
|
2022-02-07 01:08:57 +11:00
|
|
|
fn drop(&mut self) {
|
2022-02-09 06:16:39 +11:00
|
|
|
self.egui_state.open.store(false, Ordering::Release);
|
2022-02-07 01:08:57 +11:00
|
|
|
// XXX: This should automatically happen when the handle gets dropped, but apparently not
|
|
|
|
self.window.close();
|
|
|
|
}
|
|
|
|
}
|