Add atom.showSaveDialog

Reply messages now always begin with an array of the reply id and the callback index.
This commit is contained in:
Nathan Sobo 2012-08-30 10:52:35 -06:00
parent 3cc8297460
commit 8b5b7de6e0
4 changed files with 43 additions and 7 deletions

View File

@ -53,6 +53,11 @@ bool AtomCefClient::OnProcessMessageReceived(CefRefPtr<CefBrowser> browser,
return true;
}
if (name == "showSaveDialog") {
ShowSaveDialog(messageId, browser);
return true;
}
return false;
}

View File

@ -112,6 +112,8 @@ class AtomCefClient : public CefClient,
std::vector<std::string> buttonLabels,
CefRefPtr<CefBrowser> browser);
void ToggleDevTools(CefRefPtr<CefBrowser> browser);
void ShowSaveDialog(int replyId, CefRefPtr<CefBrowser> browser);
CefRefPtr<CefListValue> CreateReplyDescriptor(int replyId, int callbackIndex);
IMPLEMENT_REFCOUNTING(AtomCefClient);
IMPLEMENT_LOCKING(AtomCefClient);

View File

@ -42,9 +42,8 @@ void AtomCefClient::Confirm(int replyId,
CefRefPtr<CefProcessMessage> replyMessage = CefProcessMessage::Create("reply");
CefRefPtr<CefListValue> replyArguments = replyMessage->GetArgumentList();
replyArguments->SetSize(2);
replyArguments->SetInt(0, replyId);
replyArguments->SetInt(1, clickedButtonTag);
replyArguments->SetSize(1);
replyArguments->SetList(0, CreateReplyDescriptor(replyId, clickedButtonTag));
browser->SendProcessMessage(PID_RENDERER, replyMessage);
}
@ -52,3 +51,29 @@ void AtomCefClient::ToggleDevTools(CefRefPtr<CefBrowser> browser) {
AtomWindowController *windowController = [[browser->GetHost()->GetWindowHandle() window] windowController];
[windowController toggleDevTools];
}
void AtomCefClient::ShowSaveDialog(int replyId, CefRefPtr<CefBrowser> browser) {
CefRefPtr<CefProcessMessage> replyMessage = CefProcessMessage::Create("reply");
CefRefPtr<CefListValue> replyArguments = replyMessage->GetArgumentList();
NSSavePanel *panel = [NSSavePanel savePanel];
if ([panel runModal] == NSFileHandlingPanelOKButton) {
CefString path = CefString([[[panel URL] path] UTF8String]);
replyArguments->SetSize(2);
replyArguments->SetString(1, path);
}
else {
replyArguments->SetSize(1);
}
replyArguments->SetList(0, CreateReplyDescriptor(replyId, 0));
browser->SendProcessMessage(PID_RENDERER, replyMessage);
}
CefRefPtr<CefListValue> AtomCefClient::CreateReplyDescriptor(int replyId, int callbackIndex) {
CefRefPtr<CefListValue> descriptor = CefListValue::Create();
descriptor->SetSize(2);
descriptor->SetInt(0, replyId);
descriptor->SetInt(1, callbackIndex);
return descriptor;
}

View File

@ -8,16 +8,17 @@ originalSendMessageToBrowserProcess = atom.sendMessageToBrowserProcess
atom.pendingBrowserProcessCallbacks = {}
atom.sendMessageToBrowserProcess = (name, data, callback) ->
atom.sendMessageToBrowserProcess = (name, data, callbacks) ->
messageId = messageIdCounter++
data.unshift(messageId)
@pendingBrowserProcessCallbacks[messageId] = callback
callbacks = [callbacks] if typeof callbacks is 'function'
@pendingBrowserProcessCallbacks[messageId] = callbacks
originalSendMessageToBrowserProcess(name, data)
atom.receiveMessageFromBrowserProcess = (name, data) ->
if name is 'reply'
[messageId, callbackIndex] = data
@pendingBrowserProcessCallbacks[messageId]?[callbackIndex]?()
[messageId, callbackIndex] = data.shift()
@pendingBrowserProcessCallbacks[messageId]?[callbackIndex]?(data)
atom.open = (args...) ->
@sendMessageToBrowserProcess('open', args)
@ -33,6 +34,9 @@ atom.confirm = (message, detailedMessage, buttonLabelsAndCallbacks...) ->
callbacks.push(buttonLabelsAndCallbacks.shift())
@sendMessageToBrowserProcess('confirm', args, callbacks)
atom.showSaveDialog = (callback) ->
@sendMessageToBrowserProcess('showSaveDialog', [], callback)
atom.toggleDevTools = (args...)->
@sendMessageToBrowserProcess('toggleDevTools', args)