Capture NSWindow tab events

I have successfully implemented tabs in my macOS app through the functionality exposed in

NSWindow
(i.e.
tabbingMode
,
tabbingIdentifier
,
newWindowForTab(_:)
, etc.). I can create and close tabs in a window, create a new window and not a tab, and move programmatically tabs to new windows. However, I cannot detect when the user decides to grab a tab and detach it from a group of tabs into a new window (similar to what you would get calling
moveTabToNewWindow(_:)
)


Is there any way to detect when a tab has been detached by the user? In the case that it is impossible, I would prefer to disable that option.


Regards,

Accepted Reply

Hi @janabana, thanks for your response.


I am actually using bare

NSWindow
s without any
NSWindowController
or
NSDocument
and indeed it occurred to me to monitor the array of tab windows or even attach an observer to check when the
window.tabGroup
has been altered. It just seem unnecessarily cumbersome since then you need to keep track all windows (tabbed and not tabbed) and the groups attached to them. I was hoping there was a hidden responder chain message somewhere or any other obscure way that won't require me to keep/diff a lot of state.


In any case, thank you for taking the time to answer me. It is appreciated.

Replies

This sounded like an interesting puzzle so I did a bit of experimenting with the following code:


-(void)doStuff {
    NSArray *theTabs = [myWindow tabbedWindows];
    NSMutableArray *tabDocs = [[NSMutableArray alloc] init];
    if([theTabs count] > 1) {
        for(NSInteger i = 0; i < [theTabs count]; i++) {
            [tabDocs addObject:[[NSDocumentController sharedDocumentController] documentForWindow:[theTabs objectAtIndex:i]]];
        }
    }
    NSArray *theDocuments = [[NSDocumentController sharedDocumentController] documents];
    NSLog(@"tabs %@ docs: %@",tabDocs, theDocuments);
}

I put this in the WindowController with its window named myWindow in an IBOutlet. I suspect a better place for this code would maybe be in the DocumentController.


When I run this, if I have one window with multiple tabs, the array of documents in theDocuments matches the documents in tabDocs. If a tabbed window is dragged out then the arrays no longer match. Maybe this can give you a clue for how to solve your problem.

Hi @janabana, thanks for your response.


I am actually using bare

NSWindow
s without any
NSWindowController
or
NSDocument
and indeed it occurred to me to monitor the array of tab windows or even attach an observer to check when the
window.tabGroup
has been altered. It just seem unnecessarily cumbersome since then you need to keep track all windows (tabbed and not tabbed) and the groups attached to them. I was hoping there was a hidden responder chain message somewhere or any other obscure way that won't require me to keep/diff a lot of state.


In any case, thank you for taking the time to answer me. It is appreciated.