| 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 #import "ui/views/cocoa/bridged_native_widget.h" | 5 #import "ui/views/cocoa/bridged_native_widget.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/mac/mac_util.h" | 8 #include "base/mac/mac_util.h" |
| 9 #import "base/mac/sdk_forward_declarations.h" | 9 #import "base/mac/sdk_forward_declarations.h" |
| 10 #include "ui/base/ime/input_method.h" | 10 #include "ui/base/ime/input_method.h" |
| 11 #include "ui/base/ime/input_method_factory.h" | 11 #include "ui/base/ime/input_method_factory.h" |
| 12 #include "ui/base/ui_base_switches_util.h" | 12 #include "ui/base/ui_base_switches_util.h" |
| 13 #import "ui/gfx/mac/coordinate_conversion.h" | 13 #import "ui/gfx/mac/coordinate_conversion.h" |
| 14 #import "ui/views/cocoa/bridged_content_view.h" | 14 #import "ui/views/cocoa/bridged_content_view.h" |
| 15 #import "ui/views/cocoa/views_nswindow_delegate.h" | 15 #import "ui/views/cocoa/views_nswindow_delegate.h" |
| 16 #include "ui/views/widget/native_widget_mac.h" | 16 #include "ui/views/widget/native_widget_mac.h" |
| 17 #include "ui/views/ime/input_method_bridge.h" | 17 #include "ui/views/ime/input_method_bridge.h" |
| 18 #include "ui/views/ime/null_input_method.h" | 18 #include "ui/views/ime/null_input_method.h" |
| 19 #include "ui/views/view.h" | 19 #include "ui/views/view.h" |
| 20 #include "ui/views/widget/widget.h" | 20 #include "ui/views/widget/widget.h" |
| 21 | 21 |
| 22 namespace views { | 22 namespace views { |
| 23 | 23 |
| 24 BridgedNativeWidget::BridgedNativeWidget(NativeWidgetMac* parent) | 24 BridgedNativeWidget::BridgedNativeWidget(NativeWidgetMac* parent) |
| 25 : native_widget_mac_(parent), | 25 : native_widget_mac_(parent), |
| 26 focus_manager_(NULL), | 26 focus_manager_(NULL), |
| 27 target_fullscreen_state_(false), | 27 target_fullscreen_state_(false), |
| 28 in_fullscreen_transition_(false) { | 28 in_fullscreen_transition_(false), |
| 29 window_visible_(false) { |
| 29 DCHECK(parent); | 30 DCHECK(parent); |
| 30 window_delegate_.reset( | 31 window_delegate_.reset( |
| 31 [[ViewsNSWindowDelegate alloc] initWithBridgedNativeWidget:this]); | 32 [[ViewsNSWindowDelegate alloc] initWithBridgedNativeWidget:this]); |
| 32 } | 33 } |
| 33 | 34 |
| 34 BridgedNativeWidget::~BridgedNativeWidget() { | 35 BridgedNativeWidget::~BridgedNativeWidget() { |
| 35 RemoveOrDestroyChildren(); | 36 RemoveOrDestroyChildren(); |
| 36 SetFocusManager(NULL); | 37 SetFocusManager(NULL); |
| 37 SetRootView(NULL); | 38 SetRootView(NULL); |
| 38 if ([window_ delegate]) { | 39 if ([window_ delegate]) { |
| 39 // If the delegate is still set, it means OnWindowWillClose has not been | 40 // If the delegate is still set, it means OnWindowWillClose has not been |
| 40 // called and the window is still open. Calling -[NSWindow close] will | 41 // called and the window is still open. Calling -[NSWindow close] will |
| 41 // synchronously call OnWindowWillClose and notify NativeWidgetMac. | 42 // synchronously call OnWindowWillClose and notify NativeWidgetMac. |
| 42 [window_ close]; | 43 [window_ close]; |
| 43 } | 44 } |
| 44 DCHECK(![window_ delegate]); | 45 DCHECK(![window_ delegate]); |
| 45 } | 46 } |
| 46 | 47 |
| 47 void BridgedNativeWidget::Init(base::scoped_nsobject<NSWindow> window, | 48 void BridgedNativeWidget::Init(base::scoped_nsobject<NSWindow> window, |
| 48 const Widget::InitParams& params) { | 49 const Widget::InitParams& params) { |
| 49 DCHECK(!window_); | 50 DCHECK(!window_); |
| 50 window_.swap(window); | 51 window_.swap(window); |
| 51 [window_ setDelegate:window_delegate_]; | 52 [window_ setDelegate:window_delegate_]; |
| 52 | 53 |
| 54 // Register for application hide notifications so that visibility can be |
| 55 // properly tracked. This is not done in the delegate so that the lifetime is |
| 56 // tied to the C++ object, rather than the delegate (which may be reference |
| 57 // counted). This is required since the application hides do not send an |
| 58 // orderOut: to individual windows. Unhide, however, does send an order |
| 59 // message. |
| 60 [[NSNotificationCenter defaultCenter] |
| 61 addObserver:window_delegate_ |
| 62 selector:@selector(onWindowOrderChanged:) |
| 63 name:NSApplicationDidHideNotification |
| 64 object:nil]; |
| 65 |
| 53 // Validate the window's initial state, otherwise the bridge's initial | 66 // Validate the window's initial state, otherwise the bridge's initial |
| 54 // tracking state will be incorrect. | 67 // tracking state will be incorrect. |
| 55 DCHECK(![window_ isVisible]); | 68 DCHECK(![window_ isVisible]); |
| 56 DCHECK_EQ(0u, [window_ styleMask] & NSFullScreenWindowMask); | 69 DCHECK_EQ(0u, [window_ styleMask] & NSFullScreenWindowMask); |
| 57 | 70 |
| 58 if (params.parent) { | 71 if (params.parent) { |
| 59 // Use NSWindow to manage child windows. This won't automatically close them | 72 // Use NSWindow to manage child windows. This won't automatically close them |
| 60 // but it will maintain relative positioning of the window layer and origin. | 73 // but it will maintain relative positioning of the window layer and origin. |
| 61 [[params.parent window] addChildWindow:window_ ordered:NSWindowAbove]; | 74 [[params.parent window] addChildWindow:window_ ordered:NSWindowAbove]; |
| 62 } | 75 } |
| (...skipping 27 matching lines...) Expand all Loading... |
| 90 // Objective C initializers can return nil. However, if |view| is non-NULL | 103 // Objective C initializers can return nil. However, if |view| is non-NULL |
| 91 // this should be treated as an error and caught early. | 104 // this should be treated as an error and caught early. |
| 92 CHECK(bridged_view_); | 105 CHECK(bridged_view_); |
| 93 } | 106 } |
| 94 [window_ setContentView:bridged_view_]; | 107 [window_ setContentView:bridged_view_]; |
| 95 } | 108 } |
| 96 | 109 |
| 97 void BridgedNativeWidget::OnWindowWillClose() { | 110 void BridgedNativeWidget::OnWindowWillClose() { |
| 98 [[window_ parentWindow] removeChildWindow:window_]; | 111 [[window_ parentWindow] removeChildWindow:window_]; |
| 99 [window_ setDelegate:nil]; | 112 [window_ setDelegate:nil]; |
| 113 [[NSNotificationCenter defaultCenter] removeObserver:window_delegate_]; |
| 100 native_widget_mac_->OnWindowWillClose(); | 114 native_widget_mac_->OnWindowWillClose(); |
| 101 } | 115 } |
| 102 | 116 |
| 103 void BridgedNativeWidget::OnFullscreenTransitionStart( | 117 void BridgedNativeWidget::OnFullscreenTransitionStart( |
| 104 bool target_fullscreen_state) { | 118 bool target_fullscreen_state) { |
| 105 DCHECK_NE(target_fullscreen_state, target_fullscreen_state_); | 119 DCHECK_NE(target_fullscreen_state, target_fullscreen_state_); |
| 106 target_fullscreen_state_ = target_fullscreen_state; | 120 target_fullscreen_state_ = target_fullscreen_state; |
| 107 in_fullscreen_transition_ = true; | 121 in_fullscreen_transition_ = true; |
| 108 | 122 |
| 109 // If going into fullscreen, store an answer for GetRestoredBounds(). | 123 // If going into fullscreen, store an answer for GetRestoredBounds(). |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 148 | 162 |
| 149 // Since fullscreen requests are ignored if the collection behavior does not | 163 // Since fullscreen requests are ignored if the collection behavior does not |
| 150 // allow it, save the collection behavior and restore it after. | 164 // allow it, save the collection behavior and restore it after. |
| 151 NSWindowCollectionBehavior behavior = [window_ collectionBehavior]; | 165 NSWindowCollectionBehavior behavior = [window_ collectionBehavior]; |
| 152 [window_ setCollectionBehavior:behavior | | 166 [window_ setCollectionBehavior:behavior | |
| 153 NSWindowCollectionBehaviorFullScreenPrimary]; | 167 NSWindowCollectionBehaviorFullScreenPrimary]; |
| 154 [window_ toggleFullScreen:nil]; | 168 [window_ toggleFullScreen:nil]; |
| 155 [window_ setCollectionBehavior:behavior]; | 169 [window_ setCollectionBehavior:behavior]; |
| 156 } | 170 } |
| 157 | 171 |
| 172 void BridgedNativeWidget::OnVisibilityChanged() { |
| 173 if (window_visible_ == [window_ isVisible]) |
| 174 return; |
| 175 |
| 176 window_visible_ = [window_ isVisible]; |
| 177 native_widget_mac_->GetWidget()->OnNativeWidgetVisibilityChanged( |
| 178 window_visible_); |
| 179 } |
| 180 |
| 158 InputMethod* BridgedNativeWidget::CreateInputMethod() { | 181 InputMethod* BridgedNativeWidget::CreateInputMethod() { |
| 159 if (switches::IsTextInputFocusManagerEnabled()) | 182 if (switches::IsTextInputFocusManagerEnabled()) |
| 160 return new NullInputMethod(); | 183 return new NullInputMethod(); |
| 161 | 184 |
| 162 return new InputMethodBridge(this, GetHostInputMethod(), true); | 185 return new InputMethodBridge(this, GetHostInputMethod(), true); |
| 163 } | 186 } |
| 164 | 187 |
| 165 ui::InputMethod* BridgedNativeWidget::GetHostInputMethod() { | 188 ui::InputMethod* BridgedNativeWidget::GetHostInputMethod() { |
| 166 if (!input_method_) { | 189 if (!input_method_) { |
| 167 // Delegate is NULL because Mac IME does not need DispatchKeyEventPostIME | 190 // Delegate is NULL because Mac IME does not need DispatchKeyEventPostIME |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 205 // BridgedNativeWidget, private: | 228 // BridgedNativeWidget, private: |
| 206 | 229 |
| 207 void BridgedNativeWidget::RemoveOrDestroyChildren() { | 230 void BridgedNativeWidget::RemoveOrDestroyChildren() { |
| 208 // TODO(tapted): Implement unowned child windows if required. | 231 // TODO(tapted): Implement unowned child windows if required. |
| 209 base::scoped_nsobject<NSArray> child_windows( | 232 base::scoped_nsobject<NSArray> child_windows( |
| 210 [[NSArray alloc] initWithArray:[window_ childWindows]]); | 233 [[NSArray alloc] initWithArray:[window_ childWindows]]); |
| 211 [child_windows makeObjectsPerformSelector:@selector(close)]; | 234 [child_windows makeObjectsPerformSelector:@selector(close)]; |
| 212 } | 235 } |
| 213 | 236 |
| 214 } // namespace views | 237 } // namespace views |
| OLD | NEW |