Index: ui/views/widget/native_widget_mac.mm |
diff --git a/ui/views/widget/native_widget_mac.mm b/ui/views/widget/native_widget_mac.mm |
index 62ce2487b76a50d91f497e41b0ba4419936378cf..5a75cb1e9d5fde487280a7350247a6cbd2b5979d 100644 |
--- a/ui/views/widget/native_widget_mac.mm |
+++ b/ui/views/widget/native_widget_mac.mm |
@@ -9,12 +9,38 @@ |
#include "base/mac/foundation_util.h" |
#include "base/mac/scoped_nsobject.h" |
#include "ui/gfx/font_list.h" |
+#import "ui/gfx/mac/coordinate_conversion.h" |
#include "ui/native_theme/native_theme.h" |
#import "ui/views/cocoa/bridged_content_view.h" |
#import "ui/views/cocoa/bridged_native_widget.h" |
#import "ui/views/cocoa/views_nswindow_delegate.h" |
namespace views { |
+namespace { |
+ |
+NSInteger StyleMaskForParams(const Widget::InitParams& params) { |
+ // TODO(tapted): Determine better masks when there are use cases for it. |
+ if (params.type == Widget::InitParams::TYPE_WINDOW) { |
+ return NSTitledWindowMask | NSClosableWindowMask | |
+ NSMiniaturizableWindowMask | NSResizableWindowMask; |
+ } |
+ return NSBorderlessWindowMask; |
+} |
+ |
+NSRect ValidateContentRect(NSRect content_rect) { |
+ // A contentRect with zero width or height is a banned practice in Chrome, due |
+ // to unpredictable OSX treatment. For now, silently give a minimum dimension. |
+ // TODO(tapted): Add a DCHECK, or add emulation logic (e.g. to auto-hide). |
+ if (NSWidth(content_rect) == 0) |
+ content_rect.size.width = 1; |
+ |
+ if (NSHeight(content_rect) == 0) |
+ content_rect.size.height = 1; |
+ |
+ return content_rect; |
+} |
+ |
+} // namespace |
//////////////////////////////////////////////////////////////////////////////// |
// NativeWidgetMac, public: |
@@ -48,12 +74,11 @@ void NativeWidgetMac::OnWindowWillClose() { |
void NativeWidgetMac::InitNativeWidget(const Widget::InitParams& params) { |
ownership_ = params.ownership; |
- // TODO(tapted): Convert position into Cocoa's flipped coordinate space. |
- NSRect content_rect = |
- NSMakeRect(0, 0, params.bounds.width(), params.bounds.height()); |
- // TODO(tapted): Determine a good initial style mask from |params|. |
- NSInteger style_mask = NSTitledWindowMask | NSClosableWindowMask | |
- NSMiniaturizableWindowMask | NSResizableWindowMask; |
+ NSInteger style_mask = StyleMaskForParams(params); |
+ NSRect content_rect = ValidateContentRect( |
+ [NSWindow contentRectForFrameRect:gfx::ScreenRectToNSRect(params.bounds) |
+ styleMask:style_mask]); |
+ |
base::scoped_nsobject<NSWindow> window( |
[[NSWindow alloc] initWithContentRect:content_rect |
styleMask:style_mask |
@@ -193,13 +218,13 @@ void NativeWidgetMac::InitModalType(ui::ModalType modal_type) { |
} |
gfx::Rect NativeWidgetMac::GetWindowBoundsInScreen() const { |
- NOTIMPLEMENTED(); |
- return gfx::Rect(); |
+ return gfx::ScreenRectFromNSRect([GetNativeWindow() frame]); |
} |
gfx::Rect NativeWidgetMac::GetClientAreaBoundsInScreen() const { |
- NOTIMPLEMENTED(); |
- return gfx::Rect(); |
+ NSWindow* window = GetNativeWindow(); |
+ return gfx::ScreenRectFromNSRect( |
+ [window contentRectForFrameRect:[window frame]]); |
} |
gfx::Rect NativeWidgetMac::GetRestoredBounds() const { |
@@ -208,11 +233,15 @@ gfx::Rect NativeWidgetMac::GetRestoredBounds() const { |
} |
void NativeWidgetMac::SetBounds(const gfx::Rect& bounds) { |
- NOTIMPLEMENTED(); |
+ [GetNativeWindow() setFrame:gfx::ScreenRectToNSRect(bounds) |
+ display:YES |
+ animate:NO]; |
} |
void NativeWidgetMac::SetSize(const gfx::Size& size) { |
- [GetNativeWindow() setContentSize:NSMakeSize(size.width(), size.height())]; |
+ // Ensure the top-left corner stays in-place (rather than the bottom-left, |
+ // which -[NSWindow setContentSize:] would do). |
+ SetBounds(gfx::Rect(GetWindowBoundsInScreen().origin(), size)); |
} |
void NativeWidgetMac::StackAbove(gfx::NativeView native_view) { |
@@ -232,10 +261,13 @@ void NativeWidgetMac::SetShape(gfx::NativeRegion shape) { |
} |
void NativeWidgetMac::Close() { |
- // Calling performClose: will momentarily highlight the close button. |
- [GetNativeWindow() performSelector:@selector(performClose:) |
- withObject:nil |
- afterDelay:0]; |
+ NSWindow* window = GetNativeWindow(); |
+ // Calling performClose: will momentarily highlight the close button, but |
+ // AppKit will reject it if there is no close button. |
+ SEL close_selector = ([window styleMask] & NSClosableWindowMask) |
+ ? @selector(performClose:) |
+ : @selector(close); |
+ [window performSelector:close_selector withObject:nil afterDelay:0]; |
} |
void NativeWidgetMac::CloseNow() { |