Question

I'm invoking a NSOpenPanel from a thread created by boost C++.

the panel behaves erratically and doesn't respond well to mouse, that is clicking on objects does nothing sometime when clicking on top level combo box does improve response.

do i've to run a separate runloop I'm doing a runModalForDirectory which should take care of running its own loop.

I've also created a separate objc class which does performSelectorOnMainThread to show panel in main thread but still the behavior is same.

[ps performSelectorOnMainThread:@selector(showOpenPanel) withObject:nil 
                      waitUntilDone:YES
                      modes:[NSArray arrayWithObject:NSRunLoopCommonModes]];

I've also tried with waitUntilDone:NO and running a CFRunLoopRunInMode which isn't helping either.

- (bool) showOpenPanel
{
    NSOpenPanel *op = [NSOpenPanel openPanel];
    [op setAllowsMultipleSelection:YES];
    [op setTitle:@"Choose File"];
    [op setMessage:@"Choose file for Importing."];
    [op setFloatingPanel:true]; 
    bool result =[op runModalForDirectory:NSHomeDirectory() file:nil types:self.fileTypes];
    if (result == NSOKButton) {
        [self setSelectedFiles:[op filenames]];
        [self setLastShowResult:true];
    }
    else {
        [self setLastShowResult:false];
    }

    [self setPanelIsDone:true]; 
    return self.lastShowResult;
}
Was it helpful?

Solution

NSOpenPanel is part of AppKit. AppKit functions and classes can only be safely used on the main thread.

Show us the code you used with performSelectorOnMainThread so we can help figure out why you might still be seeing problems. I suspect you're calling individual methods with it--don't; it won't work the way you expect. Call back to the main thread for the totality of your interaction with NSOpenPanel.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top