Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ui/views/widget/native_widget_mac.h" | 5 #include "ui/views/widget/native_widget_mac.h" |
| 6 | 6 |
| 7 #import <Cocoa/Cocoa.h> | 7 #import <Cocoa/Cocoa.h> |
| 8 | 8 |
| 9 #include "base/mac/foundation_util.h" | 9 #include "base/mac/foundation_util.h" |
| 10 #include "base/mac/scoped_nsobject.h" | 10 #include "base/mac/scoped_nsobject.h" |
| 11 #include "ui/gfx/font_list.h" | 11 #include "ui/gfx/font_list.h" |
| 12 #include "ui/gfx/mac/point_utils.h" | |
| 12 #include "ui/native_theme/native_theme.h" | 13 #include "ui/native_theme/native_theme.h" |
| 13 #import "ui/views/cocoa/bridged_content_view.h" | 14 #import "ui/views/cocoa/bridged_content_view.h" |
| 14 #import "ui/views/cocoa/bridged_native_widget.h" | 15 #import "ui/views/cocoa/bridged_native_widget.h" |
| 15 #import "ui/views/cocoa/views_nswindow_delegate.h" | 16 #import "ui/views/cocoa/views_nswindow_delegate.h" |
| 16 | 17 |
| 17 namespace views { | 18 namespace views { |
| 19 namespace { | |
| 20 | |
| 21 NSInteger StyleMaskForParams(const Widget::InitParams& params) { | |
| 22 // TODO(tapted): Determine better masks when there are use cases for it. | |
| 23 if (params.type == Widget::InitParams::TYPE_WINDOW) { | |
| 24 return NSTitledWindowMask | NSClosableWindowMask | | |
| 25 NSMiniaturizableWindowMask | NSResizableWindowMask; | |
| 26 } | |
| 27 return NSBorderlessWindowMask; | |
| 28 } | |
| 29 | |
| 30 NSRect ValidateContentRect(NSRect content_rect) { | |
| 31 // A contentRect with zero width or height is a banned practice in Chrome, due | |
| 32 // to unpredictable OSX treatment. For now, silently give a minimum dimension. | |
| 33 // TODO(tapted): Add a DCHECK, or add emulation logic (e.g. to auto-hide). | |
| 34 if (NSWidth(content_rect) == 0) | |
| 35 content_rect.size.width = 1; | |
| 36 | |
| 37 if (NSHeight(content_rect) == 0) | |
| 38 content_rect.size.height = 1; | |
| 39 | |
| 40 return content_rect; | |
| 41 } | |
| 42 | |
| 43 } // namespace | |
| 18 | 44 |
| 19 //////////////////////////////////////////////////////////////////////////////// | 45 //////////////////////////////////////////////////////////////////////////////// |
| 20 // NativeWidgetMac, public: | 46 // NativeWidgetMac, public: |
| 21 | 47 |
| 22 NativeWidgetMac::NativeWidgetMac(internal::NativeWidgetDelegate* delegate) | 48 NativeWidgetMac::NativeWidgetMac(internal::NativeWidgetDelegate* delegate) |
| 23 : delegate_(delegate), | 49 : delegate_(delegate), |
| 24 bridge_(new BridgedNativeWidget(this)), | 50 bridge_(new BridgedNativeWidget(this)), |
| 25 ownership_(Widget::InitParams::NATIVE_WIDGET_OWNS_WIDGET) { | 51 ownership_(Widget::InitParams::NATIVE_WIDGET_OWNS_WIDGET) { |
| 26 } | 52 } |
| 27 | 53 |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 41 if (ownership_ == Widget::InitParams::NATIVE_WIDGET_OWNS_WIDGET) | 67 if (ownership_ == Widget::InitParams::NATIVE_WIDGET_OWNS_WIDGET) |
| 42 delete this; | 68 delete this; |
| 43 } | 69 } |
| 44 | 70 |
| 45 //////////////////////////////////////////////////////////////////////////////// | 71 //////////////////////////////////////////////////////////////////////////////// |
| 46 // NativeWidgetMac, internal::NativeWidgetPrivate implementation: | 72 // NativeWidgetMac, internal::NativeWidgetPrivate implementation: |
| 47 | 73 |
| 48 void NativeWidgetMac::InitNativeWidget(const Widget::InitParams& params) { | 74 void NativeWidgetMac::InitNativeWidget(const Widget::InitParams& params) { |
| 49 ownership_ = params.ownership; | 75 ownership_ = params.ownership; |
| 50 | 76 |
| 51 // TODO(tapted): Convert position into Cocoa's flipped coordinate space. | 77 NSInteger style_mask = StyleMaskForParams(params); |
| 52 NSRect content_rect = | 78 NSRect content_rect = ValidateContentRect( |
| 53 NSMakeRect(0, 0, params.bounds.width(), params.bounds.height()); | 79 [NSWindow contentRectForFrameRect:gfx::ScreenRectToNSRect(params.bounds) |
| 54 // TODO(tapted): Determine a good initial style mask from |params|. | 80 styleMask:style_mask]); |
| 55 NSInteger style_mask = NSTitledWindowMask | NSClosableWindowMask | | 81 |
| 56 NSMiniaturizableWindowMask | NSResizableWindowMask; | |
| 57 base::scoped_nsobject<NSWindow> window( | 82 base::scoped_nsobject<NSWindow> window( |
| 58 [[NSWindow alloc] initWithContentRect:content_rect | 83 [[NSWindow alloc] initWithContentRect:content_rect |
| 59 styleMask:style_mask | 84 styleMask:style_mask |
| 60 backing:NSBackingStoreBuffered | 85 backing:NSBackingStoreBuffered |
| 61 defer:NO]); | 86 defer:NO]); |
| 62 [window setReleasedWhenClosed:NO]; // Owned by scoped_nsobject. | 87 [window setReleasedWhenClosed:NO]; // Owned by scoped_nsobject. |
| 63 bridge_->Init(window, params); | 88 bridge_->Init(window, params); |
| 64 | 89 |
| 65 delegate_->OnNativeWidgetCreated(true); | 90 delegate_->OnNativeWidgetCreated(true); |
| 66 } | 91 } |
| (...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 184 void NativeWidgetMac::SetWindowIcons(const gfx::ImageSkia& window_icon, | 209 void NativeWidgetMac::SetWindowIcons(const gfx::ImageSkia& window_icon, |
| 185 const gfx::ImageSkia& app_icon) { | 210 const gfx::ImageSkia& app_icon) { |
| 186 NOTIMPLEMENTED(); | 211 NOTIMPLEMENTED(); |
| 187 } | 212 } |
| 188 | 213 |
| 189 void NativeWidgetMac::InitModalType(ui::ModalType modal_type) { | 214 void NativeWidgetMac::InitModalType(ui::ModalType modal_type) { |
| 190 NOTIMPLEMENTED(); | 215 NOTIMPLEMENTED(); |
| 191 } | 216 } |
| 192 | 217 |
| 193 gfx::Rect NativeWidgetMac::GetWindowBoundsInScreen() const { | 218 gfx::Rect NativeWidgetMac::GetWindowBoundsInScreen() const { |
| 194 NOTIMPLEMENTED(); | 219 return gfx::ScreenRectFromNSRect([GetNativeWindow() frame]); |
| 195 return gfx::Rect(); | |
| 196 } | 220 } |
| 197 | 221 |
| 198 gfx::Rect NativeWidgetMac::GetClientAreaBoundsInScreen() const { | 222 gfx::Rect NativeWidgetMac::GetClientAreaBoundsInScreen() const { |
| 199 NOTIMPLEMENTED(); | 223 NSWindow* window = GetNativeWindow(); |
| 200 return gfx::Rect(); | 224 return gfx::ScreenRectFromNSRect( |
| 225 [window contentRectForFrameRect:[window frame]]); | |
| 201 } | 226 } |
| 202 | 227 |
| 203 gfx::Rect NativeWidgetMac::GetRestoredBounds() const { | 228 gfx::Rect NativeWidgetMac::GetRestoredBounds() const { |
| 204 NOTIMPLEMENTED(); | 229 NOTIMPLEMENTED(); |
| 205 return gfx::Rect(); | 230 return gfx::Rect(); |
| 206 } | 231 } |
| 207 | 232 |
| 208 void NativeWidgetMac::SetBounds(const gfx::Rect& bounds) { | 233 void NativeWidgetMac::SetBounds(const gfx::Rect& bounds) { |
| 209 NOTIMPLEMENTED(); | 234 [GetNativeWindow() setFrame:gfx::ScreenRectToNSRect(bounds) |
| 235 display:YES | |
| 236 animate:NO]; | |
| 210 } | 237 } |
| 211 | 238 |
| 212 void NativeWidgetMac::SetSize(const gfx::Size& size) { | 239 void NativeWidgetMac::SetSize(const gfx::Size& size) { |
| 213 [GetNativeWindow() setContentSize:NSMakeSize(size.width(), size.height())]; | 240 NSWindow* window = GetNativeWindow(); |
| 241 NSRect frame_rect = NSMakeRect(0, 0, size.width(), size.height()); | |
| 242 NSRect content_rect = [window contentRectForFrameRect:frame_rect]; | |
|
Andre
2014/06/25 17:59:51
Isn't this supposed to set the window's frame inst
tapted
2014/06/26 08:44:32
This part of it was OK -- there's no -[NSWindow se
| |
| 243 [window setContentSize:content_rect.size]; | |
| 214 } | 244 } |
|
tapted
2014/06/25 10:55:56
Not sure if calling -[NSWindow setContenSize:] is
| |
| 215 | 245 |
| 216 void NativeWidgetMac::StackAbove(gfx::NativeView native_view) { | 246 void NativeWidgetMac::StackAbove(gfx::NativeView native_view) { |
| 217 NOTIMPLEMENTED(); | 247 NOTIMPLEMENTED(); |
| 218 } | 248 } |
| 219 | 249 |
| 220 void NativeWidgetMac::StackAtTop() { | 250 void NativeWidgetMac::StackAtTop() { |
| 221 NOTIMPLEMENTED(); | 251 NOTIMPLEMENTED(); |
| 222 } | 252 } |
| 223 | 253 |
| 224 void NativeWidgetMac::StackBelow(gfx::NativeView native_view) { | 254 void NativeWidgetMac::StackBelow(gfx::NativeView native_view) { |
| 225 NOTIMPLEMENTED(); | 255 NOTIMPLEMENTED(); |
| 226 } | 256 } |
| 227 | 257 |
| 228 void NativeWidgetMac::SetShape(gfx::NativeRegion shape) { | 258 void NativeWidgetMac::SetShape(gfx::NativeRegion shape) { |
| 229 NOTIMPLEMENTED(); | 259 NOTIMPLEMENTED(); |
| 230 } | 260 } |
| 231 | 261 |
| 232 void NativeWidgetMac::Close() { | 262 void NativeWidgetMac::Close() { |
| 233 // Calling performClose: will momentarily highlight the close button. | 263 NSWindow* window = GetNativeWindow(); |
| 234 [GetNativeWindow() performSelector:@selector(performClose:) | 264 // Calling performClose: will momentarily highlight the close button, but |
| 235 withObject:nil | 265 // AppKit will reject it if there is no close button. |
| 236 afterDelay:0]; | 266 SEL close_selector = ([window styleMask] & NSClosableWindowMask) |
| 267 ? @selector(performClose:) | |
| 268 : @selector(close); | |
| 269 [window performSelector:close_selector withObject:nil afterDelay:0]; | |
| 237 } | 270 } |
| 238 | 271 |
| 239 void NativeWidgetMac::CloseNow() { | 272 void NativeWidgetMac::CloseNow() { |
| 240 // Reset |bridge_| to NULL before destroying it. | 273 // Reset |bridge_| to NULL before destroying it. |
| 241 scoped_ptr<BridgedNativeWidget> bridge(bridge_.Pass()); | 274 scoped_ptr<BridgedNativeWidget> bridge(bridge_.Pass()); |
| 242 } | 275 } |
| 243 | 276 |
| 244 void NativeWidgetMac::Show() { | 277 void NativeWidgetMac::Show() { |
| 245 NOTIMPLEMENTED(); | 278 NOTIMPLEMENTED(); |
| 246 } | 279 } |
| (...skipping 239 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 486 } | 519 } |
| 487 | 520 |
| 488 // static | 521 // static |
| 489 gfx::FontList NativeWidgetPrivate::GetWindowTitleFontList() { | 522 gfx::FontList NativeWidgetPrivate::GetWindowTitleFontList() { |
| 490 NOTIMPLEMENTED(); | 523 NOTIMPLEMENTED(); |
| 491 return gfx::FontList(); | 524 return gfx::FontList(); |
| 492 } | 525 } |
| 493 | 526 |
| 494 } // namespace internal | 527 } // namespace internal |
| 495 } // namespace views | 528 } // namespace views |
| OLD | NEW |