native_browser_view_mac.mm 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // Copyright (c) 2017 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/native_browser_view_mac.h"
  5. #include "shell/browser/ui/inspectable_web_contents.h"
  6. #include "shell/browser/ui/inspectable_web_contents_view.h"
  7. #include "skia/ext/skia_utils_mac.h"
  8. #include "ui/gfx/geometry/rect.h"
  9. // Match view::Views behavior where the view sticks to the top-left origin.
  10. const NSAutoresizingMaskOptions kDefaultAutoResizingMask =
  11. NSViewMaxXMargin | NSViewMinYMargin;
  12. namespace electron {
  13. NativeBrowserViewMac::NativeBrowserViewMac(
  14. InspectableWebContents* inspectable_web_contents)
  15. : NativeBrowserView(inspectable_web_contents) {
  16. auto* iwc_view = GetInspectableWebContentsView();
  17. if (!iwc_view)
  18. return;
  19. auto* view = iwc_view->GetNativeView().GetNativeNSView();
  20. view.autoresizingMask = kDefaultAutoResizingMask;
  21. }
  22. NativeBrowserViewMac::~NativeBrowserViewMac() = default;
  23. void NativeBrowserViewMac::SetAutoResizeFlags(uint8_t flags) {
  24. NSAutoresizingMaskOptions autoresizing_mask = kDefaultAutoResizingMask;
  25. if (flags & kAutoResizeWidth) {
  26. autoresizing_mask |= NSViewWidthSizable;
  27. }
  28. if (flags & kAutoResizeHeight) {
  29. autoresizing_mask |= NSViewHeightSizable;
  30. }
  31. if (flags & kAutoResizeHorizontal) {
  32. autoresizing_mask |=
  33. NSViewMaxXMargin | NSViewMinXMargin | NSViewWidthSizable;
  34. }
  35. if (flags & kAutoResizeVertical) {
  36. autoresizing_mask |=
  37. NSViewMaxYMargin | NSViewMinYMargin | NSViewHeightSizable;
  38. }
  39. auto* iwc_view = GetInspectableWebContentsView();
  40. if (!iwc_view)
  41. return;
  42. auto* view = iwc_view->GetNativeView().GetNativeNSView();
  43. view.autoresizingMask = autoresizing_mask;
  44. }
  45. void NativeBrowserViewMac::SetBounds(const gfx::Rect& bounds) {
  46. auto* iwc_view = GetInspectableWebContentsView();
  47. if (!iwc_view)
  48. return;
  49. auto* view = iwc_view->GetNativeView().GetNativeNSView();
  50. const auto superview_height =
  51. view.superview ? view.superview.frame.size.height : 0;
  52. int y_coord = superview_height - bounds.y() - bounds.height();
  53. view.frame = NSMakeRect(bounds.x(), y_coord, bounds.width(), bounds.height());
  54. }
  55. gfx::Rect NativeBrowserViewMac::GetBounds() {
  56. auto* iwc_view = GetInspectableWebContentsView();
  57. if (!iwc_view)
  58. return gfx::Rect();
  59. NSView* view = iwc_view->GetNativeView().GetNativeNSView();
  60. const int superview_height =
  61. view.superview ? view.superview.frame.size.height : 0;
  62. int y_coord = superview_height - view.frame.origin.y - view.frame.size.height;
  63. return gfx::Rect(view.frame.origin.x, y_coord, view.frame.size.width,
  64. view.frame.size.height);
  65. }
  66. void NativeBrowserViewMac::SetBackgroundColor(SkColor color) {
  67. auto* iwc_view = GetInspectableWebContentsView();
  68. if (!iwc_view)
  69. return;
  70. auto* view = iwc_view->GetNativeView().GetNativeNSView();
  71. view.wantsLayer = YES;
  72. view.layer.backgroundColor = skia::CGColorCreateFromSkColor(color).get();
  73. }
  74. // static
  75. NativeBrowserView* NativeBrowserView::Create(
  76. InspectableWebContents* inspectable_web_contents) {
  77. return new NativeBrowserViewMac(inspectable_web_contents);
  78. }
  79. } // namespace electron