From 47bf8e1bb3b38e7eae0d57873cb1fce874195317 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Wed, 21 Nov 2018 22:39:59 +0900 Subject: [PATCH] view: add ResizeArea class (#15752) --- BUILD.gn | 3 + .../browser/api/views/atom_api_resize_area.cc | 60 +++++++++++++++++++ atom/browser/api/views/atom_api_resize_area.h | 43 +++++++++++++ atom/common/node_bindings.cc | 1 + lib/browser/api/module-list.js | 1 + lib/browser/api/views/resize-area.js | 15 +++++ 6 files changed, 123 insertions(+) create mode 100644 atom/browser/api/views/atom_api_resize_area.cc create mode 100644 atom/browser/api/views/atom_api_resize_area.h create mode 100644 lib/browser/api/views/resize-area.js diff --git a/BUILD.gn b/BUILD.gn index 22a24208a8e..2bc91181776 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -165,6 +165,7 @@ asar("js2asar") { "lib/browser/api/views/label-button.js", "lib/browser/api/views/layout-manager.js", "lib/browser/api/views/md-text-button.js", + "lib/browser/api/views/resize-area.js", "lib/browser/api/views/text-field.js", ] } @@ -459,6 +460,8 @@ static_library("electron_lib") { "atom/browser/api/views/atom_api_layout_manager.h", "atom/browser/api/views/atom_api_md_text_button.cc", "atom/browser/api/views/atom_api_md_text_button.h", + "atom/browser/api/views/atom_api_resize_area.cc", + "atom/browser/api/views/atom_api_resize_area.h", "atom/browser/api/views/atom_api_text_field.cc", "atom/browser/api/views/atom_api_text_field.h", ] diff --git a/atom/browser/api/views/atom_api_resize_area.cc b/atom/browser/api/views/atom_api_resize_area.cc new file mode 100644 index 00000000000..850afe348d9 --- /dev/null +++ b/atom/browser/api/views/atom_api_resize_area.cc @@ -0,0 +1,60 @@ +// 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/views/atom_api_resize_area.h" + +#include "atom/common/api/constructor.h" +#include "native_mate/dictionary.h" + +#include "atom/common/node_includes.h" + +namespace atom { + +namespace api { + +ResizeArea::ResizeArea() : View(new views::ResizeArea(this)) { + view()->set_owned_by_client(); +} + +ResizeArea::~ResizeArea() {} + +void ResizeArea::OnResize(int resize_amount, bool done_resizing) { + Emit("resize", resize_amount, done_resizing); +} + +// static +mate::WrappableBase* ResizeArea::New(mate::Arguments* args) { + // Constructor call. + auto* view = new ResizeArea(); + view->InitWith(args->isolate(), args->GetThis()); + return view; +} + +// static +void ResizeArea::BuildPrototype(v8::Isolate* isolate, + v8::Local prototype) { + prototype->SetClassName(mate::StringToV8(isolate, "ResizeArea")); +} + +} // namespace api + +} // namespace atom + +namespace { + +using atom::api::ResizeArea; + +void Initialize(v8::Local exports, + v8::Local unused, + v8::Local context, + void* priv) { + v8::Isolate* isolate = context->GetIsolate(); + mate::Dictionary dict(isolate, exports); + dict.Set("ResizeArea", mate::CreateConstructor( + isolate, base::Bind(&ResizeArea::New))); +} + +} // namespace + +NODE_BUILTIN_MODULE_CONTEXT_AWARE(atom_browser_resize_area, Initialize) diff --git a/atom/browser/api/views/atom_api_resize_area.h b/atom/browser/api/views/atom_api_resize_area.h new file mode 100644 index 00000000000..52f9f9bdd38 --- /dev/null +++ b/atom/browser/api/views/atom_api_resize_area.h @@ -0,0 +1,43 @@ +// 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_VIEWS_ATOM_API_RESIZE_AREA_H_ +#define ATOM_BROWSER_API_VIEWS_ATOM_API_RESIZE_AREA_H_ + +#include "atom/browser/api/atom_api_view.h" +#include "native_mate/handle.h" +#include "ui/views/controls/resize_area.h" +#include "ui/views/controls/resize_area_delegate.h" + +namespace atom { + +namespace api { + +class ResizeArea : public View, protected views::ResizeAreaDelegate { + public: + static mate::WrappableBase* New(mate::Arguments* args); + + static void BuildPrototype(v8::Isolate* isolate, + v8::Local prototype); + + protected: + void OnResize(int resize_amount, bool done_resizing) override; + + private: + ResizeArea(); + ~ResizeArea() override; + + views::ResizeArea* resize_area() const { + return static_cast(view()); + } + + private: + DISALLOW_COPY_AND_ASSIGN(ResizeArea); +}; + +} // namespace api + +} // namespace atom + +#endif // ATOM_BROWSER_API_VIEWS_ATOM_API_RESIZE_AREA_H_ diff --git a/atom/common/node_bindings.cc b/atom/common/node_bindings.cc index a9ff32766a9..ef0dd95af53 100644 --- a/atom/common/node_bindings.cc +++ b/atom/common/node_bindings.cc @@ -74,6 +74,7 @@ V(atom_browser_label_button) \ V(atom_browser_layout_manager) \ V(atom_browser_md_text_button) \ + V(atom_browser_resize_area) \ V(atom_browser_text_field) #define ELECTRON_DESKTOP_CAPTURER_MODULE(V) V(atom_browser_desktop_capturer) diff --git a/lib/browser/api/module-list.js b/lib/browser/api/module-list.js index 48e224daa60..d411a73dec3 100644 --- a/lib/browser/api/module-list.js +++ b/lib/browser/api/module-list.js @@ -42,6 +42,7 @@ if (features.isViewApiEnabled()) { { name: 'LabelButton', file: 'views/label-button' }, { name: 'LayoutManager', file: 'views/layout-manager' }, { name: 'MdTextButton', file: 'views/md-text-button' }, + { name: 'ResizeArea', file: 'views/resize-area' }, { name: 'TextField', file: 'views/text-field' } ) } diff --git a/lib/browser/api/views/resize-area.js b/lib/browser/api/views/resize-area.js new file mode 100644 index 00000000000..5be9d282395 --- /dev/null +++ b/lib/browser/api/views/resize-area.js @@ -0,0 +1,15 @@ +'use strict' + +const electron = require('electron') + +const { View } = electron +const { ResizeArea } = process.atomBinding('resize_area') + +Object.setPrototypeOf(ResizeArea.prototype, View.prototype) + +ResizeArea.prototype._init = function () { + // Call parent class's _init. + View.prototype._init.call(this) +} + +module.exports = ResizeArea