From ec43f740a743758f9b7d89cef279e74e6d5367e2 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Fri, 3 May 2013 21:03:26 +0800 Subject: [PATCH] Add the generialized showMessageBox API. --- atom.gyp | 6 +- ..._api_file_dialog.cc => atom_api_dialog.cc} | 62 +++++++++++++------ ...om_api_file_dialog.h => atom_api_dialog.h} | 8 ++- browser/api/lib/dialog.coffee | 3 +- browser/message_box.h | 30 +++++++++ browser/message_box_mac.mm | 44 +++++++++++++ common/api/atom_extensions.h | 2 +- 7 files changed, 129 insertions(+), 26 deletions(-) rename browser/api/{atom_api_file_dialog.cc => atom_api_dialog.cc} (75%) rename browser/api/{atom_api_file_dialog.h => atom_api_dialog.h} (86%) create mode 100644 browser/message_box.h create mode 100644 browser/message_box_mac.mm diff --git a/atom.gyp b/atom.gyp index cfd188091dd2..7f86ec5583a9 100644 --- a/atom.gyp +++ b/atom.gyp @@ -28,12 +28,12 @@ 'browser/api/atom_api_app.h', 'browser/api/atom_api_browser_ipc.cc', 'browser/api/atom_api_browser_ipc.h', + 'browser/api/atom_api_dialog.cc', + 'browser/api/atom_api_dialog.h', 'browser/api/atom_api_event.cc', 'browser/api/atom_api_event.h', 'browser/api/atom_api_event_emitter.cc', 'browser/api/atom_api_event_emitter.h', - 'browser/api/atom_api_file_dialog.cc', - 'browser/api/atom_api_file_dialog.h', 'browser/api/atom_api_window.cc', 'browser/api/atom_api_window.h', 'browser/api/atom_browser_bindings.cc', @@ -57,6 +57,8 @@ 'browser/browser.h', 'browser/browser_mac.mm', 'browser/browser_observer.h', + 'browser/message_box.h', + 'browser/message_box_mac.mm', 'browser/native_window.cc', 'browser/native_window.h', 'browser/native_window_mac.h', diff --git a/browser/api/atom_api_file_dialog.cc b/browser/api/atom_api_dialog.cc similarity index 75% rename from browser/api/atom_api_file_dialog.cc rename to browser/api/atom_api_dialog.cc index baebfe76a3b6..5590349b914b 100644 --- a/browser/api/atom_api_file_dialog.cc +++ b/browser/api/atom_api_dialog.cc @@ -2,12 +2,13 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#include "browser/api/atom_api_file_dialog.h" +#include "browser/api/atom_api_dialog.h" #include #include "base/utf_string_conversions.h" #include "base/values.h" +#include "browser/message_box.h" #include "browser/native_window.h" namespace atom { @@ -21,23 +22,44 @@ base::FilePath V8ValueToFilePath(v8::Handle path) { return base::FilePath::FromUTF8Unsafe(path_string); } -ui::SelectFileDialog::Type IntToDialogType(int type) { - switch (type) { - case ui::SelectFileDialog::SELECT_FOLDER: - return ui::SelectFileDialog::SELECT_FOLDER; - case ui::SelectFileDialog::SELECT_SAVEAS_FILE: - return ui::SelectFileDialog::SELECT_SAVEAS_FILE; - case ui::SelectFileDialog::SELECT_OPEN_FILE: - return ui::SelectFileDialog::SELECT_OPEN_FILE; - case ui::SelectFileDialog::SELECT_OPEN_MULTI_FILE: - return ui::SelectFileDialog::SELECT_OPEN_MULTI_FILE; - default: - return ui::SelectFileDialog::SELECT_NONE; - } -} - } // namespace +v8::Handle ShowMessageBox(const v8::Arguments &args) { + v8::HandleScope scope; + + if (!args[0]->IsNumber() || // process_id + !args[1]->IsNumber() || // routing_id + !args[2]->IsNumber() || // type + !args[3]->IsArray() || // buttons + !args[4]->IsString() || // title + !args[5]->IsString() || // message + !args[6]->IsString()) // detail + return node::ThrowTypeError("Bad argument"); + + int process_id = args[0]->IntegerValue(); + int routing_id = args[1]->IntegerValue(); + NativeWindow* window = NativeWindow::FromRenderView(process_id, routing_id); + if (!window) + return node::ThrowError("Window not found"); + + gfx::NativeWindow owning_window = window->GetNativeWindow(); + + MessageBoxType type = (MessageBoxType)(args[2]->IntegerValue()); + + std::vector buttons; + v8::Handle v8_buttons = v8::Handle::Cast(args[3]); + for (uint32_t i = 0; i < v8_buttons->Length(); ++i) + buttons.push_back(*v8::String::Utf8Value(v8_buttons->Get(i))); + + std::string title(*v8::String::Utf8Value(args[4])); + std::string message(*v8::String::Utf8Value(args[5])); + std::string detail(*v8::String::Utf8Value(args[6])); + + int result = atom::ShowMessageBox( + owning_window, type, buttons, title, message, detail); + return v8::Integer::New(result); +} + FileDialog::FileDialog(v8::Handle wrapper) : EventEmitter(wrapper), dialog_(ui::SelectFileDialog::Create(this, NULL)) { @@ -133,7 +155,7 @@ v8::Handle FileDialog::SelectFile(const v8::Arguments &args) { int callback_id = args[8]->IntegerValue(); self->dialog_->SelectFile( - IntToDialogType(type), + (ui::SelectFileDialog::Type)(type), UTF8ToUTF16(title), default_path, file_types.extensions.size() > 0 ? &file_types : NULL, @@ -152,7 +174,7 @@ void FileDialog::FillTypeInfo(ui::SelectFileDialog::FileTypeInfo* file_types, file_types->support_drive = true; for (uint32_t i = 0; i < v8_file_types->Length(); ++i) { - v8::Handle element = v8_file_types->Get(i)->ToObject(); + v8::Handle element = v8_file_types->Get(i)->ToObject(); std::string description(*v8::String::Utf8Value( element->Get(v8::String::New("description")))); @@ -182,10 +204,12 @@ void FileDialog::Initialize(v8::Handle target) { NODE_SET_PROTOTYPE_METHOD(t, "selectFile", SelectFile); target->Set(v8::String::NewSymbol("FileDialog"), t->GetFunction()); + + NODE_SET_METHOD(target, "showMessageBox", ShowMessageBox); } } // namespace api } // namespace atom -NODE_MODULE(atom_browser_file_dialog, atom::api::FileDialog::Initialize) +NODE_MODULE(atom_browser_dialog, atom::api::FileDialog::Initialize) diff --git a/browser/api/atom_api_file_dialog.h b/browser/api/atom_api_dialog.h similarity index 86% rename from browser/api/atom_api_file_dialog.h rename to browser/api/atom_api_dialog.h index 4ca4e822a4ff..7c7d9f4230d4 100644 --- a/browser/api/atom_api_file_dialog.h +++ b/browser/api/atom_api_dialog.h @@ -2,8 +2,8 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#ifndef ATOM_BROWSER_API_ATOM_API_FILE_DIALOG_H_ -#define ATOM_BROWSER_API_ATOM_API_FILE_DIALOG_H_ +#ifndef ATOM_BROWSER_API_ATOM_API_DIALOG_H_ +#define ATOM_BROWSER_API_ATOM_API_DIALOG_H_ #include "browser/api/atom_api_event_emitter.h" #include "ui/shell_dialogs/select_file_dialog.h" @@ -12,6 +12,8 @@ namespace atom { namespace api { +v8::Handle ShowMessageBox(const v8::Arguments &args); + class FileDialog : public EventEmitter, public ui::SelectFileDialog::Listener { public: @@ -44,4 +46,4 @@ class FileDialog : public EventEmitter, } // namespace atom -#endif // ATOM_BROWSER_API_ATOM_API_FILE_DIALOG_H_ +#endif // ATOM_BROWSER_API_ATOM_API_DIALOG_H_ diff --git a/browser/api/lib/dialog.coffee b/browser/api/lib/dialog.coffee index 250ada4d15ee..c278bc32b9e7 100644 --- a/browser/api/lib/dialog.coffee +++ b/browser/api/lib/dialog.coffee @@ -1,7 +1,8 @@ +binding = process.atomBinding 'dialog' EventEmitter = require('events').EventEmitter ipc = require 'ipc' -FileDialog = process.atomBinding('file_dialog').FileDialog +FileDialog = binding.FileDialog FileDialog.prototype.__proto__ = EventEmitter.prototype callbacksInfo = {} diff --git a/browser/message_box.h b/browser/message_box.h new file mode 100644 index 000000000000..65ca6da3f9b8 --- /dev/null +++ b/browser/message_box.h @@ -0,0 +1,30 @@ +// Copyright (c) 2013 GitHub, Inc. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef BROWSER_MESSAGE_BOX_H_ +#define BROWSER_MESSAGE_BOX_H_ + +#include +#include + +#include "ui/gfx/native_widget_types.h" + +namespace atom { + +enum MessageBoxType { + MESSAGE_BOX_TYPE_NONE = 0, + MESSAGE_BOX_TYPE_INFORMATION, + MESSAGE_BOX_TYPE_WARNING +}; + +int ShowMessageBox(gfx::NativeWindow parent, + MessageBoxType type, + const std::vector& buttons, + const std::string& title, + const std::string& message, + const std::string& detail); + +} // namespace atom + +#endif // BROWSER_MESSAGE_BOX_H_ diff --git a/browser/message_box_mac.mm b/browser/message_box_mac.mm new file mode 100644 index 000000000000..d03fe0efa756 --- /dev/null +++ b/browser/message_box_mac.mm @@ -0,0 +1,44 @@ +// Copyright (c) 2013 GitHub, Inc. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "browser/message_box.h" + +#import + +#include "base/strings/sys_string_conversions.h" + +namespace atom { + +int ShowMessageBox(gfx::NativeWindow parent, + MessageBoxType type, + const std::vector& buttons, + const std::string& title, + const std::string& message, + const std::string& detail) { + // Ignore the title; it's the window title on other platforms and ignorable. + NSAlert* alert = [[[NSAlert alloc] init] autorelease]; + [alert setMessageText:base::SysUTF8ToNSString(message)]; + [alert setInformativeText:base::SysUTF8ToNSString(detail)]; + + switch (type) { + case MESSAGE_BOX_TYPE_INFORMATION: + [alert setAlertStyle:NSInformationalAlertStyle]; + break; + case MESSAGE_BOX_TYPE_WARNING: + [alert setAlertStyle:NSWarningAlertStyle]; + break; + default: + break; + } + + for (size_t i = 0; i < buttons.size(); ++i) { + NSString* title = base::SysUTF8ToNSString(buttons[i]); + NSButton* button = [alert addButtonWithTitle:title]; + [button setTag:i]; + } + + return [alert runModal]; +} + +} // namespace atom diff --git a/common/api/atom_extensions.h b/common/api/atom_extensions.h index 4faacb02cdb3..fdff420122d4 100644 --- a/common/api/atom_extensions.h +++ b/common/api/atom_extensions.h @@ -10,7 +10,7 @@ NODE_EXT_LIST_START // Module names start with `atom_browser_` can only be used by browser process. NODE_EXT_LIST_ITEM(atom_browser_app) -NODE_EXT_LIST_ITEM(atom_browser_file_dialog) +NODE_EXT_LIST_ITEM(atom_browser_dialog) NODE_EXT_LIST_ITEM(atom_browser_ipc) NODE_EXT_LIST_ITEM(atom_browser_window)