baseview icon indicating copy to clipboard operation
baseview copied to clipboard

macOS/Linux focus issue

Open justinfrankel opened this issue 10 months ago • 8 comments

I'm one of the developers of REAPER. There is an issue where plug-is that use nih-plug and egui (which afaict uses baseview) do not set their NSViews as first responder on mouse click. On most other DAWs this is not an issue, as the NSView is often the only view in the NSWindow, however in REAPER it does present some issues.

I have written some suggested code that should fix for macOS, which compiles, but due to my unfamiliarity with the Cargo ecosystem I haven't been able to get it into the actual resulting binary.

diff --git a/src/macos/view.rs b/src/macos/view.rs
index 8b765b4..a970b33 100644
--- a/src/macos/view.rs
+++ b/src/macos/view.rs
@@ -54,12 +54,18 @@ macro_rules! add_simple_mouse_class_method {
 /// Similar to [add_simple_mouse_class_method!], but this creates its own event object for the
 /// press/release event and adds the active modifier keys to that event.
 macro_rules! add_mouse_button_class_method {
-    ($class:ident, $sel:ident, $event_ty:ident, $button:expr) => {
+    ($class:ident, $sel:ident, $event_ty:ident, $button:expr, $foc:expr) => {
         #[allow(non_snake_case)]
         extern "C" fn $sel(this: &Object, _: Sel, event: id){
             let state = unsafe { WindowState::from_view(this) };
 
             let modifiers = unsafe { NSEvent::modifierFlags(event) };
+            if $foc {
+                unsafe {
+                    let window: id = msg_send![this, window];
+                    let _: () = msg_send![window, makeFirstResponder: this];
+                }
+            }
 
             state.trigger_event(Event::Mouse($event_ty {
                 button: $button,
@@ -211,12 +217,12 @@ unsafe fn create_view_class() -> &'static Class {
         handle_notification as extern "C" fn(&Object, Sel, id),
     );
 
-    add_mouse_button_class_method!(class, mouseDown, ButtonPressed, MouseButton::Left);
-    add_mouse_button_class_method!(class, mouseUp, ButtonReleased, MouseButton::Left);
-    add_mouse_button_class_method!(class, rightMouseDown, ButtonPressed, MouseButton::Right);
-    add_mouse_button_class_method!(class, rightMouseUp, ButtonReleased, MouseButton::Right);
-    add_mouse_button_class_method!(class, otherMouseDown, ButtonPressed, MouseButton::Middle);
-    add_mouse_button_class_method!(class, otherMouseUp, ButtonReleased, MouseButton::Middle);
+    add_mouse_button_class_method!(class, mouseDown, ButtonPressed, MouseButton::Left, true);
+    add_mouse_button_class_method!(class, mouseUp, ButtonReleased, MouseButton::Left, false);
+    add_mouse_button_class_method!(class, rightMouseDown, ButtonPressed, MouseButton::Right, true);
+    add_mouse_button_class_method!(class, rightMouseUp, ButtonReleased, MouseButton::Right, false);
+    add_mouse_button_class_method!(class, otherMouseDown, ButtonPressed, MouseButton::Middle, true);
+    add_mouse_button_class_method!(class, otherMouseUp, ButtonReleased, MouseButton::Middle, false);
     add_simple_mouse_class_method!(class, mouseEntered, MouseEvent::CursorEntered);
     add_simple_mouse_class_method!(class, mouseExited, MouseEvent::CursorLeft);

Thanks for looking at this!

justinfrankel avatar Feb 10 '25 22:02 justinfrankel

I can confirm this change works, and it fixes the problem in #169. It would be great if we could get it into a release.

peastman avatar Feb 11 '25 19:02 peastman

I believe this is related to https://github.com/RustAudio/baseview/issues/152

Which has been resolved in: https://github.com/RustAudio/baseview/pull/170

But also requires the change in the respective GUI. Latest Vizia should be working (haven't tested but maybe @geom3trik can confirm) but nih_plug uses an old version as of now. I am still using the implementation/temp workaround from https://github.com/vizia/vizia/pull/463 and it is working fine.

Finally, it should be noted that these changes do not resolve the input focus issue in all DAWs. Specifically the issue is still present on Ableton Live for Windows and I believe Studio One on Windows. Reaper and Bitwig are working fine in both OSs as far as I can tell.

dathinaios avatar Mar 18 '25 08:03 dathinaios

#170 doesn't fix it for me. My plugin uses NIH-Plug and egui. When I build all dependencies from source and use the most recent baseview code, keyboard input is still broken in Reaper. But if I add the patch above, it does work.

peastman avatar Mar 19 '25 17:03 peastman

#170 doesn't fix it for me. My plugin uses NIH-Plug and egui. When I build all dependencies from source and use the most recent baseview code, keyboard input is still broken in Reaper. But if I add the patch above, it does work.

The solution is two-fold. #170 provided the interface for focusing the window but it is the responsibility of the GUI (in your case egui) to call it when it is required. So you would need to make a change in egui to implement it. Don't get me wrong, if the workaround is working for you you might want to just go with it until all these things are properly resolved. I personally had to use modified versions of baseview, vizia and nih-plug and I am getting a headache.. So if I had this solution I might have gone with it. But I don't think it is going to be added as is to baseview because it is triggered in every mouse click which is incorrect.

By the way, are you on windows by any chance? Do you know if the above addition fixes keyboard input on Ableton Live?

dathinaios avatar Mar 19 '25 19:03 dathinaios

I'm on Mac.

Does the change need to be made in egui itself, or in egui-baseview, or in NIH-Plug? Can you describe what the required change is?

peastman avatar Mar 19 '25 19:03 peastman

Probably egui-baseview but I don't know anything about egui. As to the required change it will be that a component wold request focus when necessary. With Vizia I have just added it on alt-click on the slider but Vizia's author has, I believe, used a better design for the final fix (which I haven't tried cause it is not synced with nih_plug yet).

dathinaios avatar Mar 19 '25 19:03 dathinaios

Do you have a link to the changes in Vizia?

peastman avatar Mar 19 '25 20:03 peastman

Do you have a link to the changes in Vizia?

I am using the workaround I mentioned above: https://github.com/RustAudio/baseview/issues/202#issuecomment-2732145922

Current Vizia version is doing something different. Not sure what.

dathinaios avatar Mar 19 '25 20:03 dathinaios