From 341f643b81abfb2925171e448ee99c3ca53c4de5 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Sun, 22 Mar 2020 19:11:43 +0900 Subject: [PATCH] feat: add ImageView (#22738) --- BUILD.gn | 2 + filenames.auto.gni | 1 + lib/browser/api/module-list.ts | 1 + lib/browser/api/views/image-view.js | 13 ++++ .../api/views/electron_api_image_view.cc | 63 +++++++++++++++++++ .../api/views/electron_api_image_view.h | 42 +++++++++++++ shell/common/node_bindings.cc | 1 + 7 files changed, 123 insertions(+) create mode 100644 lib/browser/api/views/image-view.js create mode 100644 shell/browser/api/views/electron_api_image_view.cc create mode 100644 shell/browser/api/views/electron_api_image_view.h diff --git a/BUILD.gn b/BUILD.gn index d548bfd0c1cc..66836a05df41 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -606,6 +606,8 @@ source_set("electron_lib") { "shell/browser/api/views/electron_api_box_layout.h", "shell/browser/api/views/electron_api_button.cc", "shell/browser/api/views/electron_api_button.h", + "shell/browser/api/views/electron_api_image_view.cc", + "shell/browser/api/views/electron_api_image_view.h", "shell/browser/api/views/electron_api_label_button.cc", "shell/browser/api/views/electron_api_label_button.h", "shell/browser/api/views/electron_api_layout_manager.cc", diff --git a/filenames.auto.gni b/filenames.auto.gni index d9fc4f873824..5041ad522e2c 100644 --- a/filenames.auto.gni +++ b/filenames.auto.gni @@ -244,6 +244,7 @@ auto_filenames = { "lib/browser/api/view.js", "lib/browser/api/views/box-layout.js", "lib/browser/api/views/button.js", + "lib/browser/api/views/image-view.js", "lib/browser/api/views/label-button.js", "lib/browser/api/views/layout-manager.js", "lib/browser/api/views/md-text-button.js", diff --git a/lib/browser/api/module-list.ts b/lib/browser/api/module-list.ts index 41e559a5b48d..479bdf6bed7f 100644 --- a/lib/browser/api/module-list.ts +++ b/lib/browser/api/module-list.ts @@ -39,6 +39,7 @@ if (features.isViewApiEnabled()) { browserModuleList.push( { name: 'BoxLayout', loader: () => require('./views/box-layout') }, { name: 'Button', loader: () => require('./views/button') }, + { name: 'ImageView', loader: () => require('./views/image-view') }, { name: 'LabelButton', loader: () => require('./views/label-button') }, { name: 'LayoutManager', loader: () => require('./views/layout-manager') }, { name: 'MdTextButton', loader: () => require('./views/md-text-button') }, diff --git a/lib/browser/api/views/image-view.js b/lib/browser/api/views/image-view.js new file mode 100644 index 000000000000..47b4ac1c6a27 --- /dev/null +++ b/lib/browser/api/views/image-view.js @@ -0,0 +1,13 @@ +const electron = require('electron'); + +const { View } = electron; +const { ImageView } = process.electronBinding('image_view'); + +Object.setPrototypeOf(ImageView.prototype, View.prototype); + +ImageView.prototype._init = function () { + // Call parent class's _init. + View.prototype._init.call(this); +}; + +module.exports = ImageView; diff --git a/shell/browser/api/views/electron_api_image_view.cc b/shell/browser/api/views/electron_api_image_view.cc new file mode 100644 index 000000000000..f6f5f94ada5a --- /dev/null +++ b/shell/browser/api/views/electron_api_image_view.cc @@ -0,0 +1,63 @@ +// Copyright (c) 2020 GitHub, Inc. +// Use of this source code is governed by the MIT license that can be +// found in the LICENSE file. + +#include "shell/browser/api/views/electron_api_image_view.h" + +#include "shell/common/gin_converters/image_converter.h" +#include "shell/common/gin_helper/constructor.h" +#include "shell/common/gin_helper/dictionary.h" +#include "shell/common/gin_helper/object_template_builder.h" +#include "shell/common/node_includes.h" + +namespace electron { + +namespace api { + +ImageView::ImageView() : View(new views::ImageView()) { + view()->set_owned_by_client(); +} + +ImageView::~ImageView() {} + +void ImageView::SetImage(const gfx::Image& image) { + image_view()->SetImage(image.AsImageSkia()); +} + +// static +gin_helper::WrappableBase* ImageView::New(gin_helper::Arguments* args) { + // Constructor call. + auto* view = new ImageView(); + view->InitWithArgs(args); + return view; +} + +// static +void ImageView::BuildPrototype(v8::Isolate* isolate, + v8::Local prototype) { + prototype->SetClassName(gin::StringToV8(isolate, "ImageView")); + gin_helper::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate()) + .SetMethod("setImage", &ImageView::SetImage); +} + +} // namespace api + +} // namespace electron + +namespace { + +using electron::api::ImageView; + +void Initialize(v8::Local exports, + v8::Local unused, + v8::Local context, + void* priv) { + v8::Isolate* isolate = context->GetIsolate(); + gin_helper::Dictionary dict(isolate, exports); + dict.Set("ImageView", gin_helper::CreateConstructor( + isolate, base::BindRepeating(&ImageView::New))); +} + +} // namespace + +NODE_LINKED_MODULE_CONTEXT_AWARE(electron_browser_image_view, Initialize) diff --git a/shell/browser/api/views/electron_api_image_view.h b/shell/browser/api/views/electron_api_image_view.h new file mode 100644 index 000000000000..c8ea5e967351 --- /dev/null +++ b/shell/browser/api/views/electron_api_image_view.h @@ -0,0 +1,42 @@ +// Copyright (c) 2020 GitHub, Inc. +// Use of this source code is governed by the MIT license that can be +// found in the LICENSE file. + +#ifndef SHELL_BROWSER_API_VIEWS_ELECTRON_API_IMAGE_VIEW_H_ +#define SHELL_BROWSER_API_VIEWS_ELECTRON_API_IMAGE_VIEW_H_ + +#include "gin/handle.h" +#include "shell/browser/api/electron_api_view.h" +#include "ui/gfx/image/image.h" +#include "ui/views/controls/image_view.h" + +namespace electron { + +namespace api { + +class ImageView : public View { + public: + static gin_helper::WrappableBase* New(gin_helper::Arguments* args); + + static void BuildPrototype(v8::Isolate* isolate, + v8::Local prototype); + + void SetImage(const gfx::Image& image); + + protected: + ImageView(); + ~ImageView() override; + + views::ImageView* image_view() const { + return static_cast(view()); + } + + private: + DISALLOW_COPY_AND_ASSIGN(ImageView); +}; + +} // namespace api + +} // namespace electron + +#endif // SHELL_BROWSER_API_VIEWS_ELECTRON_API_IMAGE_VIEW_H_ diff --git a/shell/common/node_bindings.cc b/shell/common/node_bindings.cc index b18bca67c89b..25445b767bdf 100644 --- a/shell/common/node_bindings.cc +++ b/shell/common/node_bindings.cc @@ -76,6 +76,7 @@ #define ELECTRON_VIEW_MODULES(V) \ V(electron_browser_box_layout) \ V(electron_browser_button) \ + V(electron_browser_image_view) \ V(electron_browser_label_button) \ V(electron_browser_layout_manager) \ V(electron_browser_md_text_button) \