1

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

guitarflow
  • 2,930
  • 25
  • 38

2 Answers2

3

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];
}
Justin Boo
  • 10,132
  • 8
  • 50
  • 71
  • No, this doesn't work. I just tried it. In the meantime, I integrated the "Font" menu, which makes life a lot easier. But hiding the Font panel on startup still doesn't work. I don't know where the Font Panel stores its visibility state but it seems it overrides all I do at some point later in the initialization. – guitarflow Sep 01 '12 at 11:32
  • Hey Justin, all I can say is that it doesn't work here. As this is the most obvious thing, I tried that already as stated in the question. Might be a bug in 10.8 or Xcode 4.4... – guitarflow Sep 06 '12 at 08:46
  • @Sid np, I'm glad that this helped you. – Justin Boo Dec 13 '13 at 10:23
0

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 }
}
PeterZ
  • 11
  • 2