Use native_mate to simplify clipboard api.
This commit is contained in:
Родитель
4a7e392301
Коммит
d9cd50c219
1
atom.gyp
1
atom.gyp
|
@ -143,7 +143,6 @@
|
||||||
'atom/common/api/api_messages.cc',
|
'atom/common/api/api_messages.cc',
|
||||||
'atom/common/api/api_messages.h',
|
'atom/common/api/api_messages.h',
|
||||||
'atom/common/api/atom_api_clipboard.cc',
|
'atom/common/api/atom_api_clipboard.cc',
|
||||||
'atom/common/api/atom_api_clipboard.h',
|
|
||||||
'atom/common/api/atom_api_crash_reporter.cc',
|
'atom/common/api/atom_api_crash_reporter.cc',
|
||||||
'atom/common/api/atom_api_crash_reporter.h',
|
'atom/common/api/atom_api_crash_reporter.h',
|
||||||
'atom/common/api/atom_api_event_emitter.cc',
|
'atom/common/api/atom_api_event_emitter.cc',
|
||||||
|
|
|
@ -2,64 +2,40 @@
|
||||||
// Use of this source code is governed by a BSD-style license that can be
|
// Use of this source code is governed by a BSD-style license that can be
|
||||||
// found in the LICENSE file.
|
// found in the LICENSE file.
|
||||||
|
|
||||||
#include "atom/common/api/atom_api_clipboard.h"
|
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#include "atom/common/v8/native_type_conversions.h"
|
#include "native_mate/object_template_builder.h"
|
||||||
#include "ui/base/clipboard/clipboard.h"
|
#include "ui/base/clipboard/clipboard.h"
|
||||||
|
|
||||||
#include "atom/common/v8/node_common.h"
|
#include "atom/common/v8/node_common.h"
|
||||||
|
|
||||||
namespace atom {
|
namespace {
|
||||||
|
|
||||||
namespace api {
|
|
||||||
|
|
||||||
// static
|
|
||||||
void Clipboard::Has(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
|
||||||
std::string format_string;
|
|
||||||
if (!FromV8Arguments(args, &format_string))
|
|
||||||
return node::ThrowTypeError("Bad argument");
|
|
||||||
|
|
||||||
|
bool Has(const std::string& format_string) {
|
||||||
ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread();
|
ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread();
|
||||||
ui::Clipboard::FormatType format(ui::Clipboard::GetFormatType(format_string));
|
ui::Clipboard::FormatType format(ui::Clipboard::GetFormatType(format_string));
|
||||||
|
return clipboard->IsFormatAvailable(format, ui::Clipboard::BUFFER_STANDARD);
|
||||||
args.GetReturnValue().Set(
|
|
||||||
clipboard->IsFormatAvailable(format, ui::Clipboard::BUFFER_STANDARD));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// static
|
std::string Read(const std::string& format_string) {
|
||||||
void Clipboard::Read(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
|
||||||
std::string format_string;
|
|
||||||
if (!FromV8Arguments(args, &format_string))
|
|
||||||
return node::ThrowTypeError("Bad argument");
|
|
||||||
|
|
||||||
ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread();
|
ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread();
|
||||||
ui::Clipboard::FormatType format(ui::Clipboard::GetFormatType(format_string));
|
ui::Clipboard::FormatType format(ui::Clipboard::GetFormatType(format_string));
|
||||||
|
|
||||||
std::string data;
|
std::string data;
|
||||||
clipboard->ReadData(format, &data);
|
clipboard->ReadData(format, &data);
|
||||||
|
return data;
|
||||||
args.GetReturnValue().Set(ToV8Value(data));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// static
|
std::string ReadText() {
|
||||||
void Clipboard::ReadText(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
|
||||||
ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread();
|
ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread();
|
||||||
|
|
||||||
std::string data;
|
std::string data;
|
||||||
clipboard->ReadAsciiText(ui::Clipboard::BUFFER_STANDARD, &data);
|
clipboard->ReadAsciiText(ui::Clipboard::BUFFER_STANDARD, &data);
|
||||||
|
return data;
|
||||||
args.GetReturnValue().Set(ToV8Value(data));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// static
|
void WriteText(const std::string text) {
|
||||||
void Clipboard::WriteText(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
|
||||||
std::string text;
|
|
||||||
if (!FromV8Arguments(args, &text))
|
|
||||||
return node::ThrowTypeError("Bad argument");
|
|
||||||
|
|
||||||
ui::Clipboard::ObjectMap object_map;
|
ui::Clipboard::ObjectMap object_map;
|
||||||
object_map[ui::Clipboard::CBF_TEXT].push_back(
|
object_map[ui::Clipboard::CBF_TEXT].push_back(
|
||||||
std::vector<char>(text.begin(), text.end()));
|
std::vector<char>(text.begin(), text.end()));
|
||||||
|
@ -68,22 +44,20 @@ void Clipboard::WriteText(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
||||||
clipboard->WriteObjects(ui::Clipboard::BUFFER_STANDARD, object_map);
|
clipboard->WriteObjects(ui::Clipboard::BUFFER_STANDARD, object_map);
|
||||||
}
|
}
|
||||||
|
|
||||||
// static
|
void Clear() {
|
||||||
void Clipboard::Clear(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
|
||||||
ui::Clipboard::GetForCurrentThread()->Clear(ui::Clipboard::BUFFER_STANDARD);
|
ui::Clipboard::GetForCurrentThread()->Clear(ui::Clipboard::BUFFER_STANDARD);
|
||||||
}
|
}
|
||||||
|
|
||||||
// static
|
void Initialize(v8::Handle<v8::Object> exports) {
|
||||||
void Clipboard::Initialize(v8::Handle<v8::Object> target) {
|
mate::ObjectTemplateBuilder builder(v8::Isolate::GetCurrent());
|
||||||
NODE_SET_METHOD(target, "has", Has);
|
builder.SetMethod("has", &Has)
|
||||||
NODE_SET_METHOD(target, "read", Read);
|
.SetMethod("read", &Read)
|
||||||
NODE_SET_METHOD(target, "readText", ReadText);
|
.SetMethod("readText", &ReadText)
|
||||||
NODE_SET_METHOD(target, "writeText", WriteText);
|
.SetMethod("writeText", &WriteText)
|
||||||
NODE_SET_METHOD(target, "clear", Clear);
|
.SetMethod("clear", &Clear);
|
||||||
|
exports->SetPrototype(builder.Build()->NewInstance());
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace api
|
} // namespace
|
||||||
|
|
||||||
} // namespace atom
|
NODE_MODULE(atom_common_clipboard, Initialize)
|
||||||
|
|
||||||
NODE_MODULE(atom_common_clipboard, atom::api::Clipboard::Initialize)
|
|
||||||
|
|
|
@ -1,33 +0,0 @@
|
||||||
// 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 ATOM_COMMON_API_ATOM_API_CLIPBOARD_H_
|
|
||||||
#define ATOM_COMMON_API_ATOM_API_CLIPBOARD_H_
|
|
||||||
|
|
||||||
#include "base/basictypes.h"
|
|
||||||
#include "v8/include/v8.h"
|
|
||||||
|
|
||||||
namespace atom {
|
|
||||||
|
|
||||||
namespace api {
|
|
||||||
|
|
||||||
class Clipboard {
|
|
||||||
public:
|
|
||||||
static void Initialize(v8::Handle<v8::Object> target);
|
|
||||||
|
|
||||||
private:
|
|
||||||
static void Has(const v8::FunctionCallbackInfo<v8::Value>& args);
|
|
||||||
static void Read(const v8::FunctionCallbackInfo<v8::Value>& args);
|
|
||||||
static void ReadText(const v8::FunctionCallbackInfo<v8::Value>& args);
|
|
||||||
static void WriteText(const v8::FunctionCallbackInfo<v8::Value>& args);
|
|
||||||
static void Clear(const v8::FunctionCallbackInfo<v8::Value>& args);
|
|
||||||
|
|
||||||
DISALLOW_IMPLICIT_CONSTRUCTORS(Clipboard);
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace api
|
|
||||||
|
|
||||||
} // namespace atom
|
|
||||||
|
|
||||||
#endif // ATOM_COMMON_API_ATOM_API_CLIPBOARD_H_
|
|
|
@ -2,8 +2,6 @@
|
||||||
// Use of this source code is governed by a BSD-style license that can be
|
// Use of this source code is governed by a BSD-style license that can be
|
||||||
// found in the LICENSE file.
|
// found in the LICENSE file.
|
||||||
|
|
||||||
#include "atom/common/api/atom_api_shell.h"
|
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
#include "atom/common/platform_util.h"
|
#include "atom/common/platform_util.h"
|
||||||
|
|
Загрузка…
Ссылка в новой задаче