summaryrefslogtreecommitdiffstats
path: root/ui/views/corewm/focus_controller.cc
blob: 90b48630a5cdc89cdd93892856e20f9e62d3be01 (plain)
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
// 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/views/corewm/focus_controller.h"

#include "base/auto_reset.h"
#include "ui/aura/client/aura_constants.h"
#include "ui/aura/env.h"
#include "ui/views/corewm/focus_change_event.h"
#include "ui/views/corewm/focus_rules.h"

namespace views {
namespace corewm {
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 = window->transient_parent();
  while (transient_parent) {
    transient_parent->parent()->StackChildAtTop(transient_parent);
    transient_parent = transient_parent->transient_parent();
  }
}

// Updates focused window state and dispatches changing/changed events.
void DispatchEventsAndUpdateState(ui::EventDispatcher* dispatcher,
                                  int changing_event_type,
                                  int changed_event_type,
                                  aura::Window** state,
                                  aura::Window* new_state,
                                  bool restack,
                                  ui::EventTarget** event_dispatch_target) {
  int result = ui::ER_UNHANDLED;
  {
    base::AutoReset<ui::EventTarget*> reset(event_dispatch_target, *state);
    FocusChangeEvent changing_event(changing_event_type);
    dispatcher->ProcessEvent(*state, &changing_event);
    result = changing_event.result();
  }
  DCHECK(!(result & ui::ER_CONSUMED))
      << "Focus and Activation events cannot be consumed";

  aura::Window* lost_active = *state;
  *state = new_state;

  if (restack && new_state) {
    StackTransientParentsBelowModalWindow(new_state);
    new_state->parent()->StackChildAtTop(new_state);
  }

  {
    base::AutoReset<ui::EventTarget*> reset(event_dispatch_target, *state);
    FocusChangeEvent changed_event(changed_event_type);
    FocusChangeEvent::DispatcherApi(&changed_event).set_last_focus(lost_active);
    dispatcher->ProcessEvent(*state, &changed_event);
  }
}

}  // namespace

////////////////////////////////////////////////////////////////////////////////
// FocusController, public:

FocusController::FocusController(FocusRules* rules)
    : active_window_(NULL),
      focused_window_(NULL),
      event_dispatch_target_(NULL),
      rules_(rules) {
  DCHECK(rules);
  FocusChangeEvent::RegisterEventTypes();
  aura::Env::GetInstance()->AddObserver(this);
}

FocusController::~FocusController() {
  aura::Env::GetInstance()->RemoveObserver(this);
}

////////////////////////////////////////////////////////////////////////////////
// FocusController, aura::client::ActivationClient implementation:

void FocusController::AddObserver(
    aura::client::ActivationChangeObserver* observer) {
  //NOTREACHED();
}

void FocusController::RemoveObserver(
    aura::client::ActivationChangeObserver* observer) {
  //NOTREACHED();
}

void FocusController::ActivateWindow(aura::Window* window) {
  FocusWindow(window, NULL);
}

void FocusController::DeactivateWindow(aura::Window* window) {
  FocusWindow(rules_->GetNextActivatableWindow(window), NULL);
}

aura::Window* FocusController::GetActiveWindow() {
  return active_window_;
}

aura::Window* FocusController::GetActivatableWindow(aura::Window* window) {
  return rules_->GetActivatableWindow(window);
}

bool FocusController::OnWillFocusWindow(aura::Window* window,
                                        const ui::Event* event) {
  NOTREACHED();
  return false;
}

bool FocusController::CanActivateWindow(aura::Window* window) const {
  return rules_->CanActivateWindow(window);
}

////////////////////////////////////////////////////////////////////////////////
// FocusController, aura::client::FocusClient implementation:

void FocusController::AddObserver(
    aura::client::FocusChangeObserver* observer) {
  //NOTREACHED();
}

void FocusController::RemoveObserver(
    aura::client::FocusChangeObserver* observer) {
  //NOTREACHED();
}

void FocusController::FocusWindow(aura::Window* window,
                                  const ui::Event* event) {
  // 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);
  SetActiveWindow(rules_->GetActivatableWindow(focusable));
  DCHECK(GetActiveWindow()->Contains(focusable));
  SetFocusedWindow(focusable);
}

aura::Window* FocusController::GetFocusedWindow() {
  return focused_window_;
}

////////////////////////////////////////////////////////////////////////////////
// FocusController, ui::EventHandler implementation:
ui::EventResult FocusController::OnKeyEvent(ui::KeyEvent* event) {
  return ui::ER_UNHANDLED;
}

ui::EventResult FocusController::OnMouseEvent(ui::MouseEvent* event) {
  if (event->type() == ui::ET_MOUSE_PRESSED)
    WindowFocusedFromInputEvent(static_cast<aura::Window*>(event->target()));
  return ui::ER_UNHANDLED;
}

ui::EventResult FocusController::OnScrollEvent(ui::ScrollEvent* event) {
  return ui::ER_UNHANDLED;
}

ui::EventResult FocusController::OnTouchEvent(ui::TouchEvent* event) {
  return ui::ER_UNHANDLED;
}

void FocusController::OnGestureEvent(ui::GestureEvent* event) {
  if (event->type() == ui::ET_GESTURE_BEGIN &&
      event->details().touch_points() == 1) {
    WindowFocusedFromInputEvent(static_cast<aura::Window*>(event->target()));
  }
}

////////////////////////////////////////////////////////////////////////////////
// FocusController, aura::WindowObserver implementation:

void FocusController::OnWindowVisibilityChanging(aura::Window* window,
                                                 bool visible) {
  // We need to process this change in VisibilityChanging while the window is
  // still visible, since focus events cannot be dispatched to invisible
  // windows.
  if (!visible)
    WindowLostFocusFromDispositionChange(window);
}

void FocusController::OnWindowDestroying(aura::Window* window) {
  WindowLostFocusFromDispositionChange(window);
}

void FocusController::OnWindowDestroyed(aura::Window* window) {
  window->RemoveObserver(this);
}

void FocusController::OnWillRemoveWindow(aura::Window* window) {
  WindowLostFocusFromDispositionChange(window);
}

void FocusController::OnWindowInitialized(aura::Window* window) {
  window->AddObserver(this);
}

////////////////////////////////////////////////////////////////////////////////
// FocusController, ui::EventDispatcher implementation:

bool FocusController::CanDispatchToTarget(ui::EventTarget* target) {
  return target == event_dispatch_target_;
}

////////////////////////////////////////////////////////////////////////////////
// FocusController, private:

void FocusController::SetFocusedWindow(aura::Window* window) {
  if (window == focused_window_)
    return;
  DCHECK(rules_->CanFocusWindow(window));
  if (window)
    DCHECK_EQ(window, rules_->GetFocusableWindow(window));
  DispatchEventsAndUpdateState(
      this,
      FocusChangeEvent::focus_changing_event_type(),
      FocusChangeEvent::focus_changed_event_type(),
      &focused_window_,
      window,
      /* restack */ false,
      &event_dispatch_target_);
}

void FocusController::SetActiveWindow(aura::Window* window) {
  if (window == active_window_)
    return;
  DCHECK(rules_->CanActivateWindow(window));
  if (window)
    DCHECK_EQ(window, rules_->GetActivatableWindow(window));
  DispatchEventsAndUpdateState(
      this,
      FocusChangeEvent::activation_changing_event_type(),
      FocusChangeEvent::activation_changed_event_type(),
      &active_window_,
      window,
      /* restack */ true,
      &event_dispatch_target_);
}

void FocusController::WindowLostFocusFromDispositionChange(
    aura::Window* window) {
  // 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->Contains(active_window_)) {
    aura::Window* next_activatable = rules_->GetNextActivatableWindow(window);
    SetActiveWindow(next_activatable);
    SetFocusedWindow(next_activatable);
  } else if (window->Contains(focused_window_)) {
    // Active window isn't changing, but focused window might be.
    SetFocusedWindow(rules_->GetNextFocusableWindow(window));
  }
}

void FocusController::WindowFocusedFromInputEvent(aura::Window* window) {
  FocusWindow(window, NULL);
}

}  // namespace corewm
}  // namespace views