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/services/public/cpp/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_delegate.h"
15 #include "ui/base/ime/input_method_factory.h"
16 #include "ui/base/ime/text_input_client.h"
17 #include "ui/wm/core/base_focus_rules.h"
18 #include "ui/wm/core/capture_controller.h"
19 #include "ui/wm/core/focus_controller.h"
24 // TODO: figure out what this should be.
25 class FocusRulesImpl : public wm::BaseFocusRules {
28 virtual ~FocusRulesImpl() {}
30 virtual bool SupportsChildActivation(aura::Window* window) const OVERRIDE {
35 DISALLOW_COPY_AND_ASSIGN(FocusRulesImpl);
38 class MinimalInputEventFilter : public ui::internal::InputMethodDelegate,
39 public ui::EventHandler {
41 explicit MinimalInputEventFilter(aura::Window* root)
43 ui::InitializeInputMethodForTesting();
44 input_method_ = ui::CreateInputMethod(this, gfx::kNullAcceleratedWidget);
45 input_method_->Init(true);
46 root_->AddPreTargetHandler(this);
47 root_->SetProperty(aura::client::kRootWindowInputMethodKey,
51 virtual ~MinimalInputEventFilter() {
52 root_->RemovePreTargetHandler(this);
53 root_->SetProperty(aura::client::kRootWindowInputMethodKey,
54 static_cast<ui::InputMethod*>(NULL));
59 virtual void OnKeyEvent(ui::KeyEvent* event) OVERRIDE {
60 // See the comment in InputMethodEventFilter::OnKeyEvent() for details.
61 if (event->IsTranslated()) {
62 event->SetTranslated(false);
64 if (input_method_->DispatchKeyEvent(*event))
65 event->StopPropagation();
69 // ui::internal::InputMethodDelegate:
70 virtual bool DispatchKeyEventPostIME(const ui::KeyEvent& event) OVERRIDE {
71 // See the comment in InputMethodEventFilter::DispatchKeyEventPostIME() for
73 ui::KeyEvent aura_event(event);
74 aura_event.SetTranslated(true);
75 ui::EventDispatchDetails details =
76 root_->GetHost()->dispatcher()->OnEventFromSource(&aura_event);
77 return aura_event.handled() || details.dispatcher_destroyed;
81 scoped_ptr<ui::InputMethod> input_method_;
83 DISALLOW_COPY_AND_ASSIGN(MinimalInputEventFilter);
88 NativeWidgetViewManager::NativeWidgetViewManager(
89 views::internal::NativeWidgetDelegate* delegate, View* view)
90 : NativeWidgetAura(delegate),
92 view_->AddObserver(this);
93 window_tree_host_.reset(new WindowTreeHostMojo(view_, this));
94 window_tree_host_->InitHost();
97 new MinimalInputEventFilter(window_tree_host_->window()));
99 focus_client_.reset(new wm::FocusController(new FocusRulesImpl));
101 aura::client::SetFocusClient(window_tree_host_->window(),
102 focus_client_.get());
103 aura::client::SetActivationClient(window_tree_host_->window(),
104 focus_client_.get());
105 window_tree_host_->window()->AddPreTargetHandler(focus_client_.get());
107 aura::client::SetCaptureClient(
108 window_tree_host_->window(),
109 new aura::client::DefaultCaptureClient(window_tree_host_->window()));
112 NativeWidgetViewManager::~NativeWidgetViewManager() {
114 view_->RemoveObserver(this);
117 void NativeWidgetViewManager::InitNativeWidget(
118 const views::Widget::InitParams& in_params) {
119 views::Widget::InitParams params(in_params);
120 params.parent = window_tree_host_->window();
121 NativeWidgetAura::InitNativeWidget(params);
122 capture_client_.reset(
123 new wm::ScopedCaptureClient(window_tree_host_->window()));
126 void NativeWidgetViewManager::CompositorContentsChanged(
127 const SkBitmap& bitmap) {
129 view_->SetContents(bitmap);
132 void NativeWidgetViewManager::OnViewDestroyed(View* view) {
133 DCHECK_EQ(view, view_);
134 view->RemoveObserver(this);
136 window_tree_host_.reset();
139 void NativeWidgetViewManager::OnViewBoundsChanged(View* view,
140 const gfx::Rect& old_bounds,
141 const gfx::Rect& new_bounds) {
142 GetWidget()->SetBounds(gfx::Rect(view->bounds().size()));
145 void NativeWidgetViewManager::OnViewInputEvent(View* view,
146 const EventPtr& event) {
147 scoped_ptr<ui::Event> ui_event(event.To<scoped_ptr<ui::Event> >());
149 window_tree_host_->SendEventToProcessor(ui_event.get());