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

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

Issue 1661673004: Enables hot-tracking for overflow extension buttons in the app menu (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Restores hot-tracking of extension buttons in app menu with MD (win build) Created 4 years, 10 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/macros.h" 9 #include "base/macros.h"
10 #include "base/strings/utf_string_conversions.h" 10 #include "base/strings/utf_string_conversions.h"
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
46 #endif 46 #endif
47 47
48 #if defined(USE_AURA) 48 #if defined(USE_AURA)
49 #include "ui/views/controls/menu/menu_key_event_handler.h" 49 #include "ui/views/controls/menu/menu_key_event_handler.h"
50 #endif 50 #endif
51 51
52 using base::Time; 52 using base::Time;
53 using base::TimeDelta; 53 using base::TimeDelta;
54 using ui::OSExchangeData; 54 using ui::OSExchangeData;
55 55
56 // Period of the scroll timer (in milliseconds).
57 static const int kScrollTimerMS = 30;
58
59 // Amount of time from when the drop exits the menu and the menu is hidden.
60 static const int kCloseOnExitTime = 1200;
61
62 // If a context menu is invoked by touch, we shift the menu by this offset so
63 // that the finger does not obscure the menu.
64 static const int kCenteredContextMenuYOffset = -15;
65
66 namespace views { 56 namespace views {
67 57
68 namespace { 58 namespace {
69 59
70 // When showing context menu on mouse down, the user might accidentally select 60 // When showing context menu on mouse down, the user might accidentally select
71 // the menu item on the subsequent mouse up. To prevent this, we add the 61 // the menu item on the subsequent mouse up. To prevent this, we add the
72 // following delay before the user is able to select an item. 62 // following delay before the user is able to select an item.
73 static int menu_selection_hold_time_ms = kMinimumMsPressedToActivate; 63 int menu_selection_hold_time_ms = kMinimumMsPressedToActivate;
64
65 // Period of the scroll timer (in milliseconds).
66 const int kScrollTimerMS = 30;
67
68 // Amount of time from when the drop exits the menu and the menu is hidden.
69 const int kCloseOnExitTime = 1200;
70
71 // If a context menu is invoked by touch, we shift the menu by this offset so
72 // that the finger does not obscure the menu.
73 const int kCenteredContextMenuYOffset = -15;
74 74
75 // The spacing offset for the bubble tip. 75 // The spacing offset for the bubble tip.
76 const int kBubbleTipSizeLeftRight = 12; 76 const int kBubbleTipSizeLeftRight = 12;
77 const int kBubbleTipSizeTopBottom = 11; 77 const int kBubbleTipSizeTopBottom = 11;
78 78
79 // The maximum distance (in DIPS) that the mouse can be moved before it should 79 // The maximum distance (in DIPS) that the mouse can be moved before it should
80 // trigger a mouse menu item activation (regardless of how long the menu has 80 // trigger a mouse menu item activation (regardless of how long the menu has
81 // been showing). 81 // been showing).
82 const float kMaximumLengthMovedToActivate = 4.0f; 82 const float kMaximumLengthMovedToActivate = 4.0f;
83 83
84 // Returns true if the mnemonic of |menu| matches key. 84 // Returns true if the mnemonic of |menu| matches key.
85 bool MatchesMnemonic(MenuItemView* menu, base::char16 key) { 85 bool MatchesMnemonic(MenuItemView* menu, base::char16 key) {
86 return key != 0 && menu->GetMnemonic() == key; 86 return key != 0 && menu->GetMnemonic() == key;
87 } 87 }
88 88
89 // Returns true if |menu| doesn't have a mnemonic and first character of the its 89 // Returns true if |menu| doesn't have a mnemonic and first character of the its
90 // title is |key|. 90 // title is |key|.
91 bool TitleMatchesMnemonic(MenuItemView* menu, base::char16 key) { 91 bool TitleMatchesMnemonic(MenuItemView* menu, base::char16 key) {
92 if (menu->GetMnemonic()) 92 if (menu->GetMnemonic())
93 return false; 93 return false;
94 94
95 base::string16 lower_title = base::i18n::ToLower(menu->title()); 95 base::string16 lower_title = base::i18n::ToLower(menu->title());
96 return !lower_title.empty() && lower_title[0] == key; 96 return !lower_title.empty() && lower_title[0] == key;
97 } 97 }
98 98
99 // Returns the first descendant of |view| that is hot tracked. 99 // Returns the first descendant of |view| that is hot tracked.
100 static CustomButton* GetFirstHotTrackedView(View* view) { 100 CustomButton* GetFirstHotTrackedView(View* view) {
101 if (!view) 101 if (!view)
102 return NULL; 102 return NULL;
103 CustomButton* button = CustomButton::AsCustomButton(view); 103 CustomButton* button = CustomButton::AsCustomButton(view);
104 if (button) { 104 if (button) {
105 if (button->IsHotTracked()) 105 if (button->IsHotTracked())
106 return button; 106 return button;
107 } 107 }
108 108
109 for (int i = 0; i < view->child_count(); ++i) { 109 for (int i = 0; i < view->child_count(); ++i) {
110 CustomButton* hot_view = GetFirstHotTrackedView(view->child_at(i)); 110 CustomButton* hot_view = GetFirstHotTrackedView(view->child_at(i));
111 if (hot_view) 111 if (hot_view)
112 return hot_view; 112 return hot_view;
113 } 113 }
114 return NULL; 114 return NULL;
115 } 115 }
116 116
117 // Returns a CustomButton descendant of |view| that is (a) hot-tracked and
118 // (b) not equal to |hot_button|. Returns null if such descendant is not found.
119 // This allows a caller to detect when a button is set to be hot-tracked by a
120 // mouse event and its |hot_button_| indicator becomes stale.
121 CustomButton* OtherHotTrackedButton(View* view, CustomButton* hot_button) {
sky 2016/02/19 01:07:14 GetFirstHotTrackedViewDifferingFrom (which better
varkha 2016/02/23 00:41:05 Done.
122 if (!view)
123 return nullptr;
124 CustomButton* button = CustomButton::AsCustomButton(view);
125 if (button && button != hot_button && button->IsHotTracked())
126 return button;
127
128 for (int i = 0; i < view->child_count(); ++i) {
129 button = OtherHotTrackedButton(view->child_at(i), hot_button);
130 if (button)
131 return button;
132 }
133 return nullptr;
134 }
135
117 // Recurses through the child views of |view| returning the first view starting 136 // Recurses through the child views of |view| returning the first view starting
118 // at |start| that is focusable. A value of -1 for |start| indicates to start at 137 // at |start| that is focusable. A value of -1 for |start| indicates to start at
119 // the first view (if |forward| is false, iterating starts at the last view). If 138 // the first view (if |forward| is false, iterating starts at the last view). If
120 // |forward| is true the children are considered first to last, otherwise last 139 // |forward| is true the children are considered first to last, otherwise last
121 // to first. 140 // to first.
122 static View* GetFirstFocusableView(View* view, int start, bool forward) { 141 View* GetFirstFocusableView(View* view, int start, bool forward) {
123 if (forward) { 142 if (forward) {
124 for (int i = start == -1 ? 0 : start; i < view->child_count(); ++i) { 143 for (int i = start == -1 ? 0 : start; i < view->child_count(); ++i) {
125 View* deepest = GetFirstFocusableView(view->child_at(i), -1, forward); 144 View* deepest = GetFirstFocusableView(view->child_at(i), -1, forward);
126 if (deepest) 145 if (deepest)
127 return deepest; 146 return deepest;
128 } 147 }
129 } else { 148 } else {
130 for (int i = start == -1 ? view->child_count() - 1 : start; i >= 0; --i) { 149 for (int i = start == -1 ? view->child_count() - 1 : start; i >= 0; --i) {
131 View* deepest = GetFirstFocusableView(view->child_at(i), -1, forward); 150 View* deepest = GetFirstFocusableView(view->child_at(i), -1, forward);
132 if (deepest) 151 if (deepest)
133 return deepest; 152 return deepest;
134 } 153 }
135 } 154 }
136 return view->IsFocusable() ? view : NULL; 155 return view->IsFocusable() ? view : NULL;
137 } 156 }
138 157
139 // Returns the first child of |start| that is focusable. 158 // Returns the first child of |start| that is focusable.
140 static View* GetInitialFocusableView(View* start, bool forward) { 159 View* GetInitialFocusableView(View* start, bool forward) {
141 return GetFirstFocusableView(start, -1, forward); 160 return GetFirstFocusableView(start, -1, forward);
142 } 161 }
143 162
144 // Returns the next view after |start_at| that is focusable. Returns NULL if 163 // Returns the next view after |start_at| that is focusable. Returns NULL if
145 // there are no focusable children of |ancestor| after |start_at|. 164 // there are no focusable children of |ancestor| after |start_at|.
146 static View* GetNextFocusableView(View* ancestor, 165 View* GetNextFocusableView(View* ancestor, View* start_at, bool forward) {
147 View* start_at,
148 bool forward) {
149 DCHECK(ancestor->Contains(start_at)); 166 DCHECK(ancestor->Contains(start_at));
150 View* parent = start_at; 167 View* parent = start_at;
151 do { 168 do {
152 View* new_parent = parent->parent(); 169 View* new_parent = parent->parent();
153 int index = new_parent->GetIndexOf(parent); 170 int index = new_parent->GetIndexOf(parent);
154 index += forward ? 1 : -1; 171 index += forward ? 1 : -1;
155 if (forward || index != -1) { 172 if (forward || index != -1) {
156 View* next = GetFirstFocusableView(new_parent, index, forward); 173 View* next = GetFirstFocusableView(new_parent, index, forward);
157 if (next) 174 if (next)
158 return next; 175 return next;
(...skipping 773 matching lines...) Expand 10 before | Expand all | Expand 10 after
932 BuildPathsAndCalculateDiff(pending_state_.item, menu_item, &current_path, 949 BuildPathsAndCalculateDiff(pending_state_.item, menu_item, &current_path,
933 &new_path, &paths_differ_at); 950 &new_path, &paths_differ_at);
934 951
935 size_t current_size = current_path.size(); 952 size_t current_size = current_path.size();
936 size_t new_size = new_path.size(); 953 size_t new_size = new_path.size();
937 954
938 bool pending_item_changed = pending_state_.item != menu_item; 955 bool pending_item_changed = pending_state_.item != menu_item;
939 if (pending_item_changed && pending_state_.item) { 956 if (pending_item_changed && pending_state_.item) {
940 CustomButton* button = GetFirstHotTrackedView(pending_state_.item); 957 CustomButton* button = GetFirstHotTrackedView(pending_state_.item);
941 if (button) 958 if (button)
942 button->SetHotTracked(false); 959 SetHotTrackedButton(button, false);
960 }
961 CustomButton* new_hot_button = OtherHotTrackedButton(menu_item, hot_button_);
sky 2016/02/19 01:07:14 It isn't obvious to me why are you doing this. Ple
varkha 2016/02/23 00:41:05 Done.
962 if (new_hot_button) {
963 if (hot_button_)
964 SetHotTrackedButton(hot_button_, false);
965 SetHotTrackedButton(new_hot_button, true);
943 } 966 }
944 967
945 // Notify the old path it isn't selected. 968 // Notify the old path it isn't selected.
946 MenuDelegate* current_delegate = 969 MenuDelegate* current_delegate =
947 current_path.empty() ? NULL : current_path.front()->GetDelegate(); 970 current_path.empty() ? NULL : current_path.front()->GetDelegate();
948 for (size_t i = paths_differ_at; i < current_size; ++i) { 971 for (size_t i = paths_differ_at; i < current_size; ++i) {
949 if (current_delegate && 972 if (current_delegate &&
950 current_path[i]->GetType() == MenuItemView::SUBMENU) { 973 current_path[i]->GetType() == MenuItemView::SUBMENU) {
951 current_delegate->WillHideMenu(current_path[i]); 974 current_delegate->WillHideMenu(current_path[i]);
952 } 975 }
(...skipping 231 matching lines...) Expand 10 before | Expand all | Expand 10 after
1184 drop_target_(NULL), 1207 drop_target_(NULL),
1185 drop_position_(MenuDelegate::DROP_UNKNOWN), 1208 drop_position_(MenuDelegate::DROP_UNKNOWN),
1186 owner_(NULL), 1209 owner_(NULL),
1187 possible_drag_(false), 1210 possible_drag_(false),
1188 drag_in_progress_(false), 1211 drag_in_progress_(false),
1189 did_initiate_drag_(false), 1212 did_initiate_drag_(false),
1190 valid_drop_coordinates_(false), 1213 valid_drop_coordinates_(false),
1191 last_drop_operation_(MenuDelegate::DROP_UNKNOWN), 1214 last_drop_operation_(MenuDelegate::DROP_UNKNOWN),
1192 showing_submenu_(false), 1215 showing_submenu_(false),
1193 active_mouse_view_id_(ViewStorage::GetInstance()->CreateStorageID()), 1216 active_mouse_view_id_(ViewStorage::GetInstance()->CreateStorageID()),
1217 hot_button_(nullptr),
1194 delegate_(delegate), 1218 delegate_(delegate),
1195 message_loop_depth_(0), 1219 message_loop_depth_(0),
1196 closing_event_time_(base::TimeDelta()), 1220 closing_event_time_(base::TimeDelta()),
1197 menu_start_time_(base::TimeTicks()), 1221 menu_start_time_(base::TimeTicks()),
1198 async_run_(false), 1222 async_run_(false),
1199 is_combobox_(false), 1223 is_combobox_(false),
1200 item_selected_by_touch_(false), 1224 item_selected_by_touch_(false),
1201 current_mouse_event_target_(nullptr), 1225 current_mouse_event_target_(nullptr),
1202 current_mouse_pressed_state_(0), 1226 current_mouse_pressed_state_(0),
1203 message_loop_(MenuMessageLoop::Create()) { 1227 message_loop_(MenuMessageLoop::Create()) {
(...skipping 15 matching lines...) Expand all
1219 message_loop_->Run(this, owner_, nested_menu); 1243 message_loop_->Run(this, owner_, nested_menu);
1220 } 1244 }
1221 1245
1222 bool MenuController::SendAcceleratorToHotTrackedView() { 1246 bool MenuController::SendAcceleratorToHotTrackedView() {
1223 CustomButton* hot_view = GetFirstHotTrackedView(pending_state_.item); 1247 CustomButton* hot_view = GetFirstHotTrackedView(pending_state_.item);
1224 if (!hot_view) 1248 if (!hot_view)
1225 return false; 1249 return false;
1226 1250
1227 ui::Accelerator accelerator(ui::VKEY_RETURN, ui::EF_NONE); 1251 ui::Accelerator accelerator(ui::VKEY_RETURN, ui::EF_NONE);
1228 hot_view->AcceleratorPressed(accelerator); 1252 hot_view->AcceleratorPressed(accelerator);
1229 CustomButton* button = static_cast<CustomButton*>(hot_view); 1253 SetHotTrackedButton(hot_view, true);
1230 button->SetHotTracked(true);
1231 return true; 1254 return true;
1232 } 1255 }
1233 1256
1234 void MenuController::UpdateInitialLocation(const gfx::Rect& bounds, 1257 void MenuController::UpdateInitialLocation(const gfx::Rect& bounds,
1235 MenuAnchorPosition position, 1258 MenuAnchorPosition position,
1236 bool context_menu) { 1259 bool context_menu) {
1237 pending_state_.context_menu = context_menu; 1260 pending_state_.context_menu = context_menu;
1238 pending_state_.initial_bounds = bounds; 1261 pending_state_.initial_bounds = bounds;
1239 if (bounds.height() > 1) { 1262 if (bounds.height() > 1) {
1240 // Inset the bounds slightly, otherwise drag coordinates don't line up 1263 // Inset the bounds slightly, otherwise drag coordinates don't line up
(...skipping 741 matching lines...) Expand 10 before | Expand all | Expand 10 after
1982 void MenuController::IncrementSelection( 2005 void MenuController::IncrementSelection(
1983 SelectionIncrementDirectionType direction) { 2006 SelectionIncrementDirectionType direction) {
1984 MenuItemView* item = pending_state_.item; 2007 MenuItemView* item = pending_state_.item;
1985 DCHECK(item); 2008 DCHECK(item);
1986 if (pending_state_.submenu_open && item->HasSubmenu() && 2009 if (pending_state_.submenu_open && item->HasSubmenu() &&
1987 item->GetSubmenu()->IsShowing()) { 2010 item->GetSubmenu()->IsShowing()) {
1988 // A menu is selected and open, but none of its children are selected, 2011 // A menu is selected and open, but none of its children are selected,
1989 // select the first menu item that is visible and enabled. 2012 // select the first menu item that is visible and enabled.
1990 if (item->GetSubmenu()->GetMenuItemCount()) { 2013 if (item->GetSubmenu()->GetMenuItemCount()) {
1991 MenuItemView* to_select = FindInitialSelectableMenuItem(item, direction); 2014 MenuItemView* to_select = FindInitialSelectableMenuItem(item, direction);
1992 if (to_select) 2015 SetInitialHotTrackedView(to_select, direction);
1993 SetSelection(to_select, SELECTION_DEFAULT);
1994 return; 2016 return;
1995 } 2017 }
1996 } 2018 }
1997 2019
1998 if (item->has_children()) { 2020 if (item->has_children()) {
1999 CustomButton* button = GetFirstHotTrackedView(item); 2021 CustomButton* button = GetFirstHotTrackedView(item);
2000 if (button) { 2022 if (button) {
2001 button->SetHotTracked(false); 2023 SetHotTrackedButton(button, false);
2002 View* to_make_hot = GetNextFocusableView( 2024 View* hot_view = GetNextFocusableView(
2003 item, button, direction == INCREMENT_SELECTION_DOWN); 2025 item, button, direction == INCREMENT_SELECTION_DOWN);
2004 CustomButton* button_hot = CustomButton::AsCustomButton(to_make_hot); 2026 CustomButton* hot_button = CustomButton::AsCustomButton(hot_view);
2005 if (button_hot) { 2027 if (hot_button) {
2006 button_hot->SetHotTracked(true); 2028 SetHotTrackedButton(hot_button, true);
2007 return; 2029 return;
2008 } 2030 }
2009 } else { 2031 } else {
2010 View* to_make_hot = 2032 View* hot_view =
2011 GetInitialFocusableView(item, direction == INCREMENT_SELECTION_DOWN); 2033 GetInitialFocusableView(item, direction == INCREMENT_SELECTION_DOWN);
2012 CustomButton* button_hot = CustomButton::AsCustomButton(to_make_hot); 2034 CustomButton* hot_button = CustomButton::AsCustomButton(hot_view);
2013 if (button_hot) { 2035 if (hot_button) {
2014 button_hot->SetHotTracked(true); 2036 SetHotTrackedButton(hot_button, true);
2015 return; 2037 return;
2016 } 2038 }
2017 } 2039 }
2018 } 2040 }
2019 2041
2020 MenuItemView* parent = item->GetParentMenuItem(); 2042 MenuItemView* parent = item->GetParentMenuItem();
2021 if (parent) { 2043 if (parent) {
2022 int parent_count = parent->GetSubmenu()->GetMenuItemCount(); 2044 int parent_count = parent->GetSubmenu()->GetMenuItemCount();
2023 if (parent_count > 1) { 2045 if (parent_count > 1) {
2024 for (int i = 0; i < parent_count; ++i) { 2046 for (int i = 0; i < parent_count; ++i) {
2025 if (parent->GetSubmenu()->GetMenuItemAt(i) == item) { 2047 if (parent->GetSubmenu()->GetMenuItemAt(i) == item) {
2026 MenuItemView* to_select = 2048 MenuItemView* to_select =
2027 FindNextSelectableMenuItem(parent, i, direction); 2049 FindNextSelectableMenuItem(parent, i, direction);
2028 if (!to_select) 2050 SetInitialHotTrackedView(to_select, direction);
2029 break;
2030 SetSelection(to_select, SELECTION_DEFAULT);
2031 View* to_make_hot = GetInitialFocusableView(
2032 to_select, direction == INCREMENT_SELECTION_DOWN);
2033 CustomButton* button_hot = CustomButton::AsCustomButton(to_make_hot);
2034 if (button_hot)
2035 button_hot->SetHotTracked(true);
2036 break; 2051 break;
2037 } 2052 }
2038 } 2053 }
2039 } 2054 }
2040 } 2055 }
2041 } 2056 }
2042 2057
2043 MenuItemView* MenuController::FindInitialSelectableMenuItem( 2058 MenuItemView* MenuController::FindInitialSelectableMenuItem(
2044 MenuItemView* parent, 2059 MenuItemView* parent,
2045 SelectionIncrementDirectionType direction) { 2060 SelectionIncrementDirectionType direction) {
(...skipping 544 matching lines...) Expand 10 before | Expand all | Expand 10 after
2590 pending_state_.item->GetParentMenuItem() && 2605 pending_state_.item->GetParentMenuItem() &&
2591 (!pending_state_.item->HasSubmenu() || 2606 (!pending_state_.item->HasSubmenu() ||
2592 !pending_state_.item->GetSubmenu()->IsShowing())) { 2607 !pending_state_.item->GetSubmenu()->IsShowing())) {
2593 // On exit if the user hasn't selected an item with a submenu, move the 2608 // On exit if the user hasn't selected an item with a submenu, move the
2594 // selection back to the parent menu item. 2609 // selection back to the parent menu item.
2595 SetSelection(pending_state_.item->GetParentMenuItem(), 2610 SetSelection(pending_state_.item->GetParentMenuItem(),
2596 SELECTION_OPEN_SUBMENU); 2611 SELECTION_OPEN_SUBMENU);
2597 } 2612 }
2598 } 2613 }
2599 2614
2615 void MenuController::SetHotTrackedButton(CustomButton* hot_button,
2616 bool is_hot_tracked) {
2617 DCHECK(hot_button);
sky 2016/02/19 01:07:14 What happens if hot_button is deleted while the me
varkha 2016/02/23 00:41:05 You mean |hot_tracked_| (which can be passed in li
2618 hot_button->SetHotTracked(is_hot_tracked);
2619 DCHECK(is_hot_tracked || (hot_button_ == hot_button));
2620 hot_button_ = is_hot_tracked ? hot_button : nullptr;
2621 }
2622
2623 void MenuController::SetInitialHotTrackedView(
2624 MenuItemView* item,
2625 SelectionIncrementDirectionType direction) {
2626 if (!item)
2627 return;
2628 SetSelection(item, SELECTION_DEFAULT);
2629 View* hot_view =
2630 GetInitialFocusableView(item, direction == INCREMENT_SELECTION_DOWN);
2631 CustomButton* hot_button = CustomButton::AsCustomButton(hot_view);
2632 if (hot_button)
2633 SetHotTrackedButton(hot_button, true);
2634 }
2635
2600 } // namespace views 2636 } // namespace views
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698