| 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 "services/window_manager/focus_controller.h" | |
| 6 | |
| 7 #include "base/auto_reset.h" | |
| 8 #include "mojo/services/view_manager/cpp/view_property.h" | |
| 9 #include "mojo/services/view_manager/cpp/view_tracker.h" | |
| 10 #include "services/window_manager/focus_controller_observer.h" | |
| 11 #include "services/window_manager/focus_rules.h" | |
| 12 #include "services/window_manager/view_target.h" | |
| 13 #include "services/window_manager/window_manager_app.h" | |
| 14 #include "ui/events/event.h" | |
| 15 | |
| 16 DECLARE_VIEW_PROPERTY_TYPE(window_manager::FocusController*); | |
| 17 | |
| 18 using mojo::View; | |
| 19 | |
| 20 namespace window_manager { | |
| 21 | |
| 22 namespace { | |
| 23 DEFINE_VIEW_PROPERTY_KEY(FocusController*, kRootViewFocusController, nullptr); | |
| 24 } // namespace | |
| 25 | |
| 26 FocusController::FocusController(scoped_ptr<FocusRules> rules) | |
| 27 : active_view_(nullptr), | |
| 28 focused_view_(nullptr), | |
| 29 updating_focus_(false), | |
| 30 updating_activation_(false), | |
| 31 rules_(rules.Pass()), | |
| 32 observer_manager_(this) { | |
| 33 DCHECK(rules_); | |
| 34 } | |
| 35 | |
| 36 FocusController::~FocusController() {} | |
| 37 | |
| 38 void FocusController::AddObserver(FocusControllerObserver* observer) { | |
| 39 focus_controller_observers_.AddObserver(observer); | |
| 40 } | |
| 41 | |
| 42 void FocusController::RemoveObserver(FocusControllerObserver* observer) { | |
| 43 focus_controller_observers_.RemoveObserver(observer); | |
| 44 } | |
| 45 | |
| 46 void FocusController::ActivateView(View* view) { | |
| 47 FocusView(view); | |
| 48 } | |
| 49 | |
| 50 void FocusController::DeactivateView(View* view) { | |
| 51 if (view) | |
| 52 FocusView(rules_->GetNextActivatableView(view)); | |
| 53 } | |
| 54 | |
| 55 View* FocusController::GetActiveView() { | |
| 56 return active_view_; | |
| 57 } | |
| 58 | |
| 59 View* FocusController::GetActivatableView(View* view) { | |
| 60 return rules_->GetActivatableView(view); | |
| 61 } | |
| 62 | |
| 63 View* FocusController::GetToplevelView(View* view) { | |
| 64 return rules_->GetToplevelView(view); | |
| 65 } | |
| 66 | |
| 67 bool FocusController::CanActivateView(View* view) const { | |
| 68 return rules_->CanActivateView(view); | |
| 69 } | |
| 70 | |
| 71 void FocusController::FocusView(View* view) { | |
| 72 if (view && | |
| 73 (view->Contains(focused_view_) || view->Contains(active_view_))) { | |
| 74 return; | |
| 75 } | |
| 76 | |
| 77 // Focusing a window also activates its containing activatable window. Note | |
| 78 // that the rules could redirect activation activation and/or focus. | |
| 79 View* focusable = rules_->GetFocusableView(view); | |
| 80 View* activatable = | |
| 81 focusable ? rules_->GetActivatableView(focusable) : nullptr; | |
| 82 | |
| 83 // We need valid focusable/activatable windows in the event we're not clearing | |
| 84 // focus. "Clearing focus" is inferred by whether or not |window| passed to | |
| 85 // this function is non-null. | |
| 86 if (view && (!focusable || !activatable)) | |
| 87 return; | |
| 88 DCHECK((focusable && activatable) || !view); | |
| 89 | |
| 90 // Activation change observers may change the focused window. If this happens | |
| 91 // we must not adjust the focus below since this will clobber that change. | |
| 92 View* last_focused_view = focused_view_; | |
| 93 if (!updating_activation_) | |
| 94 SetActiveView(view, activatable); | |
| 95 | |
| 96 // If the window's ActivationChangeObserver shifted focus to a valid window, | |
| 97 // we don't want to focus the window we thought would be focused by default. | |
| 98 bool activation_changed_focus = last_focused_view != focused_view_; | |
| 99 if (!updating_focus_ && (!activation_changed_focus || !focused_view_)) { | |
| 100 if (active_view_ && focusable) | |
| 101 DCHECK(active_view_->Contains(focusable)); | |
| 102 SetFocusedView(focusable); | |
| 103 } | |
| 104 } | |
| 105 | |
| 106 void FocusController::ResetFocusWithinActiveView(View* view) { | |
| 107 DCHECK(view); | |
| 108 if (!active_view_) | |
| 109 return; | |
| 110 if (!active_view_->Contains(view)) | |
| 111 return; | |
| 112 SetFocusedView(view); | |
| 113 } | |
| 114 | |
| 115 View* FocusController::GetFocusedView() { | |
| 116 return focused_view_; | |
| 117 } | |
| 118 | |
| 119 //////////////////////////////////////////////////////////////////////////////// | |
| 120 // FocusController, ui::EventHandler implementation: | |
| 121 | |
| 122 void FocusController::OnKeyEvent(ui::KeyEvent* event) { | |
| 123 } | |
| 124 | |
| 125 void FocusController::OnMouseEvent(ui::MouseEvent* event) { | |
| 126 if (event->type() == ui::ET_MOUSE_PRESSED && !event->handled()) { | |
| 127 View* view = static_cast<ViewTarget*>(event->target())->view(); | |
| 128 ViewFocusedFromInputEvent(view); | |
| 129 } | |
| 130 } | |
| 131 | |
| 132 void FocusController::OnScrollEvent(ui::ScrollEvent* event) { | |
| 133 } | |
| 134 | |
| 135 void FocusController::OnTouchEvent(ui::TouchEvent* event) { | |
| 136 if (event->type() == ui::ET_TOUCH_PRESSED && !event->handled()) { | |
| 137 View* view = static_cast<ViewTarget*>(event->target())->view(); | |
| 138 ViewFocusedFromInputEvent(view); | |
| 139 } | |
| 140 } | |
| 141 | |
| 142 void FocusController::OnGestureEvent(ui::GestureEvent* event) { | |
| 143 if (event->type() == ui::ET_GESTURE_BEGIN && | |
| 144 event->details().touch_points() == 1 && | |
| 145 !event->handled()) { | |
| 146 View* view = static_cast<ViewTarget*>(event->target())->view(); | |
| 147 ViewFocusedFromInputEvent(view); | |
| 148 } | |
| 149 } | |
| 150 | |
| 151 //////////////////////////////////////////////////////////////////////////////// | |
| 152 // FocusController, mojo::ViewObserver implementation: | |
| 153 | |
| 154 void FocusController::OnViewVisibilityChanged(View* view) { | |
| 155 bool visible = view->visible(); | |
| 156 if (!visible) | |
| 157 ViewLostFocusFromDispositionChange(view, view->parent()); | |
| 158 } | |
| 159 | |
| 160 void FocusController::OnViewDestroying(View* view) { | |
| 161 ViewLostFocusFromDispositionChange(view, view->parent()); | |
| 162 } | |
| 163 | |
| 164 void FocusController::OnTreeChanging(const TreeChangeParams& params) { | |
| 165 // TODO(erg): In the aura version, you could get into a situation where you | |
| 166 // have different focus clients, so you had to check for that. Does that | |
| 167 // happen here? Could we get away with not checking if it does? | |
| 168 if (params.receiver == active_view_ && | |
| 169 params.target->Contains(params.receiver) && | |
| 170 (!params.new_parent || | |
| 171 /* different_focus_clients */ false)) { | |
| 172 ViewLostFocusFromDispositionChange(params.receiver, params.old_parent); | |
| 173 } | |
| 174 } | |
| 175 | |
| 176 void FocusController::OnTreeChanged(const TreeChangeParams& params) { | |
| 177 // TODO(erg): Same as Changing version. | |
| 178 if (params.receiver == focused_view_ && | |
| 179 params.target->Contains(params.receiver) && | |
| 180 (!params.new_parent || | |
| 181 /* different_focus_clients */ false)) { | |
| 182 ViewLostFocusFromDispositionChange(params.receiver, params.old_parent); | |
| 183 } | |
| 184 } | |
| 185 | |
| 186 //////////////////////////////////////////////////////////////////////////////// | |
| 187 // FocusController, private: | |
| 188 | |
| 189 void FocusController::SetFocusedView(View* view) { | |
| 190 if (updating_focus_ || view == focused_view_) | |
| 191 return; | |
| 192 DCHECK(rules_->CanFocusView(view)); | |
| 193 if (view) | |
| 194 DCHECK_EQ(view, rules_->GetFocusableView(view)); | |
| 195 | |
| 196 base::AutoReset<bool> updating_focus(&updating_focus_, true); | |
| 197 View* lost_focus = focused_view_; | |
| 198 | |
| 199 // TODO(erg): In the aura version, we reset the text input client here. Do | |
| 200 // that if we bring in something like the TextInputClient. | |
| 201 | |
| 202 // Allow for the window losing focus to be deleted during dispatch. If it is | |
| 203 // deleted pass null to observers instead of a deleted window. | |
| 204 mojo::ViewTracker view_tracker; | |
| 205 if (lost_focus) | |
| 206 view_tracker.Add(lost_focus); | |
| 207 if (focused_view_ && observer_manager_.IsObserving(focused_view_) && | |
| 208 focused_view_ != active_view_) { | |
| 209 observer_manager_.Remove(focused_view_); | |
| 210 } | |
| 211 focused_view_ = view; | |
| 212 if (focused_view_ && !observer_manager_.IsObserving(focused_view_)) | |
| 213 observer_manager_.Add(focused_view_); | |
| 214 | |
| 215 FOR_EACH_OBSERVER(FocusControllerObserver, focus_controller_observers_, | |
| 216 OnFocused(focused_view_)); | |
| 217 | |
| 218 // TODO(erg): In aura, there's a concept of a single FocusChangeObserver that | |
| 219 // is attached to an aura::Window. We don't currently have this in | |
| 220 // mojo::View, but if we add it later, we should make something analogous | |
| 221 // here. | |
| 222 | |
| 223 // TODO(erg): In the aura version, we reset the TextInputClient here, too. | |
| 224 } | |
| 225 | |
| 226 void FocusController::SetActiveView(View* requested_view, View* view) { | |
| 227 if (updating_activation_) | |
| 228 return; | |
| 229 | |
| 230 if (view == active_view_) { | |
| 231 if (requested_view) { | |
| 232 FOR_EACH_OBSERVER(FocusControllerObserver, | |
| 233 focus_controller_observers_, | |
| 234 OnAttemptToReactivateView(requested_view, | |
| 235 active_view_)); | |
| 236 } | |
| 237 return; | |
| 238 } | |
| 239 | |
| 240 DCHECK(rules_->CanActivateView(view)); | |
| 241 if (view) | |
| 242 DCHECK_EQ(view, rules_->GetActivatableView(view)); | |
| 243 | |
| 244 base::AutoReset<bool> updating_activation(&updating_activation_, true); | |
| 245 View* lost_activation = active_view_; | |
| 246 // Allow for the window losing activation to be deleted during dispatch. If | |
| 247 // it is deleted pass null to observers instead of a deleted window. | |
| 248 mojo::ViewTracker view_tracker; | |
| 249 if (lost_activation) | |
| 250 view_tracker.Add(lost_activation); | |
| 251 if (active_view_ && observer_manager_.IsObserving(active_view_) && | |
| 252 focused_view_ != active_view_) { | |
| 253 observer_manager_.Remove(active_view_); | |
| 254 } | |
| 255 active_view_ = view; | |
| 256 if (active_view_ && !observer_manager_.IsObserving(active_view_)) | |
| 257 observer_manager_.Add(active_view_); | |
| 258 | |
| 259 if (active_view_) { | |
| 260 // TODO(erg): Reenable this when we have modal windows. | |
| 261 // StackTransientParentsBelowModalWindow(active_view_); | |
| 262 | |
| 263 active_view_->MoveToFront(); | |
| 264 } | |
| 265 | |
| 266 // TODO(erg): Individual windows can have a single ActivationChangeObserver | |
| 267 // set on them. In the aura version of this code, it sends an | |
| 268 // OnWindowActivated message to both the window that lost activation, and the | |
| 269 // window that gained it. | |
| 270 | |
| 271 FOR_EACH_OBSERVER(FocusControllerObserver, focus_controller_observers_, | |
| 272 OnActivated(active_view_)); | |
| 273 } | |
| 274 | |
| 275 void FocusController::ViewLostFocusFromDispositionChange( | |
| 276 View* view, | |
| 277 View* next) { | |
| 278 // TODO(erg): We clear the modality state here in the aura::Window version of | |
| 279 // this class, and should probably do the same once we have modal windows. | |
| 280 | |
| 281 // TODO(beng): See if this function can be replaced by a call to | |
| 282 // FocusWindow(). | |
| 283 // Activation adjustments are handled first in the event of a disposition | |
| 284 // changed. If an activation change is necessary, focus is reset as part of | |
| 285 // that process so there's no point in updating focus independently. | |
| 286 if (view == active_view_) { | |
| 287 View* next_activatable = rules_->GetNextActivatableView(view); | |
| 288 SetActiveView(nullptr, next_activatable); | |
| 289 if (!(active_view_ && active_view_->Contains(focused_view_))) | |
| 290 SetFocusedView(next_activatable); | |
| 291 } else if (view->Contains(focused_view_)) { | |
| 292 // Active window isn't changing, but focused window might be. | |
| 293 SetFocusedView(rules_->GetFocusableView(next)); | |
| 294 } | |
| 295 } | |
| 296 | |
| 297 void FocusController::ViewFocusedFromInputEvent(View* view) { | |
| 298 // Only focus |window| if it or any of its parents can be focused. Otherwise | |
| 299 // FocusWindow() will focus the topmost window, which may not be the | |
| 300 // currently focused one. | |
| 301 if (rules_->CanFocusView(GetToplevelView(view))) | |
| 302 FocusView(view); | |
| 303 } | |
| 304 | |
| 305 void SetFocusController(View* root_view, FocusController* focus_controller) { | |
| 306 DCHECK_EQ(root_view->GetRoot(), root_view); | |
| 307 root_view->SetLocalProperty(kRootViewFocusController, focus_controller); | |
| 308 } | |
| 309 | |
| 310 FocusController* GetFocusController(View* root_view) { | |
| 311 if (root_view) | |
| 312 DCHECK_EQ(root_view->GetRoot(), root_view); | |
| 313 return root_view ? | |
| 314 root_view->GetLocalProperty(kRootViewFocusController) : nullptr; | |
| 315 } | |
| 316 | |
| 317 } // namespace window_manager | |
| OLD | NEW |