electron_api_image_view.cc 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // Copyright (c) 2020 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 "shell/browser/api/views/electron_api_image_view.h"
  5. #include "shell/common/gin_converters/image_converter.h"
  6. #include "shell/common/gin_helper/constructor.h"
  7. #include "shell/common/gin_helper/dictionary.h"
  8. #include "shell/common/gin_helper/object_template_builder.h"
  9. #include "shell/common/node_includes.h"
  10. #include "ui/gfx/image/image.h"
  11. namespace electron::api {
  12. ImageView::ImageView() : View(new views::ImageView()) {
  13. view()->set_owned_by_client();
  14. }
  15. ImageView::~ImageView() = default;
  16. void ImageView::SetImage(const gfx::Image& image) {
  17. image_view()->SetImage(ui::ImageModel::FromImage(image));
  18. }
  19. // static
  20. gin_helper::WrappableBase* ImageView::New(gin_helper::Arguments* args) {
  21. // Constructor call.
  22. auto* view = new ImageView();
  23. view->InitWithArgs(args);
  24. return view;
  25. }
  26. // static
  27. void ImageView::BuildPrototype(v8::Isolate* isolate,
  28. v8::Local<v8::FunctionTemplate> prototype) {
  29. prototype->SetClassName(gin::StringToV8(isolate, "ImageView"));
  30. gin_helper::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate())
  31. .SetMethod("setImage", &ImageView::SetImage);
  32. }
  33. } // namespace electron::api
  34. namespace {
  35. using electron::api::ImageView;
  36. void Initialize(v8::Local<v8::Object> exports,
  37. v8::Local<v8::Value> unused,
  38. v8::Local<v8::Context> context,
  39. void* priv) {
  40. v8::Isolate* isolate = context->GetIsolate();
  41. gin_helper::Dictionary dict(isolate, exports);
  42. dict.Set("ImageView", gin_helper::CreateConstructor<ImageView>(
  43. isolate, base::BindRepeating(&ImageView::New)));
  44. }
  45. } // namespace
  46. NODE_LINKED_BINDING_CONTEXT_AWARE(electron_browser_image_view, Initialize)