| 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_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/logging.h" |
| 9 #include "base/trace_event/trace_event.h" | 9 #include "base/trace_event/trace_event.h" |
| 10 #include "build/build_config.h" | 10 #include "build/build_config.h" |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 71 window_->RemoveObserver(this); | 71 window_->RemoveObserver(this); |
| 72 window_ = nullptr; | 72 window_ = nullptr; |
| 73 } | 73 } |
| 74 | 74 |
| 75 MenuController* menu_controller_; | 75 MenuController* menu_controller_; |
| 76 SubmenuView* submenu_; | 76 SubmenuView* submenu_; |
| 77 aura::Window* window_; | 77 aura::Window* window_; |
| 78 | 78 |
| 79 DISALLOW_COPY_AND_ASSIGN(PreMenuEventDispatchHandler); | 79 DISALLOW_COPY_AND_ASSIGN(PreMenuEventDispatchHandler); |
| 80 }; | 80 }; |
| 81 |
| 82 void TransferGesture(Widget* source, Widget* target) { |
| 83 ui::GestureRecognizer::Get()->TransferEventsTo( |
| 84 source->GetNativeView(), target->GetNativeView(), |
| 85 ui::GestureRecognizer::ShouldCancelTouches::DontCancel); |
| 86 } |
| 81 #endif // OS_MACOSX | 87 #endif // OS_MACOSX |
| 82 | 88 |
| 83 } // namespace internal | 89 } // namespace internal |
| 84 | 90 |
| 85 //////////////////////////////////////////////////////////////////////////////// | 91 //////////////////////////////////////////////////////////////////////////////// |
| 86 // MenuHost, public: | 92 // MenuHost, public: |
| 87 | 93 |
| 88 MenuHost::MenuHost(SubmenuView* submenu) | 94 MenuHost::MenuHost(SubmenuView* submenu) |
| 89 : submenu_(submenu), | 95 : submenu_(submenu), |
| 90 destroying_(false), | 96 destroying_(false), |
| 91 ignore_capture_lost_(false) { | 97 ignore_capture_lost_(false) { |
| 92 set_auto_release_capture(false); | 98 set_auto_release_capture(false); |
| 93 } | 99 } |
| 94 | 100 |
| 95 MenuHost::~MenuHost() { | 101 MenuHost::~MenuHost() { |
| 102 if (owner_) |
| 103 owner_->RemoveObserver(this); |
| 96 } | 104 } |
| 97 | 105 |
| 98 void MenuHost::InitMenuHost(Widget* parent, | 106 void MenuHost::InitMenuHost(Widget* parent, |
| 99 const gfx::Rect& bounds, | 107 const gfx::Rect& bounds, |
| 100 View* contents_view, | 108 View* contents_view, |
| 101 bool do_capture) { | 109 bool do_capture) { |
| 102 TRACE_EVENT0("views", "MenuHost::InitMenuHost"); | 110 TRACE_EVENT0("views", "MenuHost::InitMenuHost"); |
| 103 Widget::InitParams params(Widget::InitParams::TYPE_MENU); | 111 Widget::InitParams params(Widget::InitParams::TYPE_MENU); |
| 104 const MenuController* menu_controller = | 112 const MenuController* menu_controller = |
| 105 submenu_->GetMenuItem()->GetMenuController(); | 113 submenu_->GetMenuItem()->GetMenuController(); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 120 // revert this change once http://crbug.com/125248 is fixed. | 128 // revert this change once http://crbug.com/125248 is fixed. |
| 121 params.force_software_compositing = true; | 129 params.force_software_compositing = true; |
| 122 #endif | 130 #endif |
| 123 Init(params); | 131 Init(params); |
| 124 | 132 |
| 125 #if !defined(OS_MACOSX) | 133 #if !defined(OS_MACOSX) |
| 126 pre_dispatch_handler_.reset(new internal::PreMenuEventDispatchHandler( | 134 pre_dispatch_handler_.reset(new internal::PreMenuEventDispatchHandler( |
| 127 menu_controller, submenu_, GetNativeView())); | 135 menu_controller, submenu_, GetNativeView())); |
| 128 #endif | 136 #endif |
| 129 | 137 |
| 138 if (owner_) |
| 139 owner_->RemoveObserver(this); |
| 140 owner_ = parent; |
| 141 if (owner_) |
| 142 owner_->AddObserver(this); |
| 143 |
| 130 SetContentsView(contents_view); | 144 SetContentsView(contents_view); |
| 131 ShowMenuHost(do_capture); | 145 ShowMenuHost(do_capture); |
| 132 } | 146 } |
| 133 | 147 |
| 134 bool MenuHost::IsMenuHostVisible() { | 148 bool MenuHost::IsMenuHostVisible() { |
| 135 return IsVisible(); | 149 return IsVisible(); |
| 136 } | 150 } |
| 137 | 151 |
| 138 void MenuHost::ShowMenuHost(bool do_capture) { | 152 void MenuHost::ShowMenuHost(bool do_capture) { |
| 139 // Doing a capture may make us get capture lost. Ignore it while we're in the | 153 // Doing a capture may make us get capture lost. Ignore it while we're in the |
| 140 // process of showing. | 154 // process of showing. |
| 141 base::AutoReset<bool> reseter(&ignore_capture_lost_, true); | 155 base::AutoReset<bool> reseter(&ignore_capture_lost_, true); |
| 142 ShowInactive(); | 156 ShowInactive(); |
| 143 if (do_capture) { | 157 if (do_capture) { |
| 158 #if !defined(OS_MACOSX) |
| 159 MenuController* menu_controller = |
| 160 submenu_->GetMenuItem()->GetMenuController(); |
| 161 if (menu_controller && menu_controller->send_gesture_events_to_owner()) { |
| 162 // TransferGesture when owner needs gesture events so that the incoming |
| 163 // touch events after MenuHost is created are properly translated into |
| 164 // gesture events instead of being dropped. |
| 165 internal::TransferGesture(owner_, this); |
| 166 } else { |
| 167 ui::GestureRecognizer::Get()->CancelActiveTouchesExcept(nullptr); |
| 168 } |
| 169 #else // defined(OS_MACOSX) |
| 144 // Cancel existing touches, so we don't miss some touch release/cancel | 170 // Cancel existing touches, so we don't miss some touch release/cancel |
| 145 // events due to the menu taking capture. | 171 // events due to the menu taking capture. |
| 146 ui::GestureRecognizer::Get()->CancelActiveTouchesExcept(nullptr); | 172 ui::GestureRecognizer::Get()->CancelActiveTouchesExcept(nullptr); |
| 173 #endif // !defined(OS_MACOSX) |
| 147 native_widget_private()->SetCapture(); | 174 native_widget_private()->SetCapture(); |
| 148 } | 175 } |
| 149 } | 176 } |
| 150 | 177 |
| 151 void MenuHost::HideMenuHost() { | 178 void MenuHost::HideMenuHost() { |
| 179 #if !defined(OS_MACOSX) |
| 180 MenuController* menu_controller = |
| 181 submenu_->GetMenuItem()->GetMenuController(); |
| 182 if (owner_ && menu_controller && |
| 183 menu_controller->send_gesture_events_to_owner()) { |
| 184 internal::TransferGesture(this, owner_); |
| 185 } |
| 186 #endif // !defined(OS_MACOSX) |
| 152 ignore_capture_lost_ = true; | 187 ignore_capture_lost_ = true; |
| 153 ReleaseMenuHostCapture(); | 188 ReleaseMenuHostCapture(); |
| 154 Hide(); | 189 Hide(); |
| 155 ignore_capture_lost_ = false; | 190 ignore_capture_lost_ = false; |
| 156 } | 191 } |
| 157 | 192 |
| 158 void MenuHost::DestroyMenuHost() { | 193 void MenuHost::DestroyMenuHost() { |
| 159 HideMenuHost(); | 194 HideMenuHost(); |
| 160 destroying_ = true; | 195 destroying_ = true; |
| 161 static_cast<MenuHostRootView*>(GetRootView())->ClearSubmenu(); | 196 static_cast<MenuHostRootView*>(GetRootView())->ClearSubmenu(); |
| 162 #if !defined(OS_MACOSX) | 197 #if !defined(OS_MACOSX) |
| 163 pre_dispatch_handler_.reset(); | 198 pre_dispatch_handler_.reset(); |
| 164 #endif | 199 #endif |
| 165 Close(); | 200 Close(); |
| 166 } | 201 } |
| 167 | 202 |
| 168 void MenuHost::SetMenuHostBounds(const gfx::Rect& bounds) { | 203 void MenuHost::SetMenuHostBounds(const gfx::Rect& bounds) { |
| 169 SetBounds(bounds); | 204 SetBounds(bounds); |
| 170 } | 205 } |
| 171 | 206 |
| 172 void MenuHost::ReleaseMenuHostCapture() { | 207 void MenuHost::ReleaseMenuHostCapture() { |
| 173 if (native_widget_private()->HasCapture()) | 208 if (native_widget_private()->HasCapture()) |
| 174 native_widget_private()->ReleaseCapture(); | 209 native_widget_private()->ReleaseCapture(); |
| 175 } | 210 } |
| 176 | 211 |
| 177 //////////////////////////////////////////////////////////////////////////////// | 212 //////////////////////////////////////////////////////////////////////////////// |
| 178 // MenuHost, Widget overrides: | 213 // MenuHost, private: |
| 179 | 214 |
| 180 internal::RootView* MenuHost::CreateRootView() { | 215 internal::RootView* MenuHost::CreateRootView() { |
| 181 return new MenuHostRootView(this, submenu_); | 216 return new MenuHostRootView(this, submenu_); |
| 182 } | 217 } |
| 183 | 218 |
| 184 void MenuHost::OnMouseCaptureLost() { | 219 void MenuHost::OnMouseCaptureLost() { |
| 185 if (destroying_ || ignore_capture_lost_) | 220 if (destroying_ || ignore_capture_lost_) |
| 186 return; | 221 return; |
| 187 MenuController* menu_controller = | 222 MenuController* menu_controller = |
| 188 submenu_->GetMenuItem()->GetMenuController(); | 223 submenu_->GetMenuItem()->GetMenuController(); |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 238 menu_delegate ? menu_delegate->ShouldCloseOnDragComplete() : should_close; | 273 menu_delegate ? menu_delegate->ShouldCloseOnDragComplete() : should_close; |
| 239 } | 274 } |
| 240 menu_controller->OnDragComplete(should_close); | 275 menu_controller->OnDragComplete(should_close); |
| 241 | 276 |
| 242 // We may have lost capture in the drag and drop, but are remaining open. | 277 // We may have lost capture in the drag and drop, but are remaining open. |
| 243 // Return capture so we get MouseCaptureLost events. | 278 // Return capture so we get MouseCaptureLost events. |
| 244 if (!should_close) | 279 if (!should_close) |
| 245 native_widget_private()->SetCapture(); | 280 native_widget_private()->SetCapture(); |
| 246 } | 281 } |
| 247 | 282 |
| 283 void MenuHost::OnWidgetDestroying(Widget* widget) { |
| 284 DCHECK_EQ(owner_, widget); |
| 285 owner_->RemoveObserver(this); |
| 286 owner_ = nullptr; |
| 287 } |
| 288 |
| 248 } // namespace views | 289 } // namespace views |
| OLD | NEW |