summaryrefslogtreecommitdiffstats
path: root/ui/aura/focus_manager.cc
blob: 9bc3673f935b6cde69c7bef72ba6572eb6b06af5 (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
// 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/aura/focus_manager.h"

#include "ui/aura/client/activation_client.h"
#include "ui/aura/client/focus_change_observer.h"
#include "ui/aura/root_window.h"
#include "ui/aura/window_delegate.h"

namespace aura {

////////////////////////////////////////////////////////////////////////////////
// FocusManager, public:

FocusManager::FocusManager() : focused_window_(NULL) {
}

FocusManager::~FocusManager() {
}

////////////////////////////////////////////////////////////////////////////////
// FocusManager, client::FocusClient implementation:

void FocusManager::AddObserver(client::FocusChangeObserver* observer) {
  observers_.AddObserver(observer);
}

void FocusManager::RemoveObserver(client::FocusChangeObserver* observer) {
  observers_.RemoveObserver(observer);
}

void FocusManager::FocusWindow(Window* focused_window, const ui::Event* event) {
  if (focused_window == focused_window_)
    return;
  if (focused_window && !focused_window->CanFocus())
    return;
  // The NULL-check of |focused_window| is essential here before asking the
  // activation client, since it is valid to clear the focus by calling
  // SetFocusedWindow() to NULL.

  if (focused_window) {
    RootWindow* root = focused_window->GetRootWindow();
    DCHECK(root);
    if (client::GetActivationClient(root) &&
        !client::GetActivationClient(root)->OnWillFocusWindow(
            focused_window, event)) {
      return;
    }
  }

  Window* old_focused_window = focused_window_;
  focused_window_ = focused_window;

  FOR_EACH_OBSERVER(client::FocusChangeObserver, observers_,
                    OnWindowFocused(focused_window, old_focused_window));
  client::FocusChangeObserver* observer =
      client::GetFocusChangeObserver(old_focused_window);
  if (observer)
    observer->OnWindowFocused(focused_window_, old_focused_window);
  observer = client::GetFocusChangeObserver(focused_window_);
  if (observer)
    observer->OnWindowFocused(focused_window_, old_focused_window);
}

Window* FocusManager::GetFocusedWindow() {
  return focused_window_;
}

void FocusManager::OnWindowHiddenInRootWindow(
    aura::Window* window,
    aura::RootWindow* root_window,
    bool destroyed) {
  Window* focused_window =
      client::GetFocusClient(root_window)->GetFocusedWindow();
  if (window->Contains(focused_window)) {
    Window* focus_to = window->transient_parent();
    if (focus_to) {
      // Has to be removed from the transient parent before focusing,
      // otherwise |window| will be focused again.
      if (destroyed)
        focus_to->RemoveTransientChild(window);
    } else {
      // If the invisible view has no visible transient window, focus to the
      // topmost visible parent window.
      focus_to = window->parent();
    }
    if (focus_to &&
        (!focus_to->IsVisible() ||
          !focus_to->CanFocus() ||
          (client::GetActivationClient(root_window) &&
          !client::GetActivationClient(root_window)->OnWillFocusWindow(
              focus_to, NULL)))) {
      focus_to = NULL;
    }
    client::GetFocusClient(root_window)->FocusWindow(focus_to, NULL);
  }
}

}  // namespace aura