In Catalina, `orderFront:` causes app to become the active application; how do I prevent this?

末鹿安然 提交于 2021-02-11 15:45:05

问题


I have a background "monitoring" app that runs either as an accessory or as a regular app (user choice depending on whether they want to see it in the dock).

The app has several never-key utility windows (NSPanels) that display the progress of background tasks. When a background task starts, the window is made visible using orderFront:.

This has worked great for years; the window would simply appear somewhere behind the active app.

Starting in Catalina (macOS 10.15), however, orderFront: causes my monitoring app to become active, stealing the focus from the user's app.

This is bad and I'd like to prevent it, but I can't seem to figure out how.


回答1:


The problem is not -orderFront:.

It turns out that in Catalina, if you change the app's activation policy to NSApplicationActivationPolicyRegular, as some point in the near future (i.e. not immediately), the app will become the active app, just as if it had been launched.

What was throwing me was the sequence of events: the activation policy changed, the window was created and made front, and then the app became the frontmost app.

Sadly, since there's no way to refuse becoming the frontmost app my only workaround is to capture the currently active app when I change the activation policy, wait for the activation, then request the original app become the active app again. Ugly, but in my application this should only happen a few times a day; here's hoping the user isn't looking at the screen when it does.



来源:https://stackoverflow.com/questions/59697506/in-catalina-orderfront-causes-app-to-become-the-active-application-how-do-i

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!