| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #include "mojo/views/native_widget_view_manager.h" | |
| 6 | |
| 7 #include "mojo/aura/window_tree_host_mojo.h" | |
| 8 #include "mojo/converters/geometry/geometry_type_converters.h" | |
| 9 #include "mojo/converters/input_events/input_events_type_converters.h" | |
| 10 #include "ui/aura/client/aura_constants.h" | |
| 11 #include "ui/aura/client/default_capture_client.h" | |
| 12 #include "ui/aura/window.h" | |
| 13 #include "ui/aura/window_event_dispatcher.h" | |
| 14 #include "ui/base/ime/input_method.h" | |
| 15 #include "ui/base/ime/input_method_base.h" | |
| 16 #include "ui/base/ime/input_method_delegate.h" | |
| 17 #include "ui/base/ime/input_method_factory.h" | |
| 18 #include "ui/base/ime/text_input_client.h" | |
| 19 #include "ui/wm/core/base_focus_rules.h" | |
| 20 #include "ui/wm/core/capture_controller.h" | |
| 21 #include "ui/wm/core/focus_controller.h" | |
| 22 | |
| 23 #if defined(OS_LINUX) | |
| 24 #include "mojo/views/input_method_mojo_linux.h" | |
| 25 #endif | |
| 26 | |
| 27 namespace mojo { | |
| 28 namespace { | |
| 29 | |
| 30 // TODO: figure out what this should be. | |
| 31 class FocusRulesImpl : public wm::BaseFocusRules { | |
| 32 public: | |
| 33 FocusRulesImpl() {} | |
| 34 virtual ~FocusRulesImpl() {} | |
| 35 | |
| 36 virtual bool SupportsChildActivation(aura::Window* window) const override { | |
| 37 return true; | |
| 38 } | |
| 39 | |
| 40 private: | |
| 41 DISALLOW_COPY_AND_ASSIGN(FocusRulesImpl); | |
| 42 }; | |
| 43 | |
| 44 class MinimalInputEventFilter : public ui::internal::InputMethodDelegate, | |
| 45 public ui::EventHandler { | |
| 46 public: | |
| 47 explicit MinimalInputEventFilter(aura::Window* root) | |
| 48 : root_(root) { | |
| 49 ui::InitializeInputMethodForTesting(); | |
| 50 #if defined(OS_LINUX) | |
| 51 input_method_.reset(new InputMethodMojoLinux(this)); | |
| 52 #else | |
| 53 input_method_ = ui::CreateInputMethod(this, gfx::kNullAcceleratedWidget); | |
| 54 #endif | |
| 55 input_method_->Init(true); | |
| 56 root_->AddPreTargetHandler(this); | |
| 57 root_->SetProperty(aura::client::kRootWindowInputMethodKey, | |
| 58 input_method_.get()); | |
| 59 } | |
| 60 | |
| 61 virtual ~MinimalInputEventFilter() { | |
| 62 root_->RemovePreTargetHandler(this); | |
| 63 root_->SetProperty(aura::client::kRootWindowInputMethodKey, | |
| 64 static_cast<ui::InputMethod*>(NULL)); | |
| 65 } | |
| 66 | |
| 67 private: | |
| 68 // ui::EventHandler: | |
| 69 virtual void OnKeyEvent(ui::KeyEvent* event) override { | |
| 70 // See the comment in InputMethodEventFilter::OnKeyEvent() for details. | |
| 71 if (event->IsTranslated()) { | |
| 72 event->SetTranslated(false); | |
| 73 } else { | |
| 74 if (input_method_->DispatchKeyEvent(*event)) | |
| 75 event->StopPropagation(); | |
| 76 } | |
| 77 } | |
| 78 | |
| 79 // ui::internal::InputMethodDelegate: | |
| 80 virtual bool DispatchKeyEventPostIME(const ui::KeyEvent& event) override { | |
| 81 // See the comment in InputMethodEventFilter::DispatchKeyEventPostIME() for | |
| 82 // details. | |
| 83 ui::KeyEvent aura_event(event); | |
| 84 aura_event.SetTranslated(true); | |
| 85 ui::EventDispatchDetails details = | |
| 86 root_->GetHost()->dispatcher()->OnEventFromSource(&aura_event); | |
| 87 return aura_event.handled() || details.dispatcher_destroyed; | |
| 88 } | |
| 89 | |
| 90 aura::Window* root_; | |
| 91 scoped_ptr<ui::InputMethod> input_method_; | |
| 92 | |
| 93 DISALLOW_COPY_AND_ASSIGN(MinimalInputEventFilter); | |
| 94 }; | |
| 95 | |
| 96 } // namespace | |
| 97 | |
| 98 NativeWidgetViewManager::NativeWidgetViewManager( | |
| 99 views::internal::NativeWidgetDelegate* delegate, | |
| 100 Shell* shell, | |
| 101 View* view) | |
| 102 : NativeWidgetAura(delegate), view_(view) { | |
| 103 view_->AddObserver(this); | |
| 104 window_tree_host_.reset(new WindowTreeHostMojo(shell, view_)); | |
| 105 window_tree_host_->InitHost(); | |
| 106 | |
| 107 ime_filter_.reset( | |
| 108 new MinimalInputEventFilter(window_tree_host_->window())); | |
| 109 | |
| 110 focus_client_.reset(new wm::FocusController(new FocusRulesImpl)); | |
| 111 | |
| 112 aura::client::SetFocusClient(window_tree_host_->window(), | |
| 113 focus_client_.get()); | |
| 114 aura::client::SetActivationClient(window_tree_host_->window(), | |
| 115 focus_client_.get()); | |
| 116 window_tree_host_->window()->AddPreTargetHandler(focus_client_.get()); | |
| 117 | |
| 118 capture_client_.reset( | |
| 119 new aura::client::DefaultCaptureClient(window_tree_host_->window())); | |
| 120 } | |
| 121 | |
| 122 NativeWidgetViewManager::~NativeWidgetViewManager() { | |
| 123 if (view_) | |
| 124 view_->RemoveObserver(this); | |
| 125 } | |
| 126 | |
| 127 void NativeWidgetViewManager::InitNativeWidget( | |
| 128 const views::Widget::InitParams& in_params) { | |
| 129 views::Widget::InitParams params(in_params); | |
| 130 params.parent = window_tree_host_->window(); | |
| 131 NativeWidgetAura::InitNativeWidget(params); | |
| 132 } | |
| 133 | |
| 134 void NativeWidgetViewManager::OnWindowVisibilityChanged(aura::Window* window, | |
| 135 bool visible) { | |
| 136 view_->SetVisible(visible); | |
| 137 // NOTE: We could also update aura::Window's visibility when the View's | |
| 138 // visibilty changes, but this code isn't going to be around for very long so | |
| 139 // I'm not bothering. | |
| 140 } | |
| 141 | |
| 142 void NativeWidgetViewManager::OnViewDestroyed(View* view) { | |
| 143 DCHECK_EQ(view, view_); | |
| 144 view->RemoveObserver(this); | |
| 145 view_ = NULL; | |
| 146 // TODO(sky): WindowTreeHostMojo assumes the View outlives it. | |
| 147 // NativeWidgetViewManager needs to deal, likely by deleting this. | |
| 148 } | |
| 149 | |
| 150 void NativeWidgetViewManager::OnViewBoundsChanged(View* view, | |
| 151 const Rect& old_bounds, | |
| 152 const Rect& new_bounds) { | |
| 153 gfx::Rect view_rect = view->bounds().To<gfx::Rect>(); | |
| 154 GetWidget()->SetBounds(gfx::Rect(view_rect.size())); | |
| 155 } | |
| 156 | |
| 157 void NativeWidgetViewManager::OnViewInputEvent(View* view, | |
| 158 const EventPtr& event) { | |
| 159 scoped_ptr<ui::Event> ui_event(event.To<scoped_ptr<ui::Event> >()); | |
| 160 if (ui_event) | |
| 161 window_tree_host_->SendEventToProcessor(ui_event.get()); | |
| 162 } | |
| 163 | |
| 164 } // namespace mojo | |
| OLD | NEW |