diff --git a/src/lib.rs b/src/lib.rs index 714b70b..d35d8e7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,6 +3,7 @@ use nih_plug::prelude::{Editor, GuiContext, ParamSetter}; use serde_json::Value; use std::{ borrow::Cow, + cell::RefCell, sync::{ atomic::{AtomicU32, Ordering}, Arc, @@ -115,6 +116,8 @@ pub struct WindowHandler { events_receiver: Receiver, pub width: Arc, pub height: Arc, + // GUI-thread only resize queue - uses RefCell instead of Mutex since it never crosses threads + pending_resizes: RefCell>, } impl WindowHandler { @@ -134,6 +137,28 @@ impl WindowHandler { }); } + /// Queue a resize request to be processed later + /// This allows deferring resize operations to avoid borrow checker conflicts + pub fn queue_resize(&self, width: u32, height: u32) { + self.pending_resizes.borrow_mut().push((width, height)); + } + + /// Process any pending resize requests + /// Returns the size that was applied, if any + pub fn process_pending_resizes(&self, window: &mut baseview::Window) -> Option<(u32, u32)> { + let mut queue = self.pending_resizes.borrow_mut(); + if let Some((width, height)) = queue.pop() { + // Only process the most recent resize request to avoid lag + queue.clear(); + drop(queue); // Release the borrow before calling resize + + self.resize(window, width, height); + Some((width, height)) + } else { + None + } + } + pub fn send_json(&self, json: Value) { let json_str = json.to_string(); let json_str_quoted = @@ -255,6 +280,7 @@ impl Editor for WebViewEditor { mouse_handler, width, height, + pending_resizes: RefCell::new(Vec::new()), } }); return Box::new(Instance { window_handle });