summaryrefslogtreecommitdiff
path: root/chromium/components/mus/ws/focus_controller.cc
blob: 5728acbe15455987b36c1dd15142cfae222c9ae4 (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
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
// Copyright 2015 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 "components/mus/ws/focus_controller.h"

#include "base/macros.h"
#include "components/mus/public/interfaces/window_manager.mojom.h"
#include "components/mus/ws/focus_controller_delegate.h"
#include "components/mus/ws/focus_controller_observer.h"
#include "components/mus/ws/server_window.h"
#include "components/mus/ws/server_window_drawn_tracker.h"

namespace mus {
namespace ws {

namespace {

ServerWindow* GetDeepestLastDescendant(ServerWindow* window) {
  while (!window->children().empty())
    window = window->children().back();
  return window;
}

// This can be used to iterate over each node in a rooted tree for the purpose
// of shifting focus/activation.
class WindowTreeIterator {
 public:
  explicit WindowTreeIterator(ServerWindow* root) : root_(root) {}
  ~WindowTreeIterator() {}

  ServerWindow* GetNext(ServerWindow* window) const {
    if (window == root_ || window == nullptr)
      return GetDeepestLastDescendant(root_);

    // Return the next sibling.
    ServerWindow* parent = window->parent();
    if (parent) {
      const ServerWindow::Windows& siblings = parent->children();
      ServerWindow::Windows::const_reverse_iterator iter =
          std::find(siblings.rbegin(), siblings.rend(), window);
      DCHECK(iter != siblings.rend());
      ++iter;
      if (iter != siblings.rend())
        return GetDeepestLastDescendant(*iter);
    }

    // All children and siblings have been explored. Next is the parent.
    return parent;
  }

 private:
  ServerWindow* root_;

  DISALLOW_COPY_AND_ASSIGN(WindowTreeIterator);
};

}  // namespace

FocusController::FocusController(FocusControllerDelegate* delegate,
                                 ServerWindow* root)
    : delegate_(delegate),
      root_(root),
      focused_window_(nullptr),
      active_window_(nullptr),
      activation_reason_(ActivationChangeReason::UNKNONW) {
  DCHECK(delegate_);
  DCHECK(root_);
}

FocusController::~FocusController() {
}

bool FocusController::SetFocusedWindow(ServerWindow* window) {
  if (GetFocusedWindow() == window)
    return true;

  return SetFocusedWindowImpl(FocusControllerChangeSource::EXPLICIT, window);
}

ServerWindow* FocusController::GetFocusedWindow() {
  return focused_window_;
}

void FocusController::ActivateNextWindow() {
  WindowTreeIterator iter(root_);
  ServerWindow* activate = active_window_;
  while (true) {
    activate = iter.GetNext(activate);
    if (activation_reason_ == ActivationChangeReason::CYCLE) {
      if (activate == active_window_) {
        // We have cycled over all the activatable windows. Remove the oldest
        // window that was cycled.
        if (!cycle_windows_->windows().empty()) {
          cycle_windows_->Remove(cycle_windows_->windows().front());
          continue;
        }
      } else if (cycle_windows_->Contains(activate)) {
        // We are cycling between activated windows, and this window has already
        // been through the cycle. So skip over it.
        continue;
      }
    }
    if (activate == active_window_ || CanBeActivated(activate))
      break;
  }
  SetActiveWindow(activate, ActivationChangeReason::CYCLE);

  if (active_window_) {
    // Do not shift focus if the focused window already lives in the active
    // window.
    if (focused_window_ && active_window_->Contains(focused_window_))
      return;
    // Focus the first focusable window in the tree.
    WindowTreeIterator iter(active_window_);
    ServerWindow* focus = nullptr;
    do {
      focus = iter.GetNext(focus);
    } while (focus != active_window_ && !CanBeFocused(focus));
    SetFocusedWindow(focus);
  } else {
    SetFocusedWindow(nullptr);
  }
}

void FocusController::AddObserver(FocusControllerObserver* observer) {
  observers_.AddObserver(observer);
}

void FocusController::RemoveObserver(FocusControllerObserver* observer) {
  observers_.RemoveObserver(observer);
}

void FocusController::SetActiveWindow(ServerWindow* window,
                                      ActivationChangeReason reason) {
  DCHECK(!window || CanBeActivated(window));
  if (active_window_ == window)
    return;
  if (reason != ActivationChangeReason::CYCLE) {
    cycle_windows_.reset();
  } else if (activation_reason_ != ActivationChangeReason::CYCLE) {
    DCHECK(!cycle_windows_);
    cycle_windows_.reset(new ServerWindowTracker());
    if (active_window_)
      cycle_windows_->Add(active_window_);
  }

  ServerWindow* old_active = active_window_;
  active_window_ = window;
  activation_reason_ = reason;
  FOR_EACH_OBSERVER(FocusControllerObserver, observers_,
                    OnActivationChanged(old_active, active_window_));
  if (active_window_ && activation_reason_ == ActivationChangeReason::CYCLE)
    cycle_windows_->Add(active_window_);
}

bool FocusController::CanBeFocused(ServerWindow* window) const {
  // All ancestors of |window| must be drawn, and be focusable.
  for (ServerWindow* w = window; w; w = w->parent()) {
    if (!w->IsDrawn())
      return false;
    if (!w->can_focus())
      return false;
  }

  // |window| must be a descendent of an activatable window.
  return GetActivatableAncestorOf(window) != nullptr;
}

bool FocusController::CanBeActivated(ServerWindow* window) const {
  DCHECK(window);
  // A detached window cannot be activated.
  if (!root_->Contains(window))
    return false;

  // The parent window must be allowed to have active children.
  if (!delegate_->CanHaveActiveChildren(window->parent()))
    return false;

  if (!window->can_focus())
    return false;

  // The window must be drawn, or if it's not drawn, it must be minimized.
  if (!window->IsDrawn()) {
    bool is_minimized = false;
    const ServerWindow::Properties& props = window->properties();
    if (props.count(mojom::WindowManager::kShowState_Property)) {
      is_minimized =
          props.find(mojom::WindowManager::kShowState_Property)->second[0] ==
          static_cast<int>(mus::mojom::ShowState::MINIMIZED);
    }
    if (!is_minimized)
      return false;
  }

  // TODO(sad): If there's a transient modal window, then this cannot be
  // activated.
  return true;
}

ServerWindow* FocusController::GetActivatableAncestorOf(
    ServerWindow* window) const {
  for (ServerWindow* w = window; w; w = w->parent()) {
    if (CanBeActivated(w))
      return w;
  }
  return nullptr;
}

bool FocusController::SetFocusedWindowImpl(
    FocusControllerChangeSource change_source,
    ServerWindow* window) {
  if (window && !CanBeFocused(window))
    return false;

  ServerWindow* old_focused = GetFocusedWindow();

  DCHECK(!window || window->IsDrawn());

  // Activate the closest activatable ancestor window.
  // TODO(sad): The window to activate doesn't necessarily have to be a direct
  // ancestor (e.g. could be a transient parent).
  SetActiveWindow(GetActivatableAncestorOf(window),
                  ActivationChangeReason::FOCUS);

  FOR_EACH_OBSERVER(FocusControllerObserver, observers_,
                    OnFocusChanged(change_source, old_focused, window));

  focused_window_ = window;
  // We can currently use only a single ServerWindowDrawnTracker since focused
  // window is expected to be a direct descendant of the active window.
  if (focused_window_ && active_window_) {
    DCHECK(active_window_->Contains(focused_window_));
  }
  ServerWindow* track_window = focused_window_;
  if (!track_window)
    track_window = active_window_;
  if (track_window)
    drawn_tracker_.reset(new ServerWindowDrawnTracker(track_window, this));
  else
    drawn_tracker_.reset();
  return true;
}

void FocusController::OnDrawnStateWillChange(ServerWindow* ancestor,
                                             ServerWindow* window,
                                             bool is_drawn) {
  DCHECK(!is_drawn);
  DCHECK_NE(ancestor, window);
  DCHECK(root_->Contains(window));
  drawn_tracker_.reset();

  // Find the window that triggered this state-change notification.
  ServerWindow* trigger = window;
  while (trigger->parent() != ancestor)
    trigger = trigger->parent();
  DCHECK(trigger);
  auto will_be_hidden = [trigger](ServerWindow* w) {
    return trigger->Contains(w);
  };

  // If |window| is |active_window_|, then activate the next activatable window
  // that does not belong to the subtree which is getting hidden.
  if (window == active_window_) {
    WindowTreeIterator iter(root_);
    ServerWindow* activate = active_window_;
    do {
      activate = iter.GetNext(activate);
    } while (activate != active_window_ &&
             (will_be_hidden(activate) || !CanBeActivated(activate)));
    if (activate == window)
      activate = nullptr;
    SetActiveWindow(activate, ActivationChangeReason::DRAWN_STATE_CHANGED);

    // Now make sure focus is in the active window.
    ServerWindow* focus = nullptr;
    if (active_window_) {
      WindowTreeIterator iter(active_window_);
      focus = nullptr;
      do {
        focus = iter.GetNext(focus);
      } while (focus != active_window_ &&
               (will_be_hidden(focus) || !CanBeFocused(focus)));
      DCHECK(focus && CanBeFocused(focus));
    }
    SetFocusedWindowImpl(FocusControllerChangeSource::DRAWN_STATE_CHANGED,
                         focus);
    return;
  }

  // Move focus to the next focusable window in |active_window_|.
  DCHECK_EQ(focused_window_, window);
  WindowTreeIterator iter(active_window_);
  ServerWindow* focus = focused_window_;
  do {
    focus = iter.GetNext(focus);
  } while (focus != focused_window_ &&
           (will_be_hidden(focus) || !CanBeFocused(focus)));
  if (focus == window)
    focus = nullptr;
  SetFocusedWindowImpl(FocusControllerChangeSource::DRAWN_STATE_CHANGED, focus);
}

void FocusController::OnDrawnStateChanged(ServerWindow* ancestor,
                                          ServerWindow* window,
                                          bool is_drawn) {
  // DCHECK(false);  TODO(sadrul):
}

}  // namespace ws
}  // namespace mus