Question

I'm having hard time understanding how to close NSOpenPanel. It does close automatically, but it takes way longer than I want it to take.

Here is my code:

- (IBAction)startProcess:(id)sender
{

   NSString *path = [Document openVideoFile]; // open file

   // some other method calls here
}

// NSOpenPanel for file picking
+(NSString*) openVideoFile
{
   NSOpenPanel *openDialog = [NSOpenPanel openPanel];

   //set array of the file types

   NSArray *fileTypesArray = [[NSArray alloc] arrayWithObjects:@"mp4", nil];

   [openDialog setCanChooseFiles:YES];
   [openDialog setAllowedFileTypes:fileTypesArray];
   [openDialog setAllowsMultipleSelection:FALSE];

   if ([openDialog runModal] == NSFileHandlingPanelOKButton)
   {      
      NSArray *files = [openDialog URLs];

      return [[files objectAtIndex:0] path];   
   }
   else
   {
      return @"cancelled";
   }
   return nil; // shouldn't be reached
}

Interesting thing is that if user clicks "Cancel", the panel closes right away, but if user selects a file from the list, the panel stays open until the program reaches the end of the startProcess method.

If anyone knows how to close the panel right away, after the user clicks on OK button after selecting a file, I would really appreciate any help!

Thank you!

Was it helpful?

Solution

My guess is the system doesn't actually start the animation that removes the open panel until later in the run loop, after startProcess: returns. So if your “some other method calls here” takes a long time to run, it will take a long time before the animation starts.

The ideal solution is to perform your slow “other method calls” on a background queue, because you should try to avoid blocking the main thread. But that might require you to make various things thread-safe that currently aren't, which could be hard.

A different approach is just to put off doing them until later in the run loop:

- (IBAction)startProcess:(id)sender {
    NSString *path = [Document openVideoFile]; // open file
    dispatch_async(dispatch_get_main_queue(), ^{
       // some other method calls here
    });
}

or you might need to put it off slightly longer than that:

- (IBAction)startProcess:(id)sender {
    NSString *path = [Document openVideoFile]; // open file
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC / 100), dispatch_get_main_queue(), ^(void){
        // some other method calls here
    });
}

OTHER TIPS

The advice to do the slow work on a background thread is good.

But to close the panel sooner, just invoke [openDialog close] before you return from -openVideoFile.

Another solution is to give the runloop some time to process events before continuing by using for example [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:1.0]]; ... like

if ([openDialog runModal] == NSFileHandlingPanelOKButton)
{      
  [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:1.0]];

  NSArray *files = [openDialog URLs];

  return [[files objectAtIndex:0] path];   
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top