kingleo
kingleo

Reputation: 23

Transparent background NSWindow was not recovered

I have a custom NSWindow (BorderlessWindow.swift) and NSView (TransparentView.swift) and I add a label (NSTextField) to TransparentView.

When I hide the label, the TransparentView is not recovered:

Before Hide capture image:

enter image description here

After Hide capture image:

enter image description here

How can I fix it?

BorderlessWindow.swift

class BorderlessWindow: NSWindow {

override init(contentRect: NSRect, styleMask aStyle: Int, backing bufferingType: NSBackingStoreType, `defer` flag: Bool) {

    super.init(contentRect: contentRect,
        styleMask: NSBorderlessWindowMask,
        backing: NSBackingStoreType.Buffered,
        `defer`: false)

    self.alphaValue = 1.0
    self.opaque = false
    self.movableByWindowBackground = true

}

@IBOutlet weak var testString: NSTextField!

@IBAction func clicked(sender: NSButton) {
    if testString.hidden == false {
        testString.hidden = true
    }
    else {
        testString.hidden = false
    }
}

override var canBecomeKeyWindow:Bool {
    get {
        return true
    }
}

required init?(coder: NSCoder) {
    super.init(coder: coder)
}

TransparentView.swift

class TransparentView: NSView {

override func drawRect(dirtyRect: NSRect) {

    super.drawRect(dirtyRect)

    NSColor.clearColor().set()
    NSRectFill(self.bounds)

    let path = NSBezierPath(roundedRect: self.bounds, xRadius: 10, yRadius: 10)
    let path2 = NSBezierPath(roundedRect: NSMakeRect(self.bounds.origin.x + 10, self.bounds.origin.y + 10, self.bounds.size.width - 20, self.bounds.size.height - 20), xRadius: 10, yRadius: 10)

    NSColor(calibratedRed: 1, green: 0.7, blue: 0, alpha: 0.5).setFill()
    NSColor(calibratedRed: 1, green: 1, blue: 1, alpha: 1).setStroke()

    path2.lineWidth = 3.0
    path.lineJoinStyle = NSLineJoinStyle.RoundLineJoinStyle
    path.lineCapStyle = NSLineCapStyle.RoundLineCapStyle


    path2.fill()
    path2.stroke()

    // Drawing code here.
}



override init(frame frameRect: NSRect) {
    super.init(frame: frameRect)
}

required init?(coder: NSCoder) {
    super.init(coder: coder)
}

}

I found: When the textField's initial value of hidden property is true(textfield is initialized with hidden state), it works fine.

Upvotes: 2

Views: 689

Answers (1)

Ken Thomases
Ken Thomases

Reputation: 90641

For a window with transparent parts, you need to invalidate the shadow after you change what has been drawn. The shadow is computed from the opaque (or mostly-opaque) parts and therefore depends on exactly what gets drawn.

So, after you change the way it draws, whether that has to do with hiding views or redrawing them, you need to call invalidateShadow() on the window.

Upvotes: 2

Related Questions