OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "services/window_manager/basic_focus_rules.h" | 5 #include "services/window_manager/basic_focus_rules.h" |
6 | 6 |
7 #include "base/macros.h" | 7 #include "base/macros.h" |
8 #include "mojo/services/public/cpp/view_manager/view.h" | 8 #include "mojo/services/public/cpp/view_manager/view.h" |
9 | 9 |
10 namespace mojo { | 10 using mojo::View; |
11 | 11 |
12 BasicFocusRules::BasicFocusRules(mojo::View* window_container) | 12 namespace window_manager { |
| 13 |
| 14 BasicFocusRules::BasicFocusRules(View* window_container) |
13 : window_container_(window_container) { | 15 : window_container_(window_container) { |
14 } | 16 } |
15 | 17 |
16 BasicFocusRules::~BasicFocusRules() {} | 18 BasicFocusRules::~BasicFocusRules() {} |
17 | 19 |
18 bool BasicFocusRules::SupportsChildActivation(mojo::View* view) const { | 20 bool BasicFocusRules::SupportsChildActivation(View* view) const { |
19 return true; | 21 return true; |
20 } | 22 } |
21 | 23 |
22 bool BasicFocusRules::IsToplevelView(mojo::View* view) const { | 24 bool BasicFocusRules::IsToplevelView(View* view) const { |
23 if (!IsViewParentedToWindowContainer(view)) | 25 if (!IsViewParentedToWindowContainer(view)) |
24 return false; | 26 return false; |
25 | 27 |
26 // The window must exist within a container that supports activation. | 28 // The window must exist within a container that supports activation. |
27 // The window cannot be blocked by a modal transient. | 29 // The window cannot be blocked by a modal transient. |
28 return SupportsChildActivation(view->parent()); | 30 return SupportsChildActivation(view->parent()); |
29 } | 31 } |
30 | 32 |
31 bool BasicFocusRules::CanActivateView(mojo::View* view) const { | 33 bool BasicFocusRules::CanActivateView(View* view) const { |
32 if (!view) | 34 if (!view) |
33 return true; | 35 return true; |
34 | 36 |
35 // Only toplevel windows can be activated | 37 // Only toplevel windows can be activated |
36 if (!IsToplevelView(view)) | 38 if (!IsToplevelView(view)) |
37 return false; | 39 return false; |
38 | 40 |
39 // The view must be visible. | 41 // The view must be visible. |
40 if (!view->visible()) | 42 if (!view->visible()) |
41 return false; | 43 return false; |
42 | 44 |
43 // TODO(erg): The aura version of this class asks the aura::Window's | 45 // TODO(erg): The aura version of this class asks the aura::Window's |
44 // ActivationDelegate whether the window is activatable. | 46 // ActivationDelegate whether the window is activatable. |
45 | 47 |
46 // A window must be focusable to be activatable. We don't call | 48 // A window must be focusable to be activatable. We don't call |
47 // CanFocusWindow() from here because it will call back to us via | 49 // CanFocusWindow() from here because it will call back to us via |
48 // GetActivatableWindow(). | 50 // GetActivatableWindow(). |
49 if (!CanFocusViewImpl(view)) | 51 if (!CanFocusViewImpl(view)) |
50 return false; | 52 return false; |
51 | 53 |
52 // TODO(erg): In the aura version, we also check whether the window is | 54 // TODO(erg): In the aura version, we also check whether the window is |
53 // blocked by a modal transient window. | 55 // blocked by a modal transient window. |
54 | 56 |
55 return true; | 57 return true; |
56 } | 58 } |
57 | 59 |
58 bool BasicFocusRules::CanFocusView(mojo::View* view) const { | 60 bool BasicFocusRules::CanFocusView(View* view) const { |
59 // It is possible to focus a NULL window, it is equivalent to clearing focus. | 61 // It is possible to focus a NULL window, it is equivalent to clearing focus. |
60 if (!view) | 62 if (!view) |
61 return true; | 63 return true; |
62 | 64 |
63 // The focused view is always inside the active view, so views that aren't | 65 // The focused view is always inside the active view, so views that aren't |
64 // activatable can't contain the focused view. | 66 // activatable can't contain the focused view. |
65 View* activatable = GetActivatableView(view); | 67 View* activatable = GetActivatableView(view); |
66 if (!activatable || !activatable->Contains(view)) | 68 if (!activatable || !activatable->Contains(view)) |
67 return false; | 69 return false; |
68 return CanFocusViewImpl(view); | 70 return CanFocusViewImpl(view); |
69 } | 71 } |
70 | 72 |
71 mojo::View* BasicFocusRules::GetToplevelView(mojo::View* view) const { | 73 View* BasicFocusRules::GetToplevelView(View* view) const { |
72 View* parent = view->parent(); | 74 View* parent = view->parent(); |
73 View* child = view; | 75 View* child = view; |
74 while (parent) { | 76 while (parent) { |
75 if (IsToplevelView(child)) | 77 if (IsToplevelView(child)) |
76 return child; | 78 return child; |
77 | 79 |
78 parent = parent->parent(); | 80 parent = parent->parent(); |
79 child = child->parent(); | 81 child = child->parent(); |
80 } | 82 } |
81 | 83 |
82 return nullptr; | 84 return nullptr; |
83 } | 85 } |
84 | 86 |
85 mojo::View* BasicFocusRules::GetActivatableView(mojo::View* view) const { | 87 View* BasicFocusRules::GetActivatableView(View* view) const { |
86 View* parent = view->parent(); | 88 View* parent = view->parent(); |
87 View* child = view; | 89 View* child = view; |
88 while (parent) { | 90 while (parent) { |
89 if (CanActivateView(child)) | 91 if (CanActivateView(child)) |
90 return child; | 92 return child; |
91 | 93 |
92 // TODO(erg): In the aura version of this class, we have a whole bunch of | 94 // TODO(erg): In the aura version of this class, we have a whole bunch of |
93 // checks to support modal transient windows, and transient parents. | 95 // checks to support modal transient windows, and transient parents. |
94 | 96 |
95 parent = parent->parent(); | 97 parent = parent->parent(); |
96 child = child->parent(); | 98 child = child->parent(); |
97 } | 99 } |
98 | 100 |
99 return nullptr; | 101 return nullptr; |
100 } | 102 } |
101 | 103 |
102 mojo::View* BasicFocusRules::GetFocusableView(mojo::View* view) const { | 104 View* BasicFocusRules::GetFocusableView(View* view) const { |
103 if (CanFocusView(view)) | 105 if (CanFocusView(view)) |
104 return view; | 106 return view; |
105 | 107 |
106 // |view| may be in a hierarchy that is non-activatable, in which case we | 108 // |view| may be in a hierarchy that is non-activatable, in which case we |
107 // need to cut over to the activatable hierarchy. | 109 // need to cut over to the activatable hierarchy. |
108 View* activatable = GetActivatableView(view); | 110 View* activatable = GetActivatableView(view); |
109 if (!activatable) { | 111 if (!activatable) { |
110 // There may not be a related activatable hierarchy to cut over to, in which | 112 // There may not be a related activatable hierarchy to cut over to, in which |
111 // case we try an unrelated one. | 113 // case we try an unrelated one. |
112 View* toplevel = GetToplevelView(view); | 114 View* toplevel = GetToplevelView(view); |
113 if (toplevel) | 115 if (toplevel) |
114 activatable = GetNextActivatableView(toplevel); | 116 activatable = GetNextActivatableView(toplevel); |
115 if (!activatable) | 117 if (!activatable) |
116 return nullptr; | 118 return nullptr; |
117 } | 119 } |
118 | 120 |
119 if (!activatable->Contains(view)) { | 121 if (!activatable->Contains(view)) { |
120 // If there's already a child window focused in the activatable hierarchy, | 122 // If there's already a child window focused in the activatable hierarchy, |
121 // just use that (i.e. don't shift focus), otherwise we need to at least cut | 123 // just use that (i.e. don't shift focus), otherwise we need to at least cut |
122 // over to the activatable hierarchy. | 124 // over to the activatable hierarchy. |
123 View* focused = GetFocusableView(activatable); | 125 View* focused = GetFocusableView(activatable); |
124 return activatable->Contains(focused) ? focused : activatable; | 126 return activatable->Contains(focused) ? focused : activatable; |
125 } | 127 } |
126 | 128 |
127 while (view && !CanFocusView(view)) | 129 while (view && !CanFocusView(view)) |
128 view = view->parent(); | 130 view = view->parent(); |
129 return view; | 131 return view; |
130 } | 132 } |
131 | 133 |
132 mojo::View* BasicFocusRules::GetNextActivatableView( | 134 View* BasicFocusRules::GetNextActivatableView(View* activatable) const { |
133 mojo::View* activatable) const { | |
134 DCHECK(activatable); | 135 DCHECK(activatable); |
135 | 136 |
136 // In the basic scenarios handled by BasicFocusRules, the pool of activatable | 137 // In the basic scenarios handled by BasicFocusRules, the pool of activatable |
137 // windows is limited to the |ignore|'s siblings. | 138 // windows is limited to the |ignore|'s siblings. |
138 const View::Children& siblings = activatable->parent()->children(); | 139 const View::Children& siblings = activatable->parent()->children(); |
139 DCHECK(!siblings.empty()); | 140 DCHECK(!siblings.empty()); |
140 | 141 |
141 for (auto rit = siblings.rbegin(); rit != siblings.rend(); ++rit) { | 142 for (auto rit = siblings.rbegin(); rit != siblings.rend(); ++rit) { |
142 View* cur = *rit; | 143 View* cur = *rit; |
143 if (cur == activatable) | 144 if (cur == activatable) |
144 continue; | 145 continue; |
145 if (CanActivateView(cur)) | 146 if (CanActivateView(cur)) |
146 return cur; | 147 return cur; |
147 } | 148 } |
148 return nullptr; | 149 return nullptr; |
149 } | 150 } |
150 | 151 |
151 // TODO(erg): aura::Window::CanFocus() exists. mojo::View::CanFocus() does | 152 // TODO(erg): aura::Window::CanFocus() exists. View::CanFocus() does |
152 // not. This is a hack that does everything that Window::CanFocus() currently | 153 // not. This is a hack that does everything that Window::CanFocus() currently |
153 // does that doesn't require a delegate or an EventClient. | 154 // does that doesn't require a delegate or an EventClient. |
154 bool BasicFocusRules::CanFocusViewImpl(View* view) const { | 155 bool BasicFocusRules::CanFocusViewImpl(View* view) const { |
155 // TODO(erg): In unit tests, views will never be drawn, so we can't rely on | 156 // TODO(erg): In unit tests, views will never be drawn, so we can't rely on |
156 // IsDrawn() here. | 157 // IsDrawn() here. |
157 if (IsViewParentedToWindowContainer(view)) | 158 if (IsViewParentedToWindowContainer(view)) |
158 return view->visible(); | 159 return view->visible(); |
159 | 160 |
160 // TODO(erg): Add the intermediary delegate and event client checks once we | 161 // TODO(erg): Add the intermediary delegate and event client checks once we |
161 // have those. | 162 // have those. |
162 | 163 |
163 return CanFocusViewImpl(view->parent()); | 164 return CanFocusViewImpl(view->parent()); |
164 } | 165 } |
165 | 166 |
166 bool BasicFocusRules::IsViewParentedToWindowContainer(mojo::View* view) const { | 167 bool BasicFocusRules::IsViewParentedToWindowContainer(View* view) const { |
167 return view->parent() == window_container_; | 168 return view->parent() == window_container_; |
168 } | 169 } |
169 | 170 |
170 } // namespace mojo | 171 } // namespace mojo |
OLD | NEW |