native_browser_view_mac.mm 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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 "atom/browser/native_browser_view_mac.h"
  5. #include "brightray/browser/inspectable_web_contents_view.h"
  6. #include "skia/ext/skia_utils_mac.h"
  7. #include "ui/gfx/geometry/rect.h"
  8. // Match view::Views behavior where the view sticks to the top-left origin.
  9. const NSAutoresizingMaskOptions kDefaultAutoResizingMask =
  10. NSViewMaxXMargin | NSViewMinYMargin;
  11. namespace atom {
  12. NativeBrowserViewMac::NativeBrowserViewMac(
  13. brightray::InspectableWebContentsView* web_contents_view)
  14. : NativeBrowserView(web_contents_view) {
  15. auto* view = GetInspectableWebContentsView()->GetNativeView();
  16. view.autoresizingMask = kDefaultAutoResizingMask;
  17. }
  18. NativeBrowserViewMac::~NativeBrowserViewMac() {}
  19. void NativeBrowserViewMac::SetAutoResizeFlags(uint8_t flags) {
  20. NSAutoresizingMaskOptions autoresizing_mask = kDefaultAutoResizingMask;
  21. if (flags & kAutoResizeWidth) {
  22. autoresizing_mask |= NSViewWidthSizable;
  23. }
  24. if (flags & kAutoResizeHeight) {
  25. autoresizing_mask |= NSViewHeightSizable;
  26. }
  27. auto* view = GetInspectableWebContentsView()->GetNativeView();
  28. view.autoresizingMask = autoresizing_mask;
  29. }
  30. void NativeBrowserViewMac::SetBounds(const gfx::Rect& bounds) {
  31. auto* view = GetInspectableWebContentsView()->GetNativeView();
  32. auto* superview = view.superview;
  33. const auto superview_height = superview ? superview.frame.size.height : 0;
  34. view.frame =
  35. NSMakeRect(bounds.x(), superview_height - bounds.y() - bounds.height(),
  36. bounds.width(), bounds.height());
  37. }
  38. void NativeBrowserViewMac::SetBackgroundColor(SkColor color) {
  39. auto* view = GetInspectableWebContentsView()->GetNativeView();
  40. view.wantsLayer = YES;
  41. view.layer.backgroundColor = skia::CGColorCreateFromSkColor(color);
  42. }
  43. // static
  44. NativeBrowserView* NativeBrowserView::Create(
  45. brightray::InspectableWebContentsView* web_contents_view) {
  46. return new NativeBrowserViewMac(web_contents_view);
  47. }
  48. } // namespace atom