Search code examples
objective-cmacoscocoatoday-extensionosx-today-widget

What action is invoked when today widget center is being closed?


I need to find the way to respond to notification centre being closed/hidden. The reason is that I have a pop up NSMenu in that widget, and if you open it and then close entire notification centre, pop up menu remains on the screen.

I have already tried to implement NSWindowDelegate but there is no such event that defines closing of today widget centre. The closest things I found are -windowDidMiniaturize: and -windowWillClose:. But when side bar closes they are not invoked.


Solution

  • Finally I have found needed method in NSWindowDelegate protocol.

    First step is to add self (in that case it is viewController) as an observer for desired method: windowDidResignKey:

    -(void)viewWillAppear {
      //set this view controller delegate for selector windowDidResignKey
      [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(windowDidResignKey:) name:NSWindowDidResignKeyNotification object:self.view.window];
    }
    

    And second is to implement this method:

    - (void)windowDidResignKey:(NSNotification *)notification {
      //If window did resign key (close today center) - close menus
      if(_sourceLanguageMenu)
        [ _sourceLanguageMenu cancelTracking];
      if(_targetLanguageMenu)
        [ _targetLanguageMenu cancelTracking];
    }