This source file includes following definitions.
- accessibility_mode_
- FindNextFocusableView
- IsViewFocusableCandidate
- IsFocusable
- FindSelectedViewForGroup
- GetParent
- Contains
- FindNextFocusableViewImpl
- FindPreviousFocusableViewImpl
#include "base/logging.h"
#include "ui/views/focus/focus_manager.h"
#include "ui/views/focus/focus_search.h"
#include "ui/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();
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) {
starting_view = reverse ? root_->child_at(root_->child_count() - 1) :
root_->child_at(0);
check_starting_view = true;
} else {
DCHECK(Contains(root_, starting_view));
}
View* v = NULL;
if (!reverse) {
v = FindNextFocusableViewImpl(starting_view, check_starting_view,
true,
(direction == DOWN),
starting_view_group,
focus_traversable,
focus_traversable_view);
} else {
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);
}
if (v && v != root_ && !Contains(root_, v))
v = 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;
}
if (v) {
DCHECK(IsFocusable(v));
return v;
}
if (*focus_traversable) {
DCHECK(*focus_traversable_view);
return NULL;
}
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->IsAccessibilityFocusable();
return v && v->IsFocusable();
}
View* FocusSearch::FindSelectedViewForGroup(View* view) {
if (view->IsGroupFocusTraversable() ||
view->GetGroup() == -1)
return view;
View* selected_view = view->GetSelectedViewForGroup(view->GetGroup());
if (selected_view)
return selected_view;
return view;
}
View* FocusSearch::GetParent(View* v) {
return Contains(root_, v) ? v->parent() : NULL;
}
bool FocusSearch::Contains(View* root, const View* v) {
return root->Contains(v);
}
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);
if (IsFocusable(v))
return v;
}
*focus_traversable = starting_view->GetFocusTraversable();
if (*focus_traversable) {
*focus_traversable_view = starting_view;
return NULL;
}
}
if (can_go_down) {
if (starting_view->has_children()) {
View* v = FindNextFocusableViewImpl(starting_view->child_at(0),
true, false, true, skip_group_id,
focus_traversable,
focus_traversable_view);
if (v || *focus_traversable)
return v;
}
}
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;
}
if (can_go_up) {
View* parent = GetParent(starting_view);
while (parent && parent != root_) {
sibling = parent->GetNextFocusableView();
if (sibling) {
return FindNextFocusableViewImpl(sibling,
true, true, true,
skip_group_id,
focus_traversable,
focus_traversable_view);
}
parent = GetParent(parent);
}
}
return NULL;
}
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) {
if (can_go_down) {
*focus_traversable = starting_view->GetFocusTraversable();
if (*focus_traversable) {
*focus_traversable_view = starting_view;
return NULL;
}
if (starting_view->has_children()) {
View* view =
starting_view->child_at(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;
}
}
if (check_starting_view &&
IsViewFocusableCandidate(starting_view, skip_group_id)) {
View* v = FindSelectedViewForGroup(starting_view);
if (IsFocusable(v))
return v;
}
View* sibling = starting_view->GetPreviousFocusableView();
if (sibling) {
return FindPreviousFocusableViewImpl(sibling,
true, true, true,
skip_group_id,
focus_traversable,
focus_traversable_view);
}
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);
}
return NULL;
}
}