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
|
// Copyright (c) 2010 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 "base/logging.h"
#include "views/focus/focus_manager.h"
#include "views/focus/focus_search.h"
#include "views/view.h"
namespace views {
FocusSearch::FocusSearch(View* root, bool cycle, bool accessibility_mode)
: root_(root),
cycle_(cycle),
accessibility_mode_(accessibility_mode) {
}
View* FocusSearch::FindNextFocusableView(View* starting_view,
bool reverse,
Direction direction,
bool check_starting_view,
FocusTraversable** focus_traversable,
View** focus_traversable_view) {
*focus_traversable = NULL;
*focus_traversable_view = NULL;
if (!root_->has_children()) {
NOTREACHED();
// Nothing to focus on here.
return NULL;
}
View* initial_starting_view = starting_view;
int starting_view_group = -1;
if (starting_view)
starting_view_group = starting_view->GetGroup();
if (!starting_view) {
// Default to the first/last child
starting_view =
reverse ?
root_->GetChildViewAt(root_->child_count() - 1) :
root_->GetChildViewAt(0);
// If there was no starting view, then the one we select is a potential
// focus candidate.
check_starting_view = true;
} else {
// The starting view should be a direct or indirect child of the root.
DCHECK(root_->Contains(starting_view));
}
View* v = NULL;
if (!reverse) {
v = FindNextFocusableViewImpl(starting_view, check_starting_view,
true,
(direction == DOWN) ? true : false,
starting_view_group,
focus_traversable,
focus_traversable_view);
} else {
// If the starting view is focusable, we don't want to go down, as we are
// traversing the view hierarchy tree bottom-up.
bool can_go_down = (direction == DOWN) && !IsFocusable(starting_view);
v = FindPreviousFocusableViewImpl(starting_view, check_starting_view,
true,
can_go_down,
starting_view_group,
focus_traversable,
focus_traversable_view);
}
// Don't set the focus to something outside of this view hierarchy.
if (v && v != root_ && !root_->Contains(v))
v = NULL;
// If |cycle_| is true, prefer to keep cycling rather than returning NULL.
if (cycle_ && !v && initial_starting_view) {
v = FindNextFocusableView(NULL, reverse, direction, check_starting_view,
focus_traversable, focus_traversable_view);
DCHECK(IsFocusable(v));
return v;
}
// Doing some sanity checks.
if (v) {
DCHECK(IsFocusable(v));
return v;
}
if (*focus_traversable) {
DCHECK(*focus_traversable_view);
return NULL;
}
// Nothing found.
return NULL;
}
bool FocusSearch::IsViewFocusableCandidate(View* v, int skip_group_id) {
return IsFocusable(v) &&
(v->IsGroupFocusTraversable() || skip_group_id == -1 ||
v->GetGroup() != skip_group_id);
}
bool FocusSearch::IsFocusable(View* v) {
if (accessibility_mode_)
return v && v->IsAccessibilityFocusableInRootView();
return v && v->IsFocusableInRootView();
}
View* FocusSearch::FindSelectedViewForGroup(View* view) {
if (view->IsGroupFocusTraversable() ||
view->GetGroup() == -1) // No group for that view.
return view;
View* selected_view = view->GetSelectedViewForGroup(view->GetGroup());
if (selected_view)
return selected_view;
// No view selected for that group, default to the specified view.
return view;
}
View* FocusSearch::GetParent(View* v) {
return root_->Contains(v) ? v->parent() : NULL;
}
// Strategy for finding the next focusable view:
// - keep going down the first child, stop when you find a focusable view or
// a focus traversable view (in that case return it) or when you reach a view
// with no children.
// - go to the right sibling and start the search from there (by invoking
// FindNextFocusableViewImpl on that view).
// - if the view has no right sibling, go up the parents until you find a parent
// with a right sibling and start the search from there.
View* FocusSearch::FindNextFocusableViewImpl(
View* starting_view,
bool check_starting_view,
bool can_go_up,
bool can_go_down,
int skip_group_id,
FocusTraversable** focus_traversable,
View** focus_traversable_view) {
if (check_starting_view) {
if (IsViewFocusableCandidate(starting_view, skip_group_id)) {
View* v = FindSelectedViewForGroup(starting_view);
// The selected view might not be focusable (if it is disabled for
// example).
if (IsFocusable(v))
return v;
}
*focus_traversable = starting_view->GetFocusTraversable();
if (*focus_traversable) {
*focus_traversable_view = starting_view;
return NULL;
}
}
// First let's try the left child.
if (can_go_down) {
if (starting_view->has_children()) {
View* v = FindNextFocusableViewImpl(starting_view->GetChildViewAt(0),
true, false, true, skip_group_id,
focus_traversable,
focus_traversable_view);
if (v || *focus_traversable)
return v;
}
}
// Then try the right sibling.
View* sibling = starting_view->GetNextFocusableView();
if (sibling) {
View* v = FindNextFocusableViewImpl(sibling,
true, false, true, skip_group_id,
focus_traversable,
focus_traversable_view);
if (v || *focus_traversable)
return v;
}
// Then go up to the parent sibling.
if (can_go_up) {
View* parent = GetParent(starting_view);
while (parent) {
sibling = parent->GetNextFocusableView();
if (sibling) {
return FindNextFocusableViewImpl(sibling,
true, true, true,
skip_group_id,
focus_traversable,
focus_traversable_view);
}
parent = GetParent(parent);
}
}
// We found nothing.
return NULL;
}
// Strategy for finding the previous focusable view:
// - keep going down on the right until you reach a view with no children, if it
// it is a good candidate return it.
// - start the search on the left sibling.
// - if there are no left sibling, start the search on the parent (without going
// down).
View* FocusSearch::FindPreviousFocusableViewImpl(
View* starting_view,
bool check_starting_view,
bool can_go_up,
bool can_go_down,
int skip_group_id,
FocusTraversable** focus_traversable,
View** focus_traversable_view) {
// Let's go down and right as much as we can.
if (can_go_down) {
// Before we go into the direct children, we have to check if this view has
// a FocusTraversable.
*focus_traversable = starting_view->GetFocusTraversable();
if (*focus_traversable) {
*focus_traversable_view = starting_view;
return NULL;
}
if (starting_view->has_children()) {
View* view =
starting_view->GetChildViewAt(starting_view->child_count() - 1);
View* v = FindPreviousFocusableViewImpl(view, true, false, true,
skip_group_id,
focus_traversable,
focus_traversable_view);
if (v || *focus_traversable)
return v;
}
}
// Then look at this view. Here, we do not need to see if the view has
// a FocusTraversable, since we do not want to go down any more.
if (check_starting_view &&
IsViewFocusableCandidate(starting_view, skip_group_id)) {
View* v = FindSelectedViewForGroup(starting_view);
// The selected view might not be focusable (if it is disabled for
// example).
if (IsFocusable(v))
return v;
}
// Then try the left sibling.
View* sibling = starting_view->GetPreviousFocusableView();
if (sibling) {
return FindPreviousFocusableViewImpl(sibling,
true, true, true,
skip_group_id,
focus_traversable,
focus_traversable_view);
}
// Then go up the parent.
if (can_go_up) {
View* parent = GetParent(starting_view);
if (parent)
return FindPreviousFocusableViewImpl(parent,
true, true, false,
skip_group_id,
focus_traversable,
focus_traversable_view);
}
// We found nothing.
return NULL;
}
} // namespace views
|