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