1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ui/wm/core/focus_controller.h"
#include "base/auto_reset.h"
#include "ui/aura/client/aura_constants.h"
#include "ui/aura/client/capture_client.h"
#include "ui/aura/client/focus_change_observer.h"
#include "ui/aura/env.h"
#include "ui/aura/window_tracker.h"
#include "ui/base/ime/text_input_focus_manager.h"
#include "ui/events/event.h"
#include "ui/wm/core/focus_rules.h"
#include "ui/wm/core/window_util.h"
#include "ui/wm/public/activation_change_observer.h"
namespace wm {
namespace {
// When a modal window is activated, we bring its entire transient parent chain
// to the front. This function must be called before the modal transient is
// stacked at the top to ensure correct stacking order.
void StackTransientParentsBelowModalWindow(aura::Window* window) {
if (window->GetProperty(aura::client::kModalKey) != ui::MODAL_TYPE_WINDOW)
return;
aura::Window* transient_parent = wm::GetTransientParent(window);
while (transient_parent) {
transient_parent->parent()->StackChildAtTop(transient_parent);
transient_parent = wm::GetTransientParent(transient_parent);
}
}
} // namespace
////////////////////////////////////////////////////////////////////////////////
// FocusController, public:
FocusController::FocusController(FocusRules* rules)
: active_window_(NULL),
focused_window_(NULL),
updating_focus_(false),
updating_activation_(false),
rules_(rules),
observer_manager_(this) {
DCHECK(rules);
}
FocusController::~FocusController() {
}
////////////////////////////////////////////////////////////////////////////////
// FocusController, aura::client::ActivationClient implementation:
void FocusController::AddObserver(
aura::client::ActivationChangeObserver* observer) {
activation_observers_.AddObserver(observer);
}
void FocusController::RemoveObserver(
aura::client::ActivationChangeObserver* observer) {
activation_observers_.RemoveObserver(observer);
}
void FocusController::ActivateWindow(aura::Window* window) {
FocusWindow(window);
}
void FocusController::DeactivateWindow(aura::Window* window) {
if (window)
FocusWindow(rules_->GetNextActivatableWindow(window));
}
aura::Window* FocusController::GetActiveWindow() {
return active_window_;
}
aura::Window* FocusController::GetActivatableWindow(aura::Window* window) {
return rules_->GetActivatableWindow(window);
}
aura::Window* FocusController::GetToplevelWindow(aura::Window* window) {
return rules_->GetToplevelWindow(window);
}
bool FocusController::CanActivateWindow(aura::Window* window) const {
return rules_->CanActivateWindow(window);
}
////////////////////////////////////////////////////////////////////////////////
// FocusController, aura::client::FocusClient implementation:
void FocusController::AddObserver(
aura::client::FocusChangeObserver* observer) {
focus_observers_.AddObserver(observer);
}
void FocusController::RemoveObserver(
aura::client::FocusChangeObserver* observer) {
focus_observers_.RemoveObserver(observer);
}
void FocusController::FocusWindow(aura::Window* window) {
if (window &&
(window->Contains(focused_window_) || window->Contains(active_window_))) {
return;
}
// Focusing a window also activates its containing activatable window. Note
// that the rules could redirect activation activation and/or focus.
aura::Window* focusable = rules_->GetFocusableWindow(window);
aura::Window* activatable =
focusable ? rules_->GetActivatableWindow(focusable) : NULL;
// We need valid focusable/activatable windows in the event we're not clearing
// focus. "Clearing focus" is inferred by whether or not |window| passed to
// this function is non-NULL.
if (window && (!focusable || !activatable))
return;
DCHECK((focusable && activatable) || !window);
// Activation change observers may change the focused window. If this happens
// we must not adjust the focus below since this will clobber that change.
aura::Window* last_focused_window = focused_window_;
if (!updating_activation_)
SetActiveWindow(window, activatable);
// If the window's ActivationChangeObserver shifted focus to a valid window,
// we don't want to focus the window we thought would be focused by default.
bool activation_changed_focus = last_focused_window != focused_window_;
if (!updating_focus_ && (!activation_changed_focus || !focused_window_)) {
if (active_window_ && focusable)
DCHECK(active_window_->Contains(focusable));
SetFocusedWindow(focusable);
}
}
void FocusController::ResetFocusWithinActiveWindow(aura::Window* window) {
DCHECK(window);
if (!active_window_)
return;
if (!active_window_->Contains(window))
return;
SetFocusedWindow(window);
}
aura::Window* FocusController::GetFocusedWindow() {
return focused_window_;
}
////////////////////////////////////////////////////////////////////////////////
// FocusController, ui::EventHandler implementation:
void FocusController::OnKeyEvent(ui::KeyEvent* event) {
}
void FocusController::OnMouseEvent(ui::MouseEvent* event) {
if (event->type() == ui::ET_MOUSE_PRESSED && !event->handled())
WindowFocusedFromInputEvent(static_cast<aura::Window*>(event->target()));
}
void FocusController::OnScrollEvent(ui::ScrollEvent* event) {
}
void FocusController::OnTouchEvent(ui::TouchEvent* event) {
}
void FocusController::OnGestureEvent(ui::GestureEvent* event) {
if (event->type() == ui::ET_GESTURE_BEGIN &&
event->details().touch_points() == 1 &&
!event->handled()) {
WindowFocusedFromInputEvent(static_cast<aura::Window*>(event->target()));
}
}
////////////////////////////////////////////////////////////////////////////////
// FocusController, aura::WindowObserver implementation:
void FocusController::OnWindowVisibilityChanged(aura::Window* window,
bool visible) {
if (!visible)
WindowLostFocusFromDispositionChange(window, window->parent());
}
void FocusController::OnWindowDestroying(aura::Window* window) {
WindowLostFocusFromDispositionChange(window, window->parent());
}
void FocusController::OnWindowHierarchyChanging(
const HierarchyChangeParams& params) {
if (params.receiver == active_window_ &&
params.target->Contains(params.receiver) && (!params.new_parent ||
aura::client::GetFocusClient(params.new_parent) !=
aura::client::GetFocusClient(params.receiver))) {
WindowLostFocusFromDispositionChange(params.receiver, params.old_parent);
}
}
void FocusController::OnWindowHierarchyChanged(
const HierarchyChangeParams& params) {
if (params.receiver == focused_window_ &&
params.target->Contains(params.receiver) && (!params.new_parent ||
aura::client::GetFocusClient(params.new_parent) !=
aura::client::GetFocusClient(params.receiver))) {
WindowLostFocusFromDispositionChange(params.receiver, params.old_parent);
}
}
////////////////////////////////////////////////////////////////////////////////
// FocusController, private:
void FocusController::SetFocusedWindow(aura::Window* window) {
if (updating_focus_ || window == focused_window_)
return;
DCHECK(rules_->CanFocusWindow(window));
if (window)
DCHECK_EQ(window, rules_->GetFocusableWindow(window));
base::AutoReset<bool> updating_focus(&updating_focus_, true);
aura::Window* lost_focus = focused_window_;
// |window| is going to get the focus, so reset the text input client.
// OnWindowFocused() may set a proper text input client if the implementation
// supports text input.
ui::TextInputFocusManager* text_input_focus_manager =
ui::TextInputFocusManager::GetInstance();
if (window)
text_input_focus_manager->FocusTextInputClient(NULL);
// Allow for the window losing focus to be deleted during dispatch. If it is
// deleted pass NULL to observers instead of a deleted window.
aura::WindowTracker window_tracker;
if (lost_focus)
window_tracker.Add(lost_focus);
if (focused_window_ && observer_manager_.IsObserving(focused_window_) &&
focused_window_ != active_window_) {
observer_manager_.Remove(focused_window_);
}
focused_window_ = window;
if (focused_window_ && !observer_manager_.IsObserving(focused_window_))
observer_manager_.Add(focused_window_);
FOR_EACH_OBSERVER(aura::client::FocusChangeObserver,
focus_observers_,
OnWindowFocused(focused_window_,
window_tracker.Contains(lost_focus) ?
lost_focus : NULL));
if (window_tracker.Contains(lost_focus)) {
aura::client::FocusChangeObserver* observer =
aura::client::GetFocusChangeObserver(lost_focus);
if (observer)
observer->OnWindowFocused(focused_window_, lost_focus);
}
aura::client::FocusChangeObserver* observer =
aura::client::GetFocusChangeObserver(focused_window_);
if (observer) {
observer->OnWindowFocused(
focused_window_,
window_tracker.Contains(lost_focus) ? lost_focus : NULL);
}
// Ensure that the text input client is reset when the window loses the focus.
if (!window)
text_input_focus_manager->FocusTextInputClient(NULL);
}
void FocusController::SetActiveWindow(aura::Window* requested_window,
aura::Window* window) {
if (updating_activation_)
return;
if (window == active_window_) {
if (requested_window) {
FOR_EACH_OBSERVER(aura::client::ActivationChangeObserver,
activation_observers_,
OnAttemptToReactivateWindow(requested_window,
active_window_));
}
return;
}
DCHECK(rules_->CanActivateWindow(window));
if (window)
DCHECK_EQ(window, rules_->GetActivatableWindow(window));
base::AutoReset<bool> updating_activation(&updating_activation_, true);
aura::Window* lost_activation = active_window_;
// Allow for the window losing activation to be deleted during dispatch. If
// it is deleted pass NULL to observers instead of a deleted window.
aura::WindowTracker window_tracker;
if (lost_activation)
window_tracker.Add(lost_activation);
if (active_window_ && observer_manager_.IsObserving(active_window_) &&
focused_window_ != active_window_) {
observer_manager_.Remove(active_window_);
}
active_window_ = window;
if (active_window_ && !observer_manager_.IsObserving(active_window_))
observer_manager_.Add(active_window_);
if (active_window_) {
StackTransientParentsBelowModalWindow(active_window_);
active_window_->parent()->StackChildAtTop(active_window_);
}
aura::client::ActivationChangeObserver* observer = NULL;
if (window_tracker.Contains(lost_activation)) {
observer = aura::client::GetActivationChangeObserver(lost_activation);
if (observer)
observer->OnWindowActivated(active_window_, lost_activation);
}
observer = aura::client::GetActivationChangeObserver(active_window_);
if (observer) {
observer->OnWindowActivated(
active_window_,
window_tracker.Contains(lost_activation) ? lost_activation : NULL);
}
FOR_EACH_OBSERVER(aura::client::ActivationChangeObserver,
activation_observers_,
OnWindowActivated(active_window_,
window_tracker.Contains(lost_activation) ?
lost_activation : NULL));
}
void FocusController::WindowLostFocusFromDispositionChange(
aura::Window* window,
aura::Window* next) {
// A window's modality state will interfere with focus restoration during its
// destruction.
window->ClearProperty(aura::client::kModalKey);
// TODO(beng): See if this function can be replaced by a call to
// FocusWindow().
// Activation adjustments are handled first in the event of a disposition
// changed. If an activation change is necessary, focus is reset as part of
// that process so there's no point in updating focus independently.
if (window == active_window_) {
aura::Window* next_activatable = rules_->GetNextActivatableWindow(window);
SetActiveWindow(NULL, next_activatable);
if (!(active_window_ && active_window_->Contains(focused_window_)))
SetFocusedWindow(next_activatable);
} else if (window->Contains(focused_window_)) {
// Active window isn't changing, but focused window might be.
SetFocusedWindow(rules_->GetFocusableWindow(next));
}
}
void FocusController::WindowFocusedFromInputEvent(aura::Window* window) {
// Only focus |window| if it or any of its parents can be focused. Otherwise
// FocusWindow() will focus the topmost window, which may not be the
// currently focused one.
if (rules_->CanFocusWindow(GetToplevelWindow(window)))
FocusWindow(window);
}
} // namespace wm
|