From 7b9eff456c04551145eb5ebde769bd82ce426b9d Mon Sep 17 00:00:00 2001 From: Robbert van der Helm Date: Tue, 29 Mar 2022 00:02:11 +0200 Subject: [PATCH] Update for new vizia resize patch This version uses a property on the context instead of events. This gets rid of all of the problems and complexity of the previous implementation. --- Cargo.lock | 8 ++--- nih_plug_vizia/src/widgets.rs | 40 ++++++++------------- nih_plug_vizia/src/widgets/resize_handle.rs | 13 +++---- src/wrapper/clap/context.rs | 6 ++-- 4 files changed, 24 insertions(+), 43 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index a6d0b4a7..93798618 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3459,7 +3459,7 @@ checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" [[package]] name = "vizia" version = "0.1.0" -source = "git+https://github.com/robbert-vdh/vizia.git?branch=patched#50b8f6e8438fd0eaa3b0911839d768bd70756c49" +source = "git+https://github.com/robbert-vdh/vizia.git?branch=patched#b58859c5e8befa055cc80a11e7bef32d8b311f75" dependencies = [ "vizia_baseview", "vizia_core", @@ -3468,7 +3468,7 @@ dependencies = [ [[package]] name = "vizia_baseview" version = "0.1.0" -source = "git+https://github.com/robbert-vdh/vizia.git?branch=patched#50b8f6e8438fd0eaa3b0911839d768bd70756c49" +source = "git+https://github.com/robbert-vdh/vizia.git?branch=patched#b58859c5e8befa055cc80a11e7bef32d8b311f75" dependencies = [ "baseview 0.1.0 (git+https://github.com/robbert-vdh/baseview.git?branch=feature/resize)", "femtovg", @@ -3480,7 +3480,7 @@ dependencies = [ [[package]] name = "vizia_core" version = "0.1.0" -source = "git+https://github.com/robbert-vdh/vizia.git?branch=patched#50b8f6e8438fd0eaa3b0911839d768bd70756c49" +source = "git+https://github.com/robbert-vdh/vizia.git?branch=patched#b58859c5e8befa055cc80a11e7bef32d8b311f75" dependencies = [ "bitflags", "copypasta", @@ -3503,7 +3503,7 @@ dependencies = [ [[package]] name = "vizia_derive" version = "0.1.0" -source = "git+https://github.com/robbert-vdh/vizia.git?branch=patched#50b8f6e8438fd0eaa3b0911839d768bd70756c49" +source = "git+https://github.com/robbert-vdh/vizia.git?branch=patched#b58859c5e8befa055cc80a11e7bef32d8b311f75" dependencies = [ "proc-macro2", "quote", diff --git a/nih_plug_vizia/src/widgets.rs b/nih_plug_vizia/src/widgets.rs index 48bf5411..cdacc9d1 100644 --- a/nih_plug_vizia/src/widgets.rs +++ b/nih_plug_vizia/src/widgets.rs @@ -99,44 +99,32 @@ impl Model for WindowModel { fn event(&mut self, cx: &mut vizia::Context, event: &mut vizia::Event) { if let Some(window_event) = event.message.downcast() { match *window_event { - WindowEvent::ResizeWindow(logical_width, logical_height) => { - let logical_size = - (logical_width.round() as u32, logical_height.round() as u32); - let old_size @ (old_logical_width, old_logical_height) = + // This gets fired whenever the inner window gets resized + WindowEvent::WindowResize => { + let logical_size = (cx.window_size.width, cx.window_size.height); + let old_logical_size @ (old_logical_width, old_logical_height) = self.vizia_state.size.load(); + let scale_factor = cx.user_scale_factor; + let old_user_scale_factor = self.vizia_state.scale_factor.load(); // Don't do anything if the current size already matches the new size, this // could otherwise also cause a feedback loop on resize failure - if logical_size == old_size { + if logical_size == old_logical_size && scale_factor == old_user_scale_factor { return; } // Our embedded baseview window will have already been resized. If the host does // not accept our new size, then we'll try to undo that self.vizia_state.size.store(logical_size); + self.vizia_state.scale_factor.store(scale_factor); if !self.context.request_resize() { - self.vizia_state.size.store(old_size); - cx.emit(WindowEvent::ResizeWindow( - old_logical_width as f32, - old_logical_height as f32, - )); - } - } - WindowEvent::SetScale(user_scale_factor) => { - let old_user_scale_factor = self.vizia_state.scale_factor.load(); - - // Don't do anything if the current scale already matches the new scale - if user_scale_factor == old_user_scale_factor { - return; - } - - // This works the same as the `ResizeWindow` handler. The actual window size - // reported to the host gets calculated from a combination of the window's - // logical size (before user scaling) and the user scale factor. - self.vizia_state.scale_factor.store(user_scale_factor); - if !self.context.request_resize() { + self.vizia_state.size.store(old_logical_size); self.vizia_state.scale_factor.store(old_user_scale_factor); - cx.emit(WindowEvent::SetScale(old_user_scale_factor)); + + // This will cause the window's size to be reverted on the next event loop + cx.window_size.width = old_logical_width; + cx.window_size.height = old_logical_height; + cx.user_scale_factor = old_user_scale_factor; } } diff --git a/nih_plug_vizia/src/widgets/resize_handle.rs b/nih_plug_vizia/src/widgets/resize_handle.rs index 60ef6202..bffd84a1 100644 --- a/nih_plug_vizia/src/widgets/resize_handle.rs +++ b/nih_plug_vizia/src/widgets/resize_handle.rs @@ -3,8 +3,6 @@ use femtovg::{Paint, Path}; use vizia::*; -use super::WindowModel; - /// A resize handle placed at the bottom right of the window that lets you resize the window. pub struct ResizeHandle { /// Will be set to `true` if we're dragging the parameter. Resetting the parameter or entering a @@ -44,9 +42,8 @@ impl View for ResizeHandle { cx.capture(); cx.current.set_active(cx, true); - let vizia_state = WindowModel::vizia_state.get(cx); self.drag_active = true; - self.start_scale_factor = vizia_state.user_scale_factor(); + self.start_scale_factor = cx.user_scale_factor; self.start_physical_coordinates = ( cx.mouse.cursorx * cx.style.dpi_factor as f32, cx.mouse.cursory * cx.style.dpi_factor as f32, @@ -63,8 +60,6 @@ impl View for ResizeHandle { WindowEvent::MouseMove(x, y) => { // TODO: Filter the hover color and dragging to the actual triangle if self.drag_active { - let vizia_state = WindowModel::vizia_state.get(cx); - // We need to convert our measurements into physical pixels relative to the // initial drag to be able to keep a consistent ratio. This 'relative to the // start' bit is important because otherwise we would be comparing the @@ -82,9 +77,9 @@ impl View for ResizeHandle { as f64) // Prevent approaching zero here because uh .max(0.25); - if new_scale_factor != vizia_state.user_scale_factor() { - cx.emit(WindowEvent::SetScale(new_scale_factor)); - } + + // If this is different then the window will automatically be resized at the end of the frame + cx.user_scale_factor = new_scale_factor; } } _ => {} diff --git a/src/wrapper/clap/context.rs b/src/wrapper/clap/context.rs index 3ac7dad9..f37f603c 100644 --- a/src/wrapper/clap/context.rs +++ b/src/wrapper/clap/context.rs @@ -30,7 +30,7 @@ impl GuiContext for WrapperGuiContext

{ fn request_resize(&self) -> bool { // Bitwig and the CLAP test host require this resize to be done from the main thread, so // we'll use a channel as a substitute for a promise here. - let (result_sender, _result_receiver) = channel::bounded(1); + let (result_sender, result_receiver) = channel::bounded(1); if !self .wrapper .do_maybe_async(Task::RequestResize(result_sender)) @@ -43,9 +43,7 @@ impl GuiContext for WrapperGuiContext

{ // rapid succession because the X11 GUI thread is not the same as the host's GUI // thread and both Bitwig and CLAP will reject (or outright SIGABRT) if this gets // called from any other thread - // result_receiver.recv().expect("Main thread died?") - - true + result_receiver.recv().expect("Main thread died?") } // All of these functions are supposed to be called from the main thread, so we'll put some