Problems with NSOpenPanel

elvman wrote on Sunday, March 01, 2015:

If you open a NSOpenPanel on OSX in a GLFW app, the OpenGL context is lost and glfwMakeContextCurrent is not setting it back. Try running the following funvtion anywhere in a GLFW app.

void testOpenPanel()
    NSOpenPanel* openPanel = [NSOpenPanel openPanel];
    openPanel.title = @"Choose a .JSON file";
    openPanel.showsResizeIndicator = YES;
    openPanel.showsHiddenFiles = NO;
    openPanel.canChooseDirectories = NO;
    openPanel.canCreateDirectories = YES;
    openPanel.allowsMultipleSelection = NO;
    openPanel.allowedFileTypes = @[@"json"];
    [openPanel beginWithCompletionHandler:^(NSInteger result) {
        if (result == NSModalResponseOK) {
            NSURL *selection = openPanel.URLs[0];
            NSString* path = [selection.path stringByResolvingSymlinksInPath];
            NSLog(@"Path: %@", path);

A quick workaround is placing the following code in main loop:

NSOpenGLContext* context = glfwGetNSGLContext(window);
[context makeCurrentContext]

This can also be done with GLFW functions:


Also glfwGetCurrentContext returns GLFW’s context although NSOpenPanel has switched it to a different one. I suggest checking current context every frame and change it back to GLFW’s one if it has changed.
Full source code is available here:

elvman wrote on Monday, March 02, 2015:

Actually another solution is to lock the NSOpenGLView before drawing and unlocking it afterwards with [context.view lockFocus] and [context.view unlockFocus] accordingly.

elvman wrote on Thursday, April 02, 2015:

Please merge my pull request. It fixes the problem, that after opening NSOpenPanel (which changes current OpenGL context), glfwMakeContextCurrent does not set it back.