1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "ui/views/controls/menu/menu_runner_impl.h"
7 #include "ui/native_theme/native_theme.h"
8 #include "ui/views/controls/button/menu_button.h"
9 #include "ui/views/controls/menu/menu_controller.h"
10 #include "ui/views/controls/menu/menu_delegate.h"
11 #include "ui/views/controls/menu/menu_item_view.h"
12 #include "ui/views/controls/menu/menu_runner_impl_adapter.h"
13 #include "ui/views/widget/widget.h"
16 #include "base/win/win_util.h"
22 #if !defined(OS_MACOSX)
23 MenuRunnerImplInterface
* MenuRunnerImplInterface::Create(
24 ui::MenuModel
* menu_model
,
26 return new MenuRunnerImplAdapter(menu_model
);
30 MenuRunnerImpl::MenuRunnerImpl(MenuItemView
* menu
)
33 delete_after_run_(false),
36 owns_controller_(false),
37 closing_event_time_(base::TimeDelta()),
41 bool MenuRunnerImpl::IsRunning() const {
45 void MenuRunnerImpl::Release() {
47 if (delete_after_run_
)
48 return; // We already canceled.
50 // The menu is running a nested message loop, we can't delete it now
51 // otherwise the stack would be in a really bad state (many frames would
52 // have deleted objects on them). Instead cancel the menu, when it returns
53 // Holder will delete itself.
54 delete_after_run_
= true;
56 // Swap in a different delegate. That way we know the original MenuDelegate
57 // won't be notified later on (when it's likely already been deleted).
58 if (!empty_delegate_
.get())
59 empty_delegate_
.reset(new MenuDelegate());
60 menu_
->set_delegate(empty_delegate_
.get());
63 // Release is invoked when MenuRunner is destroyed. Assume this is happening
64 // because the object referencing the menu has been destroyed and the menu
65 // button is no longer valid.
66 controller_
->Cancel(MenuController::EXIT_DESTROYED
);
72 MenuRunner::RunResult
MenuRunnerImpl::RunMenuAt(Widget
* parent
,
74 const gfx::Rect
& bounds
,
75 MenuAnchorPosition anchor
,
77 closing_event_time_
= base::TimeDelta();
79 // Ignore requests to show the menu while it's already showing. MenuItemView
80 // doesn't handle this very well (meaning it crashes).
81 return MenuRunner::NORMAL_EXIT
;
84 MenuController
* controller
= MenuController::GetActiveInstance();
86 if ((run_types
& MenuRunner::IS_NESTED
) != 0) {
87 if (!controller
->IsBlockingRun()) {
88 controller
->CancelAll();
92 // There's some other menu open and we're not nested. Cancel the menu.
93 controller
->CancelAll();
94 if ((run_types
& MenuRunner::FOR_DROP
) == 0) {
95 // We can't open another menu, otherwise the message loop would become
96 // twice nested. This isn't necessarily a problem, but generally isn't
98 return MenuRunner::NORMAL_EXIT
;
100 // Drop menus don't block the message loop, so it's ok to create a new
107 for_drop_
= (run_types
& MenuRunner::FOR_DROP
) != 0;
108 bool has_mnemonics
= (run_types
& MenuRunner::HAS_MNEMONICS
) != 0;
109 owns_controller_
= false;
111 // No menus are showing, show one.
112 ui::NativeTheme
* theme
=
113 parent
? parent
->GetNativeTheme() : ui::NativeTheme::instance();
114 controller
= new MenuController(theme
, !for_drop_
, this);
115 owns_controller_
= true;
117 controller
->set_is_combobox((run_types
& MenuRunner::COMBOBOX
) != 0);
118 controller_
= controller
;
119 menu_
->set_controller(controller_
);
120 menu_
->PrepareForRun(owns_controller_
,
122 !for_drop_
&& ShouldShowMnemonics(button
));
125 int mouse_event_flags
= 0;
126 MenuItemView
* result
=
127 controller
->Run(parent
,
132 (run_types
& MenuRunner::CONTEXT_MENU
) != 0,
133 (run_types
& MenuRunner::NESTED_DRAG
) != 0,
135 // Get the time of the event which closed this menu.
136 closing_event_time_
= controller
->closing_event_time();
138 // Drop menus return immediately. We finish processing in DropMenuClosed.
139 return MenuRunner::NORMAL_EXIT
;
141 return MenuDone(result
, mouse_event_flags
);
144 void MenuRunnerImpl::Cancel() {
146 controller_
->Cancel(MenuController::EXIT_ALL
);
149 base::TimeDelta
MenuRunnerImpl::GetClosingEventTime() const {
150 return closing_event_time_
;
153 void MenuRunnerImpl::DropMenuClosed(NotifyType type
, MenuItemView
* menu
) {
156 if (type
== NOTIFY_DELEGATE
&& menu
->GetDelegate()) {
157 // Delegate is null when invoked from the destructor.
158 menu
->GetDelegate()->DropMenuClosed(menu
);
162 void MenuRunnerImpl::SiblingMenuCreated(MenuItemView
* menu
) {
163 if (menu
!= menu_
&& sibling_menus_
.count(menu
) == 0)
164 sibling_menus_
.insert(menu
);
167 MenuRunnerImpl::~MenuRunnerImpl() {
169 for (std::set
<MenuItemView
*>::iterator i
= sibling_menus_
.begin();
170 i
!= sibling_menus_
.end();
175 MenuRunner::RunResult
MenuRunnerImpl::MenuDone(MenuItemView
* result
,
176 int mouse_event_flags
) {
177 menu_
->RemoveEmptyMenus();
178 menu_
->set_controller(NULL
);
180 if (owns_controller_
) {
181 // We created the controller and need to delete it.
183 owns_controller_
= false;
186 // Make sure all the windows we created to show the menus have been
188 menu_
->DestroyAllMenuHosts();
189 if (delete_after_run_
) {
191 return MenuRunner::MENU_DELETED
;
194 if (result
&& menu_
->GetDelegate()) {
195 // Executing the command may also delete this.
196 base::WeakPtr
<MenuRunnerImpl
> ref(weak_factory_
.GetWeakPtr());
197 menu_
->GetDelegate()->ExecuteCommand(result
->GetCommand(),
200 return MenuRunner::MENU_DELETED
;
202 return MenuRunner::NORMAL_EXIT
;
205 bool MenuRunnerImpl::ShouldShowMnemonics(MenuButton
* button
) {
206 // Show mnemonics if the button has focus or alt is pressed.
207 bool show_mnemonics
= button
? button
->HasFocus() : false;
209 // This is only needed on Windows.
211 show_mnemonics
= base::win::IsAltPressed();
213 return show_mnemonics
;
216 } // namespace internal