Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make NSPopover 's detached window become modal window

In MAC OS X 10.7, Apple introduced a new class called NSPopover and you can even drag away that popover view to become an independent NSWindow. However, I want to prevent user interaction to the main window until the detached window is closed. How can I safely do this?

Actually, a more common (and even more stupid) question should be, how to prevent any user interaction until current front window returns? I am noob to tread programming also.

like image 830
schen Avatar asked Dec 14 '25 16:12

schen


1 Answers

I kind of found the solution myself. It looks working fine now.

To do this, after the detached window ordered to front and become key window, the following code will make it a modal window (where currModalSession is an iVar defined by myself).

- (void)windowDidBecomeKey:(NSNotification *)notification {
    if (notification.object == detachedWindow) {
        if (!detachedWindow.isModalPanel) {
            currModalSession = [NSApp beginModalSessionForWindow:detachedWindow];
            [NSApp runModalSession:currModalSession];
        }
    }
}

Also, you have to end each Modal Session you have opened. So the following code does the job:

- (void)windowWillClose:(NSNotification *)notification {
    if (notification.object == detachedWindow) {
        if (currModalSession) {
            [NSApp endModalSession:currModalSession];
        }
    }
}

Note: you have to use Modal Session here rather than runModalForWindow for two reasons:

  1. otherwise the main window won't be blocked right away. I don't quite get the reason yet. One possible explanation is: runModalForWindow will not just block user interactions but also internal communications, so main window might need more time to be ready.
  2. if you plan to run another framework modal dialog (e.g. NSOpenPanel) from the detached window, when return, the detached window will become key window before the new modal dialog close, namely runModalForWindow will freeze another to be closed window. That means to be closed window won't be closed.
like image 172
schen Avatar answered Dec 19 '25 06:12

schen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!