Chromium Code Reviews| Index: chrome/browser/ui/cocoa/apps/native_app_window_cocoa.mm |
| diff --git a/chrome/browser/ui/cocoa/apps/native_app_window_cocoa.mm b/chrome/browser/ui/cocoa/apps/native_app_window_cocoa.mm |
| index cbaaa437c5491a1f4112d94b0b20e8ddfb9fb9ca..f2516c760525b8b95a4d7509e64e3a76fc670315 100644 |
| --- a/chrome/browser/ui/cocoa/apps/native_app_window_cocoa.mm |
| +++ b/chrome/browser/ui/cocoa/apps/native_app_window_cocoa.mm |
| @@ -87,6 +87,33 @@ NSInteger AlwaysOnTopWindowLevel() { |
| return NSFloatingWindowLevel; |
| } |
| +NSRect GfxToCocoaBounds(const gfx::Rect& bounds, |
| + bool ensure_position_initialized) { |
|
benwells
2014/03/05 02:33:30
Nit: indenting.
tapted
2014/03/05 03:14:03
nit: indenting. But also this is always called wit
|
| + typedef apps::AppWindow::BoundsSpecification BoundsSpecification; |
| + |
| + // Flip coordinates based on the primary screen. |
| + NSRect main_screen_rect = [[[NSScreen screens] objectAtIndex:0] frame]; |
| + NSRect cocoa_bounds = NSMakeRect( |
| + bounds.x(), |
| + NSHeight(main_screen_rect) - bounds.y() - bounds.height(), |
|
tapted
2014/03/05 03:14:03
nit (a bit nicer):
`NSHeight(main_screen_rect) - b
|
| + bounds.width(), |
| + bounds.height()); |
| + |
| + // If coordinates are < 0, center window on primary screen. |
|
tapted
2014/03/05 03:14:03
This comment doesn't really match (-ve coordinates
|
| + if (ensure_position_initialized && |
| + bounds.x() == BoundsSpecification::kUnspecifiedPosition) { |
| + cocoa_bounds.origin.x = |
| + floor((NSWidth(main_screen_rect) - NSWidth(cocoa_bounds)) / 2); |
| + } |
| + if (ensure_position_initialized && |
| + bounds.y() == BoundsSpecification::kUnspecifiedPosition) { |
| + cocoa_bounds.origin.y = |
| + floor((NSHeight(main_screen_rect) - NSHeight(cocoa_bounds)) / 2); |
| + } |
| + |
| + return cocoa_bounds; |
| +} |
| + |
| } // namespace |
| @implementation NativeAppWindowController |
| @@ -284,29 +311,13 @@ NativeAppWindowCocoa::NativeAppWindowCocoa( |
| is_hidden_with_app_(false), |
| is_maximized_(false), |
| is_fullscreen_(false), |
| + is_resizable_(params.resizable), |
| + shows_resize_controls_(true), |
| + shows_fullscreen_controls_(true), |
| attention_request_id_(0), |
| use_system_drag_(true) { |
| Observe(web_contents()); |
| - // Flip coordinates based on the primary screen. |
| - NSRect main_screen_rect = [[[NSScreen screens] objectAtIndex:0] frame]; |
| - NSRect cocoa_bounds = NSMakeRect(params.bounds.x(), |
| - NSHeight(main_screen_rect) - params.bounds.y() - params.bounds.height(), |
| - params.bounds.width(), params.bounds.height()); |
| - |
| - // If coordinates are < 0, center window on primary screen. |
| - if (params.bounds.x() == INT_MIN) { |
| - cocoa_bounds.origin.x = |
| - floor((NSWidth(main_screen_rect) - NSWidth(cocoa_bounds)) / 2); |
| - } |
| - if (params.bounds.y() == INT_MIN) { |
| - cocoa_bounds.origin.y = |
| - floor((NSHeight(main_screen_rect) - NSHeight(cocoa_bounds)) / 2); |
| - } |
| - |
| - // Initialize |restored_bounds_| after |cocoa_bounds| have been sanitized. |
| - restored_bounds_ = cocoa_bounds; |
| - |
| base::scoped_nsobject<NSWindow> window; |
| Class window_class; |
| if (has_frame_) { |
| @@ -319,12 +330,10 @@ NativeAppWindowCocoa::NativeAppWindowCocoa( |
| window_class = [ShellFramelessNSWindow class]; |
| } |
| - size_constraints_.set_minimum_size(params.minimum_size); |
| - size_constraints_.set_maximum_size(params.maximum_size); |
| - shows_resize_controls_ = |
| - params.resizable && !size_constraints_.HasFixedSize(); |
| - shows_fullscreen_controls_ = |
| - params.resizable && !size_constraints_.HasMaximumSize(); |
| + // Estimate the initial bounds of the window. Once the frame insets are known, |
| + // the window bounds and constraints can be set precisely. |
| + NSRect cocoa_bounds = GfxToCocoaBounds( |
| + params.GetInitialWindowBounds(gfx::Insets()), true); |
| window.reset([[window_class alloc] |
| initWithContentRect:cocoa_bounds |
| styleMask:GetWindowStyleMask() |
| @@ -340,12 +349,6 @@ NativeAppWindowCocoa::NativeAppWindowCocoa( |
| [window setLevel:AlwaysOnTopWindowLevel()]; |
| InitCollectionBehavior(window); |
| - // Set the window to participate in Lion Fullscreen mode. Setting this flag |
| - // has no effect on Snow Leopard or earlier. UI controls for fullscreen are |
| - // only shown for apps that have unbounded size. |
| - if (shows_fullscreen_controls_) |
| - SetFullScreenCollectionBehavior(window, true); |
| - |
| window_controller_.reset( |
| [[NativeAppWindowController alloc] initWithWindow:window.release()]); |
| @@ -364,9 +367,14 @@ NativeAppWindowCocoa::NativeAppWindowCocoa( |
| [[window_controller_ window] setDelegate:window_controller_]; |
| [window_controller_ setAppWindow:this]; |
| - // Update the size constraints of the NSWindow. |
| - SetMinimumSize(params.minimum_size); |
| - SetMaximumSize(params.maximum_size); |
| + // We can now compute the precise window bounds and constraints. |
| + gfx::Insets insets = GetFrameInsets(); |
| + SetBounds(params.GetInitialWindowBounds(insets)); |
| + SetContentSizeConstraints(params.GetContentMinimumSize(insets), |
| + params.GetContentMaximumSize(insets)); |
| + |
| + // Initialize |restored_bounds_|. |
| + restored_bounds_ = [this->window() frame]; |
| extension_keybinding_registry_.reset(new ExtensionKeybindingRegistryCocoa( |
| Profile::FromBrowserContext(app_window_->browser_context()), |
| @@ -613,13 +621,7 @@ void NativeAppWindowCocoa::SetBounds(const gfx::Rect& bounds) { |
| if (checked_bounds.height() > max_size.height) |
| checked_bounds.set_height(max_size.height); |
| - NSRect cocoa_bounds = NSMakeRect(checked_bounds.x(), 0, |
| - checked_bounds.width(), |
| - checked_bounds.height()); |
| - // Flip coordinates based on the primary screen. |
| - NSScreen* screen = [[NSScreen screens] objectAtIndex:0]; |
| - cocoa_bounds.origin.y = NSHeight([screen frame]) - checked_bounds.bottom(); |
| - |
| + NSRect cocoa_bounds = GfxToCocoaBounds(checked_bounds, true); |
| [window() setFrame:cocoa_bounds display:YES]; |
| // setFrame: without animate: does not trigger a windowDidEndLiveResize: so |
| // call it here. |
| @@ -1037,34 +1039,56 @@ void NativeAppWindowCocoa::UpdateRestoredBounds() { |
| restored_bounds_ = [window() frame]; |
| } |
| +void NativeAppWindowCocoa::SetContentSizeConstraints( |
| + const gfx::Size& minimum_size, const gfx::Size& maximum_size) { |
| + // Update the size constraints. |
| + size_constraints_.set_minimum_size(minimum_size); |
| + size_constraints_.set_maximum_size(maximum_size); |
| + |
| + gfx::Size min_size = size_constraints_.GetMinimumSize(); |
| + [window() setContentMinSize:NSMakeSize(min_size.width(), min_size.height())]; |
| + |
| + gfx::Size max_size = size_constraints_.GetMaximumSize(); |
| + const int kUnboundedSize = apps::SizeConstraints::kUnboundedSize; |
| + CGFloat max_width = max_size.width() == kUnboundedSize ? |
| + CGFLOAT_MAX : max_size.width(); |
| + CGFloat max_height = max_size.height() == kUnboundedSize ? |
| + CGFLOAT_MAX : max_size.height(); |
| + [window() setContentMaxSize:NSMakeSize(max_width, max_height)]; |
| + |
| + // Update the window controls. |
| + shows_resize_controls_ = |
| + is_resizable_ && !size_constraints_.HasFixedSize(); |
| + shows_fullscreen_controls_ = |
| + is_resizable_ && !size_constraints_.HasMaximumSize(); |
| + |
| + if (!is_fullscreen_) { |
| + [window() setStyleMask:GetWindowStyleMask()]; |
| + |
| + // Set the window to participate in Lion Fullscreen mode. Setting this flag |
| + // has no effect on Snow Leopard or earlier. UI controls for fullscreen are |
| + // only shown for apps that have unbounded size. |
| + SetFullScreenCollectionBehavior(window(), shows_fullscreen_controls_); |
| + } |
| +} |
| + |
| void NativeAppWindowCocoa::UpdateShelfMenu() { |
| // TODO(tmdiep): To be implemented for Mac. |
| NOTIMPLEMENTED(); |
| } |
| -gfx::Size NativeAppWindowCocoa::GetMinimumSize() const { |
| +gfx::Size NativeAppWindowCocoa::GetContentMinimumSize() const { |
| return size_constraints_.GetMinimumSize(); |
| } |
| -void NativeAppWindowCocoa::SetMinimumSize(const gfx::Size& size) { |
| - size_constraints_.set_minimum_size(size); |
| - |
| - gfx::Size min_size = size_constraints_.GetMinimumSize(); |
| - [window() setContentMinSize:NSMakeSize(min_size.width(), min_size.height())]; |
| +void NativeAppWindowCocoa::SetContentMinimumSize(const gfx::Size& size) { |
| + SetContentSizeConstraints(size, size_constraints_.GetMaximumSize()); |
|
tmdiep
2014/03/04 03:13:14
Fixed bug - when the constraints were changed afte
|
| } |
| -gfx::Size NativeAppWindowCocoa::GetMaximumSize() const { |
| +gfx::Size NativeAppWindowCocoa::GetContentMaximumSize() const { |
| return size_constraints_.GetMaximumSize(); |
| } |
| -void NativeAppWindowCocoa::SetMaximumSize(const gfx::Size& size) { |
| - size_constraints_.set_maximum_size(size); |
| - |
| - gfx::Size max_size = size_constraints_.GetMaximumSize(); |
| - const int kUnboundedSize = apps::SizeConstraints::kUnboundedSize; |
| - CGFloat max_width = max_size.width() == kUnboundedSize ? |
| - CGFLOAT_MAX : max_size.width(); |
| - CGFloat max_height = max_size.height() == kUnboundedSize ? |
| - CGFLOAT_MAX : max_size.height(); |
| - [window() setContentMaxSize:NSMakeSize(max_width, max_height)]; |
| +void NativeAppWindowCocoa::SetContentMaximumSize(const gfx::Size& size) { |
| + SetContentSizeConstraints(size_constraints_.GetMinimumSize(), size); |
| } |