truce_core/editor.rs
1use std::ops::Deref;
2use std::sync::Arc;
3
4use truce_params::Params;
5use truce_params::sample::Float;
6
7use crate::events::TransportInfo;
8
9/// A raw pointer wrapper that is `Send + Sync`.
10///
11/// Used to capture `*const Params` / host-handle pointers in
12/// `PluginContext` closures without the `ptr as usize` hack. The
13/// `Send`/`Sync` impls are unconditional in `T` - they have to be,
14/// because the wrapped types are typically `#[repr(C)]` host structs
15/// that are themselves `!Send + !Sync` by default. Construction is
16/// therefore `unsafe`: each call site must justify why cross-thread
17/// access to the pointed-to data is sound.
18///
19/// Justifications used in-tree:
20/// - **`P: Params`** - fields are atomic; concurrent reads from the
21/// GUI thread while the audio thread writes are safe by design.
22/// - **Format-host handles** (`clap_host`, `AEffect`, etc.) - used
23/// only from a single thread (UI), and the wrapping is purely for
24/// capturing in `Send + Sync` closures stored in `PluginContext`.
25///
26/// The pointed-to data must outlive the `SendPtr`. In the plugin
27/// context, the plugin instance (which owns the params) always
28/// outlives the editor.
29pub struct SendPtr<T>(*const T);
30
31impl<T> SendPtr<T> {
32 /// Wrap a raw pointer.
33 ///
34 /// # Safety
35 /// The caller must ensure that:
36 /// 1. The pointed-to data outlives every clone of this `SendPtr`.
37 /// 2. Cross-thread access to `*ptr` is sound - either because `T`
38 /// is `Sync`, because access is synchronized externally
39 /// (atomic fields, Mutex, single-thread-only access pattern),
40 /// or because the wrapper is only ever read on a thread where
41 /// `T: Sync` would hold.
42 pub unsafe fn new(ptr: *const T) -> Self {
43 Self(ptr)
44 }
45
46 /// Dereference the pointer.
47 ///
48 /// # Safety
49 /// The pointed-to data must still be alive.
50 #[must_use]
51 pub unsafe fn get(&self) -> &T {
52 unsafe { &*self.0 }
53 }
54
55 /// Get the raw pointer.
56 #[must_use]
57 pub fn as_ptr(&self) -> *const T {
58 self.0
59 }
60}
61
62impl<T> Clone for SendPtr<T> {
63 fn clone(&self) -> Self {
64 *self
65 }
66}
67
68impl<T> Copy for SendPtr<T> {}
69
70// SAFETY: justified at each `unsafe SendPtr::new(...)` call site.
71unsafe impl<T> Send for SendPtr<T> {}
72unsafe impl<T> Sync for SendPtr<T> {}
73
74/// Raw platform window handle for GUI parenting.
75#[derive(Clone, Copy, Debug)]
76pub enum RawWindowHandle {
77 AppKit(*mut std::ffi::c_void), // macOS NSView*
78 UiKit(*mut std::ffi::c_void), // iOS / iPadOS UIView*
79 Win32(*mut std::ffi::c_void), // HWND
80 X11(u64), // X11 Window ID
81}
82
83/// Plugin GUI editor.
84pub trait Editor: Send {
85 /// Initial window size in logical points.
86 ///
87 /// On a 2x Retina display, `(400, 300)` produces an 800x600 pixel window.
88 /// On a 1x display, it produces a 400x300 pixel window.
89 fn size(&self) -> (u32, u32);
90
91 /// Create the GUI as a child of the host-provided parent window.
92 fn open(&mut self, parent: RawWindowHandle, context: PluginContext);
93
94 /// Destroy the GUI.
95 fn close(&mut self);
96
97 /// Called ~60fps on the host's UI thread for repaint/animation.
98 fn idle(&mut self) {}
99
100 /// Host requests a resize. Return true to accept.
101 fn set_size(&mut self, _width: u32, _height: u32) -> bool {
102 false
103 }
104
105 /// Whether the plugin supports resizing.
106 fn can_resize(&self) -> bool {
107 false
108 }
109
110 /// Host notifies the editor of a new content scale factor.
111 ///
112 /// DPI/scale is a host→plugin concept: on VST3 Windows the host
113 /// delivers it via `IPlugViewContentScaleSupport`; on CLAP via
114 /// `clap_plugin_gui::set_scale`; on macOS/Cocoa `AppKit` handles
115 /// Retina backing automatically and hosts typically never call
116 /// this at all. Editors that need to size off-screen buffers in
117 /// physical pixels should react here, not by exposing a pull-style
118 /// `scale_factor()` method that format wrappers were tempted to
119 /// multiply `size()` by (which caused double-scaling on macOS VST3).
120 fn set_scale_factor(&mut self, _factor: f64) {}
121
122 /// Plugin state was restored (preset recall, undo, session load).
123 ///
124 /// Called after `load_state()` while the editor is open. Re-read any
125 /// cached state from the plugin. Parameter values are already updated
126 /// and will be picked up on the next render - this is only needed for
127 /// custom state stored outside the parameter system.
128 fn state_changed(&mut self) {}
129
130 /// Render a headless screenshot of the editor at its natural size.
131 ///
132 /// `params` is a type-erased default-state instance the caller
133 /// constructs from the plugin's `Params` type. Backends use it to
134 /// build a synthetic `PluginContext` / render context so the
135 /// screenshot reflects parameter defaults without needing a live
136 /// host.
137 ///
138 /// Returns `(rgba_pixels, physical_width, physical_height)` - RGBA8
139 /// row-major, ready to feed into `truce_test::assert_screenshot_pixels`.
140 /// Default impl returns `None`; backends that support headless
141 /// capture (built-in widgets, egui, iced, slint) override.
142 ///
143 /// Used by `truce_test::assert_screenshot::<Plugin>(...)` for one-line
144 /// snapshot regression tests. Editors backed by frameworks that
145 /// don't expose a headless render path (e.g. raw-window-handle
146 /// users wiring their own Metal/OpenGL) keep the default `None`.
147 fn screenshot(&mut self, params: Arc<dyn truce_params::Params>) -> Option<(Vec<u8>, u32, u32)> {
148 let _ = params;
149 None
150 }
151}
152
153/// Fluent terminal for `editor()` impls: box any concrete editor into
154/// the `Box<dyn Editor>` the trait returns, dropping the `Box::new(…)`
155/// wrapper.
156///
157/// ```ignore
158/// fn editor(&self) -> Box<dyn Editor> {
159/// EguiEditor::new(self.params.clone(), (W, H), ui)
160/// .with_visuals(theme)
161/// .into_editor()
162/// }
163/// ```
164///
165/// Implemented for every [`Editor`] via a blanket impl and re-exported
166/// from every `truce::prelude*`, so it's in scope without an extra
167/// import - egui / iced / slint / hand-rolled editors all use it.
168/// Layout-only plugins use `truce_gui::IntoLayoutEditor` instead (its
169/// `into_editor` takes `&Arc<Params>` and picks the built-in renderer).
170pub trait IntoEditor {
171 /// Box this editor into a `Box<dyn Editor>`.
172 fn into_editor(self) -> Box<dyn Editor>;
173}
174
175impl<E: Editor + 'static> IntoEditor for E {
176 fn into_editor(self) -> Box<dyn Editor> {
177 Box::new(self)
178 }
179}
180
181/// Bridge between the editor and the host / plugin. Format wrappers
182/// (CLAP / VST3 / VST2 / AU / AAX / LV2) implement this trait - or
183/// build a [`ClosureBridge`] from per-method closures - and pass an
184/// `Arc<dyn EditorBridge>` to the editor through [`PluginContext`].
185///
186/// Editors call into the bridge for everything they can't do
187/// directly: starting / ending an automation gesture, reading or
188/// writing parameters in normalized or plain form, requesting a
189/// window resize, exchanging custom state, sampling the host's
190/// transport. Implementations carry whatever per-format pointers
191/// the work needs (`clap_host*`, `AEffect*`, an `Arc<P>` for the
192/// param store, etc.).
193///
194/// `Send + Sync` is required so editors can clone the
195/// `Arc<dyn EditorBridge>` and hand it to UI worker threads or
196/// background animation timers without forcing every implementor to
197/// rederive thread-safety bounds.
198pub trait EditorBridge: Send + Sync {
199 /// Start an automation gesture for `id`. Hosts that show "touched"
200 /// state in the automation lane use this to render the
201 /// in-progress edit.
202 fn begin_edit(&self, id: u32);
203 /// Set parameter `id` to `normalized` (clamped to `0.0..=1.0`).
204 /// Format wrappers usually plumb this through both the plugin's
205 /// own param store and the host's automation channel.
206 fn set_param(&self, id: u32, normalized: f64);
207 /// End the automation gesture started by [`Self::begin_edit`].
208 fn end_edit(&self, id: u32);
209 /// Ask the host to resize the editor window to `(w, h)` logical
210 /// points. Returns `true` if the host accepted the request.
211 fn request_resize(&self, w: u32, h: u32) -> bool;
212 /// Read the parameter's current normalized value from the plugin
213 /// (host→GUI sync path).
214 fn get_param(&self, id: u32) -> f64;
215 /// Read the parameter's current plain (denormalized) value.
216 fn get_param_plain(&self, id: u32) -> f64;
217 /// Format the parameter's current value as a display string,
218 /// applying the plugin's `format_value` impl + unit suffix.
219 fn format_param(&self, id: u32) -> String;
220 /// Format into a caller-provided buffer instead of returning a
221 /// fresh `String`. The default impl calls
222 /// [`Self::format_param`] and copies, so the *bridge-internal*
223 /// allocation still happens; the win for the caller is that the
224 /// `out` buffer's capacity is reused across calls (e.g.
225 /// `ParamCache::sync` polls one label per changed param per
226 /// frame and would otherwise drop+reallocate the cached
227 /// `String` slot every time). Bridges that produce the formatted
228 /// string from raw value bytes can override to drop the
229 /// internal allocation too.
230 fn format_param_into(&self, id: u32, out: &mut String) {
231 out.clear();
232 out.push_str(&self.format_param(id));
233 }
234 /// Read a meter value (0.0–1.0) by meter ID. Returns 0.0 if the
235 /// meter ID isn't registered.
236 fn get_meter(&self, id: u32) -> f32;
237 /// Read the plugin's custom state (everything outside the
238 /// parameter system). Returns an empty `Vec` when the plugin has
239 /// no custom state.
240 fn get_state(&self) -> Vec<u8>;
241 /// Write custom state back to the plugin (calls `load_state()`).
242 fn set_state(&self, data: Vec<u8>);
243 /// Most-recently-reported host transport state, or `None` if the
244 /// host does not expose transport to plugin editors or the plugin
245 /// has not yet received a process block.
246 ///
247 /// Format wrappers populate a shared [`TransportSlot`](crate::TransportSlot)
248 /// from their process callback; this method reads from it.
249 fn transport(&self) -> Option<TransportInfo>;
250}
251
252/// Adapter that implements [`EditorBridge`] over per-method closures.
253///
254/// Format wrappers that prefer to compose state inline via closures
255/// construct one of these and wrap it in an `Arc<dyn EditorBridge>`.
256/// Wrappers that already have a typed host-pointer struct should
257/// `impl EditorBridge` for that struct directly and skip this
258/// adapter; one less layer of indirection per call.
259pub struct ClosureBridge {
260 pub begin_edit: Box<dyn Fn(u32) + Send + Sync>,
261 pub set_param: Box<dyn Fn(u32, f64) + Send + Sync>,
262 pub end_edit: Box<dyn Fn(u32) + Send + Sync>,
263 pub request_resize: Box<dyn Fn(u32, u32) -> bool + Send + Sync>,
264 pub get_param: Box<dyn Fn(u32) -> f64 + Send + Sync>,
265 pub get_param_plain: Box<dyn Fn(u32) -> f64 + Send + Sync>,
266 pub format_param: Box<dyn Fn(u32) -> String + Send + Sync>,
267 pub get_meter: Box<dyn Fn(u32) -> f32 + Send + Sync>,
268 pub get_state: Box<dyn Fn() -> Vec<u8> + Send + Sync>,
269 pub set_state: Box<dyn Fn(Vec<u8>) + Send + Sync>,
270 pub transport: Box<dyn Fn() -> Option<TransportInfo> + Send + Sync>,
271}
272
273impl EditorBridge for ClosureBridge {
274 fn begin_edit(&self, id: u32) {
275 (self.begin_edit)(id);
276 }
277 fn set_param(&self, id: u32, normalized: f64) {
278 (self.set_param)(id, normalized);
279 }
280 fn end_edit(&self, id: u32) {
281 (self.end_edit)(id);
282 }
283 fn request_resize(&self, w: u32, h: u32) -> bool {
284 (self.request_resize)(w, h)
285 }
286 fn get_param(&self, id: u32) -> f64 {
287 (self.get_param)(id)
288 }
289 fn get_param_plain(&self, id: u32) -> f64 {
290 (self.get_param_plain)(id)
291 }
292 fn format_param(&self, id: u32) -> String {
293 (self.format_param)(id)
294 }
295 fn get_meter(&self, id: u32) -> f32 {
296 (self.get_meter)(id)
297 }
298 fn get_state(&self) -> Vec<u8> {
299 (self.get_state)()
300 }
301 fn set_state(&self, data: Vec<u8>) {
302 (self.set_state)(data);
303 }
304 fn transport(&self) -> Option<TransportInfo> {
305 (self.transport)()
306 }
307}
308
309/// Context passed to [`Editor::open`]. Carries:
310///
311/// - An `Arc<dyn EditorBridge>` - the host-plugin protocol surface
312/// (begin/set/end edit, `request_resize`, `get_state`, transport, …).
313/// - An `Arc<P>` typed parameter store - plugin authors `Deref` to
314/// `&P` and read fields directly: `state.gain.read()`.
315///
316/// The default `P = dyn Params` keeps the trait-object boundary
317/// (`Editor::open(ctx: PluginContext)`) one-typed; editor crates
318/// that want typed access (truce-egui, truce-slint, truce-iced) carry
319/// their own `<P>` and reconstitute `PluginContext<P>` internally
320/// via [`PluginContext::with_params`] using the `Arc<P>` they stored
321/// at editor construction.
322///
323/// `Clone` is two refcount bumps (bridge + params). Editors that need
324/// to hand the context to UI worker threads or animation timers clone
325/// freely.
326pub struct PluginContext<P: ?Sized = dyn Params> {
327 bridge: Arc<dyn EditorBridge>,
328 params: Arc<P>,
329}
330
331impl<P: ?Sized> Clone for PluginContext<P> {
332 fn clone(&self) -> Self {
333 Self {
334 bridge: Arc::clone(&self.bridge),
335 params: Arc::clone(&self.params),
336 }
337 }
338}
339
340impl<P: ?Sized> PluginContext<P> {
341 /// Build a typed context from any [`EditorBridge`] implementor and
342 /// the plugin's typed param store.
343 pub fn new(bridge: Arc<dyn EditorBridge>, params: Arc<P>) -> Self {
344 Self { bridge, params }
345 }
346
347 /// Access the underlying bridge handle. Editors that want to clone
348 /// the bridge into a worker thread without cloning the surrounding
349 /// `PluginContext` use this.
350 #[must_use]
351 pub fn bridge(&self) -> &Arc<dyn EditorBridge> {
352 &self.bridge
353 }
354
355 /// Access the typed param store as an `Arc`. Use this when you
356 /// need to capture the params in a `'static` closure (e.g. an iced
357 /// `Subscription` or a worker thread).
358 #[must_use]
359 pub fn params(&self) -> &Arc<P> {
360 &self.params
361 }
362
363 /// Replace the param-store generic parameter while reusing the
364 /// same bridge. Used by editor crates that receive the dyn-erased
365 /// `PluginContext` from [`Editor::open`] and want the typed
366 /// `PluginContext<P>` for their UI closure.
367 pub fn with_params<Q: ?Sized>(&self, params: Arc<Q>) -> PluginContext<Q> {
368 PluginContext {
369 bridge: Arc::clone(&self.bridge),
370 params,
371 }
372 }
373
374 pub fn begin_edit(&self, id: impl Into<u32>) {
375 self.bridge.begin_edit(id.into());
376 }
377 pub fn set_param(&self, id: impl Into<u32>, normalized: f64) {
378 self.bridge.set_param(id.into(), normalized);
379 }
380 pub fn end_edit(&self, id: impl Into<u32>) {
381 self.bridge.end_edit(id.into());
382 }
383 /// Begin + set + end in one call. Use for click-to-toggle widgets
384 /// and similar single-shot edits where the gesture and the value
385 /// arrive together.
386 pub fn automate(&self, id: impl Into<u32>, normalized: f64) {
387 let id = id.into();
388 self.bridge.begin_edit(id);
389 self.bridge.set_param(id, normalized);
390 self.bridge.end_edit(id);
391 }
392 #[must_use]
393 pub fn request_resize(&self, w: u32, h: u32) -> bool {
394 self.bridge.request_resize(w, h)
395 }
396 pub fn format_param(&self, id: impl Into<u32>) -> String {
397 self.bridge.format_param(id.into())
398 }
399 /// Format into a caller-owned buffer. See
400 /// [`EditorBridge::format_param_into`] for the allocation
401 /// trade-off - the caller's buffer is reused, but bridges that
402 /// don't override the default impl still allocate internally.
403 pub fn format_param_into(&self, id: impl Into<u32>, out: &mut String) {
404 self.bridge.format_param_into(id.into(), out);
405 }
406 pub fn get_meter(&self, id: impl Into<u32>) -> f32 {
407 self.bridge.get_meter(id.into())
408 }
409 #[must_use]
410 pub fn get_state(&self) -> Vec<u8> {
411 self.bridge.get_state()
412 }
413 pub fn set_state(&self, data: Vec<u8>) {
414 self.bridge.set_state(data);
415 }
416 #[must_use]
417 pub fn transport(&self) -> Option<TransportInfo> {
418 self.bridge.transport()
419 }
420}
421
422impl PluginContext<dyn Params> {
423 /// Build a dyn-erased context from a [`ClosureBridge`]. Convenience
424 /// for format wrappers that compose state inline via closures.
425 pub fn from_closures(bridge: ClosureBridge, params: Arc<dyn Params>) -> Self {
426 Self {
427 bridge: Arc::new(bridge),
428 params,
429 }
430 }
431}
432
433impl<P: Params + 'static> PluginContext<P> {
434 /// Drop the typed `<P>` and return the dyn-erased context that
435 /// crosses the `Editor::open` trait-object boundary.
436 #[must_use]
437 pub fn dyn_erase(self) -> PluginContext<dyn Params> {
438 PluginContext {
439 bridge: self.bridge,
440 params: self.params as Arc<dyn Params>,
441 }
442 }
443}
444
445/// Plugin authors read parameter fields directly via `Deref`:
446/// `state.gain.read()`, `state.bypass.value()`. The `state`
447/// here is `&PluginContext<MyParams>` and `Deref::Target = MyParams`.
448impl<P: ?Sized> Deref for PluginContext<P> {
449 type Target = P;
450 fn deref(&self) -> &P {
451 &self.params
452 }
453}
454
455/// Build a [`PluginContext`] backed only by `params`. All write
456/// closures are no-ops; reads delegate to the params `Arc`; the
457/// transport reports the deterministic
458/// [`crate::events::TransportInfo::for_screenshot`] state so
459/// screenshot tests stay reproducible across CI runs.
460///
461/// Used by editor backends inside their `Editor::screenshot()` impl,
462/// and re-exported from `truce-test` for plugin authors that want to
463/// drive snapshot tests directly.
464pub fn for_test_params(params: Arc<dyn Params>) -> PluginContext<dyn Params> {
465 let p_get = Arc::clone(¶ms);
466 let p_plain = Arc::clone(¶ms);
467 let p_fmt = Arc::clone(¶ms);
468 let transport = TransportInfo::for_screenshot();
469 PluginContext::from_closures(
470 ClosureBridge {
471 begin_edit: Box::new(|_| {}),
472 set_param: Box::new(|_, _| {}),
473 end_edit: Box::new(|_| {}),
474 request_resize: Box::new(|_, _| false),
475 get_param: Box::new(move |id| p_get.get_normalized(id).unwrap_or(0.5)),
476 get_param_plain: Box::new(move |id| p_plain.get_plain(id).unwrap_or(0.0)),
477 format_param: Box::new(move |id| {
478 let plain = p_fmt.get_plain(id).unwrap_or(0.0);
479 p_fmt
480 .format_value(id, plain)
481 .unwrap_or_else(|| format!("{plain:.2}"))
482 }),
483 get_meter: Box::new(|_| 0.0),
484 get_state: Box::new(Vec::new),
485 set_state: Box::new(|_| {}),
486 transport: Box::new(move || Some(transport)),
487 },
488 params,
489 )
490}
491
492// ---------------------------------------------------------------------------
493// Precision-routed parameter reads
494//
495// The editor-bridge surface is sample-agnostic (`f64` on the wire, the
496// lossless lowest-common-denominator that round-trips any host
497// automation precision). These two extension traits route the call
498// site to the user's chosen precision - same pattern as
499// `FloatParamReadF32` / `FloatParamReadF64` for the audio-thread
500// param reads. Brought into scope via `pub use ... as _;` in each
501// prelude:
502// - `prelude` / `prelude32` → `PluginContextReadF32`
503// - `prelude64` / `prelude64m` → `PluginContextReadF64`
504//
505// Single-prelude code dispatches unambiguously. Importing both
506// preludes in the same file collides on `get_param` - the right
507// error if the file hasn't committed to a precision.
508// ---------------------------------------------------------------------------
509
510/// `f32`-precision parameter reads on `PluginContext`. Brought into
511/// scope by `truce::prelude` / `truce::prelude32` / `truce::prelude64m`
512/// (the `f32`-buffer preludes). GUI binding crates (slint, egui,
513/// iced) take `f32` natively, so this is the common case.
514pub trait PluginContextReadF32 {
515 /// Normalized `[0, 1]` value of the parameter, narrowed to `f32`.
516 fn get_param(&self, id: impl Into<u32>) -> f32;
517 /// Plain (denormalized) value of the parameter, narrowed to `f32`.
518 fn get_param_plain(&self, id: impl Into<u32>) -> f32;
519}
520
521/// `f64`-precision parameter reads on `PluginContext`. Brought into
522/// scope by `truce::prelude64`. Same surface as
523/// [`PluginContextReadF32`] but returns the bridge's `f64` value
524/// directly without narrowing.
525pub trait PluginContextReadF64 {
526 /// Normalized `[0, 1]` value of the parameter.
527 fn get_param(&self, id: impl Into<u32>) -> f64;
528 /// Plain (denormalized) value of the parameter.
529 fn get_param_plain(&self, id: impl Into<u32>) -> f64;
530}
531
532impl<P: ?Sized> PluginContextReadF32 for PluginContext<P> {
533 fn get_param(&self, id: impl Into<u32>) -> f32 {
534 self.bridge.get_param(id.into()).to_f32()
535 }
536 fn get_param_plain(&self, id: impl Into<u32>) -> f32 {
537 self.bridge.get_param_plain(id.into()).to_f32()
538 }
539}
540
541impl<P: ?Sized> PluginContextReadF64 for PluginContext<P> {
542 fn get_param(&self, id: impl Into<u32>) -> f64 {
543 self.bridge.get_param(id.into())
544 }
545 fn get_param_plain(&self, id: impl Into<u32>) -> f64 {
546 self.bridge.get_param_plain(id.into())
547 }
548}