OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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_runner_impl.h" | 5 #include "ui/views/controls/menu/menu_runner_impl.h" |
6 | 6 |
7 #include "build/build_config.h" | 7 #include "build/build_config.h" |
8 #include "ui/native_theme/native_theme.h" | 8 #include "ui/native_theme/native_theme.h" |
9 #include "ui/views/controls/button/menu_button.h" | 9 #include "ui/views/controls/button/menu_button.h" |
10 #include "ui/views/controls/menu/menu_controller.h" | 10 #include "ui/views/controls/menu/menu_controller.h" |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
66 // happening because the object referencing the menu has been destroyed | 66 // happening because the object referencing the menu has been destroyed |
67 // and the menu button is no longer valid. | 67 // and the menu button is no longer valid. |
68 controller_->Cancel(MenuController::EXIT_DESTROYED); | 68 controller_->Cancel(MenuController::EXIT_DESTROYED); |
69 return; | 69 return; |
70 } | 70 } |
71 } | 71 } |
72 | 72 |
73 delete this; | 73 delete this; |
74 } | 74 } |
75 | 75 |
76 MenuRunner::RunResult MenuRunnerImpl::RunMenuAt(Widget* parent, | 76 void MenuRunnerImpl::RunMenuAt(Widget* parent, |
77 MenuButton* button, | 77 MenuButton* button, |
78 const gfx::Rect& bounds, | 78 const gfx::Rect& bounds, |
79 MenuAnchorPosition anchor, | 79 MenuAnchorPosition anchor, |
80 int32_t run_types) { | 80 int32_t run_types) { |
81 closing_event_time_ = base::TimeTicks(); | 81 closing_event_time_ = base::TimeTicks(); |
82 if (running_) { | 82 if (running_) { |
83 // Ignore requests to show the menu while it's already showing. MenuItemView | 83 // Ignore requests to show the menu while it's already showing. MenuItemView |
84 // doesn't handle this very well (meaning it crashes). | 84 // doesn't handle this very well (meaning it crashes). |
85 return MenuRunner::NORMAL_EXIT; | 85 return; |
86 } | 86 } |
87 | 87 |
88 MenuController* controller = MenuController::GetActiveInstance(); | 88 MenuController* controller = MenuController::GetActiveInstance(); |
89 if (controller) { | 89 if (controller) { |
90 if ((run_types & MenuRunner::IS_NESTED) != 0) { | 90 if ((run_types & MenuRunner::IS_NESTED) != 0) { |
91 if (!controller->IsBlockingRun()) { | 91 if (!controller->IsBlockingRun()) { |
92 controller->CancelAll(); | 92 controller->CancelAll(); |
93 controller = NULL; | 93 controller = NULL; |
94 } else { | 94 } else { |
95 // Only nest the delegate when not cancelling drag-and-drop. When | 95 // Only nest the delegate when not cancelling drag-and-drop. When |
96 // cancelling this will become the root delegate of the new | 96 // cancelling this will become the root delegate of the new |
97 // MenuController | 97 // MenuController |
98 controller->AddNestedDelegate(this); | 98 controller->AddNestedDelegate(this); |
99 } | 99 } |
100 } else { | 100 } else { |
101 // There's some other menu open and we're not nested. Cancel the menu. | 101 // There's some other menu open and we're not nested. Cancel the menu. |
102 controller->CancelAll(); | 102 controller->CancelAll(); |
103 if ((run_types & MenuRunner::FOR_DROP) == 0) { | 103 if ((run_types & MenuRunner::FOR_DROP) == 0) { |
104 // We can't open another menu, otherwise the message loop would become | 104 // We can't open another menu, otherwise the message loop would become |
105 // twice nested. This isn't necessarily a problem, but generally isn't | 105 // twice nested. This isn't necessarily a problem, but generally isn't |
106 // expected. | 106 // expected. |
107 return MenuRunner::NORMAL_EXIT; | 107 return; |
108 } | 108 } |
109 // Drop menus don't block the message loop, so it's ok to create a new | 109 // Drop menus don't block the message loop, so it's ok to create a new |
110 // MenuController. | 110 // MenuController. |
111 controller = NULL; | 111 controller = NULL; |
112 } | 112 } |
113 } | 113 } |
114 | 114 |
115 running_ = true; | 115 running_ = true; |
116 for_drop_ = (run_types & MenuRunner::FOR_DROP) != 0; | 116 for_drop_ = (run_types & MenuRunner::FOR_DROP) != 0; |
117 bool has_mnemonics = (run_types & MenuRunner::HAS_MNEMONICS) != 0; | 117 bool has_mnemonics = (run_types & MenuRunner::HAS_MNEMONICS) != 0; |
118 owns_controller_ = false; | 118 owns_controller_ = false; |
119 if (!controller) { | 119 if (!controller) { |
120 // No menus are showing, show one. | 120 // No menus are showing, show one. |
121 controller = new MenuController(!for_drop_, this); | 121 controller = new MenuController(!for_drop_, this); |
122 owns_controller_ = true; | 122 owns_controller_ = true; |
123 } | 123 } |
124 controller->set_is_combobox((run_types & MenuRunner::COMBOBOX) != 0); | 124 controller->set_is_combobox((run_types & MenuRunner::COMBOBOX) != 0); |
125 controller_ = controller->AsWeakPtr(); | 125 controller_ = controller->AsWeakPtr(); |
126 menu_->set_controller(controller_.get()); | 126 menu_->set_controller(controller_.get()); |
127 menu_->PrepareForRun(owns_controller_, | 127 menu_->PrepareForRun(owns_controller_, |
128 has_mnemonics, | 128 has_mnemonics, |
129 !for_drop_ && ShouldShowMnemonics(button)); | 129 !for_drop_ && ShouldShowMnemonics(button)); |
130 | 130 |
131 int mouse_event_flags = 0; | |
132 controller->Run(parent, button, menu_, bounds, anchor, | 131 controller->Run(parent, button, menu_, bounds, anchor, |
133 (run_types & MenuRunner::CONTEXT_MENU) != 0, | 132 (run_types & MenuRunner::CONTEXT_MENU) != 0, |
134 (run_types & MenuRunner::NESTED_DRAG) != 0, | 133 (run_types & MenuRunner::NESTED_DRAG) != 0); |
135 &mouse_event_flags); | |
136 // We finish processing results in OnMenuClosed. | |
137 return MenuRunner::NORMAL_EXIT; | |
138 } | 134 } |
139 | 135 |
140 void MenuRunnerImpl::Cancel() { | 136 void MenuRunnerImpl::Cancel() { |
141 if (running_) | 137 if (running_) |
142 controller_->Cancel(MenuController::EXIT_ALL); | 138 controller_->Cancel(MenuController::EXIT_ALL); |
143 } | 139 } |
144 | 140 |
145 base::TimeTicks MenuRunnerImpl::GetClosingEventTime() const { | 141 base::TimeTicks MenuRunnerImpl::GetClosingEventTime() const { |
146 return closing_event_time_; | 142 return closing_event_time_; |
147 } | 143 } |
(...skipping 22 matching lines...) Expand all Loading... |
170 running_ = false; | 166 running_ = false; |
171 if (menu_->GetDelegate()) { | 167 if (menu_->GetDelegate()) { |
172 // Executing the command may also delete this. | 168 // Executing the command may also delete this. |
173 base::WeakPtr<MenuRunnerImpl> ref(weak_factory_.GetWeakPtr()); | 169 base::WeakPtr<MenuRunnerImpl> ref(weak_factory_.GetWeakPtr()); |
174 if (menu && !for_drop_) { | 170 if (menu && !for_drop_) { |
175 // Do not execute the menu that was dragged/dropped. | 171 // Do not execute the menu that was dragged/dropped. |
176 menu_->GetDelegate()->ExecuteCommand(menu->GetCommand(), | 172 menu_->GetDelegate()->ExecuteCommand(menu->GetCommand(), |
177 mouse_event_flags); | 173 mouse_event_flags); |
178 } | 174 } |
179 // Only notify the delegate if it did not delete this. | 175 // Only notify the delegate if it did not delete this. |
180 if (!ref) | 176 if (ref && type == NOTIFY_DELEGATE) |
181 return; | 177 menu_->GetDelegate()->OnMenuClosed(menu); |
182 else if (type == NOTIFY_DELEGATE) | |
183 menu_->GetDelegate()->OnMenuClosed(menu, MenuRunner::NORMAL_EXIT); | |
184 } | 178 } |
185 } | 179 } |
186 | 180 |
187 void MenuRunnerImpl::SiblingMenuCreated(MenuItemView* menu) { | 181 void MenuRunnerImpl::SiblingMenuCreated(MenuItemView* menu) { |
188 if (menu != menu_ && sibling_menus_.count(menu) == 0) | 182 if (menu != menu_ && sibling_menus_.count(menu) == 0) |
189 sibling_menus_.insert(menu); | 183 sibling_menus_.insert(menu); |
190 } | 184 } |
191 | 185 |
192 MenuRunnerImpl::~MenuRunnerImpl() { | 186 MenuRunnerImpl::~MenuRunnerImpl() { |
193 delete menu_; | 187 delete menu_; |
194 for (std::set<MenuItemView*>::iterator i = sibling_menus_.begin(); | 188 for (std::set<MenuItemView*>::iterator i = sibling_menus_.begin(); |
195 i != sibling_menus_.end(); ++i) | 189 i != sibling_menus_.end(); ++i) |
196 delete *i; | 190 delete *i; |
197 } | 191 } |
198 | 192 |
199 bool MenuRunnerImpl::ShouldShowMnemonics(MenuButton* button) { | 193 bool MenuRunnerImpl::ShouldShowMnemonics(MenuButton* button) { |
200 // Show mnemonics if the button has focus or alt is pressed. | 194 // Show mnemonics if the button has focus or alt is pressed. |
201 bool show_mnemonics = button ? button->HasFocus() : false; | 195 bool show_mnemonics = button ? button->HasFocus() : false; |
202 #if defined(OS_WIN) | 196 #if defined(OS_WIN) |
203 // This is only needed on Windows. | 197 // This is only needed on Windows. |
204 if (!show_mnemonics) | 198 if (!show_mnemonics) |
205 show_mnemonics = ui::win::IsAltPressed(); | 199 show_mnemonics = ui::win::IsAltPressed(); |
206 #endif | 200 #endif |
207 return show_mnemonics; | 201 return show_mnemonics; |
208 } | 202 } |
209 | 203 |
210 } // namespace internal | 204 } // namespace internal |
211 } // namespace views | 205 } // namespace views |
OLD | NEW |