Skip to main content

aura_core/
editor.rs

1//! Host-facing editor trait (windowing: `aura-baseview`; adapter: `aura-editor`).
2
3use std::sync::Arc;
4
5use aura_params::Params;
6
7/// Raw platform window handle for GUI parenting.
8#[derive(Clone, Copy, Debug)]
9pub enum RawWindowHandle {
10    AppKit(*mut std::ffi::c_void),
11    Win32(*mut std::ffi::c_void),
12    X11(u64),
13    /// Wayland / other — opaque for now.
14    Other(*mut std::ffi::c_void),
15}
16
17// SAFETY: handles are only used on the UI thread by format wrappers / editor.
18unsafe impl Send for RawWindowHandle {}
19
20/// Bridge editor ↔ host / param store (minimal).
21///
22/// Format wrappers will implement this; `aura-editor` adapter calls it.
23pub trait EditorBridge: Send + Sync {
24    fn begin_edit(&self, id: u32);
25    fn set_param(&self, id: u32, normalized: f64);
26    fn end_edit(&self, id: u32);
27    fn get_param(&self, id: u32) -> f64;
28    fn get_param_plain(&self, id: u32) -> f64;
29    fn request_resize(&self, w: u32, h: u32) -> bool {
30        let _ = (w, h);
31        false
32    }
33
34    /// Content-scale hint the editor discovered itself — e.g. the real OS DPI
35    /// on Windows when the host never called the format's `set_scale` (Bitwig
36    /// on Win32 sizes the embed frame from OS DPI, not `clap gui.set_scale`).
37    ///
38    /// Wrappers store it in the same cell `get_size` reads, so the reported
39    /// host frame becomes `logical × hint` and matches the rendered child.
40    /// Default: no-op (host owns the scale).
41    fn set_scale_hint(&self, scale: f64) {
42        let _ = scale;
43    }
44}
45
46/// Context passed to [`Editor::open`].
47#[derive(Clone)]
48pub struct PluginContext {
49    pub params: Arc<dyn Params>,
50    pub bridge: Option<Arc<dyn EditorBridge>>,
51    pub sample_rate: f64,
52}
53
54impl PluginContext {
55    #[must_use]
56    pub fn new(params: Arc<dyn Params>) -> Self {
57        Self {
58            params,
59            bridge: None,
60            sample_rate: 44_100.0,
61        }
62    }
63
64    #[must_use]
65    pub fn with_bridge(mut self, bridge: Arc<dyn EditorBridge>) -> Self {
66        self.bridge = Some(bridge);
67        self
68    }
69
70    #[must_use]
71    pub fn with_sample_rate(mut self, sr: f64) -> Self {
72        self.sample_rate = sr;
73        self
74    }
75}
76
77/// Plugin GUI editor — host open/close/idle/resize.
78///
79/// Concrete UIs: window stack in `aura-baseview`; host adapter in `aura-editor`.
80pub trait Editor: Send {
81    fn size(&self) -> (u32, u32);
82
83    fn open(&mut self, parent: RawWindowHandle, context: PluginContext);
84
85    fn close(&mut self);
86
87    /// Show the editor window (CLAP `gui.show`). Default: no-op.
88    fn show(&mut self) {}
89
90    /// Hide the editor window without destroying it (CLAP `gui.hide`).
91    fn hide(&mut self) {}
92
93    fn idle(&mut self) {}
94
95    fn set_size(&mut self, _width: u32, _height: u32) -> bool {
96        false
97    }
98
99    fn can_resize(&self) -> bool {
100        false
101    }
102
103    fn min_size(&self) -> (u32, u32) {
104        (1, 1)
105    }
106
107    fn max_size(&self) -> (u32, u32) {
108        (u32::MAX, u32::MAX)
109    }
110
111    fn set_scale(&mut self, _scale: f64) {}
112
113    fn state_changed(&mut self) {}
114
115    /// Native widget handle for hosts that require it (e.g. LV2 UI).
116    ///
117    /// `None` means the toolkit did not expose a handle, or the editor is
118    /// closed. The pointer is only valid while the editor is open.
119    fn native_handle(&self) -> Option<RawWindowHandle> {
120        None
121    }
122}
123
124/// Box a concrete editor into `Box<dyn Editor>`.
125pub trait IntoEditor {
126    fn into_editor(self) -> Box<dyn Editor>;
127}
128
129impl<E: Editor + 'static> IntoEditor for E {
130    fn into_editor(self) -> Box<dyn Editor> {
131        Box::new(self)
132    }
133}