Add the generialized showMessageBox API.
authorCheng Zhao <zcbenz@gmail.com>
Fri, 3 May 2013 13:03:26 +0000 (21:03 +0800)
committerCheng Zhao <zcbenz@gmail.com>
Fri, 3 May 2013 13:03:26 +0000 (21:03 +0800)
atom.gyp
browser/api/atom_api_dialog.cc [moved from browser/api/atom_api_file_dialog.cc with 75% similarity]
browser/api/atom_api_dialog.h [moved from browser/api/atom_api_file_dialog.h with 86% similarity]
browser/api/lib/dialog.coffee
browser/message_box.h [new file with mode: 0644]
browser/message_box_mac.mm [new file with mode: 0644]
common/api/atom_extensions.h

index cfd1880..7f86ec5 100644 (file)
--- a/atom.gyp
+++ b/atom.gyp
       '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',
similarity index 75%
rename from browser/api/atom_api_file_dialog.cc
rename to browser/api/atom_api_dialog.cc
index baebfe7..5590349 100644 (file)
@@ -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 <string>
 
 #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<v8::Value> 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<v8::Value> 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<std::string> buttons;
+  v8::Handle<v8::Array> v8_buttons = v8::Handle<v8::Array>::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<v8::Object> wrapper)
     : EventEmitter(wrapper),
       dialog_(ui::SelectFileDialog::Create(this, NULL)) {
@@ -133,7 +155,7 @@ v8::Handle<v8::Value> 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<v8::Object> element = v8_file_types->Get(i)->ToObject(); 
+    v8::Handle<v8::Object> 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<v8::Object> 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)
similarity index 86%
rename from browser/api/atom_api_file_dialog.h
rename to browser/api/atom_api_dialog.h
index 4ca4e82..7c7d9f4 100644 (file)
@@ -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<v8::Value> 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_
index 250ada4..c278bc3 100644 (file)
@@ -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 (file)
index 0000000..65ca6da
--- /dev/null
@@ -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 <string>
+#include <vector>
+
+#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<std::string>& 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 (file)
index 0000000..d03fe0e
--- /dev/null
@@ -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 <Cocoa/Cocoa.h>
+
+#include "base/strings/sys_string_conversions.h"
+
+namespace atom {
+
+int ShowMessageBox(gfx::NativeWindow parent,
+                   MessageBoxType type,
+                   const std::vector<std::string>& 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
index 4faacb0..fdff420 100644 (file)
@@ -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)