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 "ui/base/ime/input_method.h" | 8 #include "ui/base/ime/input_method.h" |
9 #include "ui/base/ime/input_method_factory.h" | 9 #include "ui/base/ime/input_method_factory.h" |
10 #include "ui/base/ui_base_switches_util.h" | 10 #include "ui/base/ui_base_switches_util.h" |
11 #import "ui/views/cocoa/bridged_content_view.h" | 11 #import "ui/views/cocoa/bridged_content_view.h" |
12 #import "ui/views/cocoa/views_nswindow_delegate.h" | 12 #import "ui/views/cocoa/views_nswindow_delegate.h" |
13 #include "ui/views/widget/native_widget_mac.h" | 13 #include "ui/views/widget/native_widget_mac.h" |
14 #include "ui/views/ime/input_method_bridge.h" | 14 #include "ui/views/ime/input_method_bridge.h" |
15 #include "ui/views/ime/null_input_method.h" | 15 #include "ui/views/ime/null_input_method.h" |
16 #include "ui/views/view.h" | 16 #include "ui/views/view.h" |
| 17 #include "ui/views/widget/widget.h" |
17 | 18 |
18 namespace views { | 19 namespace views { |
19 | 20 |
20 BridgedNativeWidget::BridgedNativeWidget(NativeWidgetMac* parent) | 21 BridgedNativeWidget::BridgedNativeWidget(NativeWidgetMac* parent) |
21 : native_widget_mac_(parent) { | 22 : native_widget_mac_(parent), focus_manager_(NULL) { |
22 DCHECK(parent); | 23 DCHECK(parent); |
23 window_delegate_.reset( | 24 window_delegate_.reset( |
24 [[ViewsNSWindowDelegate alloc] initWithBridgedNativeWidget:this]); | 25 [[ViewsNSWindowDelegate alloc] initWithBridgedNativeWidget:this]); |
25 } | 26 } |
26 | 27 |
27 BridgedNativeWidget::~BridgedNativeWidget() { | 28 BridgedNativeWidget::~BridgedNativeWidget() { |
28 RemoveOrDestroyChildren(); | 29 RemoveOrDestroyChildren(); |
| 30 SetFocusManager(NULL); |
29 SetRootView(NULL); | 31 SetRootView(NULL); |
30 if ([window_ delegate]) { | 32 if ([window_ delegate]) { |
31 // If the delegate is still set, it means OnWindowWillClose has not been | 33 // If the delegate is still set, it means OnWindowWillClose has not been |
32 // called and the window is still open. Calling -[NSWindow close] will | 34 // called and the window is still open. Calling -[NSWindow close] will |
33 // synchronously call OnWindowWillClose and notify NativeWidgetMac. | 35 // synchronously call OnWindowWillClose and notify NativeWidgetMac. |
34 [window_ close]; | 36 [window_ close]; |
35 } | 37 } |
36 DCHECK(![window_ delegate]); | 38 DCHECK(![window_ delegate]); |
37 } | 39 } |
38 | 40 |
39 void BridgedNativeWidget::Init(base::scoped_nsobject<NSWindow> window, | 41 void BridgedNativeWidget::Init(base::scoped_nsobject<NSWindow> window, |
40 const Widget::InitParams& params) { | 42 const Widget::InitParams& params) { |
41 DCHECK(!window_); | 43 DCHECK(!window_); |
42 window_.swap(window); | 44 window_.swap(window); |
43 [window_ setDelegate:window_delegate_]; | 45 [window_ setDelegate:window_delegate_]; |
44 | 46 |
45 if (params.parent) { | 47 if (params.parent) { |
46 // Use NSWindow to manage child windows. This won't automatically close them | 48 // Use NSWindow to manage child windows. This won't automatically close them |
47 // but it will maintain relative positioning of the window layer and origin. | 49 // but it will maintain relative positioning of the window layer and origin. |
48 [[params.parent window] addChildWindow:window_ ordered:NSWindowAbove]; | 50 [[params.parent window] addChildWindow:window_ ordered:NSWindowAbove]; |
49 } | 51 } |
50 } | 52 } |
51 | 53 |
| 54 void BridgedNativeWidget::SetFocusManager(FocusManager* focus_manager) { |
| 55 if (focus_manager_ == focus_manager) |
| 56 return; |
| 57 |
| 58 if (focus_manager_) |
| 59 focus_manager_->RemoveFocusChangeListener(this); |
| 60 |
| 61 if (focus_manager) |
| 62 focus_manager->AddFocusChangeListener(this); |
| 63 |
| 64 focus_manager_ = focus_manager; |
| 65 } |
| 66 |
52 void BridgedNativeWidget::SetRootView(views::View* view) { | 67 void BridgedNativeWidget::SetRootView(views::View* view) { |
53 if (view == [bridged_view_ hostedView]) | 68 if (view == [bridged_view_ hostedView]) |
54 return; | 69 return; |
55 | 70 |
56 [bridged_view_ clearView]; | 71 [bridged_view_ clearView]; |
57 bridged_view_.reset(); | 72 bridged_view_.reset(); |
58 // Note that there can still be references to the old |bridged_view_| | 73 // Note that there can still be references to the old |bridged_view_| |
59 // floating around in Cocoa libraries at this point. However, references to | 74 // floating around in Cocoa libraries at this point. However, references to |
60 // the old views::View will be gone, so any method calls will become no-ops. | 75 // the old views::View will be gone, so any method calls will become no-ops. |
61 | 76 |
(...skipping 27 matching lines...) Expand all Loading... |
89 } | 104 } |
90 return input_method_.get(); | 105 return input_method_.get(); |
91 } | 106 } |
92 | 107 |
93 //////////////////////////////////////////////////////////////////////////////// | 108 //////////////////////////////////////////////////////////////////////////////// |
94 // BridgedNativeWidget, internal::InputMethodDelegate: | 109 // BridgedNativeWidget, internal::InputMethodDelegate: |
95 | 110 |
96 void BridgedNativeWidget::DispatchKeyEventPostIME(const ui::KeyEvent& key) { | 111 void BridgedNativeWidget::DispatchKeyEventPostIME(const ui::KeyEvent& key) { |
97 // Mac key events don't go through this, but some unit tests that use | 112 // Mac key events don't go through this, but some unit tests that use |
98 // MockInputMethod do. | 113 // MockInputMethod do. |
99 Widget* widget = [bridged_view_ hostedView]->GetWidget(); | 114 DCHECK(focus_manager_); |
100 widget->OnKeyEvent(const_cast<ui::KeyEvent*>(&key)); | 115 native_widget_mac_->GetWidget()->OnKeyEvent(const_cast<ui::KeyEvent*>(&key)); |
101 if (!key.handled() && widget->GetFocusManager()) | 116 if (!key.handled()) |
102 widget->GetFocusManager()->OnKeyEvent(key); | 117 focus_manager_->OnKeyEvent(key); |
| 118 } |
| 119 |
| 120 void BridgedNativeWidget::OnWillChangeFocus(View* focused_before, |
| 121 View* focused_now) { |
| 122 } |
| 123 |
| 124 void BridgedNativeWidget::OnDidChangeFocus(View* focused_before, |
| 125 View* focused_now) { |
| 126 ui::TextInputClient* input_client = |
| 127 focused_now ? focused_now->GetTextInputClient() : NULL; |
| 128 [bridged_view_ setTextInputClient:input_client]; |
103 } | 129 } |
104 | 130 |
105 //////////////////////////////////////////////////////////////////////////////// | 131 //////////////////////////////////////////////////////////////////////////////// |
106 // BridgedNativeWidget, private: | 132 // BridgedNativeWidget, private: |
107 | 133 |
108 void BridgedNativeWidget::RemoveOrDestroyChildren() { | 134 void BridgedNativeWidget::RemoveOrDestroyChildren() { |
109 // TODO(tapted): Implement unowned child windows if required. | 135 // TODO(tapted): Implement unowned child windows if required. |
110 base::scoped_nsobject<NSArray> child_windows( | 136 base::scoped_nsobject<NSArray> child_windows( |
111 [[NSArray alloc] initWithArray:[window_ childWindows]]); | 137 [[NSArray alloc] initWithArray:[window_ childWindows]]); |
112 [child_windows makeObjectsPerformSelector:@selector(close)]; | 138 [child_windows makeObjectsPerformSelector:@selector(close)]; |
113 } | 139 } |
114 | 140 |
115 } // namespace views | 141 } // namespace views |
OLD | NEW |