I have a Cocoa application that displays an application-modal alert using the NSAlert
class. I'd like the alert window to float above all other applications' windows. Can this be done with NSAlert
, or do I need to implement my own window?
I don't know if any of this matters, but the application is an agent application (LSUIElement
is true) implemented as an NSStatusItem
. (For more info about the app, including source code, look <here>.)
Here is the code that displays the alert:
- (void)showTimerExpiredAlert {
[NSApp activateIgnoringOtherApps:YES];
NSAlert *alert = [[NSAlert alloc] init];
[alert setAlertStyle:NSInformationalAlertStyle];
[alert setMessageText:NSLocalizedString(@"Menubar Countdown Complete", @"Expiration message")];
[alert setInformativeText:NSLocalizedString(@"The countdown timer has reached 00:00:00.",
@"Expiration information")];
[alert addButtonWithTitle:NSLocalizedString(@"OK", @"OK button title")];
[alert addButtonWithTitle:NSLocalizedString(@"Restart Countdown...", @"Restart button title")];
NSInteger clickedButton = [alert runModal];
[alert release];
if (clickedButton == NSAlertSecondButtonReturn) {
// ...
}
}
I've tried putting this before the runModal
call:
[[alert window] setFloatingPanel:YES];
I've also tried this:
[[alert window] setLevel:NSFloatingWindowLevel];
But neither of those makes the window stay above others if I click another application's window. I suspect runModal
just doesn't honor either of those settings.