Skip to content

macOS/Linux focus issue #202

Closed
Closed

Description

@justinfrankel

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!

Activity

  1. peastman commented on Feb 11, 2025

    @peastman

    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.

  2. dathinaios commented on Mar 18, 2025

    @dathinaios
    Contributor

    I believe this is related to #152

    Which has been resolved in: #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 vizia/vizia#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.

  3. peastman commented on Mar 19, 2025

    @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.

  4. dathinaios commented on Mar 19, 2025

    @dathinaios
    Contributor

    #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?

  5. peastman commented on Mar 19, 2025

    @peastman

    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?

  6. dathinaios commented on Mar 19, 2025

    @dathinaios
    Contributor

    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).

  7. peastman commented on Mar 19, 2025

    @peastman

    Do you have a link to the changes in Vizia?

  8. dathinaios commented on Mar 19, 2025

    @dathinaios
    Contributor

    Do you have a link to the changes in Vizia?

    I am using the workaround I mentioned above: #202 (comment)

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

  9. dathinaios commented on May 3, 2026

    @dathinaios
    Contributor

    Has this been solved by b519246?

  10. dathinaios commented on May 3, 2026

    @dathinaios
    Contributor

    Tested with the gain example ( BillyDM 's nih_plug fork at https://github.com/BillyDM/nih-plug) and current baseview master.

    • MacOS, Mac Studio M1
    • Reaper & Bitwig
    • CLAP/VST
    • egui gain example

    Keyboard focus / mouse click focus seem to be working fine. If there are specific steps to reproduce just let me know and I am happy to test further.

  11. prokopyl commented on May 3, 2026

    @prokopyl
    Member

    It does seem that b519246 fixed the issue indeed.

    I have tested various plugins from BillyDM's fork of NIH-plug (with current baseview master), on macOS 26.2, as CLAP/VST3 in Reaper, Bitwig & the clack-host-cpal example, and I couldn't find focus issues.

    I am going to close this issue for now. But if anyone still has the issue, feel free to comment here, and I will consider reopening this (or opening another issue if that ends up being more appropriate).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions