Preserve all tabs of last window on close. (Like Finder)

Hey gang,

I like Finder's behavior of restoring all tabs of the final window that was closed. I would like to mimic this behavior in my own app. The problems I've had doing this seem to stem from the fact that tab closures appear just like window closures and there's no indication that a grouping of tabs is being closed all at once.


My first thoughts for doing this:

  • Instead of removing and deallocating the last window, just hide it.

    Does not work because all tabs are closed except the last one, which gets hidden

  • Hide the entire application when the user clicks the "red" close button and there's only one "tab-group-window" left.

    I can't differentiate between the red-close button (which I want to close that app) and a tab close button(which I would want to use to actually close the tab)


My problem's source seems to be that tab closures appear just like window closures and there's no indication that a grouping of tabs is being closed all at once.

Any other strategies for this? Maybe I'm thinking about it the wrong way?

Replies

I would try and do it this way :


- create a global var to store tab (windows) names :

var tabNames = Array<String>()


- each time a window is opened, keep its name in this array

tabNames.append(newWindowName)


- when closing a tab, remove windowName from the array

// You need to identify the tab by window name (but 2 tabs may have the same name ?), or may be set an identifier when you open the new window


- when closing the mother window (red dot), save the array in user settings


- when you reopen, read the user settings and open as many windows as array.count with the stored name.

I'm actually retaining an array of windowController references. Essentially using their address as the identifier.


Unfortunately, I cannot find a way to differentiate closing a "tab" with the X-button and closing the mother window. Both get reported the same way, unless I'm missing a delegate callback that differentiates the two.

When you close tab by x-button, you only close the tab ? Exact ?

Could you test in WindowWillClose delegate function which is the windowName, to check if a single tab is recognized.

You could also print tabNames to check.