diff --git a/native/atom_cef_client.cpp b/native/atom_cef_client.cpp index 985441c23..b9adee9fd 100644 --- a/native/atom_cef_client.cpp +++ b/native/atom_cef_client.cpp @@ -22,19 +22,29 @@ bool AtomCefClient::OnProcessMessageReceived(CefRefPtr browser, CefProcessId source_process, CefRefPtr message) { std::string name = message->GetName().ToString(); - CefRefPtr argumentList = message->GetArgumentList(); + CefRefPtr argumentList = message->GetArgumentList(); int messageId = argumentList->GetInt(0); - if (name == "open") { - bool hasArguments = message->GetArgumentList()->GetSize() > 1; - hasArguments ? Open(message->GetArgumentList()->GetString(1)) : Open(); + bool hasArguments = argumentList->GetSize() > 1; + hasArguments ? Open(argumentList->GetString(1)) : Open(); return true; } if (name == "newWindow") { NewWindow(); return true; } + if (name == "confirm") { + std::string message = argumentList->GetString(1).ToString(); + std::string detailedMessage = argumentList->GetString(2).ToString(); + std::vector buttonLabels(argumentList->GetSize() - 3); + for (int i = 3; i < argumentList->GetSize(); i++) { + buttonLabels[i - 3] = argumentList->GetString(i).ToString(); + } + + Confirm(messageId, message, detailedMessage, buttonLabels, browser); + return true; + } return false; diff --git a/native/atom_cef_client.h b/native/atom_cef_client.h index 014de4701..424f7b821 100644 --- a/native/atom_cef_client.h +++ b/native/atom_cef_client.h @@ -91,7 +91,7 @@ class AtomCefClient : public CefClient, // CefLifeSpanHandler methods virtual void OnAfterCreated(CefRefPtr browser) OVERRIDE; virtual void OnBeforeClose(CefRefPtr browser) OVERRIDE; - + // CefLoadHandler methods virtual void OnLoadError(CefRefPtr browser, @@ -108,6 +108,11 @@ class AtomCefClient : public CefClient, void Open(std::string path); void Open(); void NewWindow(); + void Confirm(int replyId, + std::string message, + std::string detailedMessage, + std::vector buttonLabels, + CefRefPtr browser); IMPLEMENT_REFCOUNTING(AtomCefClient); IMPLEMENT_LOCKING(AtomCefClient); diff --git a/native/atom_cef_client_mac.mm b/native/atom_cef_client_mac.mm index bbd95d2a8..def640cca 100644 --- a/native/atom_cef_client_mac.mm +++ b/native/atom_cef_client_mac.mm @@ -20,4 +20,29 @@ void AtomCefClient::Open() { void AtomCefClient::NewWindow() { [(AtomApplication *)[AtomApplication sharedApplication] open:nil]; -} \ No newline at end of file +} + +void AtomCefClient::Confirm(int replyId, + std::string message, + std::string detailedMessage, + std::vector buttonLabels, + CefRefPtr browser) { + NSAlert *alert = [[[NSAlert alloc] init] autorelease]; + [alert setMessageText:[NSString stringWithCString:message.c_str() encoding:NSUTF8StringEncoding]]; + [alert setInformativeText:[NSString stringWithCString:detailedMessage.c_str() encoding:NSUTF8StringEncoding]]; + + for (int i = 0; i < buttonLabels.size(); i++) { + NSString *title = [NSString stringWithCString:buttonLabels[i].c_str() encoding:NSUTF8StringEncoding]; + NSButton *button = [alert addButtonWithTitle:title]; + [button setTag:i]; + } + + NSUInteger clickedButtonTag = [alert runModal]; + + CefRefPtr replyMessage = CefProcessMessage::Create("reply"); + CefRefPtr replyArguments = replyMessage->GetArgumentList(); + replyArguments->SetSize(2); + replyArguments->SetInt(0, replyId); + replyArguments->SetInt(1, clickedButtonTag); + browser->SendProcessMessage(PID_RENDERER, replyMessage); +} diff --git a/src/app/atom.coffee b/src/app/atom.coffee index c406c72ef..359e88aa2 100644 --- a/src/app/atom.coffee +++ b/src/app/atom.coffee @@ -15,7 +15,9 @@ atom.sendMessageToBrowserProcess = (name, data, callback) -> originalSendMessageToBrowserProcess(name, data) atom.receiveMessageFromBrowserProcess = (name, data) -> - console.log "RECEIVE MESSAGE IN JS", name, data + if name is 'reply' + [messageId, callbackIndex] = data + @pendingBrowserProcessCallbacks[messageId]?[callbackIndex]?() atom.open = (args...) -> @sendMessageToBrowserProcess('open', args) @@ -23,6 +25,14 @@ atom.open = (args...) -> atom.newWindow = (args...) -> @sendMessageToBrowserProcess('newWindow', args) +atom.confirm = (message, detailedMessage, buttonLabelsAndCallbacks...) -> + args = [message, detailedMessage] + callbacks = [] + while buttonLabelsAndCallbacks.length + args.push(buttonLabelsAndCallbacks.shift()) + callbacks.push(buttonLabelsAndCallbacks.shift()) + @sendMessageToBrowserProcess('confirm', args, callbacks) + atom.getRootViewStateForPath = (path) -> if json = localStorage[path] JSON.parse(json)