1 // Copyright (c) 2012 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.
5 #include "ash/wm/ash_focus_rules.h"
8 #include "ash/shell_window_ids.h"
9 #include "ash/wm/window_state.h"
10 #include "ui/aura/window.h"
16 // These are the list of container ids of containers which may contain windows
17 // that need to be activated in the order that they should be activated.
18 const int kWindowContainerIds
[] = {
19 kShellWindowId_OverlayContainer
,
20 kShellWindowId_LockSystemModalContainer
,
21 kShellWindowId_SettingBubbleContainer
,
22 kShellWindowId_LockScreenContainer
,
23 kShellWindowId_SystemModalContainer
,
24 kShellWindowId_AlwaysOnTopContainer
,
25 kShellWindowId_AppListContainer
,
26 kShellWindowId_DefaultContainer
,
28 // Docked, panel, launcher and status are intentionally checked after other
29 // containers even though these layers are higher. The user expects their
30 // windows to be focused before these elements.
31 kShellWindowId_DockedContainer
,
32 kShellWindowId_PanelContainer
,
33 kShellWindowId_ShelfContainer
,
34 kShellWindowId_StatusContainer
, };
36 bool BelongsToContainerWithEqualOrGreaterId(const aura::Window
* window
,
38 for (; window
; window
= window
->parent()) {
39 if (window
->id() >= container_id
)
47 ////////////////////////////////////////////////////////////////////////////////
48 // AshFocusRules, public:
50 AshFocusRules::AshFocusRules() {
53 AshFocusRules::~AshFocusRules() {
56 ////////////////////////////////////////////////////////////////////////////////
57 // AshFocusRules, ::wm::FocusRules:
59 bool AshFocusRules::SupportsChildActivation(aura::Window
* window
) const {
60 if (window
->id() == kShellWindowId_DefaultContainer
)
63 for (size_t i
= 0; i
< arraysize(kWindowContainerIds
); i
++) {
64 if (window
->id() == kWindowContainerIds
[i
])
70 bool AshFocusRules::IsWindowConsideredVisibleForActivation(
71 aura::Window
* window
) const {
72 if (BaseFocusRules::IsWindowConsideredVisibleForActivation(window
))
75 // Minimized windows are hidden in their minimized state, but they can always
77 if (wm::GetWindowState(window
)->IsMinimized())
80 return window
->TargetVisibility() &&
81 (window
->parent()->id() == kShellWindowId_DefaultContainer
||
82 window
->parent()->id() == kShellWindowId_LockScreenContainer
);
85 bool AshFocusRules::CanActivateWindow(aura::Window
* window
) const {
86 // Clearing activation is always permissible.
90 if (!BaseFocusRules::CanActivateWindow(window
))
93 if (Shell::GetInstance()->IsSystemModalWindowOpen()) {
94 return BelongsToContainerWithEqualOrGreaterId(
95 window
, kShellWindowId_SystemModalContainer
);
101 aura::Window
* AshFocusRules::GetNextActivatableWindow(
102 aura::Window
* ignore
) const {
105 int starting_container_index
= 0;
106 // If the container of the window losing focus is in the list, start from that
108 aura::Window
* root
= ignore
->GetRootWindow();
110 root
= Shell::GetTargetRootWindow();
111 int container_count
= static_cast<int>(arraysize(kWindowContainerIds
));
112 for (int i
= 0; ignore
&& i
< container_count
; i
++) {
113 aura::Window
* container
= Shell::GetContainer(root
, kWindowContainerIds
[i
]);
114 if (container
&& container
->Contains(ignore
)) {
115 starting_container_index
= i
;
120 // Look for windows to focus in |ignore|'s container. If none are found, we
121 // look in all the containers in front of |ignore|'s container, then all
123 aura::Window
* window
= NULL
;
124 for (int i
= starting_container_index
; !window
&& i
< container_count
; i
++)
125 window
= GetTopmostWindowToActivateForContainerIndex(i
, ignore
);
126 if (!window
&& starting_container_index
> 0) {
127 for (int i
= starting_container_index
- 1; !window
&& i
>= 0; i
--)
128 window
= GetTopmostWindowToActivateForContainerIndex(i
, ignore
);
133 ////////////////////////////////////////////////////////////////////////////////
134 // AshFocusRules, private:
136 aura::Window
* AshFocusRules::GetTopmostWindowToActivateForContainerIndex(
138 aura::Window
* ignore
) const {
139 aura::Window
* window
= NULL
;
140 aura::Window
* root
= ignore
? ignore
->GetRootWindow() : NULL
;
141 aura::Window::Windows containers
= Shell::GetContainersFromAllRootWindows(
142 kWindowContainerIds
[index
], root
);
143 for (aura::Window::Windows::const_iterator iter
= containers
.begin();
144 iter
!= containers
.end() && !window
; ++iter
) {
145 window
= GetTopmostWindowToActivateInContainer((*iter
), ignore
);
150 aura::Window
* AshFocusRules::GetTopmostWindowToActivateInContainer(
151 aura::Window
* container
,
152 aura::Window
* ignore
) const {
153 for (aura::Window::Windows::const_reverse_iterator i
=
154 container
->children().rbegin();
155 i
!= container
->children().rend();
157 WindowState
* window_state
= GetWindowState(*i
);
159 window_state
->CanActivate() &&
160 !window_state
->IsMinimized())