atom_api_view.cc 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // Copyright (c) 2018 GitHub, Inc.
  2. // Use of this source code is governed by the MIT license that can be
  3. // found in the LICENSE file.
  4. #include "atom/browser/api/atom_api_view.h"
  5. #include "atom/common/node_includes.h"
  6. #include "native_mate/dictionary.h"
  7. namespace atom {
  8. namespace api {
  9. View::View(views::View* view) : view_(view) {}
  10. View::View() : view_(new views::View()) {
  11. view_->set_owned_by_client();
  12. }
  13. View::~View() {
  14. if (delete_view_)
  15. delete view_;
  16. }
  17. #if BUILDFLAG(ENABLE_VIEW_API)
  18. void View::SetLayoutManager(mate::Handle<LayoutManager> layout_manager) {
  19. layout_manager_.Reset(isolate(), layout_manager->GetWrapper());
  20. view()->SetLayoutManager(layout_manager->TakeOver());
  21. }
  22. void View::AddChildView(mate::Handle<View> child) {
  23. AddChildViewAt(child, child_views_.size());
  24. }
  25. void View::AddChildViewAt(mate::Handle<View> child, size_t index) {
  26. if (index > child_views_.size())
  27. return;
  28. child_views_.emplace(child_views_.begin() + index, // index
  29. isolate(), child->GetWrapper()); // v8::Global(args...)
  30. view()->AddChildViewAt(child->view(), index);
  31. }
  32. #endif
  33. // static
  34. mate::WrappableBase* View::New(mate::Arguments* args) {
  35. auto* view = new View();
  36. view->InitWith(args->isolate(), args->GetThis());
  37. return view;
  38. }
  39. // static
  40. void View::BuildPrototype(v8::Isolate* isolate,
  41. v8::Local<v8::FunctionTemplate> prototype) {
  42. prototype->SetClassName(mate::StringToV8(isolate, "View"));
  43. #if BUILDFLAG(ENABLE_VIEW_API)
  44. mate::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate())
  45. .SetMethod("setLayoutManager", &View::SetLayoutManager)
  46. .SetMethod("addChildView", &View::AddChildView)
  47. .SetMethod("addChildViewAt", &View::AddChildViewAt);
  48. #endif
  49. }
  50. } // namespace api
  51. } // namespace atom
  52. namespace {
  53. using atom::api::View;
  54. void Initialize(v8::Local<v8::Object> exports,
  55. v8::Local<v8::Value> unused,
  56. v8::Local<v8::Context> context,
  57. void* priv) {
  58. v8::Isolate* isolate = context->GetIsolate();
  59. View::SetConstructor(isolate, base::Bind(&View::New));
  60. mate::Dictionary constructor(
  61. isolate,
  62. View::GetConstructor(isolate)->GetFunction(context).ToLocalChecked());
  63. mate::Dictionary dict(isolate, exports);
  64. dict.Set("View", constructor);
  65. }
  66. } // namespace
  67. NODE_LINKED_MODULE_CONTEXT_AWARE(atom_browser_view, Initialize)