Reputation: 2970
I'm creating a feature for my application where I want to use the NSFontPanel. I don't want to have a "Font" menu in my application.
Opening and closing the font panel when a menu item is clicked is done like that
- (IBAction) showOverlayControls:(id)sender
{
if ( [[NSFontPanel sharedFontPanel] isVisible])
{
NSLog(@"Test");
[[NSFontPanel sharedFontPanel] orderOut:self];
}
else
{
NSFontManager* fontMgr = [NSFontManager sharedFontManager];
[fontMgr setTarget:self];
NSFontPanel* fontPanel = [NSFontPanel sharedFontPanel];
[fontPanel orderFront:self];
}
}
It works ok. The problem arises when I try to close the font panel on application launch in case it is shown. I tried around with
if ( [[NSFontPanel sharedFontPanel] isVisible] )
[[NSFontPanel sharedFontPanel] close];
or
if ( [[NSFontPanel sharedFontPanel] isVisible] )
[[NSFontPanel sharedFontPanel] orderOut:self];
I also tried it without the if statement, still no luck. If the panel is shown when the app is closed, it always pops up again when the app is opened. I also tried to close the font panel in the appWillTerminate method of my app delegate. Same behavior.
Would appreciate any hints. Thanks in advance,
Flo
Upvotes: 1
Views: 477
Reputation: 11
If the app closes while NSFontPanel or NSColorPanel are still visible, this solution might help. Add the following code to your AppDelegate class to avoid restoring the NSFontPanel or NSColorPanel windows when the app is launched. Thanks to https://christiantietze.de/posts/2019/06/observe-nswindow-changes/ for a way of detecting when windows are added.
func applicationDidUpdate(_ notification: Notification) {
NSApp.windows
.filter { ["NSFontPanel", "NSColorPanel"].contains($0.className) }
.filter { $0.isVisible }
.forEach { $0.isRestorable = false }
}
Upvotes: 0
Reputation: 10198
Where are You calling these methods? It must work.
You can call it in AppDelegate -applicationDidFinishLaunching:
notification like this:
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
if ([[NSFontPanel sharedFontPanel] isVisible])
[[NSFontPanel sharedFontPanel] orderOut:self];
}
Upvotes: 3