Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSClosableWindowMask dialog acting strange

I have a Safari browser plugin, and in it, I want to open an NSWindow to display a copyright note. While the Ok button in the dialog closes the dialog's window, and works perfectly, when I click on the red close window "x" in the top left corner, it also closes the window, but it's parent window (the entire browser tab, I had the plugin running in), remains disabled, as if still a modal window was open somewhere.

I even tried to attach a new selector to the window close notification, which runs the same code as the Ok button, but still, it's not working properly.

Here is the relevant part of the code:

- (void) closeBox
{
    // called when the Ok button pressed
    [NSApp abortModal];

}

- (void)closeClicked:(NSNotification *)notification
{
    // called when the close window 'x' button pressed
    NSLog(@"Closed");
    [NSApp abortModal];
}


- (void) openBox
{
    NSRect frame = NSMakeRect(0, 0, 300, 250);
     mwin  = [[[NSWindow alloc] initWithContentRect:frame
                                     styleMask:NSClosableWindowMask |NSTitledWindowMask
                                       backing:NSBackingStoreBuffered
                                         defer:NO] autorelease];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(closeClicked:)
                                             name:NSWindowWillCloseNotification
                                           object:mwin];
    NSButton * bn;
    bn = [[[NSButton alloc] initWithFrame:NSMakeRect(10, 10, 100, 20) ] autorelease];

    [bn setButtonType:NSMomentaryPushInButton];
    [bn setTitle:@"Ok"];
    [bn setTarget:self];
    [bn setAction:@selector(closeBox)];

    [[mwin contentView] addSubview:bn];

    [NSApp runModalForWindow:mwin];   
}
like image 253
Mkoch Avatar asked Dec 10 '13 10:12

Mkoch


1 Answers

I have modified your code try like this below:-

- (void) closeBox
{
    // called when the Ok button pressed
  //Commented this line 
 // [NSApp abortModal];

    [mwin performClose:mwin];// Modified this line 


}

   //Modified below notification just comment the parameter
    - (void)closeClicked/*:(NSNotification *)notification*/
    {

    [NSApp abortModal];

}


- (void) openBox
{
    NSRect frame = NSMakeRect(0, 0, 300, 250);
    mwin  = [[[[NSWindow alloc] initWithContentRect:frame
                                         styleMask:NSClosableWindowMask |NSTitledWindowMask
                                           backing:NSBackingStoreBuffered
                                            defer:NO]retain]autorelease];
  //Modified notification below    

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(closeClicked)
                                                 name:NSWindowWillCloseNotification
                                               object:nil];
    NSButton * bn;
    bn = [[[NSButton alloc] initWithFrame:NSMakeRect(10, 10, 100, 20) ] autorelease];

    [bn setButtonType:NSMomentaryPushInButton];
    [bn setTitle:@"Ok"];
    [bn setTarget:self];
    [bn setAction:@selector(closeBox)];

    [[mwin contentView] addSubview:bn];

    [NSApp runModalForWindow:mwin];   
}
like image 182
Hussain Shabbir Avatar answered Oct 04 '22 20:10

Hussain Shabbir