简体   繁体   中英

NSDocument Tab Window Restoration

This question deals with tab window restoration in a document-based app.

In an OSX, document-based app, which allows a user to create and convert tab windows, I need to preserve and restore the 'tab' state of each window.

Currently, my document controller restores its documents windows, but not the tab deployment; I get back individual windows; I can merge all back into one, but this is too heavy-handed as their former groupings are lost.

My app document class's - makeWindowControllers() function is where I affect the new controllers, whether they should cascade, which I'd read be false, during restore:

//  Determine cascade based on state of application delegate
controller.shouldCascadeWindows = <app did receive applicationWillFinishLaunching>

so it would be false until it's finished launching.

Finally, my window's class features methods:

override func addTabbedWindow(_ window: NSWindow, ordered: NSWindow.OrderingMode) {
    super.addTabbedWindow(window, ordered: ordered)
    window.invalidateRestorableState()
}
override func moveTabToNewWindow(_ sender: Any?) {
    super.moveTabToNewWindow(sender)
    self.invalidateRestorableState()
}

override func encodeRestorableState(with coder: NSCoder) {
    if let tabGroup = self.tabGroup {
        let tabIndex = tabGroup.windows.firstIndex(of: self)
        coder.encode(tabIndex, forKey: "tabIndex" )
        Swift.print("<- tabIndex: \(String(describing: tabIndex))")
    }
}

override func restoreState(with coder: NSCoder) {
    let tabIndex = coder.decodeInt64(forKey: "tabIndex")
    Swift.print("-> tabIndex: \(tabIndex)")
}

to invalidate the window restore state when the tab state is changed. But I'm not sure with the NSWindowRestoration protocol implementation, who or what needs to implement the protocol when a document controller is involved.

I think this is the reason the last function is never called. I get debug output about the encoding but during the next app execution the restoreStore(coder:) function is never called.

So who implements this window restore protocol in such an environment I guess is my question, or a decent example doing so.

My question reveals you not require anything special for a document based app; I've updated my prototype which features this support and environment here SimpleViewer , which features Swift5, a document based app supporting tabs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM