shell_native_app_window_mac.mm 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // Copyright 2014 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #import "extensions/shell/browser/shell_native_app_window_mac.h"
  5. #import <Cocoa/Cocoa.h>
  6. #include "base/mac/foundation_util.h"
  7. #include "base/notreached.h"
  8. #include "base/strings/sys_string_conversions.h"
  9. #include "content/public/browser/web_contents.h"
  10. #include "ui/display/display.h"
  11. #include "ui/display/screen.h"
  12. #include "ui/gfx/geometry/size.h"
  13. #import "ui/gfx/mac/coordinate_conversion.h"
  14. @implementation ShellNativeAppWindowController
  15. @synthesize appWindow = _appWindow;
  16. - (void)windowWillClose:(NSNotification*)notification {
  17. if (_appWindow)
  18. _appWindow->WindowWillClose();
  19. }
  20. @end
  21. namespace extensions {
  22. ShellNativeAppWindowMac::ShellNativeAppWindowMac(
  23. AppWindow* app_window,
  24. const AppWindow::CreateParams& params)
  25. : ShellNativeAppWindow(app_window, params) {
  26. base::scoped_nsobject<NSWindow> shell_window;
  27. NSUInteger style_mask = NSWindowStyleMaskTitled | NSWindowStyleMaskClosable;
  28. NSRect cocoa_bounds = gfx::ScreenRectToNSRect(
  29. params.GetInitialWindowBounds(gfx::Insets()));
  30. // TODO(yoz): Do we need to handle commands (keyboard shortcuts)?
  31. // Do we need need ChromeEventProcessingWindow or UnderlayOpenGLHostingWindow?
  32. shell_window.reset([[NSWindow alloc]
  33. initWithContentRect:cocoa_bounds
  34. styleMask:style_mask
  35. backing:NSBackingStoreBuffered
  36. defer:NO]);
  37. [shell_window setReleasedWhenClosed:NO];
  38. [shell_window setTitleVisibility:NSWindowTitleHidden];
  39. window_controller_.reset([[ShellNativeAppWindowController alloc]
  40. initWithWindow:shell_window]);
  41. [window() setDelegate:window_controller_];
  42. [window_controller_ setAppWindow:this];
  43. NSView* view = app_window->web_contents()->GetNativeView().GetNativeNSView();
  44. NSView* frameView = [window() contentView];
  45. [view setFrame:[frameView bounds]];
  46. [frameView addSubview:view];
  47. }
  48. ShellNativeAppWindowMac::~ShellNativeAppWindowMac() {
  49. [window() setDelegate:nil];
  50. [window() close];
  51. }
  52. bool ShellNativeAppWindowMac::IsActive() const {
  53. return [window() isKeyWindow];
  54. }
  55. gfx::NativeWindow ShellNativeAppWindowMac::GetNativeWindow() const {
  56. return window();
  57. }
  58. gfx::Rect ShellNativeAppWindowMac::GetBounds() const {
  59. return gfx::ScreenRectFromNSRect([window() frame]);
  60. }
  61. void ShellNativeAppWindowMac::Show() {
  62. [window_controller_ showWindow:nil];
  63. }
  64. void ShellNativeAppWindowMac::Hide() {
  65. NOTIMPLEMENTED();
  66. }
  67. bool ShellNativeAppWindowMac::IsVisible() const {
  68. return [window() isVisible];
  69. }
  70. void ShellNativeAppWindowMac::Activate() {
  71. // TODO(yoz): Activate in front of other applications.
  72. [window() makeKeyAndOrderFront:window_controller_];
  73. }
  74. void ShellNativeAppWindowMac::Deactivate() {
  75. // See crbug.com/51364.
  76. NOTIMPLEMENTED();
  77. }
  78. void ShellNativeAppWindowMac::SetBounds(const gfx::Rect& bounds) {
  79. // TODO(yoz): Windows should be fullscreen.
  80. NOTIMPLEMENTED();
  81. }
  82. gfx::Size ShellNativeAppWindowMac::GetContentMinimumSize() const {
  83. // Content fills the display and cannot be resized.
  84. return display::Screen::GetScreen()->GetPrimaryDisplay().bounds().size();
  85. }
  86. gfx::Size ShellNativeAppWindowMac::GetContentMaximumSize() const {
  87. return GetContentMinimumSize();
  88. }
  89. void ShellNativeAppWindowMac::WindowWillClose() {
  90. [window_controller_ setAppWindow:nullptr];
  91. app_window()->OnNativeWindowChanged();
  92. app_window()->OnNativeClose();
  93. }
  94. NSWindow* ShellNativeAppWindowMac::window() const {
  95. return [window_controller_ window];
  96. }
  97. } // namespace extensions