title: Make Cocoa Window Fullscreen
body: <p>I'm currently in the process of adding a fullscreen view to my OpenGL window in OS X. I'm not using OS X's "fullscreen mode" because I don't like the idea of taking over the users screen.</p>
  
  <p>I have my own custom window class derived from <code>NSWindow</code> and in that class, I've overwritten the <code>toggleFullscreen:</code> selector:</p>
  
  <pre><code>- (void)toggleFullScreen:(id)sender
  {
      _fullscreen = !_fullscreen;
      if(_fullscreen) {
          NSRect screenRect = [[NSScreen mainScreen] frame];
          [self setStyleMask:NSBorderlessWindowMask];
          [self setFrame:screenRect display:YES];
          [self setLevel:NSMainMenuWindowLevel+1];
          [self setOpaque:YES];
          [self setHidesOnDeactivate:YES];
      } else {
          NSRect screenRect = [[NSScreen mainScreen] frame];
          unsigned int width = (unsigned int)(screenRect.size.width/2.0f);
          unsigned int height = (unsigned int)(screenRect.size.height/2.0f);
          NSRect frame = NSMakeRect(0, screenRect.size.height-height, width, height);
  
          [self setStyleMask:NSTitledWindowMask | NSResizableWindowMask];
          [self setFrame:frame display:YES];
          [self setLevel:NSNormalWindowLevel];
          [self setOpaque:YES];
          [self setHidesOnDeactivate:NO];
      }
  }
  </code></pre>
  
  <p>This seems to work mostly correctly, toggling between a fullscreen window (per Apple's documentation <a href="http://developer.apple.com/library/mac/#documentation/graphicsimaging/conceptual/OpenGL-MacProgGuide/opengl_fullscreen/opengl_cgl.html#//apple_ref/doc/uid/TP40001987-CH210-SW6" rel="nofollow">here</a>) and a window at half the screens resolution.</p>
  
  <p>The problem is that toggling between the two modes makes my window "lose focus". If I hit the keyboard shortcut I'm using right after I switch the mode, I get the OS X error "beep". I have to click in the window to "reactivate" it before I can use the toggle again.</p>
  
  <p>I've tried adding a <code>[self makeKeyAndOrderFront:nil];</code> to my function, but that doesn't fix the issue. All I need is a way to keep the window "focused" during the transition. Except for that issue, this method works perfectly and requires no changes to my <code>NSView</code>.</p>
  
  <p><strong>Edit</strong></p>
  
  <p>Not sure if its relevant, but I'm creating this window programmatically, not loading it from a Nib.</p>