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_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"
23 #include "mojo/views/input_method_mojo_linux.h"
29 // TODO: figure out what this should be.
30 class FocusRulesImpl
: public wm::BaseFocusRules
{
33 virtual ~FocusRulesImpl() {}
35 virtual bool SupportsChildActivation(aura::Window
* window
) const OVERRIDE
{
40 DISALLOW_COPY_AND_ASSIGN(FocusRulesImpl
);
43 class MinimalInputEventFilter
: public ui::internal::InputMethodDelegate
,
44 public ui::EventHandler
{
46 explicit MinimalInputEventFilter(aura::Window
* root
)
48 ui::InitializeInputMethodForTesting();
50 input_method_
.reset(new InputMethodMojoLinux(this));
52 input_method_
= ui::CreateInputMethod(this, gfx::kNullAcceleratedWidget
);
54 input_method_
->Init(true);
55 root_
->AddPreTargetHandler(this);
56 root_
->SetProperty(aura::client::kRootWindowInputMethodKey
,
60 virtual ~MinimalInputEventFilter() {
61 root_
->RemovePreTargetHandler(this);
62 root_
->SetProperty(aura::client::kRootWindowInputMethodKey
,
63 static_cast<ui::InputMethod
*>(NULL
));
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);
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
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
;
90 scoped_ptr
<ui::InputMethod
> input_method_
;
92 DISALLOW_COPY_AND_ASSIGN(MinimalInputEventFilter
);
97 NativeWidgetViewManager::NativeWidgetViewManager(
98 views::internal::NativeWidgetDelegate
* delegate
, View
* view
)
99 : NativeWidgetAura(delegate
),
101 view_
->AddObserver(this);
102 window_tree_host_
.reset(new WindowTreeHostMojo(view_
, this));
103 window_tree_host_
->InitHost();
106 new MinimalInputEventFilter(window_tree_host_
->window()));
108 focus_client_
.reset(new wm::FocusController(new FocusRulesImpl
));
110 aura::client::SetFocusClient(window_tree_host_
->window(),
111 focus_client_
.get());
112 aura::client::SetActivationClient(window_tree_host_
->window(),
113 focus_client_
.get());
114 window_tree_host_
->window()->AddPreTargetHandler(focus_client_
.get());
116 aura::client::SetCaptureClient(
117 window_tree_host_
->window(),
118 new aura::client::DefaultCaptureClient(window_tree_host_
->window()));
121 NativeWidgetViewManager::~NativeWidgetViewManager() {
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
);
131 capture_client_
.reset(
132 new wm::ScopedCaptureClient(window_tree_host_
->window()));
135 void NativeWidgetViewManager::CompositorContentsChanged(
136 const SkBitmap
& bitmap
) {
138 view_
->SetContents(bitmap
);
141 void NativeWidgetViewManager::OnViewDestroyed(View
* view
) {
142 DCHECK_EQ(view
, view_
);
143 view
->RemoveObserver(this);
145 window_tree_host_
.reset();
148 void NativeWidgetViewManager::OnViewBoundsChanged(View
* view
,
149 const gfx::Rect
& old_bounds
,
150 const gfx::Rect
& new_bounds
) {
151 GetWidget()->SetBounds(gfx::Rect(view
->bounds().size()));
154 void NativeWidgetViewManager::OnViewInputEvent(View
* view
,
155 const EventPtr
& event
) {
156 scoped_ptr
<ui::Event
> ui_event(event
.To
<scoped_ptr
<ui::Event
> >());
158 window_tree_host_
->SendEventToProcessor(ui_event
.get());