RAM2118 commited on
Commit
58c47dc
·
verified ·
1 Parent(s): 1dd0405

Fix: Use Accessory activation policy — GUI shows without stealing focus from Ableton

Browse files
Files changed (1) hide show
  1. Source/Helper/HelperPluginHost.cpp +29 -15
Source/Helper/HelperPluginHost.cpp CHANGED
@@ -9,28 +9,39 @@
9
  #include <objc/message.h>
10
  #endif
11
 
12
- // Force helper process to become a proper GUI app on macOS
13
- static void makeGuiProcess()
14
  {
15
  #if JUCE_MAC
16
- // [NSApp setActivationPolicy:NSApplicationActivationPolicyRegular]
17
  id nsApp = ((id(*)(id, SEL))objc_msgSend)((id)objc_getClass("NSApplication"),
18
  sel_registerName("sharedApplication"));
19
- ((void(*)(id, SEL, long))objc_msgSend)(nsApp, sel_registerName("setActivationPolicy:"), 0); // 0 = Regular
20
- ((void(*)(id, SEL, int))objc_msgSend)(nsApp, sel_registerName("activateIgnoringOtherApps:"), 1);
21
  #endif
22
  }
23
 
24
- // Custom window with working close button
 
 
 
 
 
 
 
 
 
 
 
 
 
25
  class PluginGuiWindow : public juce::DocumentWindow
26
  {
27
  public:
28
  PluginGuiWindow(const juce::String& name)
29
  : DocumentWindow(name, juce::Colour(0xff1a1a2e), DocumentWindow::allButtons, true)
30
  {
31
- // Use JUCE's own title bar — works regardless of macOS activation policy
32
- setUsingNativeTitleBar(false);
33
- setTitleBarHeight(28);
34
  }
35
 
36
  void closeButtonPressed() override
@@ -266,14 +277,14 @@ void HelperPluginHost::showGui()
266
  {
267
  if (!plugin) return;
268
 
269
- // Activate as GUI process (macOS requires this for window interaction)
270
- makeGuiProcess();
271
 
272
  // If window exists but hidden, just show it
273
  if (guiWindow)
274
  {
275
  guiWindow->setVisible(true);
276
- guiWindow->toFront(true);
277
  return;
278
  }
279
 
@@ -283,9 +294,11 @@ void HelperPluginHost::showGui()
283
  guiWindow = std::make_unique<PluginGuiWindow>(plugin->getName());
284
  guiWindow->setContentOwned(editor, true);
285
  guiWindow->setResizable(true, true);
286
- guiWindow->centreWithSize(editor->getWidth(), editor->getHeight() + 28); // +28 for title bar
287
  guiWindow->setVisible(true);
288
- guiWindow->toFront(true);
 
 
289
  });
290
  }
291
 
@@ -295,7 +308,8 @@ void HelperPluginHost::hideGui()
295
  {
296
  juce::MessageManager::callAsync([this]()
297
  {
298
- guiWindow.reset();
 
299
  });
300
  }
301
  }
 
9
  #include <objc/message.h>
10
  #endif
11
 
12
+ // Make helper an accessory app — can show windows but doesn't steal focus or appear in Dock
13
+ static void makeAccessoryProcess()
14
  {
15
  #if JUCE_MAC
 
16
  id nsApp = ((id(*)(id, SEL))objc_msgSend)((id)objc_getClass("NSApplication"),
17
  sel_registerName("sharedApplication"));
18
+ // NSApplicationActivationPolicyAccessory = 1 (windows allowed, no Dock icon, no focus steal)
19
+ ((void(*)(id, SEL, long))objc_msgSend)(nsApp, sel_registerName("setActivationPolicy:"), 1);
20
  #endif
21
  }
22
 
23
+ // Show window without stealing focus from Ableton
24
+ static void showWindowWithoutFocus(juce::Component* window)
25
+ {
26
+ #if JUCE_MAC
27
+ if (auto* peer = window->getPeer())
28
+ {
29
+ auto* nsWindow = (id)peer->getNativeHandle();
30
+ // [nsWindow orderFrontRegardless] — shows window without activating the app
31
+ ((void(*)(id, SEL))objc_msgSend)(nsWindow, sel_registerName("orderFrontRegardless"));
32
+ }
33
+ #endif
34
+ }
35
+
36
+ // Custom window
37
  class PluginGuiWindow : public juce::DocumentWindow
38
  {
39
  public:
40
  PluginGuiWindow(const juce::String& name)
41
  : DocumentWindow(name, juce::Colour(0xff1a1a2e), DocumentWindow::allButtons, true)
42
  {
43
+ setUsingNativeTitleBar(true);
44
+ setAlwaysOnTop(true);
 
45
  }
46
 
47
  void closeButtonPressed() override
 
277
  {
278
  if (!plugin) return;
279
 
280
+ // Set as accessory app — allows windows without stealing focus
281
+ makeAccessoryProcess();
282
 
283
  // If window exists but hidden, just show it
284
  if (guiWindow)
285
  {
286
  guiWindow->setVisible(true);
287
+ showWindowWithoutFocus(guiWindow.get());
288
  return;
289
  }
290
 
 
294
  guiWindow = std::make_unique<PluginGuiWindow>(plugin->getName());
295
  guiWindow->setContentOwned(editor, true);
296
  guiWindow->setResizable(true, true);
297
+ guiWindow->centreWithSize(editor->getWidth(), editor->getHeight());
298
  guiWindow->setVisible(true);
299
+
300
+ // Show without stealing focus from Ableton
301
+ showWindowWithoutFocus(guiWindow.get());
302
  });
303
  }
304
 
 
308
  {
309
  juce::MessageManager::callAsync([this]()
310
  {
311
+ if (guiWindow)
312
+ guiWindow->setVisible(false);
313
  });
314
  }
315
  }