Why does the window background remain black in fullscreen?

527 Views Asked by At

I have created a window without title by-

 override func windowDidLoad() {
    super.windowDidLoad()
    self.window?.styleMask = NSBorderlessWindowMask 
    self.window?.movableByWindowBackground = true
}

I have set canBecomeKeyWindow by-

override var canBecomeKeyWindow:Bool
{
    get{
        return true
    }

}

I have changed the background color of container view by-

  override func drawRect(dirtyRect: NSRect) {
    super.drawRect(dirtyRect)

    var viewcolor = NSColor.whiteColor()
    viewcolor.setFill()
    NSRectFill(dirtyRect)


}

And a custom view is added as a subview to this container view and I changed background color of custom view by-

 override func drawRect(dirtyRect: NSRect) {
    super.drawRect(dirtyRect)

    NSColor(SRGBRed: 0.8, green: 0.26, blue: 0.33, alpha:1.0).set()
    NSRectFill(self.bounds)
}

Now when I do a toggleFullscreen like-

 @IBAction func goFullScreen(sender: AnyObject) {
    self.view.window?.toggleFullScreen(sender)


}

The whole area of screen is not filled. There is blank(black) background behind the window created. The app should fill the space, there shouldn't be black background in this screenshotFull screen state

How can I fix this behaviour. Thanks for your help.

2

There are 2 best solutions below

0
On BEST ANSWER

The problem is that your window is not resizable. Setting the styleMask to NSBorderlessWindowMask inadvertently removed NSResizableWindowMask.

You should set the styleMask to NSBorderlessWindowMask | NSResizableWindowMask.

5
On

dirtyRect is not always equal to bounds. So, in a window's drawRect call NSRectFill(self.bounds) instead. Fill operation is not that heavy to affect the overall drawing performance.

Or you may mark the whole window rect as dirty with setNeedsDisplay call in resizing handler and be able to work with dirty region only.

If you're aware of possible performance slowdown issue, you may mark window's content as layer-backed and set the layer's color to white - that will be more efficient (this is also the best option if you perform any animation on that view).

There are a few more techniques that apple describes in Optimizing View Drawing topic you may find helpful.