Commit 5d26d2ba authored by Dominic Mazzoni's avatar Dominic Mazzoni Committed by Commit Bot

Use enums as arguments to FocusSearch to improve readability.

Also replace NULL with nullptr throughout FocusManager and
FocusSearch.

Bug: 764918
Change-Id: Ic90607c470130ac53375b26e601c1226c34a501a
Reviewed-on: https://chromium-review.googlesource.com/1024990Reviewed-by: default avatarScott Violet <sky@chromium.org>
Commit-Queue: Dominic Mazzoni <dmazzoni@chromium.org>
Cr-Commit-Position: refs/heads/master@{#552907}
parent e65471c5
...@@ -109,7 +109,11 @@ views::View* FindFirstOrLastFocusableChild(views::View* root, bool reverse) { ...@@ -109,7 +109,11 @@ views::View* FindFirstOrLastFocusableChild(views::View* root, bool reverse) {
views::FocusTraversable* dummy_focus_traversable; views::FocusTraversable* dummy_focus_traversable;
views::View* dummy_focus_traversable_view; views::View* dummy_focus_traversable_view;
return search.FindNextFocusableView( return search.FindNextFocusableView(
root, reverse, views::FocusSearch::DOWN, false /*check_starting_view*/, root,
reverse ? views::FocusSearch::SearchDirection::kBackwards
: views::FocusSearch::SearchDirection::kForwards,
views::FocusSearch::TraversalDirection::kDown,
views::FocusSearch::StartingViewPolicy::kSkipStartingView,
&dummy_focus_traversable, &dummy_focus_traversable_view); &dummy_focus_traversable, &dummy_focus_traversable_view);
} }
......
...@@ -126,12 +126,13 @@ class ShelfFocusSearch : public views::FocusSearch { ...@@ -126,12 +126,13 @@ class ShelfFocusSearch : public views::FocusSearch {
~ShelfFocusSearch() override = default; ~ShelfFocusSearch() override = default;
// views::FocusSearch: // views::FocusSearch:
View* FindNextFocusableView(View* starting_view, View* FindNextFocusableView(
bool reverse, View* starting_view,
Direction direction, FocusSearch::SearchDirection search_direction,
bool check_starting_view, FocusSearch::TraversalDirection traversal_direction,
views::FocusTraversable** focus_traversable, FocusSearch::StartingViewPolicy check_starting_view,
View** focus_traversable_view) override { views::FocusTraversable** focus_traversable,
View** focus_traversable_view) override {
int index = view_model_->GetIndexOfView(starting_view); int index = view_model_->GetIndexOfView(starting_view);
// The back button (item with index 0 on the model) only exists in tablet // The back button (item with index 0 on the model) only exists in tablet
// mode, so punt focus to the app list button (item with index 1 on the // mode, so punt focus to the app list button (item with index 1 on the
...@@ -143,7 +144,7 @@ class ShelfFocusSearch : public views::FocusSearch { ...@@ -143,7 +144,7 @@ class ShelfFocusSearch : public views::FocusSearch {
// Increment or decrement index based on the cycle, unless we are at either // Increment or decrement index based on the cycle, unless we are at either
// edge, then we loop to the back or front. Skip the back button (item with // edge, then we loop to the back or front. Skip the back button (item with
// index 0) when not in tablet mode. // index 0) when not in tablet mode.
if (reverse) { if (search_direction == FocusSearch::SearchDirection::kBackwards) {
--index; --index;
if (index < 0 || (index == 0 && !tablet_mode)) if (index < 0 || (index == 0 && !tablet_mode))
index = view_model_->view_size() - 1; index = view_model_->view_size() - 1;
......
...@@ -43,9 +43,12 @@ views::View* FindFirstOrLastFocusableChild(views::View* root, ...@@ -43,9 +43,12 @@ views::View* FindFirstOrLastFocusableChild(views::View* root,
views::FocusTraversable* dummy_focus_traversable; views::FocusTraversable* dummy_focus_traversable;
views::View* dummy_focus_traversable_view; views::View* dummy_focus_traversable_view;
return search.FindNextFocusableView( return search.FindNextFocusableView(
root, find_last_child, views::FocusSearch::DOWN, root,
false /*check_starting_view*/, &dummy_focus_traversable, find_last_child ? views::FocusSearch::SearchDirection::kBackwards
&dummy_focus_traversable_view); : views::FocusSearch::SearchDirection::kForwards,
views::FocusSearch::TraversalDirection::kDown,
views::FocusSearch::StartingViewPolicy::kSkipStartingView,
&dummy_focus_traversable, &dummy_focus_traversable_view);
} }
} // namespace } // namespace
......
...@@ -92,7 +92,9 @@ class SheetView : public views::View, public views::FocusTraversable { ...@@ -92,7 +92,9 @@ class SheetView : public views::View, public views::FocusTraversable {
views::FocusTraversable* dummy_focus_traversable; views::FocusTraversable* dummy_focus_traversable;
views::View* dummy_focus_traversable_view; views::View* dummy_focus_traversable_view;
first_focusable = focus_search_->FindNextFocusableView( first_focusable = focus_search_->FindNextFocusableView(
nullptr, false, views::FocusSearch::DOWN, false, nullptr, views::FocusSearch::SearchDirection::kForwards,
views::FocusSearch::TraversalDirection::kDown,
views::FocusSearch::StartingViewPolicy::kSkipStartingView,
&dummy_focus_traversable, &dummy_focus_traversable_view); &dummy_focus_traversable, &dummy_focus_traversable_view);
} }
......
...@@ -25,8 +25,9 @@ class AccessiblePaneViewFocusSearch : public FocusSearch { ...@@ -25,8 +25,9 @@ class AccessiblePaneViewFocusSearch : public FocusSearch {
protected: protected:
View* GetParent(View* v) override { View* GetParent(View* v) override {
return accessible_pane_view_->ContainsForFocusSearch(root(), v) ? return accessible_pane_view_->ContainsForFocusSearch(root(), v)
accessible_pane_view_->GetParentForFocusSearch(v) : NULL; ? accessible_pane_view_->GetParentForFocusSearch(v)
: nullptr;
} }
// Returns true if |v| is contained within the hierarchy rooted at |root|. // Returns true if |v| is contained within the hierarchy rooted at |root|.
...@@ -43,7 +44,7 @@ class AccessiblePaneViewFocusSearch : public FocusSearch { ...@@ -43,7 +44,7 @@ class AccessiblePaneViewFocusSearch : public FocusSearch {
AccessiblePaneView::AccessiblePaneView() AccessiblePaneView::AccessiblePaneView()
: pane_has_focus_(false), : pane_has_focus_(false),
allow_deactivate_on_esc_(false), allow_deactivate_on_esc_(false),
focus_manager_(NULL), focus_manager_(nullptr),
home_key_(ui::VKEY_HOME, ui::EF_NONE), home_key_(ui::VKEY_HOME, ui::EF_NONE),
end_key_(ui::VKEY_END, ui::EF_NONE), end_key_(ui::VKEY_END, ui::EF_NONE),
escape_key_(ui::VKEY_ESCAPE, ui::EF_NONE), escape_key_(ui::VKEY_ESCAPE, ui::EF_NONE),
...@@ -109,7 +110,7 @@ bool AccessiblePaneView::SetPaneFocusAndFocusDefault() { ...@@ -109,7 +110,7 @@ bool AccessiblePaneView::SetPaneFocusAndFocusDefault() {
} }
views::View* AccessiblePaneView::GetDefaultFocusableChild() { views::View* AccessiblePaneView::GetDefaultFocusableChild() {
return NULL; return nullptr;
} }
View* AccessiblePaneView::GetParentForFocusSearch(View* v) { View* AccessiblePaneView::GetParentForFocusSearch(View* v) {
...@@ -135,7 +136,9 @@ views::View* AccessiblePaneView::GetFirstFocusableChild() { ...@@ -135,7 +136,9 @@ views::View* AccessiblePaneView::GetFirstFocusableChild() {
FocusTraversable* dummy_focus_traversable; FocusTraversable* dummy_focus_traversable;
views::View* dummy_focus_traversable_view; views::View* dummy_focus_traversable_view;
return focus_search_->FindNextFocusableView( return focus_search_->FindNextFocusableView(
NULL, false, views::FocusSearch::DOWN, false, nullptr, FocusSearch::SearchDirection::kForwards,
FocusSearch::TraversalDirection::kDown,
FocusSearch::StartingViewPolicy::kSkipStartingView,
&dummy_focus_traversable, &dummy_focus_traversable_view); &dummy_focus_traversable, &dummy_focus_traversable_view);
} }
...@@ -143,7 +146,9 @@ views::View* AccessiblePaneView::GetLastFocusableChild() { ...@@ -143,7 +146,9 @@ views::View* AccessiblePaneView::GetLastFocusableChild() {
FocusTraversable* dummy_focus_traversable; FocusTraversable* dummy_focus_traversable;
views::View* dummy_focus_traversable_view; views::View* dummy_focus_traversable_view;
return focus_search_->FindNextFocusableView( return focus_search_->FindNextFocusableView(
this, true, views::FocusSearch::DOWN, false, this, FocusSearch::SearchDirection::kBackwards,
FocusSearch::TraversalDirection::kDown,
FocusSearch::StartingViewPolicy::kSkipStartingView,
&dummy_focus_traversable, &dummy_focus_traversable_view); &dummy_focus_traversable, &dummy_focus_traversable_view);
} }
...@@ -154,7 +159,7 @@ views::FocusTraversable* AccessiblePaneView::GetPaneFocusTraversable() { ...@@ -154,7 +159,7 @@ views::FocusTraversable* AccessiblePaneView::GetPaneFocusTraversable() {
if (pane_has_focus_) if (pane_has_focus_)
return this; return this;
else else
return NULL; return nullptr;
} }
bool AccessiblePaneView::AcceleratorPressed( bool AccessiblePaneView::AcceleratorPressed(
...@@ -247,12 +252,12 @@ views::FocusSearch* AccessiblePaneView::GetFocusSearch() { ...@@ -247,12 +252,12 @@ views::FocusSearch* AccessiblePaneView::GetFocusSearch() {
views::FocusTraversable* AccessiblePaneView::GetFocusTraversableParent() { views::FocusTraversable* AccessiblePaneView::GetFocusTraversableParent() {
DCHECK(pane_has_focus_); DCHECK(pane_has_focus_);
return NULL; return nullptr;
} }
views::View* AccessiblePaneView::GetFocusTraversableParentView() { views::View* AccessiblePaneView::GetFocusTraversableParentView() {
DCHECK(pane_has_focus_); DCHECK(pane_has_focus_);
return NULL; return nullptr;
} }
} // namespace views } // namespace views
...@@ -113,7 +113,7 @@ bool FocusManager::ContainsView(View* view) { ...@@ -113,7 +113,7 @@ bool FocusManager::ContainsView(View* view) {
} }
void FocusManager::AdvanceFocus(bool reverse) { void FocusManager::AdvanceFocus(bool reverse) {
View* v = GetNextFocusableView(focused_view_, NULL, reverse, false); View* v = GetNextFocusableView(focused_view_, nullptr, reverse, false);
// Note: Do not skip this next block when v == focused_view_. If the user // Note: Do not skip this next block when v == focused_view_. If the user
// tabs past the last focusable element in a webpage, we'll get here, and if // tabs past the last focusable element in a webpage, we'll get here, and if
// the TabContentsContainerView is the only focusable view (possible in // the TabContentsContainerView is the only focusable view (possible in
...@@ -199,9 +199,9 @@ View* FocusManager::GetNextFocusableView(View* original_starting_view, ...@@ -199,9 +199,9 @@ View* FocusManager::GetNextFocusableView(View* original_starting_view,
DCHECK(!focused_view_ || ContainsView(focused_view_)) DCHECK(!focused_view_ || ContainsView(focused_view_))
<< " focus_view=" << focused_view_; << " focus_view=" << focused_view_;
FocusTraversable* focus_traversable = NULL; FocusTraversable* focus_traversable = nullptr;
View* starting_view = NULL; View* starting_view = nullptr;
if (original_starting_view) { if (original_starting_view) {
// Search up the containment hierarchy to see if a view is acting as // Search up the containment hierarchy to see if a view is acting as
// a pane, and wants to implement its own focus traversable to keep // a pane, and wants to implement its own focus traversable to keep
...@@ -254,9 +254,14 @@ View* FocusManager::GetNextFocusableView(View* original_starting_view, ...@@ -254,9 +254,14 @@ View* FocusManager::GetNextFocusableView(View* original_starting_view,
FocusTraversable* new_focus_traversable = nullptr; FocusTraversable* new_focus_traversable = nullptr;
View* new_starting_view = nullptr; View* new_starting_view = nullptr;
// When we are going backward, the parent view might gain the next focus. // When we are going backward, the parent view might gain the next focus.
bool check_starting_view = reverse; auto check_starting_view =
reverse ? FocusSearch::StartingViewPolicy::kCheckStartingView
: FocusSearch::StartingViewPolicy::kSkipStartingView;
v = parent_focus_traversable->GetFocusSearch()->FindNextFocusableView( v = parent_focus_traversable->GetFocusSearch()->FindNextFocusableView(
starting_view, reverse, FocusSearch::UP, check_starting_view, starting_view,
reverse ? FocusSearch::SearchDirection::kBackwards
: FocusSearch::SearchDirection::kForwards,
FocusSearch::TraversalDirection::kUp, check_starting_view,
&new_focus_traversable, &new_starting_view); &new_focus_traversable, &new_starting_view);
if (new_focus_traversable) { if (new_focus_traversable) {
...@@ -281,7 +286,7 @@ View* FocusManager::GetNextFocusableView(View* original_starting_view, ...@@ -281,7 +286,7 @@ View* FocusManager::GetNextFocusableView(View* original_starting_view,
return nullptr; return nullptr;
// Easy, just clear the selection and press tab again. // Easy, just clear the selection and press tab again.
// By calling with NULL as the starting view, we'll start from either // By calling with nullptr as the starting view, we'll start from either
// the starting views widget or |widget_|. // the starting views widget or |widget_|.
Widget* widget = original_starting_view->GetWidget(); Widget* widget = original_starting_view->GetWidget();
if (widget->widget_delegate()->ShouldAdvanceFocusToTopLevelWidget()) if (widget->widget_delegate()->ShouldAdvanceFocusToTopLevelWidget())
...@@ -351,10 +356,10 @@ void FocusManager::SetFocusedViewWithReason(View* view, ...@@ -351,10 +356,10 @@ void FocusManager::SetFocusedViewWithReason(View* view,
} }
void FocusManager::ClearFocus() { void FocusManager::ClearFocus() {
// SetFocusedView(NULL) is going to clear out the stored view to. We need to // SetFocusedView(nullptr) is going to clear out the stored view to. We need
// persist it in this case. // to persist it in this case.
views::View* focused_view = GetStoredFocusView(); views::View* focused_view = GetStoredFocusView();
SetFocusedView(NULL); SetFocusedView(nullptr);
ClearNativeFocus(); ClearNativeFocus();
SetStoredFocusView(focused_view); SetStoredFocusView(focused_view);
} }
...@@ -376,8 +381,8 @@ void FocusManager::AdvanceFocusIfNecessary() { ...@@ -376,8 +381,8 @@ void FocusManager::AdvanceFocusIfNecessary() {
void FocusManager::StoreFocusedView(bool clear_native_focus) { void FocusManager::StoreFocusedView(bool clear_native_focus) {
View* focused_view = focused_view_; View* focused_view = focused_view_;
// Don't do anything if no focused view. Storing the view (which is NULL), in // Don't do anything if no focused view. Storing the view (which is nullptr),
// this case, would clobber the view that was previously saved. // in this case, would clobber the view that was previously saved.
if (!focused_view_) if (!focused_view_)
return; return;
...@@ -392,7 +397,7 @@ void FocusManager::StoreFocusedView(bool clear_native_focus) { ...@@ -392,7 +397,7 @@ void FocusManager::StoreFocusedView(bool clear_native_focus) {
// ClearFocus() also stores the focused view. // ClearFocus() also stores the focused view.
ClearFocus(); ClearFocus();
} else { } else {
SetFocusedView(NULL); SetFocusedView(nullptr);
SetStoredFocusView(focused_view); SetStoredFocusView(focused_view);
} }
...@@ -440,20 +445,28 @@ View* FocusManager::GetStoredFocusView() { ...@@ -440,20 +445,28 @@ View* FocusManager::GetStoredFocusView() {
View* FocusManager::FindFocusableView(FocusTraversable* focus_traversable, View* FocusManager::FindFocusableView(FocusTraversable* focus_traversable,
View* starting_view, View* starting_view,
bool reverse) { bool reverse) {
FocusTraversable* new_focus_traversable = NULL; FocusTraversable* new_focus_traversable = nullptr;
View* new_starting_view = NULL; View* new_starting_view = nullptr;
View* v = focus_traversable->GetFocusSearch()->FindNextFocusableView( View* v = focus_traversable->GetFocusSearch()->FindNextFocusableView(
starting_view, reverse, FocusSearch::DOWN, false, &new_focus_traversable, starting_view,
&new_starting_view); reverse ? FocusSearch::SearchDirection::kBackwards
: FocusSearch::SearchDirection::kForwards,
FocusSearch::TraversalDirection::kDown,
FocusSearch::StartingViewPolicy::kSkipStartingView,
&new_focus_traversable, &new_starting_view);
// Let's go down the FocusTraversable tree as much as we can. // Let's go down the FocusTraversable tree as much as we can.
while (new_focus_traversable) { while (new_focus_traversable) {
DCHECK(!v); DCHECK(!v);
focus_traversable = new_focus_traversable; focus_traversable = new_focus_traversable;
new_focus_traversable = NULL; new_focus_traversable = nullptr;
starting_view = NULL; starting_view = nullptr;
v = focus_traversable->GetFocusSearch()->FindNextFocusableView( v = focus_traversable->GetFocusSearch()->FindNextFocusableView(
starting_view, reverse, FocusSearch::DOWN, false, starting_view,
reverse ? FocusSearch::SearchDirection::kBackwards
: FocusSearch::SearchDirection::kForwards,
FocusSearch::TraversalDirection::kDown,
FocusSearch::StartingViewPolicy::kSkipStartingView,
&new_focus_traversable, &new_starting_view); &new_focus_traversable, &new_starting_view);
} }
return v; return v;
...@@ -499,7 +512,7 @@ void FocusManager::ViewRemoved(View* removed) { ...@@ -499,7 +512,7 @@ void FocusManager::ViewRemoved(View* removed) {
// be called while the top level widget is being destroyed. // be called while the top level widget is being destroyed.
DCHECK(removed); DCHECK(removed);
if (removed->Contains(focused_view_)) if (removed->Contains(focused_view_))
SetFocusedView(NULL); SetFocusedView(nullptr);
} }
void FocusManager::AddFocusChangeListener(FocusChangeListener* listener) { void FocusManager::AddFocusChangeListener(FocusChangeListener* listener) {
......
This diff is collapsed.
...@@ -21,9 +21,19 @@ class VIEWS_EXPORT FocusSearch { ...@@ -21,9 +21,19 @@ class VIEWS_EXPORT FocusSearch {
// goal is to switch to focusable views on the same level when using the arrow // goal is to switch to focusable views on the same level when using the arrow
// keys (ala Windows: in a dialog box, arrow keys typically move between the // keys (ala Windows: in a dialog box, arrow keys typically move between the
// dialog OK, Cancel buttons). // dialog OK, Cancel buttons).
enum Direction { enum class TraversalDirection {
UP = 0, kUp,
DOWN kDown,
};
enum class SearchDirection {
kForwards,
kBackwards,
};
enum class StartingViewPolicy {
kSkipStartingView,
kCheckStartingView,
}; };
// Constructor. // Constructor.
...@@ -52,20 +62,22 @@ class VIEWS_EXPORT FocusSearch { ...@@ -52,20 +62,22 @@ class VIEWS_EXPORT FocusSearch {
// - |starting_view| is the view that should be used as the starting point // - |starting_view| is the view that should be used as the starting point
// when looking for the previous/next view. It may be NULL (in which case // when looking for the previous/next view. It may be NULL (in which case
// the first/last view should be used depending if normal/reverse). // the first/last view should be used depending if normal/reverse).
// - |reverse| whether we should find the next (reverse is false) or the // - |search_direction| whether we should find the next (kForwards) or
// previous (reverse is true) view. // previous (kReverse) view.
// - |direction| specifies whether we are traversing down (meaning we should // - |traversal_direction| specifies whether we are traversing down (meaning
// look into child views) or traversing up (don't look at child views). // we should look into child views) or traversing up (don't look at
// - |check_starting_view| is true if starting_view may obtain the next focus. // child views).
// - |check_starting_view| indicated if starting_view may obtain the next
// focus.
// - |focus_traversable| is set to the focus traversable that should be // - |focus_traversable| is set to the focus traversable that should be
// traversed if one is found (in which case the call returns NULL). // traversed if one is found (in which case the call returns NULL).
// - |focus_traversable_view| is set to the view associated with the // - |focus_traversable_view| is set to the view associated with the
// FocusTraversable set in the previous parameter (it is used as the // FocusTraversable set in the previous parameter (it is used as the
// starting view when looking for the next focusable view). // starting view when looking for the next focusable view).
virtual View* FindNextFocusableView(View* starting_view, virtual View* FindNextFocusableView(View* starting_view,
bool reverse, SearchDirection search_direction,
Direction direction, TraversalDirection traversal_direction,
bool check_starting_view, StartingViewPolicy check_starting_view,
FocusTraversable** focus_traversable, FocusTraversable** focus_traversable,
View** focus_traversable_view); View** focus_traversable_view);
...@@ -102,7 +114,7 @@ class VIEWS_EXPORT FocusSearch { ...@@ -102,7 +114,7 @@ class VIEWS_EXPORT FocusSearch {
// traversal of the views hierarchy. |skip_group_id| specifies a group_id, // traversal of the views hierarchy. |skip_group_id| specifies a group_id,
// -1 means no group. All views from a group are traversed in one pass. // -1 means no group. All views from a group are traversed in one pass.
View* FindNextFocusableViewImpl(View* starting_view, View* FindNextFocusableViewImpl(View* starting_view,
bool check_starting_view, StartingViewPolicy check_starting_view,
bool can_go_up, bool can_go_up,
bool can_go_down, bool can_go_down,
int skip_group_id, int skip_group_id,
...@@ -111,7 +123,7 @@ class VIEWS_EXPORT FocusSearch { ...@@ -111,7 +123,7 @@ class VIEWS_EXPORT FocusSearch {
// Same as FindNextFocusableViewImpl but returns the previous focusable view. // Same as FindNextFocusableViewImpl but returns the previous focusable view.
View* FindPreviousFocusableViewImpl(View* starting_view, View* FindPreviousFocusableViewImpl(View* starting_view,
bool check_starting_view, StartingViewPolicy check_starting_view,
bool can_go_up, bool can_go_up,
bool can_go_down, bool can_go_down,
int skip_group_id, int skip_group_id,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment