Add the generialized showMessageBox API.

This commit is contained in:
Cheng Zhao 2013-05-03 21:03:26 +08:00
Родитель 4aeb5e1388
Коммит ec43f740a7
7 изменённых файлов: 129 добавлений и 26 удалений

Просмотреть файл

@ -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',

Просмотреть файл

@ -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)

Просмотреть файл

@ -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_

Просмотреть файл

@ -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 = {}

30
browser/message_box.h Normal file
Просмотреть файл

@ -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_

Просмотреть файл

@ -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

Просмотреть файл

@ -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)