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

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

Issue 1565013002: Don't send touch events to windows like menus when the touch occurs outside the menu bounds. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Small cleanup in the WindowEventDispatcher::RepostEvent function Created 4 years, 11 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_host.h" 5 #include "ui/views/controls/menu/menu_host.h"
6 6
7 #include "base/auto_reset.h" 7 #include "base/auto_reset.h"
8 #include "base/logging.h"
8 #include "base/trace_event/trace_event.h" 9 #include "base/trace_event/trace_event.h"
9 #include "build/build_config.h" 10 #include "build/build_config.h"
11 #include "ui/aura/window_observer.h"
10 #include "ui/events/gestures/gesture_recognizer.h" 12 #include "ui/events/gestures/gesture_recognizer.h"
11 #include "ui/gfx/path.h" 13 #include "ui/gfx/path.h"
12 #include "ui/native_theme/native_theme.h" 14 #include "ui/native_theme/native_theme.h"
13 #include "ui/views/controls/menu/menu_controller.h" 15 #include "ui/views/controls/menu/menu_controller.h"
14 #include "ui/views/controls/menu/menu_host_root_view.h" 16 #include "ui/views/controls/menu/menu_host_root_view.h"
15 #include "ui/views/controls/menu/menu_item_view.h" 17 #include "ui/views/controls/menu/menu_item_view.h"
16 #include "ui/views/controls/menu/menu_scroll_view_container.h" 18 #include "ui/views/controls/menu/menu_scroll_view_container.h"
17 #include "ui/views/controls/menu/submenu_view.h" 19 #include "ui/views/controls/menu/submenu_view.h"
18 #include "ui/views/round_rect_painter.h" 20 #include "ui/views/round_rect_painter.h"
19 #include "ui/views/widget/native_widget_private.h" 21 #include "ui/views/widget/native_widget_private.h"
20 #include "ui/views/widget/widget.h" 22 #include "ui/views/widget/widget.h"
21 23
24 #if !defined(OS_MACOSX)
25 #include "ui/aura/window.h"
26 #endif
27
22 namespace views { 28 namespace views {
23 29
30 namespace internal {
31
32 #if !defined(OS_MACOSX)
33 // This class adds itself as the pre target handler for the |window|
34 // passed in. It currently handles touch events and forwards them to the
35 // controller. Reason for this approach is views does not get raw touch
36 // events which we need to determine if a touch happened outside the bounds
37 // of the menu.
38 class PreMenuEventDispatchHandler : public ui::EventHandler,
39 aura::WindowObserver {
40 public:
41 explicit PreMenuEventDispatchHandler(const MenuController* controller,
sky 2016/01/13 16:31:13 nit: no explict
ananta 2016/01/13 21:34:47 Done.
42 SubmenuView* submenu,
43 aura::Window* window)
44 : menu_controller_(const_cast<MenuController*>(controller)),
45 submenu_(submenu),
46 window_(window) {
47 window_->AddPreTargetHandler(this);
48 window_->AddObserver(this);
49 }
50
51 ~PreMenuEventDispatchHandler() override {
52 StopObserving();
53 }
54
55 // ui::EventHandler overrides.
56 void OnTouchEvent(ui::TouchEvent* event) override {
57 menu_controller_->OnTouchEvent(submenu_, event);
58 }
59
60 // aura::WindowObserver overrides.
61 void OnWindowDestroying(aura::Window* window) override {
62 DCHECK(window_ == window);
63 StopObserving();
64 }
65
66 private:
67 void StopObserving() {
68 if (!window_)
69 return;
70 window_->RemovePreTargetHandler(this);
71 window_->RemoveObserver(this);
72 window_ = nullptr;
73 }
74
75 MenuController* menu_controller_;
76 SubmenuView* submenu_;
77 aura::Window* window_;
78
79 DISALLOW_COPY_AND_ASSIGN(PreMenuEventDispatchHandler);
80 };
81 #endif // OS_MACOSX
82
83 } // namespace internal
84
24 //////////////////////////////////////////////////////////////////////////////// 85 ////////////////////////////////////////////////////////////////////////////////
25 // MenuHost, public: 86 // MenuHost, public:
26 87
27 MenuHost::MenuHost(SubmenuView* submenu) 88 MenuHost::MenuHost(SubmenuView* submenu)
28 : submenu_(submenu), 89 : submenu_(submenu),
29 destroying_(false), 90 destroying_(false),
30 ignore_capture_lost_(false) { 91 ignore_capture_lost_(false) {
31 set_auto_release_capture(false); 92 set_auto_release_capture(false);
32 } 93 }
33 94
(...skipping 20 matching lines...) Expand all
54 params.parent = parent ? parent->GetNativeView() : NULL; 115 params.parent = parent ? parent->GetNativeView() : NULL;
55 params.bounds = bounds; 116 params.bounds = bounds;
56 #if defined(OS_WIN) 117 #if defined(OS_WIN)
57 // On Windows use the software compositor to ensure that we don't block 118 // On Windows use the software compositor to ensure that we don't block
58 // the UI thread blocking issue during command buffer creation. We can 119 // the UI thread blocking issue during command buffer creation. We can
59 // revert this change once http://crbug.com/125248 is fixed. 120 // revert this change once http://crbug.com/125248 is fixed.
60 params.force_software_compositing = true; 121 params.force_software_compositing = true;
61 #endif 122 #endif
62 Init(params); 123 Init(params);
63 124
125 #if !defined(OS_MACOSX)
126 pre_dispatch_handler_.reset(new internal::PreMenuEventDispatchHandler(
127 menu_controller, submenu_, GetNativeView()));
128 #endif
129
64 SetContentsView(contents_view); 130 SetContentsView(contents_view);
65 ShowMenuHost(do_capture); 131 ShowMenuHost(do_capture);
66 } 132 }
67 133
68 bool MenuHost::IsMenuHostVisible() { 134 bool MenuHost::IsMenuHostVisible() {
69 return IsVisible(); 135 return IsVisible();
70 } 136 }
71 137
72 void MenuHost::ShowMenuHost(bool do_capture) { 138 void MenuHost::ShowMenuHost(bool do_capture) {
73 // Doing a capture may make us get capture lost. Ignore it while we're in the 139 // Doing a capture may make us get capture lost. Ignore it while we're in the
(...skipping 12 matching lines...) Expand all
86 ignore_capture_lost_ = true; 152 ignore_capture_lost_ = true;
87 ReleaseMenuHostCapture(); 153 ReleaseMenuHostCapture();
88 Hide(); 154 Hide();
89 ignore_capture_lost_ = false; 155 ignore_capture_lost_ = false;
90 } 156 }
91 157
92 void MenuHost::DestroyMenuHost() { 158 void MenuHost::DestroyMenuHost() {
93 HideMenuHost(); 159 HideMenuHost();
94 destroying_ = true; 160 destroying_ = true;
95 static_cast<MenuHostRootView*>(GetRootView())->ClearSubmenu(); 161 static_cast<MenuHostRootView*>(GetRootView())->ClearSubmenu();
162 #if !defined(OS_MACOSX)
163 pre_dispatch_handler_.reset();
164 #endif
96 Close(); 165 Close();
97 } 166 }
98 167
99 void MenuHost::SetMenuHostBounds(const gfx::Rect& bounds) { 168 void MenuHost::SetMenuHostBounds(const gfx::Rect& bounds) {
100 SetBounds(bounds); 169 SetBounds(bounds);
101 } 170 }
102 171
103 void MenuHost::ReleaseMenuHostCapture() { 172 void MenuHost::ReleaseMenuHostCapture() {
104 if (native_widget_private()->HasCapture()) 173 if (native_widget_private()->HasCapture())
105 native_widget_private()->ReleaseCapture(); 174 native_widget_private()->ReleaseCapture();
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
166 } 235 }
167 menu_controller->OnDragComplete(should_close); 236 menu_controller->OnDragComplete(should_close);
168 237
169 // We may have lost capture in the drag and drop, but are remaining open. 238 // We may have lost capture in the drag and drop, but are remaining open.
170 // Return capture so we get MouseCaptureLost events. 239 // Return capture so we get MouseCaptureLost events.
171 if (!should_close) 240 if (!should_close)
172 native_widget_private()->SetCapture(); 241 native_widget_private()->SetCapture();
173 } 242 }
174 243
175 } // namespace views 244 } // namespace views
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698