| 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 #import "ui/gfx/mac/coordinate_conversion.h" | |
| 13 #include "ui/native_theme/native_theme.h" | 12 #include "ui/native_theme/native_theme.h" |
| 14 #import "ui/views/cocoa/bridged_content_view.h" | 13 #import "ui/views/cocoa/bridged_content_view.h" |
| 15 #import "ui/views/cocoa/bridged_native_widget.h" | 14 #import "ui/views/cocoa/bridged_native_widget.h" |
| 16 #import "ui/views/cocoa/views_nswindow_delegate.h" | 15 #import "ui/views/cocoa/views_nswindow_delegate.h" |
| 17 | 16 |
| 18 namespace views { | 17 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 | |
| 44 | 18 |
| 45 //////////////////////////////////////////////////////////////////////////////// | 19 //////////////////////////////////////////////////////////////////////////////// |
| 46 // NativeWidgetMac, public: | 20 // NativeWidgetMac, public: |
| 47 | 21 |
| 48 NativeWidgetMac::NativeWidgetMac(internal::NativeWidgetDelegate* delegate) | 22 NativeWidgetMac::NativeWidgetMac(internal::NativeWidgetDelegate* delegate) |
| 49 : delegate_(delegate), | 23 : delegate_(delegate), |
| 50 bridge_(new BridgedNativeWidget(this)), | 24 bridge_(new BridgedNativeWidget(this)), |
| 51 ownership_(Widget::InitParams::NATIVE_WIDGET_OWNS_WIDGET) { | 25 ownership_(Widget::InitParams::NATIVE_WIDGET_OWNS_WIDGET) { |
| 52 } | 26 } |
| 53 | 27 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 67 if (ownership_ == Widget::InitParams::NATIVE_WIDGET_OWNS_WIDGET) | 41 if (ownership_ == Widget::InitParams::NATIVE_WIDGET_OWNS_WIDGET) |
| 68 delete this; | 42 delete this; |
| 69 } | 43 } |
| 70 | 44 |
| 71 //////////////////////////////////////////////////////////////////////////////// | 45 //////////////////////////////////////////////////////////////////////////////// |
| 72 // NativeWidgetMac, internal::NativeWidgetPrivate implementation: | 46 // NativeWidgetMac, internal::NativeWidgetPrivate implementation: |
| 73 | 47 |
| 74 void NativeWidgetMac::InitNativeWidget(const Widget::InitParams& params) { | 48 void NativeWidgetMac::InitNativeWidget(const Widget::InitParams& params) { |
| 75 ownership_ = params.ownership; | 49 ownership_ = params.ownership; |
| 76 | 50 |
| 77 NSInteger style_mask = StyleMaskForParams(params); | 51 // TODO(tapted): Convert position into Cocoa's flipped coordinate space. |
| 78 NSRect content_rect = ValidateContentRect( | 52 NSRect content_rect = |
| 79 [NSWindow contentRectForFrameRect:gfx::ScreenRectToNSRect(params.bounds) | 53 NSMakeRect(0, 0, params.bounds.width(), params.bounds.height()); |
| 80 styleMask:style_mask]); | 54 // TODO(tapted): Determine a good initial style mask from |params|. |
| 81 | 55 NSInteger style_mask = NSTitledWindowMask | NSClosableWindowMask | |
| 56 NSMiniaturizableWindowMask | NSResizableWindowMask; |
| 82 base::scoped_nsobject<NSWindow> window( | 57 base::scoped_nsobject<NSWindow> window( |
| 83 [[NSWindow alloc] initWithContentRect:content_rect | 58 [[NSWindow alloc] initWithContentRect:content_rect |
| 84 styleMask:style_mask | 59 styleMask:style_mask |
| 85 backing:NSBackingStoreBuffered | 60 backing:NSBackingStoreBuffered |
| 86 defer:NO]); | 61 defer:NO]); |
| 87 [window setReleasedWhenClosed:NO]; // Owned by scoped_nsobject. | 62 [window setReleasedWhenClosed:NO]; // Owned by scoped_nsobject. |
| 88 bridge_->Init(window, params); | 63 bridge_->Init(window, params); |
| 89 | 64 |
| 90 delegate_->OnNativeWidgetCreated(true); | 65 delegate_->OnNativeWidgetCreated(true); |
| 91 | 66 |
| (...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 211 void NativeWidgetMac::SetWindowIcons(const gfx::ImageSkia& window_icon, | 186 void NativeWidgetMac::SetWindowIcons(const gfx::ImageSkia& window_icon, |
| 212 const gfx::ImageSkia& app_icon) { | 187 const gfx::ImageSkia& app_icon) { |
| 213 NOTIMPLEMENTED(); | 188 NOTIMPLEMENTED(); |
| 214 } | 189 } |
| 215 | 190 |
| 216 void NativeWidgetMac::InitModalType(ui::ModalType modal_type) { | 191 void NativeWidgetMac::InitModalType(ui::ModalType modal_type) { |
| 217 NOTIMPLEMENTED(); | 192 NOTIMPLEMENTED(); |
| 218 } | 193 } |
| 219 | 194 |
| 220 gfx::Rect NativeWidgetMac::GetWindowBoundsInScreen() const { | 195 gfx::Rect NativeWidgetMac::GetWindowBoundsInScreen() const { |
| 221 return gfx::ScreenRectFromNSRect([GetNativeWindow() frame]); | 196 NOTIMPLEMENTED(); |
| 197 return gfx::Rect(); |
| 222 } | 198 } |
| 223 | 199 |
| 224 gfx::Rect NativeWidgetMac::GetClientAreaBoundsInScreen() const { | 200 gfx::Rect NativeWidgetMac::GetClientAreaBoundsInScreen() const { |
| 225 NSWindow* window = GetNativeWindow(); | 201 NOTIMPLEMENTED(); |
| 226 return gfx::ScreenRectFromNSRect( | 202 return gfx::Rect(); |
| 227 [window contentRectForFrameRect:[window frame]]); | |
| 228 } | 203 } |
| 229 | 204 |
| 230 gfx::Rect NativeWidgetMac::GetRestoredBounds() const { | 205 gfx::Rect NativeWidgetMac::GetRestoredBounds() const { |
| 231 NOTIMPLEMENTED(); | 206 NOTIMPLEMENTED(); |
| 232 return gfx::Rect(); | 207 return gfx::Rect(); |
| 233 } | 208 } |
| 234 | 209 |
| 235 void NativeWidgetMac::SetBounds(const gfx::Rect& bounds) { | 210 void NativeWidgetMac::SetBounds(const gfx::Rect& bounds) { |
| 236 [GetNativeWindow() setFrame:gfx::ScreenRectToNSRect(bounds) | 211 NOTIMPLEMENTED(); |
| 237 display:YES | |
| 238 animate:NO]; | |
| 239 } | 212 } |
| 240 | 213 |
| 241 void NativeWidgetMac::SetSize(const gfx::Size& size) { | 214 void NativeWidgetMac::SetSize(const gfx::Size& size) { |
| 242 // Ensure the top-left corner stays in-place (rather than the bottom-left, | 215 [GetNativeWindow() setContentSize:NSMakeSize(size.width(), size.height())]; |
| 243 // which -[NSWindow setContentSize:] would do). | |
| 244 SetBounds(gfx::Rect(GetWindowBoundsInScreen().origin(), size)); | |
| 245 } | 216 } |
| 246 | 217 |
| 247 void NativeWidgetMac::StackAbove(gfx::NativeView native_view) { | 218 void NativeWidgetMac::StackAbove(gfx::NativeView native_view) { |
| 248 NOTIMPLEMENTED(); | 219 NOTIMPLEMENTED(); |
| 249 } | 220 } |
| 250 | 221 |
| 251 void NativeWidgetMac::StackAtTop() { | 222 void NativeWidgetMac::StackAtTop() { |
| 252 NOTIMPLEMENTED(); | 223 NOTIMPLEMENTED(); |
| 253 } | 224 } |
| 254 | 225 |
| 255 void NativeWidgetMac::StackBelow(gfx::NativeView native_view) { | 226 void NativeWidgetMac::StackBelow(gfx::NativeView native_view) { |
| 256 NOTIMPLEMENTED(); | 227 NOTIMPLEMENTED(); |
| 257 } | 228 } |
| 258 | 229 |
| 259 void NativeWidgetMac::SetShape(gfx::NativeRegion shape) { | 230 void NativeWidgetMac::SetShape(gfx::NativeRegion shape) { |
| 260 NOTIMPLEMENTED(); | 231 NOTIMPLEMENTED(); |
| 261 } | 232 } |
| 262 | 233 |
| 263 void NativeWidgetMac::Close() { | 234 void NativeWidgetMac::Close() { |
| 264 NSWindow* window = GetNativeWindow(); | 235 // Calling performClose: will momentarily highlight the close button. |
| 265 // Calling performClose: will momentarily highlight the close button, but | 236 [GetNativeWindow() performSelector:@selector(performClose:) |
| 266 // AppKit will reject it if there is no close button. | 237 withObject:nil |
| 267 SEL close_selector = ([window styleMask] & NSClosableWindowMask) | 238 afterDelay:0]; |
| 268 ? @selector(performClose:) | |
| 269 : @selector(close); | |
| 270 [window performSelector:close_selector withObject:nil afterDelay:0]; | |
| 271 } | 239 } |
| 272 | 240 |
| 273 void NativeWidgetMac::CloseNow() { | 241 void NativeWidgetMac::CloseNow() { |
| 274 // Reset |bridge_| to NULL before destroying it. | 242 // Reset |bridge_| to NULL before destroying it. |
| 275 scoped_ptr<BridgedNativeWidget> bridge(bridge_.Pass()); | 243 scoped_ptr<BridgedNativeWidget> bridge(bridge_.Pass()); |
| 276 } | 244 } |
| 277 | 245 |
| 278 void NativeWidgetMac::Show() { | 246 void NativeWidgetMac::Show() { |
| 279 NOTIMPLEMENTED(); | 247 NOTIMPLEMENTED(); |
| 280 } | 248 } |
| (...skipping 239 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 520 } | 488 } |
| 521 | 489 |
| 522 // static | 490 // static |
| 523 gfx::FontList NativeWidgetPrivate::GetWindowTitleFontList() { | 491 gfx::FontList NativeWidgetPrivate::GetWindowTitleFontList() { |
| 524 NOTIMPLEMENTED(); | 492 NOTIMPLEMENTED(); |
| 525 return gfx::FontList(); | 493 return gfx::FontList(); |
| 526 } | 494 } |
| 527 | 495 |
| 528 } // namespace internal | 496 } // namespace internal |
| 529 } // namespace views | 497 } // namespace views |
| OLD | NEW |