]> khamidou — GitHub mirrors - electron.git/commitdiff
Emulate DOM's Event class.
authorCheng Zhao <zcbenz@gmail.com>
Thu, 18 Apr 2013 13:42:20 +0000 (21:42 +0800)
committerCheng Zhao <zcbenz@gmail.com>
Thu, 18 Apr 2013 13:42:20 +0000 (21:42 +0800)
atom.gyp
browser/api/atom_api_event.cc [new file with mode: 0644]
browser/api/atom_api_event.h [new file with mode: 0644]
browser/api/atom_api_event_emitter.h

index 335dbbbe3b63daadb5805c4ea8e95db6c4beccdc..c59fbd1293fc95c926b3579f176096d5035c1abc 100644 (file)
--- a/atom.gyp
+++ b/atom.gyp
@@ -13,6 +13,8 @@
     'lib_sources': [
       'app/atom_main_delegate.cc',
       'app/atom_main_delegate.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_extensions.cc',
diff --git a/browser/api/atom_api_event.cc b/browser/api/atom_api_event.cc
new file mode 100644 (file)
index 0000000..c7d4422
--- /dev/null
@@ -0,0 +1,58 @@
+// 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/api/atom_api_event.h"
+
+using node::node_isolate;
+
+namespace atom {
+
+namespace api {
+
+v8::Persistent<v8::FunctionTemplate> Event::constructor_template_;
+
+Event::Event()
+    : prevent_default_(false) {
+}
+
+Event::~Event() {
+}
+
+// static
+v8::Handle<v8::Object> Event::CreateV8Object() {
+  v8::HandleScope scope;
+
+  if (constructor_template_.IsEmpty()) {
+    v8::Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New(New);
+    constructor_template_ = v8::Persistent<v8::FunctionTemplate>::New(
+        node_isolate, t);
+    constructor_template_->InstanceTemplate()->SetInternalFieldCount(1);
+    constructor_template_->SetClassName(v8::String::NewSymbol("Event"));
+
+    NODE_SET_PROTOTYPE_METHOD(t, "preventDefault", PreventDefault);
+  }
+
+  v8::Handle<v8::Object> v8_event =
+      constructor_template_->GetFunction()->NewInstance(0, NULL);
+
+  return scope.Close(v8_event);
+}
+
+v8::Handle<v8::Value> Event::New(const v8::Arguments &args) {
+  Event* event = new Event;
+  event->Wrap(args.This());
+
+  return args.This();
+}
+
+v8::Handle<v8::Value> Event::PreventDefault(const v8::Arguments &args) {
+  Event* event = Unwrap<Event>(args.This());
+  event->prevent_default_ = true;
+
+  return v8::Undefined();
+}
+
+}  // namespace api
+
+}  // namespace atom
diff --git a/browser/api/atom_api_event.h b/browser/api/atom_api_event.h
new file mode 100644 (file)
index 0000000..c0ab000
--- /dev/null
@@ -0,0 +1,46 @@
+// 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_BROWSER_ATOM_API_EVENT_H_
+#define ATOM_BROWSER_ATOM_API_EVENT_H_
+
+#include "base/basictypes.h"
+#include "vendor/node/src/node_object_wrap.h"
+
+namespace atom {
+
+namespace api {
+
+class Event : public node::ObjectWrap {
+ public:
+  virtual ~Event();
+
+  // Create a V8 Event object.
+  static v8::Handle<v8::Object> CreateV8Object();
+
+  // Accessor to return handle_, this follows Google C++ Style.
+  v8::Persistent<v8::Object>& handle() { return handle_; }
+
+  // Whether event.preventDefault() is called.
+  bool prevent_default() const { return prevent_default_; }
+
+ protected:
+  Event();
+
+ private:
+  static v8::Handle<v8::Value> New(const v8::Arguments &args);
+  static v8::Handle<v8::Value> PreventDefault(const v8::Arguments &args);
+
+  static v8::Persistent<v8::FunctionTemplate> constructor_template_;
+
+  bool prevent_default_;
+
+  DISALLOW_COPY_AND_ASSIGN(Event);
+};
+
+}  // namespace api
+
+}  // namespace atom
+
+#endif  // ATOM_BROWSER_ATOM_API_EVENT_H_
index 8bcfc62adce45b38a124e59f05d0bd07add2b758..336821423e1db285307644c808bbd28f9bd1c136 100644 (file)
@@ -6,7 +6,6 @@
 #define ATOM_BROWSER_API_ATOM_API_EVENT_EMITTER_H_
 
 #include "base/basictypes.h"
-#include "base/memory/scoped_ptr.h"
 #include "vendor/node/src/node_object_wrap.h"
 
 namespace atom {