OLD | NEW |
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) | 40 #if defined(OS_WIN) |
56 #include "ui/base/win/internal_constants.h" | 41 #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" | 42 #include "ui/views/win/hwnd_util.h" |
59 #else | |
60 #include "ui/views/controls/menu/menu_event_dispatcher_linux.h" | |
61 #endif | 43 #endif |
62 | 44 |
63 using aura::client::ScreenPositionClient; | |
64 using base::Time; | 45 using base::Time; |
65 using base::TimeDelta; | 46 using base::TimeDelta; |
66 using ui::OSExchangeData; | 47 using ui::OSExchangeData; |
67 | 48 |
68 // Period of the scroll timer (in milliseconds). | 49 // Period of the scroll timer (in milliseconds). |
69 static const int kScrollTimerMS = 30; | 50 static const int kScrollTimerMS = 30; |
70 | 51 |
71 // Amount of time from when the drop exits the menu and the menu is hidden. | 52 // Amount of time from when the drop exits the menu and the menu is hidden. |
72 static const int kCloseOnExitTime = 1200; | 53 static const int kCloseOnExitTime = 1200; |
73 | 54 |
(...skipping 27 matching lines...) Expand all Loading... |
101 // Returns true if |menu| doesn't have a mnemonic and first character of the its | 82 // Returns true if |menu| doesn't have a mnemonic and first character of the its |
102 // title is |key|. | 83 // title is |key|. |
103 bool TitleMatchesMnemonic(MenuItemView* menu, base::char16 key) { | 84 bool TitleMatchesMnemonic(MenuItemView* menu, base::char16 key) { |
104 if (menu->GetMnemonic()) | 85 if (menu->GetMnemonic()) |
105 return false; | 86 return false; |
106 | 87 |
107 base::string16 lower_title = base::i18n::ToLower(menu->title()); | 88 base::string16 lower_title = base::i18n::ToLower(menu->title()); |
108 return !lower_title.empty() && lower_title[0] == key; | 89 return !lower_title.empty() && lower_title[0] == key; |
109 } | 90 } |
110 | 91 |
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 | 92 } // namespace |
173 | 93 |
174 // Returns the first descendant of |view| that is hot tracked. | 94 // Returns the first descendant of |view| that is hot tracked. |
175 static CustomButton* GetFirstHotTrackedView(View* view) { | 95 static CustomButton* GetFirstHotTrackedView(View* view) { |
176 if (!view) | 96 if (!view) |
177 return NULL; | 97 return NULL; |
178 CustomButton* button = CustomButton::AsCustomButton(view); | 98 CustomButton* button = CustomButton::AsCustomButton(view); |
179 if (button) { | 99 if (button) { |
180 if (button->IsHotTracked()) | 100 if (button->IsHotTracked()) |
181 return button; | 101 return button; |
(...skipping 687 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
869 View::ConvertPointFromScreen( | 789 View::ConvertPointFromScreen( |
870 root_submenu->GetWidget()->GetRootView(), &point); | 790 root_submenu->GetWidget()->GetRootView(), &point); |
871 HandleMouseLocation(submenu, point); | 791 HandleMouseLocation(submenu, point); |
872 } | 792 } |
873 } | 793 } |
874 | 794 |
875 void MenuController::OnWidgetDestroying(Widget* widget) { | 795 void MenuController::OnWidgetDestroying(Widget* widget) { |
876 DCHECK_EQ(owner_, widget); | 796 DCHECK_EQ(owner_, widget); |
877 owner_->RemoveObserver(this); | 797 owner_->RemoveObserver(this); |
878 owner_ = NULL; | 798 owner_ = NULL; |
| 799 message_loop_->ClearOwner(); |
879 } | 800 } |
880 | 801 |
881 // static | 802 // static |
882 void MenuController::TurnOffMenuSelectionHoldForTest() { | 803 void MenuController::TurnOffMenuSelectionHoldForTest() { |
883 menu_selection_hold_time_ms = -1; | 804 menu_selection_hold_time_ms = -1; |
884 } | 805 } |
885 | 806 |
886 void MenuController::SetSelection(MenuItemView* menu_item, | 807 void MenuController::SetSelection(MenuItemView* menu_item, |
887 int selection_types) { | 808 int selection_types) { |
888 size_t paths_differ_at = 0; | 809 size_t paths_differ_at = 0; |
(...skipping 264 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1153 last_drop_operation_(MenuDelegate::DROP_UNKNOWN), | 1074 last_drop_operation_(MenuDelegate::DROP_UNKNOWN), |
1154 showing_submenu_(false), | 1075 showing_submenu_(false), |
1155 menu_button_(NULL), | 1076 menu_button_(NULL), |
1156 active_mouse_view_id_(ViewStorage::GetInstance()->CreateStorageID()), | 1077 active_mouse_view_id_(ViewStorage::GetInstance()->CreateStorageID()), |
1157 delegate_(delegate), | 1078 delegate_(delegate), |
1158 message_loop_depth_(0), | 1079 message_loop_depth_(0), |
1159 menu_config_(theme), | 1080 menu_config_(theme), |
1160 closing_event_time_(base::TimeDelta()), | 1081 closing_event_time_(base::TimeDelta()), |
1161 menu_start_time_(base::TimeTicks()), | 1082 menu_start_time_(base::TimeTicks()), |
1162 is_combobox_(false), | 1083 is_combobox_(false), |
1163 item_selected_by_touch_(false) { | 1084 item_selected_by_touch_(false), |
| 1085 message_loop_(MenuMessageLoop::Create()) { |
1164 active_instance_ = this; | 1086 active_instance_ = this; |
1165 } | 1087 } |
1166 | 1088 |
1167 MenuController::~MenuController() { | 1089 MenuController::~MenuController() { |
1168 DCHECK(!showing_); | 1090 DCHECK(!showing_); |
1169 if (owner_) | 1091 if (owner_) |
1170 owner_->RemoveObserver(this); | 1092 owner_->RemoveObserver(this); |
1171 if (active_instance_ == this) | 1093 if (active_instance_ == this) |
1172 active_instance_ = NULL; | 1094 active_instance_ = NULL; |
1173 StopShowTimer(); | 1095 StopShowTimer(); |
1174 StopCancelAllTimer(); | 1096 StopCancelAllTimer(); |
1175 } | 1097 } |
1176 | 1098 |
1177 #if defined(OS_WIN) | |
1178 void MenuController::RunMessageLoop(bool nested_menu) { | 1099 void MenuController::RunMessageLoop(bool nested_menu) { |
1179 internal::MenuMessagePumpDispatcher nested_dispatcher(this); | 1100 message_loop_->Run(this, owner_, nested_menu); |
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 } | 1101 } |
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 | 1102 |
1223 MenuController::SendAcceleratorResultType | 1103 MenuController::SendAcceleratorResultType |
1224 MenuController::SendAcceleratorToHotTrackedView() { | 1104 MenuController::SendAcceleratorToHotTrackedView() { |
1225 CustomButton* hot_view = GetFirstHotTrackedView(pending_state_.item); | 1105 CustomButton* hot_view = GetFirstHotTrackedView(pending_state_.item); |
1226 if (!hot_view) | 1106 if (!hot_view) |
1227 return ACCELERATOR_NOT_PROCESSED; | 1107 return ACCELERATOR_NOT_PROCESSED; |
1228 | 1108 |
1229 ui::Accelerator accelerator(ui::VKEY_RETURN, ui::EF_NONE); | 1109 ui::Accelerator accelerator(ui::VKEY_RETURN, ui::EF_NONE); |
1230 hot_view->AcceleratorPressed(accelerator); | 1110 hot_view->AcceleratorPressed(accelerator); |
1231 CustomButton* button = static_cast<CustomButton*>(hot_view); | 1111 CustomButton* button = static_cast<CustomButton*>(hot_view); |
(...skipping 991 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2223 WPARAM target = client_area ? event.native_event().wParam : nc_hit_result; | 2103 WPARAM target = client_area ? event.native_event().wParam : nc_hit_result; |
2224 LPARAM window_coords = MAKELPARAM(window_x, window_y); | 2104 LPARAM window_coords = MAKELPARAM(window_x, window_y); |
2225 PostMessage(target_window, event_type, target, window_coords); | 2105 PostMessage(target_window, event_type, target, window_coords); |
2226 return; | 2106 return; |
2227 } | 2107 } |
2228 #endif | 2108 #endif |
2229 // Non-Windows Aura or |window| is in metro mode. | 2109 // Non-Windows Aura or |window| is in metro mode. |
2230 if (!window) | 2110 if (!window) |
2231 return; | 2111 return; |
2232 | 2112 |
2233 aura::Window* root = window->GetRootWindow(); | 2113 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 } | 2114 } |
2246 | 2115 |
2247 void MenuController::SetDropMenuItem( | 2116 void MenuController::SetDropMenuItem( |
2248 MenuItemView* new_target, | 2117 MenuItemView* new_target, |
2249 MenuDelegate::DropPosition new_position) { | 2118 MenuDelegate::DropPosition new_position) { |
2250 if (new_target == drop_target_ && new_position == drop_position_) | 2119 if (new_target == drop_target_ && new_position == drop_position_) |
2251 return; | 2120 return; |
2252 | 2121 |
2253 if (drop_target_) { | 2122 if (drop_target_) { |
2254 drop_target_->GetParentMenuItem()->GetSubmenu()->SetDropMenuItem( | 2123 drop_target_->GetParentMenuItem()->GetSubmenu()->SetDropMenuItem( |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2369 exit_type_ = type; | 2238 exit_type_ = type; |
2370 // Exit nested message loops as soon as possible. We do this as | 2239 // Exit nested message loops as soon as possible. We do this as |
2371 // MessagePumpDispatcher is only invoked before native events, which means | 2240 // MessagePumpDispatcher is only invoked before native events, which means |
2372 // its entirely possible for a Widget::CloseNow() task to be processed before | 2241 // 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 | 2242 // 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 | 2243 // possible to avoid having deleted views classes (such as widgets and |
2375 // rootviews) on the stack when the nested message loop stops. | 2244 // rootviews) on the stack when the nested message loop stops. |
2376 // | 2245 // |
2377 // It's safe to invoke QuitNestedMessageLoop() multiple times, it only effects | 2246 // It's safe to invoke QuitNestedMessageLoop() multiple times, it only effects |
2378 // the current loop. | 2247 // the current loop. |
2379 bool quit_now = ShouldQuitNow() && exit_type_ != EXIT_NONE && | 2248 bool quit_now = message_loop_->ShouldQuitNow() && exit_type_ != EXIT_NONE && |
2380 message_loop_depth_; | 2249 message_loop_depth_; |
2381 if (quit_now) | 2250 if (quit_now) |
2382 TerminateNestedMessageLoop(); | 2251 TerminateNestedMessageLoop(); |
2383 } | 2252 } |
2384 | 2253 |
2385 void MenuController::TerminateNestedMessageLoop() { | 2254 void MenuController::TerminateNestedMessageLoop() { |
2386 if (owner_) { | 2255 message_loop_->QuitNow(); |
2387 aura::Window* root = owner_->GetNativeWindow()->GetRootWindow(); | |
2388 aura::client::GetDispatcherClient(root)->QuitNestedMessageLoop(); | |
2389 } else { | |
2390 base::MessageLoop::current()->QuitNow(); | |
2391 } | |
2392 // Restore the previous dispatcher. | |
2393 nested_dispatcher_.reset(); | |
2394 } | |
2395 | |
2396 bool MenuController::ShouldQuitNow() const { | |
2397 aura::Window* root = GetOwnerRootWindow(owner_); | |
2398 return !aura::client::GetDragDropClient(root) || | |
2399 !aura::client::GetDragDropClient(root)->IsDragDropInProgress(); | |
2400 } | 2256 } |
2401 | 2257 |
2402 void MenuController::HandleMouseLocation(SubmenuView* source, | 2258 void MenuController::HandleMouseLocation(SubmenuView* source, |
2403 const gfx::Point& mouse_location) { | 2259 const gfx::Point& mouse_location) { |
2404 if (showing_submenu_) | 2260 if (showing_submenu_) |
2405 return; | 2261 return; |
2406 | 2262 |
2407 // Ignore mouse events if we're closing the menu. | 2263 // Ignore mouse events if we're closing the menu. |
2408 if (exit_type_ != EXIT_NONE) | 2264 if (exit_type_ != EXIT_NONE) |
2409 return; | 2265 return; |
(...skipping 15 matching lines...) Expand all Loading... |
2425 (!pending_state_.item->HasSubmenu() || | 2281 (!pending_state_.item->HasSubmenu() || |
2426 !pending_state_.item->GetSubmenu()->IsShowing())) { | 2282 !pending_state_.item->GetSubmenu()->IsShowing())) { |
2427 // On exit if the user hasn't selected an item with a submenu, move the | 2283 // On exit if the user hasn't selected an item with a submenu, move the |
2428 // selection back to the parent menu item. | 2284 // selection back to the parent menu item. |
2429 SetSelection(pending_state_.item->GetParentMenuItem(), | 2285 SetSelection(pending_state_.item->GetParentMenuItem(), |
2430 SELECTION_OPEN_SUBMENU); | 2286 SELECTION_OPEN_SUBMENU); |
2431 } | 2287 } |
2432 } | 2288 } |
2433 | 2289 |
2434 gfx::Screen* MenuController::GetScreen() { | 2290 gfx::Screen* MenuController::GetScreen() { |
2435 aura::Window* root = GetOwnerRootWindow(owner_); | 2291 Widget* root = owner_ ? owner_->GetTopLevelWidget() : NULL; |
2436 return root ? gfx::Screen::GetScreenFor(root) | 2292 return root ? gfx::Screen::GetScreenFor(root->GetNativeView()) |
2437 : gfx::Screen::GetNativeScreen(); | 2293 : gfx::Screen::GetNativeScreen(); |
2438 } | 2294 } |
2439 | 2295 |
2440 } // namespace views | 2296 } // namespace views |
OLD | NEW |