tseli0s/nvdialog

Dialog stuck after pressing Ok button on macos

Closed this issue · 6 comments

Hi,
I tried to compile and use this library on MacOS (12.6 Monterey), but the problem is that when I show a dialog if I click on OK button it hangs.
The issue is exactly same as this one:

https://developer.apple.com/forums/thread/19540

Sorry, that problem was related to other parts of my application. I created a test project which only includes this library and it worked fine.

Could you share the actual problem? I'd like to add some note in the documentation so that other users don't hit the same bug.

I could not actually find the source of issue. If I use that in a sample project it works fine but when I add it to my main project, it stuck and I think the issue might be related to context,
so I started modifying the library to use "CFUserNotificationDisplayAlert" instead of NSAlert (by adding a definition at top of the file so I can easily switch between them) and it worked perfectly.

here is the content of "nvdialog_dialog_box.m":

#include "nvdialog.h"
#include "nvdialog_cocoa.h"

#include <AppKit/AppKit.h>

#include "../../nvdialog_assert.h"

#define useNSAlert 0

struct _NvdDialogBox {
#if useNSAlert
    NSAlert         *window_handle;
#else
    int             *window_handle;
#endif
    const char      *msg;
    const char      *content;
    NvdDialogType   type;
};

NvdDialogBox *nvd_dialog_box_cocoa(const char *title, const char *message, NvdDialogType type)
{
    NvdDialogBox *dialog = malloc(sizeof(struct _NvdDialogBox));
   	NVD_RETURN_IF_NULL(dialog);

    dialog->content = message;
    dialog->msg = title;
    dialog->type = type;

#if useNSAlert
    
    NSAlert *nativedlg = dialog->window_handle = [[NSAlert alloc] init];

    nativedlg.messageText = @(dialog->msg);
    nativedlg.informativeText = @(dialog->content);

    switch (type) {
    default:
    case NVD_DIALOG_SIMPLE:
        nativedlg.alertStyle = NSAlertStyleInformational;
        break;
    case NVD_DIALOG_WARNING:
        nativedlg.alertStyle = NSAlertStyleWarning;
        break;
    case NVD_DIALOG_ERROR:
        nativedlg.alertStyle = NSAlertStyleCritical;
        break;
    }
#else
    
    CFOptionFlags alertStyle;
    
    switch (type) {
    default:
    case NVD_DIALOG_SIMPLE:
            alertStyle = kCFUserNotificationNoteAlertLevel;
        break;
    case NVD_DIALOG_WARNING:
            alertStyle = kCFUserNotificationCautionAlertLevel;
        break;
    case NVD_DIALOG_ERROR:
            alertStyle = kCFUserNotificationStopAlertLevel;
        break;
    }

    CFStringRef alertHeader = CFStringCreateWithCString(kCFAllocatorDefault, dialog->msg, kCFStringEncodingUTF8);
    
    CFStringRef alertContent = CFStringCreateWithCString(kCFAllocatorDefault, dialog->content, kCFStringEncodingUTF8);
    
    CFOptionFlags cfRes;
    
    CFUserNotificationDisplayAlert(-1, alertStyle,
            NULL, NULL, NULL,
            alertHeader,
            alertContent,
            CFSTR("OK"),
            NULL,
            NULL,
            &cfRes);
#endif
    return dialog;
}

void nvd_show_dialog_cocoa(NvdDialogBox *dialog)
{
#if useNSAlert
    [dialog->window_handle runModal];
//    [dialog->window_handle release];
#else
#endif
}

void *nvd_dialog_box_get_raw_cocoa(NvdDialogBox *dlg)
{
#if useNSAlert
    return (__bridge void *)(dlg->window_handle);
#else
    return dlg->window_handle;
#endif
}

Consider making a PR then if the code you've posted actually works. Unfortunately I can't detect any issues with macOS since I don't use it.

@tseli0s, I need to spend some time to update all other objective-c files.
about the issue you can take a look at this link as well, might be helpful
https://stackoverflow.com/questions/48550829/nsalert-wont-disappear

Sure, take your time. But don't close the issue, this one has not been resolved yet so it has to remain open.