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
3 // found in the LICENSE file.
5 #import "ui/views/cocoa/bridged_native_widget.h"
7 #include "base/logging.h"
8 #include "ui/base/ime/input_method.h"
9 #include "ui/base/ime/input_method_factory.h"
10 #include "ui/base/ui_base_switches_util.h"
11 #import "ui/views/cocoa/bridged_content_view.h"
12 #include "ui/views/ime/input_method_bridge.h"
13 #include "ui/views/ime/null_input_method.h"
14 #include "ui/views/view.h"
15 #include "ui/views/widget/widget.h"
19 BridgedNativeWidget::BridgedNativeWidget() {
22 BridgedNativeWidget::~BridgedNativeWidget() {
26 void BridgedNativeWidget::Init(base::scoped_nsobject<NSWindow> window) {
31 void BridgedNativeWidget::SetRootView(views::View* view) {
32 if (view == [bridged_view_ hostedView])
35 [bridged_view_ clearView];
36 bridged_view_.reset();
37 // Note that there can still be references to the old |bridged_view_|
38 // floating around in Cocoa libraries at this point. However, references to
39 // the old views::View will be gone, so any method calls will become no-ops.
42 bridged_view_.reset([[BridgedContentView alloc] initWithView:view]);
43 // Objective C initializers can return nil. However, if |view| is non-NULL
44 // this should be treated as an error and caught early.
47 [window_ setContentView:bridged_view_];
50 InputMethod* BridgedNativeWidget::CreateInputMethod() {
51 if (switches::IsTextInputFocusManagerEnabled())
52 return new NullInputMethod();
54 return new InputMethodBridge(this, GetHostInputMethod(), true);
57 ui::InputMethod* BridgedNativeWidget::GetHostInputMethod() {
59 // Delegate is NULL because Mac IME does not need DispatchKeyEventPostIME
61 input_method_ = ui::CreateInputMethod(NULL, nil);
63 return input_method_.get();
66 ////////////////////////////////////////////////////////////////////////////////
67 // BridgedNativeWidget, internal::InputMethodDelegate:
69 void BridgedNativeWidget::DispatchKeyEventPostIME(const ui::KeyEvent& key) {
70 // Mac key events don't go through this, but some unit tests that use
71 // MockInputMethod do.
72 Widget* widget = [bridged_view_ hostedView]->GetWidget();
73 widget->OnKeyEvent(const_cast<ui::KeyEvent*>(&key));
74 if (!key.handled() && widget->GetFocusManager())
75 widget->GetFocusManager()->OnKeyEvent(key);