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

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

Issue 267593005: Refactor menu controller to isolate aura dependency. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Cleanup Created 6 years, 7 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 | Annotate | Revision Log
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 #if defined(OS_WIN)
8 #include <windowsx.h>
9 #endif
10
11 #include "base/i18n/case_conversion.h" 7 #include "base/i18n/case_conversion.h"
12 #include "base/i18n/rtl.h" 8 #include "base/i18n/rtl.h"
13 #include "base/run_loop.h"
14 #include "base/strings/utf_string_conversions.h" 9 #include "base/strings/utf_string_conversions.h"
15 #include "base/time/time.h" 10 #include "base/time/time.h"
16 #include "ui/aura/client/screen_position_client.h"
17 #include "ui/aura/env.h"
18 #include "ui/aura/window.h"
19 #include "ui/aura/window_event_dispatcher.h"
20 #include "ui/aura/window_tree_host.h"
21 #include "ui/base/dragdrop/drag_utils.h" 11 #include "ui/base/dragdrop/drag_utils.h"
22 #include "ui/base/dragdrop/os_exchange_data.h" 12 #include "ui/base/dragdrop/os_exchange_data.h"
23 #include "ui/base/l10n/l10n_util.h" 13 #include "ui/base/l10n/l10n_util.h"
24 #include "ui/events/event.h" 14 #include "ui/events/event.h"
25 #include "ui/events/event_utils.h" 15 #include "ui/events/event_utils.h"
26 #include "ui/events/platform/platform_event_source.h"
27 #include "ui/events/platform/scoped_event_dispatcher.h"
28 #include "ui/gfx/canvas.h" 16 #include "ui/gfx/canvas.h"
29 #include "ui/gfx/native_widget_types.h" 17 #include "ui/gfx/native_widget_types.h"
30 #include "ui/gfx/point.h" 18 #include "ui/gfx/point.h"
31 #include "ui/gfx/screen.h" 19 #include "ui/gfx/screen.h"
32 #include "ui/gfx/vector2d.h" 20 #include "ui/gfx/vector2d.h"
33 #include "ui/native_theme/native_theme.h" 21 #include "ui/native_theme/native_theme.h"
34 #include "ui/views/controls/button/menu_button.h" 22 #include "ui/views/controls/button/menu_button.h"
35 #include "ui/views/controls/menu/menu_config.h" 23 #include "ui/views/controls/menu/menu_config.h"
36 #include "ui/views/controls/menu/menu_controller_delegate.h" 24 #include "ui/views/controls/menu/menu_controller_delegate.h"
37 #include "ui/views/controls/menu/menu_host_root_view.h" 25 #include "ui/views/controls/menu/menu_host_root_view.h"
38 #include "ui/views/controls/menu/menu_item_view.h" 26 #include "ui/views/controls/menu/menu_item_view.h"
27 #include "ui/views/controls/menu/menu_message_loop.h"
39 #include "ui/views/controls/menu/menu_scroll_view_container.h" 28 #include "ui/views/controls/menu/menu_scroll_view_container.h"
40 #include "ui/views/controls/menu/submenu_view.h" 29 #include "ui/views/controls/menu/submenu_view.h"
41 #include "ui/views/drag_utils.h" 30 #include "ui/views/drag_utils.h"
42 #include "ui/views/focus/view_storage.h" 31 #include "ui/views/focus/view_storage.h"
43 #include "ui/views/mouse_constants.h" 32 #include "ui/views/mouse_constants.h"
44 #include "ui/views/view.h" 33 #include "ui/views/view.h"
45 #include "ui/views/view_constants.h" 34 #include "ui/views/view_constants.h"
46 #include "ui/views/views_delegate.h" 35 #include "ui/views/views_delegate.h"
47 #include "ui/views/widget/root_view.h" 36 #include "ui/views/widget/root_view.h"
48 #include "ui/views/widget/tooltip_manager.h" 37 #include "ui/views/widget/tooltip_manager.h"
49 #include "ui/views/widget/widget.h" 38 #include "ui/views/widget/widget.h"
50 #include "ui/wm/public/activation_change_observer.h"
51 #include "ui/wm/public/activation_client.h"
52 #include "ui/wm/public/dispatcher_client.h"
53 #include "ui/wm/public/drag_drop_client.h"
54 39
55 #if defined(OS_WIN)
56 #include "ui/base/win/internal_constants.h"
57 #include "ui/views/controls/menu/menu_message_pump_dispatcher_win.h"
58 #include "ui/views/win/hwnd_util.h"
59 #else
60 #include "ui/views/controls/menu/menu_event_dispatcher_linux.h"
61 #endif
62
63 using aura::client::ScreenPositionClient;
64 using base::Time; 40 using base::Time;
65 using base::TimeDelta; 41 using base::TimeDelta;
66 using ui::OSExchangeData; 42 using ui::OSExchangeData;
67 43
68 // Period of the scroll timer (in milliseconds). 44 // Period of the scroll timer (in milliseconds).
69 static const int kScrollTimerMS = 30; 45 static const int kScrollTimerMS = 30;
70 46
71 // Amount of time from when the drop exits the menu and the menu is hidden. 47 // Amount of time from when the drop exits the menu and the menu is hidden.
72 static const int kCloseOnExitTime = 1200; 48 static const int kCloseOnExitTime = 1200;
73 49
(...skipping 27 matching lines...) Expand all
101 // Returns true if |menu| doesn't have a mnemonic and first character of the its 77 // Returns true if |menu| doesn't have a mnemonic and first character of the its
102 // title is |key|. 78 // title is |key|.
103 bool TitleMatchesMnemonic(MenuItemView* menu, base::char16 key) { 79 bool TitleMatchesMnemonic(MenuItemView* menu, base::char16 key) {
104 if (menu->GetMnemonic()) 80 if (menu->GetMnemonic())
105 return false; 81 return false;
106 82
107 base::string16 lower_title = base::i18n::ToLower(menu->title()); 83 base::string16 lower_title = base::i18n::ToLower(menu->title());
108 return !lower_title.empty() && lower_title[0] == key; 84 return !lower_title.empty() && lower_title[0] == key;
109 } 85 }
110 86
111 aura::Window* GetOwnerRootWindow(views::Widget* owner) {
112 return owner ? owner->GetNativeWindow()->GetRootWindow() : NULL;
113 }
114
115 // ActivationChangeObserverImpl is used to observe activation changes and close
116 // the menu. Additionally it listens for the root window to be destroyed and
117 // cancel the menu as well.
118 class ActivationChangeObserverImpl
119 : public aura::client::ActivationChangeObserver,
120 public aura::WindowObserver,
121 public ui::EventHandler {
122 public:
123 ActivationChangeObserverImpl(MenuController* controller, aura::Window* root)
124 : controller_(controller),
125 root_(root) {
126 aura::client::GetActivationClient(root_)->AddObserver(this);
127 root_->AddObserver(this);
128 root_->AddPreTargetHandler(this);
129 }
130
131 virtual ~ActivationChangeObserverImpl() {
132 Cleanup();
133 }
134
135 // aura::client::ActivationChangeObserver:
136 virtual void OnWindowActivated(aura::Window* gained_active,
137 aura::Window* lost_active) OVERRIDE {
138 if (!controller_->drag_in_progress())
139 controller_->CancelAll();
140 }
141
142 // aura::WindowObserver:
143 virtual void OnWindowDestroying(aura::Window* window) OVERRIDE {
144 Cleanup();
145 }
146
147 // ui::EventHandler:
148 virtual void OnCancelMode(ui::CancelModeEvent* event) OVERRIDE {
149 controller_->CancelAll();
150 }
151
152 private:
153 void Cleanup() {
154 if (!root_)
155 return;
156 // The ActivationClient may have been destroyed by the time we get here.
157 aura::client::ActivationClient* client =
158 aura::client::GetActivationClient(root_);
159 if (client)
160 client->RemoveObserver(this);
161 root_->RemovePreTargetHandler(this);
162 root_->RemoveObserver(this);
163 root_ = NULL;
164 }
165
166 MenuController* controller_;
167 aura::Window* root_;
168
169 DISALLOW_COPY_AND_ASSIGN(ActivationChangeObserverImpl);
170 };
171
172 } // namespace 87 } // namespace
173 88
174 // Returns the first descendant of |view| that is hot tracked. 89 // Returns the first descendant of |view| that is hot tracked.
175 static CustomButton* GetFirstHotTrackedView(View* view) { 90 static CustomButton* GetFirstHotTrackedView(View* view) {
176 if (!view) 91 if (!view)
177 return NULL; 92 return NULL;
178 CustomButton* button = CustomButton::AsCustomButton(view); 93 CustomButton* button = CustomButton::AsCustomButton(view);
179 if (button) { 94 if (button) {
180 if (button->IsHotTracked()) 95 if (button->IsHotTracked())
181 return button; 96 return button;
(...skipping 256 matching lines...) Expand 10 before | Expand all | Expand 10 after
438 // Make sure Chrome doesn't attempt to shut down while the menu is showing. 353 // Make sure Chrome doesn't attempt to shut down while the menu is showing.
439 if (ViewsDelegate::views_delegate) 354 if (ViewsDelegate::views_delegate)
440 ViewsDelegate::views_delegate->AddRef(); 355 ViewsDelegate::views_delegate->AddRef();
441 356
442 // We need to turn on nestable tasks as in some situations (pressing alt-f for 357 // We need to turn on nestable tasks as in some situations (pressing alt-f for
443 // one) the menus are run from a task. If we don't do this and are invoked 358 // one) the menus are run from a task. If we don't do this and are invoked
444 // from a task none of the tasks we schedule are processed and the menu 359 // from a task none of the tasks we schedule are processed and the menu
445 // appears totally broken. 360 // appears totally broken.
446 message_loop_depth_++; 361 message_loop_depth_++;
447 DCHECK_LE(message_loop_depth_, 2); 362 DCHECK_LE(message_loop_depth_, 2);
448 RunMessageLoop(nested_menu); 363 if (!message_loop_)
364 message_loop_.reset(MenuMessageLoop::Create());
tapted 2014/05/06 00:02:46 I think maybe do this unconditionally in the const
Andre 2014/05/07 23:12:39 Done.
365 message_loop_->Run(this, owner_, nested_menu);
449 message_loop_depth_--; 366 message_loop_depth_--;
450 367
451 if (ViewsDelegate::views_delegate) 368 if (ViewsDelegate::views_delegate)
452 ViewsDelegate::views_delegate->ReleaseRef(); 369 ViewsDelegate::views_delegate->ReleaseRef();
453 370
454 // Close any open menus. 371 // Close any open menus.
455 SetSelection(NULL, SELECTION_UPDATE_IMMEDIATELY | SELECTION_EXIT); 372 SetSelection(NULL, SELECTION_UPDATE_IMMEDIATELY | SELECTION_EXIT);
456 373
457 #if defined(OS_WIN) 374 #if defined(OS_WIN)
458 // On Windows, if we select the menu item by touch and if the window at the 375 // On Windows, if we select the menu item by touch and if the window at the
(...skipping 708 matching lines...) Expand 10 before | Expand all | Expand 10 after
1167 MenuController::~MenuController() { 1084 MenuController::~MenuController() {
1168 DCHECK(!showing_); 1085 DCHECK(!showing_);
1169 if (owner_) 1086 if (owner_)
1170 owner_->RemoveObserver(this); 1087 owner_->RemoveObserver(this);
1171 if (active_instance_ == this) 1088 if (active_instance_ == this)
1172 active_instance_ = NULL; 1089 active_instance_ = NULL;
1173 StopShowTimer(); 1090 StopShowTimer();
1174 StopCancelAllTimer(); 1091 StopCancelAllTimer();
1175 } 1092 }
1176 1093
1177 #if defined(OS_WIN)
1178 void MenuController::RunMessageLoop(bool nested_menu) {
1179 internal::MenuMessagePumpDispatcher nested_dispatcher(this);
1180
1181 // |owner_| may be NULL.
1182 aura::Window* root = GetOwnerRootWindow(owner_);
1183 if (root) {
1184 scoped_ptr<ActivationChangeObserverImpl> observer;
1185 if (!nested_menu)
1186 observer.reset(new ActivationChangeObserverImpl(this, root));
1187 aura::client::GetDispatcherClient(root)
1188 ->RunWithDispatcher(&nested_dispatcher);
1189 } else {
1190 base::MessageLoopForUI* loop = base::MessageLoopForUI::current();
1191 base::MessageLoop::ScopedNestableTaskAllower allow(loop);
1192 base::RunLoop run_loop(&nested_dispatcher);
1193 run_loop.Run();
1194 }
1195 }
1196 #else
1197 void MenuController::RunMessageLoop(bool nested_menu) {
1198 internal::MenuEventDispatcher event_dispatcher(this);
1199 scoped_ptr<ui::ScopedEventDispatcher> old_dispatcher =
1200 nested_dispatcher_.Pass();
1201 if (ui::PlatformEventSource::GetInstance()) {
1202 nested_dispatcher_ =
1203 ui::PlatformEventSource::GetInstance()->OverrideDispatcher(
1204 &event_dispatcher);
1205 }
1206 // |owner_| may be NULL.
1207 aura::Window* root = GetOwnerRootWindow(owner_);
1208 if (root) {
1209 scoped_ptr<ActivationChangeObserverImpl> observer;
1210 if (!nested_menu)
1211 observer.reset(new ActivationChangeObserverImpl(this, root));
1212 aura::client::GetDispatcherClient(root)->RunWithDispatcher(NULL);
1213 } else {
1214 base::MessageLoopForUI* loop = base::MessageLoopForUI::current();
1215 base::MessageLoop::ScopedNestableTaskAllower allow(loop);
1216 base::RunLoop run_loop;
1217 run_loop.Run();
1218 }
1219 nested_dispatcher_ = old_dispatcher.Pass();
1220 }
1221 #endif
1222
1223 MenuController::SendAcceleratorResultType 1094 MenuController::SendAcceleratorResultType
1224 MenuController::SendAcceleratorToHotTrackedView() { 1095 MenuController::SendAcceleratorToHotTrackedView() {
1225 CustomButton* hot_view = GetFirstHotTrackedView(pending_state_.item); 1096 CustomButton* hot_view = GetFirstHotTrackedView(pending_state_.item);
1226 if (!hot_view) 1097 if (!hot_view)
1227 return ACCELERATOR_NOT_PROCESSED; 1098 return ACCELERATOR_NOT_PROCESSED;
1228 1099
1229 ui::Accelerator accelerator(ui::VKEY_RETURN, ui::EF_NONE); 1100 ui::Accelerator accelerator(ui::VKEY_RETURN, ui::EF_NONE);
1230 hot_view->AcceleratorPressed(accelerator); 1101 hot_view->AcceleratorPressed(accelerator);
1231 CustomButton* button = static_cast<CustomButton*>(hot_view); 1102 CustomButton* button = static_cast<CustomButton*>(hot_view);
1232 button->SetHotTracked(true); 1103 button->SetHotTracked(true);
(...skipping 990 matching lines...) Expand 10 before | Expand all | Expand 10 after
2223 WPARAM target = client_area ? event.native_event().wParam : nc_hit_result; 2094 WPARAM target = client_area ? event.native_event().wParam : nc_hit_result;
2224 LPARAM window_coords = MAKELPARAM(window_x, window_y); 2095 LPARAM window_coords = MAKELPARAM(window_x, window_y);
2225 PostMessage(target_window, event_type, target, window_coords); 2096 PostMessage(target_window, event_type, target, window_coords);
2226 return; 2097 return;
2227 } 2098 }
2228 #endif 2099 #endif
2229 // Non-Windows Aura or |window| is in metro mode. 2100 // Non-Windows Aura or |window| is in metro mode.
2230 if (!window) 2101 if (!window)
2231 return; 2102 return;
2232 2103
2233 aura::Window* root = window->GetRootWindow(); 2104 message_loop_->RepostEventToWindow(event, window, screen_loc);
2234 ScreenPositionClient* spc = aura::client::GetScreenPositionClient(root);
2235 if (!spc)
2236 return;
2237
2238 gfx::Point root_loc(screen_loc);
2239 spc->ConvertPointFromScreen(root, &root_loc);
2240
2241 ui::MouseEvent clone(static_cast<const ui::MouseEvent&>(event));
2242 clone.set_location(root_loc);
2243 clone.set_root_location(root_loc);
2244 root->GetHost()->dispatcher()->RepostEvent(clone);
2245 } 2105 }
2246 2106
2247 void MenuController::SetDropMenuItem( 2107 void MenuController::SetDropMenuItem(
2248 MenuItemView* new_target, 2108 MenuItemView* new_target,
2249 MenuDelegate::DropPosition new_position) { 2109 MenuDelegate::DropPosition new_position) {
2250 if (new_target == drop_target_ && new_position == drop_position_) 2110 if (new_target == drop_target_ && new_position == drop_position_)
2251 return; 2111 return;
2252 2112
2253 if (drop_target_) { 2113 if (drop_target_) {
2254 drop_target_->GetParentMenuItem()->GetSubmenu()->SetDropMenuItem( 2114 drop_target_->GetParentMenuItem()->GetSubmenu()->SetDropMenuItem(
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after
2369 exit_type_ = type; 2229 exit_type_ = type;
2370 // Exit nested message loops as soon as possible. We do this as 2230 // Exit nested message loops as soon as possible. We do this as
2371 // MessagePumpDispatcher is only invoked before native events, which means 2231 // MessagePumpDispatcher is only invoked before native events, which means
2372 // its entirely possible for a Widget::CloseNow() task to be processed before 2232 // its entirely possible for a Widget::CloseNow() task to be processed before
2373 // the next native message. We quite the nested message loop as soon as 2233 // the next native message. We quite the nested message loop as soon as
2374 // possible to avoid having deleted views classes (such as widgets and 2234 // possible to avoid having deleted views classes (such as widgets and
2375 // rootviews) on the stack when the nested message loop stops. 2235 // rootviews) on the stack when the nested message loop stops.
2376 // 2236 //
2377 // It's safe to invoke QuitNestedMessageLoop() multiple times, it only effects 2237 // It's safe to invoke QuitNestedMessageLoop() multiple times, it only effects
2378 // the current loop. 2238 // the current loop.
2379 bool quit_now = ShouldQuitNow() && exit_type_ != EXIT_NONE && 2239 bool quit_now = message_loop_->ShouldQuitNow() && exit_type_ != EXIT_NONE &&
2380 message_loop_depth_; 2240 message_loop_depth_;
2381 2241
2382 if (quit_now) { 2242 if (quit_now)
2383 if (owner_) { 2243 message_loop_->QuitNow();
2384 aura::Window* root = owner_->GetNativeWindow()->GetRootWindow();
2385 aura::client::GetDispatcherClient(root)->QuitNestedMessageLoop();
2386 } else {
2387 base::MessageLoop::current()->QuitNow();
2388 }
2389 // Restore the previous dispatcher.
2390 nested_dispatcher_.reset();
2391 }
2392 }
2393
2394 bool MenuController::ShouldQuitNow() const {
2395 aura::Window* root = GetOwnerRootWindow(owner_);
2396 return !aura::client::GetDragDropClient(root) ||
2397 !aura::client::GetDragDropClient(root)->IsDragDropInProgress();
2398 } 2244 }
2399 2245
2400 void MenuController::HandleMouseLocation(SubmenuView* source, 2246 void MenuController::HandleMouseLocation(SubmenuView* source,
2401 const gfx::Point& mouse_location) { 2247 const gfx::Point& mouse_location) {
2402 if (showing_submenu_) 2248 if (showing_submenu_)
2403 return; 2249 return;
2404 2250
2405 // Ignore mouse events if we're closing the menu. 2251 // Ignore mouse events if we're closing the menu.
2406 if (exit_type_ != EXIT_NONE) 2252 if (exit_type_ != EXIT_NONE)
2407 return; 2253 return;
(...skipping 15 matching lines...) Expand all
2423 (!pending_state_.item->HasSubmenu() || 2269 (!pending_state_.item->HasSubmenu() ||
2424 !pending_state_.item->GetSubmenu()->IsShowing())) { 2270 !pending_state_.item->GetSubmenu()->IsShowing())) {
2425 // On exit if the user hasn't selected an item with a submenu, move the 2271 // On exit if the user hasn't selected an item with a submenu, move the
2426 // selection back to the parent menu item. 2272 // selection back to the parent menu item.
2427 SetSelection(pending_state_.item->GetParentMenuItem(), 2273 SetSelection(pending_state_.item->GetParentMenuItem(),
2428 SELECTION_OPEN_SUBMENU); 2274 SELECTION_OPEN_SUBMENU);
2429 } 2275 }
2430 } 2276 }
2431 2277
2432 gfx::Screen* MenuController::GetScreen() { 2278 gfx::Screen* MenuController::GetScreen() {
2433 aura::Window* root = GetOwnerRootWindow(owner_); 2279 Widget* root = owner_ ? owner_->GetTopLevelWidget() : NULL;
2434 return root ? gfx::Screen::GetScreenFor(root) 2280 return root ? gfx::Screen::GetScreenFor(root->GetNativeView())
2435 : gfx::Screen::GetNativeScreen(); 2281 : gfx::Screen::GetNativeScreen();
2436 } 2282 }
2437 2283
2438 } // namespace views 2284 } // namespace views
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698