title: Dismiss Custom Window for NSStatusItem
body: <p>I've got a custom window popping up when an NSStatusItem is clicked. The code is based on <a href="http://mattgemmell.com/source#maattachedwindow" rel="nofollow">MAAtachedwindow</a>. Everything is working great but I can't figure out a way to dismiss the window when the user clicks on something else like another status bar item, or another app.</p>
  
  <p>Here's my code for creating the window:</p>
  
  <pre><code>    statusItem = [[[NSStatusBar systemStatusBar] statusItemWithLength:width] retain];
  
      //setup custom status menu view
      CGFloat height = [[NSStatusBar systemStatusBar] thickness];
      NSRect viewFrame = NSMakeRect(0.0f, 0.0f, width, height);
      statusMenuView = [[[_ISStatusMenuView alloc] initWithFrame:viewFrame] retain];
      statusMenuView.offset = aOffset;
      statusItem.view = statusMenuView;
  
      //setup the window to show when clicked
      NSRect contentRect = NSZeroRect;
      contentRect.size = aView.frame.size;
      statusMenuWindow = [[[NSWindow alloc] initWithContentRect:contentRect
                                     styleMask:NSBorderlessWindowMask
                                       backing:NSBackingStoreBuffered 
                                         defer:NO] retain];
  
      [statusMenuWindow setLevel:NSPopUpMenuWindowLevel];
      [statusMenuWindow setBackgroundColor:[NSColor clearColor]];
      [statusMenuWindow setMovableByWindowBackground:NO];
      [statusMenuWindow setExcludedFromWindowsMenu:YES];
      [statusMenuWindow setOpaque:NO];
      [statusMenuWindow setHasShadow:NO];
      [statusMenuWindow useOptimizedDrawing:YES];
      [[statusMenuWindow contentView] addSubview:aView];
      [statusMenuWindow setDelegate:self];
  
      statusMenuView.statusMenuWindow = statusMenuWindow;
  </code></pre>
  
  <p>And here is how I'm showing the window:</p>
  
  <pre><code>- (void)centerView{
      NSRect menuFrame = self.window.frame;
      NSRect windowFrame = self.statusMenuWindow.frame;
      NSPoint menuPoint = NSMakePoint(NSMidX(menuFrame), NSMinY(menuFrame));
      menuPoint.x -= windowFrame.size.width*0.5f;
      menuPoint.y -= windowFrame.size.height+self.offset;
      [self.statusMenuWindow setFrameOrigin:menuPoint];
      [self.statusMenuWindow makeKeyAndOrderFront:self];
  }
  </code></pre>
  
  <p>I was hoping the <code>windowDidResignKey</code> delegate method would do the trick but it doesn't go off with this configuration. The delegate is working because <code>windowDidMove</code> does run.</p>
  
  <pre><code>- (void)windowDidResignKey:(NSNotification *)notification{
      NSLog(@"windowDidResignKey");
      [statusMenuView hideView];
  }
  
  - (void)windowDidResignMain:(NSNotification *)notification{
      NSLog(@"windowDidResignMain");
  }
  
  - (void)windowDidMove:(NSNotification *)notification{
      NSLog(@"windowDidMove");
  }
  </code></pre>
  
  <p>So to recap, how can I hide my custom window when the user clicks on anything else, the way the standard status bar menus work?</p>
  
  <p><em>Edit</em>
  After looking at the <a href="http://blog.shpakovski.com/2011/07/cocoa-popup-window-in-status-bar.html" rel="nofollow">Popup</a> example the only thing I was missing was I had to subclass NSPanel and make it so it  could become the key window.</p>
  
  <pre><code>@interface Panel : NSPanel
  @end
  
  @implementation Panel
  
  - (BOOL)canBecomeKeyWindow{
      return YES;
  }
  
  @end
  </code></pre>