Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

macOS: Activate NSApp and put the windows on top #81

Closed

Conversation

karelbilek
Copy link
Contributor

Currently, macOS dialogs don't really work correctly, because the NSApp is not properly activated and, in case of dialogs, is not put on top.

This commit fixes that. Also, I deleted the comment in example, as it's no longer true with these changes.

Currently, macOS dialogs don't really work correctly, because the NSApp is not properly activated
and, in case of dialogs, is not put on top.

This commit fixes that. Also, I deleted the comment in example, as it's no longer true with these changes.
@sqweek
Copy link
Owner

sqweek commented Feb 18, 2024

Hmm... Note that dialog's primary use case is to run alongside an existing GUI app and provide access to native dialogs. example/simple is not representative of this, as the comment hints at.

Changing the underlying library like this to get example/simple to work concerns me because:

  1. dialog is a library not an application; it doesn't really have any business twiddling application level knobs
  2. I'm not a fan of forcing dialogs to the top because I don't want to interfere with the system/end-user's focus policy

It's also been awhile since I touched cocoa and I don't have a good test environment these days. Reading through this stackoverflow answer I think there is a reasonable argument that spawning a dialog qualifies as an exception where it is ok to call [NSApp activateIgnoringOtherApps:YES], but unless there is a demonstrable issue when used alongside a GUI I don't really want to risk changing behaviour.

@karelbilek
Copy link
Contributor Author

karelbilek commented Feb 18, 2024 via email

@karelbilek
Copy link
Contributor Author

karelbilek commented Feb 18, 2024 via email

@sqweek
Copy link
Owner

sqweek commented Feb 18, 2024

I'm not outright opposed to a "put me on top of everything else" option -- see #7 for previous discussion. Your code might provide the final piece to that puzzle; as I mentioned there I didn't think it was possible on OSX.

edit: and if this enables the command-line use case then all the better

cocoa/dlg.m Outdated
@@ -52,6 +52,11 @@ - (DlgResult)run {
[alert addButtonWithTitle:@"OK"];
break;
}
[NSApp setActivationPolicy:NSApplicationActivationPolicyRegular];
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Judging by the docs, the type of command-line usage you are talking about should probably use NSApplicationActivationPolicyAccessory?

This is the kind of application-level decision that I don't think dialog really has any business fiddling with, however I would be ok with this being done conditionally -- ie. if [NSApp activationPolicy] is NSApplicationActivationPolicyProhibited then it's reasonable for dialog to change it in the interest of ease-of-use, but if it is already set to another value then we shouldn't touch it.

@karelbilek
Copy link
Contributor Author

Hah, thanks for the tip; setting it as ...Accessory (conditionally?) actually fixes the issues easier than making it a separate app, and it doesn't need any OnTop options.

@karelbilek karelbilek force-pushed the fix_macos_nsapp_activation branch from 75f530b to a2449a9 Compare February 19, 2024 12:42
@karelbilek
Copy link
Contributor Author

See now

@sqweek
Copy link
Owner

sqweek commented Feb 26, 2024

Thanks very much! I squashed and landed with two very minor modifications:

  • setActivationPolicy() renamed to checkActivationPolicy() to better reflect its conditional behaviour
  • updated README.md to track the minimum required macOS version (10.6+ for NSApplicationActivationPolicyAccessory)

prosavage pushed a commit to lucidoc/dialog that referenced this pull request Mar 20, 2024
Currently, macOS dialogs don't work from the command-line, because the
NSApp activation policy defaults to "Prohibited" in that context. This
commit fixes that by setting the "Accessory" activation policy before
opening a dialog, if (and only if) we are in the Prohibited state.

Closes sqweek#81. Fixes sqweek#15.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants