How to use NSWindowOcclusionState.Visible in Swift

I'm trying to implement window switching (something I've done many times in Objective-C), but now in Swift. It's the seams that I am getting using NSWindowOcclusionState.Visible wrong, but I really don't see my problem. After the initial window is created, only the line w.makeKeyAndOrderFront (self) is called.

Any suggestions?

var fileArchiveListWindow: NSWindow? = nil

@IBAction func tougleFileArchiveList(sender: NSMenuItem) {
    if let w = fileArchiveListWindow {
        if w.occlusionState == NSWindowOcclusionState.Visible {
            w.orderOut(self)
        }
        else {
            w.makeKeyAndOrderFront(self)
        }
    }
    else {
        let sb = NSStoryboard(name: "FileArchiveOverview",bundle: nil)
        let controller: FileArchiveOverviewWindowController = sb?.instantiateControllerWithIdentifier("FileArchiveOverviewController") as FileArchiveOverviewWindowController
        fileArchiveListWindow = controller.window
        fileArchiveListWindow?.makeKeyAndOrderFront(self)
    }
}

      

+3


source to share


2 answers


Old question, but I just ran into the same problem. Validation occlusionState

is done differently in Swift using the binary operator AND

:



if (window.occlusionState & NSWindowOcclusionState.Visible != nil) {
    // visible
}
else {
    // not visible
}

      

+1


source


In recent SDKs, the bitmask is NSWindowOcclusionState

imported into Swift as OptionSet

. You can use window.occlusionState.contains(.visible)

to check if the window is visible or not (completely closed).

Example:



observerToken = NotificationCenter.default.addObserver(forName: NSWindow.didChangeOcclusionStateNotification, object: window, queue: nil) { note in
    let window = note.object as! NSWindow
    if window.occlusionState.contains(.visible) {
        // window at least partially visible, resume power-hungry calculations
    } else {
        // window completely occluded, throttle down timers, CPU, etc.
    }
}

      

0


source







All Articles