78 lines
2.8 KiB
Rust
78 lines
2.8 KiB
Rust
use std::sync::{Arc, atomic::Ordering};
|
|
use baseview::{Window, WindowOpenOptions, WindowScalePolicy, gl::GlConfig};
|
|
use crossbeam::atomic::AtomicCell;
|
|
use nih_plug::{editor::Editor, plugin::Plugin};
|
|
use crate::{AirFreshener, editor::EditorHandle, parameters::PluginParams, gui::PluginGui};
|
|
|
|
pub struct EditorWindow {
|
|
params: Arc<PluginParams>,
|
|
scaling_factor: Arc<AtomicCell<Option<f32>>>,
|
|
}
|
|
|
|
impl EditorWindow {
|
|
pub const WINDOW_SIZE: (u32, u32) = (230, 320);
|
|
pub fn new(params: Arc<PluginParams>) -> Self {
|
|
Self {
|
|
params,
|
|
#[cfg(target_os = "macos")]
|
|
scaling_factor: Arc::ne(AtomicCell::new(None)),
|
|
#[cfg(not(target_os = "macos"))]
|
|
scaling_factor: Arc::new(AtomicCell::new(Some(1.0))),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl Editor for EditorWindow {
|
|
fn spawn(
|
|
&self,
|
|
parent: nih_plug::prelude::ParentWindowHandle,
|
|
context: Arc<dyn nih_plug::prelude::GuiContext>,
|
|
) -> Box<dyn std::any::Any + Send> {
|
|
let (unscaled_width, unscaled_height) = self.params.editor_state.size();
|
|
let scaling_factor = self.scaling_factor.load();
|
|
let move_scaling_factor = self.scaling_factor.clone();
|
|
let gui_context = context.clone();
|
|
let params = self.params.clone();
|
|
let window = Window::open_parented(&parent, WindowOpenOptions {
|
|
title: AirFreshener::NAME.to_owned(),
|
|
size: baseview::Size { width: unscaled_width as f64, height: unscaled_height as f64 },
|
|
scale: scaling_factor.map(|factor| WindowScalePolicy::ScaleFactor(factor as f64)).unwrap_or(WindowScalePolicy::SystemScaleFactor),
|
|
gl_config: Some(GlConfig {
|
|
version: (3, 2),
|
|
red_bits: 8,
|
|
green_bits: 8,
|
|
blue_bits: 8,
|
|
alpha_bits: 8,
|
|
samples: None,
|
|
srgb: true,
|
|
double_buffer: true,
|
|
vsync: false,
|
|
..Default::default()
|
|
})
|
|
}, move |window: &mut baseview::Window<'_>| -> PluginGui {
|
|
PluginGui::new(window, gui_context, params, move_scaling_factor)
|
|
});
|
|
self.params.editor_state.open.store(true, Ordering::Release);
|
|
Box::new(EditorHandle {
|
|
state: self.params.editor_state.clone(),
|
|
window
|
|
})
|
|
}
|
|
|
|
fn size(&self) -> (u32, u32) {
|
|
self.params.editor_state.size()
|
|
}
|
|
|
|
fn set_scale_factor(&self, factor: f32) -> bool {
|
|
if self.params.editor_state.is_open() {
|
|
return false;
|
|
}
|
|
self.scaling_factor.store(Some(factor));
|
|
true
|
|
}
|
|
|
|
fn param_value_changed(&self, _id: &str, _normalized_value: f32) {}
|
|
fn param_modulation_changed(&self, _id: &str, _modulation_offset: f32) {}
|
|
fn param_values_changed(&self) {}
|
|
}
|