#import #include "dialogs.h" using crossbind::PageDialog; namespace { NSString *ns(const std::string &text) { return [NSString stringWithUTF8String:text.c_str()] ?: @""; } NSAlert *alertOf(const PageDialog &dialog, NSArray *buttons) { NSAlert *alert = [NSAlert new]; alert.messageText = ns(crossbind::appName()); alert.informativeText = ns(dialog.message()); for (NSString *button in buttons) [alert addButtonWithTitle:button]; return alert; } // A sheet on the page's window, as Safari shows them; without one, a dialog of its own. void show(NSAlert *alert, const PageDialog &dialog, void (^done)(NSModalResponse)) { NSWindow *window = (__bridge NSWindow *)windowOf(dialog); if (window) { [alert beginSheetModalForWindow:window completionHandler:done]; } else { done([alert runModal]); } } } // namespace void showPageDialog(PageDialog dialog) { switch (dialog.kind()) { case PageDialog::Kind::alert: { show(alertOf(dialog, @[ @"OK" ]), dialog, ^(NSModalResponse) { dialog.accept(); }); return; } case PageDialog::Kind::confirm: { show(alertOf(dialog, @[ @"OK", @"Cancel" ]), dialog, ^(NSModalResponse response) { if (response == NSAlertFirstButtonReturn) dialog.accept(); else dialog.cancel(); }); return; } case PageDialog::Kind::prompt: { NSAlert *alert = alertOf(dialog, @[ @"OK", @"Cancel" ]); NSTextField *field = [NSTextField textFieldWithString:ns(dialog.defaultText())]; field.frame = NSMakeRect(0, 0, 260, 24); alert.accessoryView = field; alert.window.initialFirstResponder = field; show(alert, dialog, ^(NSModalResponse response) { if (response == NSAlertFirstButtonReturn) dialog.accept(field.stringValue.UTF8String ?: ""); else dialog.cancel(); }); return; } } }