Juce Application steals focus on exit


#1

Hi !

I have a juce Application that runs in background (no window) for a while, and then automatically exits.
I noticed that right when the app exits, I lose the focus on whatever window I have selected at the time. This happens for all juce Applications that exit while they are not focused.

After a bit of digging it looks like it comes from this code in juce_mac_MessageManager.mm:

static void shutdownNSApp()
{
    [NSApp stop: nil];
    [NSApp activateIgnoringOtherApps: YES]; // (if the app is inactive, it sits there and ignores the quit request until the next time it gets activated)
    [NSEvent startPeriodicEventsAfterDelay: 0  withPeriod: 0.1];
}

And specifically from the [NSApp activateIgnoringOtherApps: YES];

According to this post, it was made to ensure that the app quits immediately instead of waiting for the next event. But all the tests I’ve done seem to show that only the last line is needed.

If I replace the code with this:

static void shutdownNSApp()
{
    [NSApp stop: nil];
    if ([NSApp isActive] == NO) {
        std::cout << "Not active !" << std::endl;
    }
    [NSEvent startPeriodicEventsAfterDelay: 0  withPeriod: 0.1];
}

I can see that the app exits immediately, even when not active, and it doesn’t steal the focus. On the other hand if I comment the last line the app never exits.

My question is, can I safely remove the [NSApp activateIgnoringOtherApps: YES];, or are there other cases where it’s actually needed ?


#2

I’m experiencing the exact same thing. Did you find another way to solve that rather than changing JUCE-based code?


#3

I’ve removed the call to [NSApp activateIgnoringOtherApps: YES]; on develop. This should solve your issue.