Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(226)

Side by Side Diff: ui/views/controls/menu/menu_controller.cc

Issue 1230163004: Selects last item in a menu when pressing 'up' initially (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Selects last item in a menu when pressing 'up' initially (comments) Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 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 "ui/views/controls/menu/menu_controller.h" 5 #include "ui/views/controls/menu/menu_controller.h"
6 6
7 #include "base/i18n/case_conversion.h" 7 #include "base/i18n/case_conversion.h"
8 #include "base/i18n/rtl.h" 8 #include "base/i18n/rtl.h"
9 #include "base/strings/utf_string_conversions.h" 9 #include "base/strings/utf_string_conversions.h"
10 #include "base/time/time.h" 10 #include "base/time/time.h"
(...skipping 1886 matching lines...) Expand 10 before | Expand all | Expand 10 after
1897 } 1897 }
1898 1898
1899 void MenuController::IncrementSelection(int delta) { 1899 void MenuController::IncrementSelection(int delta) {
1900 MenuItemView* item = pending_state_.item; 1900 MenuItemView* item = pending_state_.item;
1901 DCHECK(item); 1901 DCHECK(item);
1902 if (pending_state_.submenu_open && item->HasSubmenu() && 1902 if (pending_state_.submenu_open && item->HasSubmenu() &&
1903 item->GetSubmenu()->IsShowing()) { 1903 item->GetSubmenu()->IsShowing()) {
1904 // A menu is selected and open, but none of its children are selected, 1904 // A menu is selected and open, but none of its children are selected,
1905 // select the first menu item that is visible and enabled. 1905 // select the first menu item that is visible and enabled.
1906 if (item->GetSubmenu()->GetMenuItemCount()) { 1906 if (item->GetSubmenu()->GetMenuItemCount()) {
1907 MenuItemView* to_select = FindFirstSelectableMenuItem(item); 1907 MenuItemView* to_select = FindInitialSelectableMenuItem(item, delta);
1908 if (to_select) 1908 if (to_select)
1909 SetSelection(to_select, SELECTION_DEFAULT); 1909 SetSelection(to_select, SELECTION_DEFAULT);
1910 return; 1910 return;
1911 } 1911 }
1912 } 1912 }
1913 1913
1914 if (item->has_children()) { 1914 if (item->has_children()) {
1915 CustomButton* button = GetFirstHotTrackedView(item); 1915 CustomButton* button = GetFirstHotTrackedView(item);
1916 if (button) { 1916 if (button) {
1917 button->SetHotTracked(false); 1917 button->SetHotTracked(false);
(...skipping 28 matching lines...) Expand all
1946 CustomButton* button_hot = CustomButton::AsCustomButton(to_make_hot); 1946 CustomButton* button_hot = CustomButton::AsCustomButton(to_make_hot);
1947 if (button_hot) 1947 if (button_hot)
1948 button_hot->SetHotTracked(true); 1948 button_hot->SetHotTracked(true);
1949 break; 1949 break;
1950 } 1950 }
1951 } 1951 }
1952 } 1952 }
1953 } 1953 }
1954 } 1954 }
1955 1955
1956 MenuItemView* MenuController::FindFirstSelectableMenuItem( 1956 MenuItemView* MenuController::FindInitialSelectableMenuItem(
1957 MenuItemView* parent) { 1957 MenuItemView* parent,
1958 MenuItemView* child = parent->GetSubmenu()->GetMenuItemAt(0); 1958 int delta) {
1959 if (!child->visible() || !child->enabled()) 1959 return FindNextSelectableMenuItem(parent, delta > 0 ? -1 : 0, delta);
1960 child = FindNextSelectableMenuItem(parent, 0, 1);
1961 return child;
1962 } 1960 }
1963 1961
1964 MenuItemView* MenuController::FindNextSelectableMenuItem(MenuItemView* parent, 1962 MenuItemView* MenuController::FindNextSelectableMenuItem(MenuItemView* parent,
1965 int index, 1963 int index,
1966 int delta) { 1964 int delta) {
1967 int start_index = index;
1968 int parent_count = parent->GetSubmenu()->GetMenuItemCount(); 1965 int parent_count = parent->GetSubmenu()->GetMenuItemCount();
1966 int stop_index = (index + parent_count) % parent_count;
1967 bool include_all_items = index + delta < 0 || index < 0;
sadrul 2015/07/15 19:21:52 Hm, what's the use of |include_all_items|?
varkha 2015/07/15 19:51:34 Just below. Normally we iterate on all but one cur
1969 // Loop through the menu items skipping any invisible menus. The loop stops 1968 // Loop through the menu items skipping any invisible menus. The loop stops
1970 // when we wrap or find a visible and enabled child. 1969 // when we wrap or find a visible and enabled child.
1971 do { 1970 do {
1972 index = (index + delta + parent_count) % parent_count; 1971 index = (index + delta + parent_count) % parent_count;
1973 if (index == start_index) 1972 if (index == stop_index && !include_all_items)
1974 return NULL; 1973 return NULL;
1975 MenuItemView* child = parent->GetSubmenu()->GetMenuItemAt(index); 1974 MenuItemView* child = parent->GetSubmenu()->GetMenuItemAt(index);
1976 if (child->visible() && child->enabled()) 1975 if (child->visible() && child->enabled())
1977 return child; 1976 return child;
1978 } while (index != start_index); 1977 } while (index != stop_index);
1979 return NULL; 1978 return NULL;
1980 } 1979 }
1981 1980
1982 void MenuController::OpenSubmenuChangeSelectionIfCan() { 1981 void MenuController::OpenSubmenuChangeSelectionIfCan() {
1983 MenuItemView* item = pending_state_.item; 1982 MenuItemView* item = pending_state_.item;
1984 if (!item->HasSubmenu() || !item->enabled()) 1983 if (!item->HasSubmenu() || !item->enabled())
1985 return; 1984 return;
1986 MenuItemView* to_select = NULL; 1985 MenuItemView* to_select = NULL;
1987 if (item->GetSubmenu()->GetMenuItemCount() > 0) 1986 if (item->GetSubmenu()->GetMenuItemCount() > 0)
1988 to_select = FindFirstSelectableMenuItem(item); 1987 to_select = FindInitialSelectableMenuItem(item, 1);
1989 if (to_select) { 1988 if (to_select) {
1990 SetSelection(to_select, SELECTION_UPDATE_IMMEDIATELY); 1989 SetSelection(to_select, SELECTION_UPDATE_IMMEDIATELY);
1991 return; 1990 return;
1992 } 1991 }
1993 // No menu items, just show the sub-menu. 1992 // No menu items, just show the sub-menu.
1994 SetSelection(item, SELECTION_OPEN_SUBMENU | SELECTION_UPDATE_IMMEDIATELY); 1993 SetSelection(item, SELECTION_OPEN_SUBMENU | SELECTION_UPDATE_IMMEDIATELY);
1995 } 1994 }
1996 1995
1997 void MenuController::CloseSubmenu() { 1996 void MenuController::CloseSubmenu() {
1998 MenuItemView* item = state_.item; 1997 MenuItemView* item = state_.item;
(...skipping 358 matching lines...) Expand 10 before | Expand all | Expand 10 after
2357 } 2356 }
2358 } 2357 }
2359 2358
2360 gfx::Screen* MenuController::GetScreen() { 2359 gfx::Screen* MenuController::GetScreen() {
2361 Widget* root = owner_ ? owner_->GetTopLevelWidget() : NULL; 2360 Widget* root = owner_ ? owner_->GetTopLevelWidget() : NULL;
2362 return root ? gfx::Screen::GetScreenFor(root->GetNativeView()) 2361 return root ? gfx::Screen::GetScreenFor(root->GetNativeView())
2363 : gfx::Screen::GetNativeScreen(); 2362 : gfx::Screen::GetNativeScreen();
2364 } 2363 }
2365 2364
2366 } // namespace views 2365 } // namespace views
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698