Roll src/third_party/skia bc92163:71856d5
[chromium-blink-merge.git] / mojo / views / native_widget_view_manager.cc
bloba9bd40bd50572c6252e9012049e109222b51b204
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 #include "mojo/views/native_widget_view_manager.h"
7 #include "mojo/aura/window_tree_host_mojo.h"
8 #include "mojo/converters/input_events/input_events_type_converters.h"
9 #include "ui/aura/client/aura_constants.h"
10 #include "ui/aura/client/default_capture_client.h"
11 #include "ui/aura/window.h"
12 #include "ui/aura/window_event_dispatcher.h"
13 #include "ui/base/ime/input_method.h"
14 #include "ui/base/ime/input_method_base.h"
15 #include "ui/base/ime/input_method_delegate.h"
16 #include "ui/base/ime/input_method_factory.h"
17 #include "ui/base/ime/text_input_client.h"
18 #include "ui/wm/core/base_focus_rules.h"
19 #include "ui/wm/core/capture_controller.h"
20 #include "ui/wm/core/focus_controller.h"
22 #if defined(OS_LINUX)
23 #include "mojo/views/input_method_mojo_linux.h"
24 #endif
26 namespace mojo {
27 namespace {
29 // TODO: figure out what this should be.
30 class FocusRulesImpl : public wm::BaseFocusRules {
31 public:
32 FocusRulesImpl() {}
33 virtual ~FocusRulesImpl() {}
35 virtual bool SupportsChildActivation(aura::Window* window) const override {
36 return true;
39 private:
40 DISALLOW_COPY_AND_ASSIGN(FocusRulesImpl);
43 class MinimalInputEventFilter : public ui::internal::InputMethodDelegate,
44 public ui::EventHandler {
45 public:
46 explicit MinimalInputEventFilter(aura::Window* root)
47 : root_(root) {
48 ui::InitializeInputMethodForTesting();
49 #if defined(OS_LINUX)
50 input_method_.reset(new InputMethodMojoLinux(this));
51 #else
52 input_method_ = ui::CreateInputMethod(this, gfx::kNullAcceleratedWidget);
53 #endif
54 input_method_->Init(true);
55 root_->AddPreTargetHandler(this);
56 root_->SetProperty(aura::client::kRootWindowInputMethodKey,
57 input_method_.get());
60 virtual ~MinimalInputEventFilter() {
61 root_->RemovePreTargetHandler(this);
62 root_->SetProperty(aura::client::kRootWindowInputMethodKey,
63 static_cast<ui::InputMethod*>(NULL));
66 private:
67 // ui::EventHandler:
68 virtual void OnKeyEvent(ui::KeyEvent* event) override {
69 // See the comment in InputMethodEventFilter::OnKeyEvent() for details.
70 if (event->IsTranslated()) {
71 event->SetTranslated(false);
72 } else {
73 if (input_method_->DispatchKeyEvent(*event))
74 event->StopPropagation();
78 // ui::internal::InputMethodDelegate:
79 virtual bool DispatchKeyEventPostIME(const ui::KeyEvent& event) override {
80 // See the comment in InputMethodEventFilter::DispatchKeyEventPostIME() for
81 // details.
82 ui::KeyEvent aura_event(event);
83 aura_event.SetTranslated(true);
84 ui::EventDispatchDetails details =
85 root_->GetHost()->dispatcher()->OnEventFromSource(&aura_event);
86 return aura_event.handled() || details.dispatcher_destroyed;
89 aura::Window* root_;
90 scoped_ptr<ui::InputMethod> input_method_;
92 DISALLOW_COPY_AND_ASSIGN(MinimalInputEventFilter);
95 } // namespace
97 NativeWidgetViewManager::NativeWidgetViewManager(
98 views::internal::NativeWidgetDelegate* delegate,
99 Shell* shell,
100 View* view)
101 : NativeWidgetAura(delegate), view_(view) {
102 view_->AddObserver(this);
103 window_tree_host_.reset(new WindowTreeHostMojo(shell, view_));
104 window_tree_host_->InitHost();
106 ime_filter_.reset(
107 new MinimalInputEventFilter(window_tree_host_->window()));
109 focus_client_.reset(new wm::FocusController(new FocusRulesImpl));
111 aura::client::SetFocusClient(window_tree_host_->window(),
112 focus_client_.get());
113 aura::client::SetActivationClient(window_tree_host_->window(),
114 focus_client_.get());
115 window_tree_host_->window()->AddPreTargetHandler(focus_client_.get());
117 capture_client_.reset(
118 new aura::client::DefaultCaptureClient(window_tree_host_->window()));
121 NativeWidgetViewManager::~NativeWidgetViewManager() {
122 if (view_)
123 view_->RemoveObserver(this);
126 void NativeWidgetViewManager::InitNativeWidget(
127 const views::Widget::InitParams& in_params) {
128 views::Widget::InitParams params(in_params);
129 params.parent = window_tree_host_->window();
130 NativeWidgetAura::InitNativeWidget(params);
133 void NativeWidgetViewManager::OnViewDestroyed(View* view) {
134 DCHECK_EQ(view, view_);
135 view->RemoveObserver(this);
136 view_ = NULL;
137 // TODO(sky): WindowTreeHostMojo assumes the View outlives it.
138 // NativeWidgetViewManager needs to deal, likely by deleting this.
141 void NativeWidgetViewManager::OnViewBoundsChanged(View* view,
142 const gfx::Rect& old_bounds,
143 const gfx::Rect& new_bounds) {
144 GetWidget()->SetBounds(gfx::Rect(view->bounds().size()));
147 void NativeWidgetViewManager::OnViewInputEvent(View* view,
148 const EventPtr& event) {
149 scoped_ptr<ui::Event> ui_event(event.To<scoped_ptr<ui::Event> >());
150 if (ui_event)
151 window_tree_host_->SendEventToProcessor(ui_event.get());
154 } // namespace mojo