Browse Source

feat: add Button API

Cheng Zhao 7 years ago
parent
commit
c8e8cb86ce

+ 54 - 0
atom/browser/api/atom_api_button.cc

@@ -0,0 +1,54 @@
+// Copyright (c) 2018 GitHub, Inc.
+// Use of this source code is governed by the MIT license that can be
+// found in the LICENSE file.
+
+#include "atom/browser/api/atom_api_button.h"
+
+#include "atom/common/api/constructor.h"
+#include "native_mate/dictionary.h"
+
+#include "atom/common/node_includes.h"
+
+namespace atom {
+
+namespace api {
+
+Button::Button(views::Button* button) : View(button) {
+  view()->set_owned_by_client();
+}
+
+Button::~Button() {}
+
+// static
+mate::WrappableBase* Button::New(mate::Arguments* args) {
+  args->ThrowError("Button can not be created directly");
+  return nullptr;
+}
+
+// static
+void Button::BuildPrototype(v8::Isolate* isolate,
+                            v8::Local<v8::FunctionTemplate> prototype) {
+  prototype->SetClassName(mate::StringToV8(isolate, "Button"));
+}
+
+}  // namespace api
+
+}  // namespace atom
+
+namespace {
+
+using atom::api::Button;
+
+void Initialize(v8::Local<v8::Object> exports,
+                v8::Local<v8::Value> unused,
+                v8::Local<v8::Context> context,
+                void* priv) {
+  v8::Isolate* isolate = context->GetIsolate();
+  mate::Dictionary dict(isolate, exports);
+  dict.Set("Button",
+           mate::CreateConstructor<Button>(isolate, base::Bind(&Button::New)));
+}
+
+}  // namespace
+
+NODE_BUILTIN_MODULE_CONTEXT_AWARE(atom_browser_button, Initialize)

+ 35 - 0
atom/browser/api/atom_api_button.h

@@ -0,0 +1,35 @@
+// Copyright (c) 2018 GitHub, Inc.
+// Use of this source code is governed by the MIT license that can be
+// found in the LICENSE file.
+
+#ifndef ATOM_BROWSER_API_ATOM_API_BUTTON_H_
+#define ATOM_BROWSER_API_ATOM_API_BUTTON_H_
+
+#include "atom/browser/api/atom_api_view.h"
+#include "native_mate/handle.h"
+#include "ui/views/controls/button/label_button.h"
+
+namespace atom {
+
+namespace api {
+
+class Button : public View, views::ButtonListener {
+ public:
+  static mate::WrappableBase* New(mate::Arguments* args);
+
+  static void BuildPrototype(v8::Isolate* isolate,
+                             v8::Local<v8::FunctionTemplate> prototype);
+
+ private:
+  explicit Button(views::Button* view);
+  ~Button() override;
+
+ private:
+  DISALLOW_COPY_AND_ASSIGN(Button);
+};
+
+}  // namespace api
+
+}  // namespace atom
+
+#endif  // ATOM_BROWSER_API_ATOM_API_BUTTON_H_

+ 1 - 0
atom/common/node_bindings.cc

@@ -66,6 +66,7 @@
 
 #define ELECTRON_VIEW_MODULES(V) \
   V(atom_browser_box_layout)     \
+  V(atom_browser_button)         \
   V(atom_browser_layout_manager) \
   V(atom_browser_text_field)
 

+ 3 - 0
filenames.gypi

@@ -784,10 +784,13 @@
       ['enable_view_api==1', {
         'js_sources': [
           'lib/browser/api/box-layout.js',
+          'lib/browser/api/button.js',
           'lib/browser/api/layout-manager.js',
           'lib/browser/api/text-field.js',
         ],
         'lib_sources': [
+          'atom/browser/api/atom_api_button.cc',
+          'atom/browser/api/atom_api_button.h',
           'atom/browser/api/atom_api_box_layout.cc',
           'atom/browser/api/atom_api_box_layout.h',
           'atom/browser/api/atom_api_layout_manager.cc',

+ 15 - 0
lib/browser/api/button.js

@@ -0,0 +1,15 @@
+'use strict'
+
+const electron = require('electron')
+
+const {View} = electron
+const {Button} = process.atomBinding('button')
+
+Object.setPrototypeOf(Button.prototype, View.prototype)
+
+Button.prototype._init = function () {
+  // Call parent class's _init.
+  View.prototype._init.call(this)
+}
+
+module.exports = Button

+ 1 - 0
lib/browser/api/module-list.js

@@ -3,6 +3,7 @@ module.exports = [
   {name: 'app', file: 'app'},
   {name: 'autoUpdater', file: 'auto-updater'},
   {name: 'BoxLayout', file: 'box-layout'},
+  {name: 'Button', file: 'button'},
   {name: 'BrowserView', file: 'browser-view'},
   {name: 'BrowserWindow', file: 'browser-window'},
   {name: 'contentTracing', file: 'content-tracing'},