Browse Source

Merge pull request #13110 from electron/entry-and-button

feat: add TextField and Button APIs
Cheng Zhao 6 years ago
parent
commit
a564744cd0

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

@@ -0,0 +1,58 @@
+// 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() {}
+
+void Button::ButtonPressed(views::Button* sender, const ui::Event& event) {
+  Emit("click");
+}
+
+// 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)

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

@@ -0,0 +1,38 @@
+// 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/button.h"
+
+namespace atom {
+
+namespace api {
+
+class Button : public View, public views::ButtonListener {
+ public:
+  static mate::WrappableBase* New(mate::Arguments* args);
+
+  static void BuildPrototype(v8::Isolate* isolate,
+                             v8::Local<v8::FunctionTemplate> prototype);
+
+ protected:
+  explicit Button(views::Button* view);
+  ~Button() override;
+
+  // views::ButtonListener:
+  void ButtonPressed(views::Button* sender, const ui::Event& event) override;
+
+ private:
+  DISALLOW_COPY_AND_ASSIGN(Button);
+};
+
+}  // namespace api
+
+}  // namespace atom
+
+#endif  // ATOM_BROWSER_API_ATOM_API_BUTTON_H_

+ 58 - 0
atom/browser/api/atom_api_label_button.cc

@@ -0,0 +1,58 @@
+// 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_label_button.h"
+
+#include "atom/common/api/constructor.h"
+#include "base/strings/utf_string_conversions.h"
+#include "native_mate/dictionary.h"
+#include "ui/views/controls/button/label_button.h"
+
+#include "atom/common/node_includes.h"
+
+namespace atom {
+
+namespace api {
+
+LabelButton::LabelButton(const std::string& text)
+    : Button(new views::LabelButton(this, base::UTF8ToUTF16(text))) {}
+
+LabelButton::~LabelButton() {}
+
+// static
+mate::WrappableBase* LabelButton::New(mate::Arguments* args,
+                                      const std::string& text) {
+  // Constructor call.
+  auto* view = new LabelButton(text);
+  view->InitWith(args->isolate(), args->GetThis());
+  return view;
+}
+
+// static
+void LabelButton::BuildPrototype(v8::Isolate* isolate,
+                                 v8::Local<v8::FunctionTemplate> prototype) {
+  prototype->SetClassName(mate::StringToV8(isolate, "LabelButton"));
+}
+
+}  // namespace api
+
+}  // namespace atom
+
+namespace {
+
+using atom::api::LabelButton;
+
+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("LabelButton", mate::CreateConstructor<LabelButton>(
+                              isolate, base::Bind(&LabelButton::New)));
+}
+
+}  // namespace
+
+NODE_BUILTIN_MODULE_CONTEXT_AWARE(atom_browser_label_button, Initialize)

+ 36 - 0
atom/browser/api/atom_api_label_button.h

@@ -0,0 +1,36 @@
+// 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_LABEL_BUTTON_H_
+#define ATOM_BROWSER_API_ATOM_API_LABEL_BUTTON_H_
+
+#include <string>
+
+#include "atom/browser/api/atom_api_button.h"
+
+namespace atom {
+
+namespace api {
+
+class LabelButton : public Button {
+ public:
+  static mate::WrappableBase* New(mate::Arguments* args,
+                                  const std::string& text);
+
+  static void BuildPrototype(v8::Isolate* isolate,
+                             v8::Local<v8::FunctionTemplate> prototype);
+
+ protected:
+  explicit LabelButton(const std::string& text);
+  ~LabelButton() override;
+
+ private:
+  DISALLOW_COPY_AND_ASSIGN(LabelButton);
+};
+
+}  // namespace api
+
+}  // namespace atom
+
+#endif  // ATOM_BROWSER_API_ATOM_API_LABEL_BUTTON_H_

+ 67 - 0
atom/browser/api/atom_api_text_field.cc

@@ -0,0 +1,67 @@
+// 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_text_field.h"
+
+#include "atom/common/api/constructor.h"
+#include "native_mate/dictionary.h"
+
+#include "atom/common/node_includes.h"
+
+namespace atom {
+
+namespace api {
+
+TextField::TextField() : View(new views::Textfield()) {
+  view()->set_owned_by_client();
+}
+
+TextField::~TextField() {}
+
+void TextField::SetText(const base::string16& new_text) {
+  text_field()->SetText(new_text);
+}
+
+base::string16 TextField::GetText() const {
+  return text_field()->text();
+}
+
+// static
+mate::WrappableBase* TextField::New(mate::Arguments* args) {
+  // Constructor call.
+  auto* view = new TextField();
+  view->InitWith(args->isolate(), args->GetThis());
+  return view;
+}
+
+// static
+void TextField::BuildPrototype(v8::Isolate* isolate,
+                               v8::Local<v8::FunctionTemplate> prototype) {
+  prototype->SetClassName(mate::StringToV8(isolate, "TextField"));
+  mate::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate())
+      .SetMethod("setText", &TextField::SetText)
+      .SetMethod("getText", &TextField::GetText);
+}
+
+}  // namespace api
+
+}  // namespace atom
+
+namespace {
+
+using atom::api::TextField;
+
+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("TextField", mate::CreateConstructor<TextField>(
+                            isolate, base::Bind(&TextField::New)));
+}
+
+}  // namespace
+
+NODE_BUILTIN_MODULE_CONTEXT_AWARE(atom_browser_text_field, Initialize)

+ 42 - 0
atom/browser/api/atom_api_text_field.h

@@ -0,0 +1,42 @@
+// 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_TEXT_FIELD_H_
+#define ATOM_BROWSER_API_ATOM_API_TEXT_FIELD_H_
+
+#include "atom/browser/api/atom_api_view.h"
+#include "native_mate/handle.h"
+#include "ui/views/controls/textfield/textfield.h"
+
+namespace atom {
+
+namespace api {
+
+class TextField : public View {
+ public:
+  static mate::WrappableBase* New(mate::Arguments* args);
+
+  static void BuildPrototype(v8::Isolate* isolate,
+                             v8::Local<v8::FunctionTemplate> prototype);
+
+  void SetText(const base::string16& new_text);
+  base::string16 GetText() const;
+
+ private:
+  TextField();
+  ~TextField() override;
+
+  views::Textfield* text_field() const {
+    return static_cast<views::Textfield*>(view());
+  }
+
+ private:
+  DISALLOW_COPY_AND_ASSIGN(TextField);
+};
+
+}  // namespace api
+
+}  // namespace atom
+
+#endif  // ATOM_BROWSER_API_ATOM_API_TEXT_FIELD_H_

+ 4 - 1
atom/common/node_bindings.cc

@@ -66,7 +66,10 @@
 
 #define ELECTRON_VIEW_MODULES(V) \
   V(atom_browser_box_layout)     \
-  V(atom_browser_layout_manager)
+  V(atom_browser_button)         \
+  V(atom_browser_label_button)   \
+  V(atom_browser_layout_manager) \
+  V(atom_browser_text_field)
 
 #define ELECTRON_DESKTOP_CAPTURER_MODULE(V) V(atom_browser_desktop_capturer)
 

+ 5 - 4
brightray/common/main_delegate.cc

@@ -7,6 +7,7 @@
 #include <memory>
 
 #include "base/command_line.h"
+#include "base/mac/bundle_locations.h"
 #include "base/path_service.h"
 #include "brightray/browser/browser_client.h"
 #include "brightray/common/content_client.h"
@@ -53,12 +54,13 @@ void LoadResourceBundle(const std::string& locale) {
   ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance();
   bundle.ReloadLocaleResources(locale);
 
-// Load other resource files.
+  // Load other resource files.
+  base::FilePath pak_dir;
 #if defined(OS_MACOSX)
-  LoadCommonResources();
+  pak_dir = base::mac::FrameworkBundlePath().Append("Resources");
 #else
-  base::FilePath pak_dir;
   PathService::Get(base::DIR_MODULE, &pak_dir);
+#endif
   bundle.AddDataPackFromPath(
       pak_dir.Append(FILE_PATH_LITERAL("content_shell.pak")),
       ui::GetSupportedScaleFactors()[0]);
@@ -79,7 +81,6 @@ void LoadResourceBundle(const std::string& locale) {
   bundle.AddDataPackFromPath(
       pak_dir.Append(FILE_PATH_LITERAL("views_resources_200_percent.pak")),
       ui::SCALE_FACTOR_200P);
-#endif
 }
 
 MainDelegate::MainDelegate() {}

+ 0 - 15
brightray/common/main_delegate_mac.mm

@@ -25,23 +25,8 @@ base::FilePath GetFrameworksPath() {
   return MainApplicationBundlePath().Append("Contents").Append("Frameworks");
 }
 
-base::FilePath GetResourcesPakFilePath(NSString* name) {
-  auto path = [base::mac::FrameworkBundle() pathForResource:name ofType:@"pak"];
-  return base::mac::NSStringToFilePath(path);
-}
-
 }  // namespace
 
-void LoadCommonResources() {
-  ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance();
-  bundle.AddDataPackFromPath(GetResourcesPakFilePath(@"content_shell"),
-                             ui::GetSupportedScaleFactors()[0]);
-#if defined(ENABLE_PDF_VIEWER)
-  bundle.AddDataPackFromPath(GetResourcesPakFilePath(@"pdf_viewer_resources"),
-                             ui::GetSupportedScaleFactors()[0]);
-#endif  // defined(ENABLE_PDF_VIEWER)
-}
-
 void MainDelegate::OverrideFrameworkBundlePath() {
   base::FilePath helper_path =
       GetFrameworksPath().Append(GetApplicationName() + " Framework.framework");

+ 5 - 1
electron.gyp

@@ -638,8 +638,12 @@
           'mac_bundle': 1,
           'mac_bundle_resources': [
             'atom/common/resources/mac/MainMenu.xib',
-            '<(libchromiumcontent_dir)/content_shell.pak',
             '<(libchromiumcontent_dir)/icudtl.dat',
+            '<(libchromiumcontent_dir)/blink_image_resources_200_percent.pak',
+            '<(libchromiumcontent_dir)/content_resources_200_percent.pak',
+            '<(libchromiumcontent_dir)/content_shell.pak',
+            '<(libchromiumcontent_dir)/ui_resources_200_percent.pak',
+            '<(libchromiumcontent_dir)/views_resources_200_percent.pak',
             '<(libchromiumcontent_dir)/natives_blob.bin',
             '<(libchromiumcontent_dir)/snapshot_blob.bin',
           ],

+ 9 - 0
filenames.gypi

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

+ 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

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

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

+ 3 - 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'},
@@ -10,6 +11,7 @@ module.exports = [
   {name: 'globalShortcut', file: 'global-shortcut'},
   {name: 'ipcMain', file: 'ipc-main'},
   {name: 'inAppPurchase', file: 'in-app-purchase'},
+  {name: 'LabelButton', file: 'label-button'},
   {name: 'LayoutManager', file: 'layout-manager'},
   {name: 'Menu', file: 'menu'},
   {name: 'MenuItem', file: 'menu-item'},
@@ -21,6 +23,7 @@ module.exports = [
   {name: 'screen', file: 'screen'},
   {name: 'session', file: 'session'},
   {name: 'systemPreferences', file: 'system-preferences'},
+  {name: 'TextField', file: 'text-field'},
   {name: 'TopLevelWindow', file: 'top-level-window'},
   {name: 'TouchBar', file: 'touch-bar'},
   {name: 'Tray', file: 'tray'},

+ 15 - 0
lib/browser/api/text-field.js

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