OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "chrome/browser/ui/views/toolbar/toolbar_action_view.h" | 5 #include "chrome/browser/ui/views/toolbar/toolbar_action_view.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/auto_reset.h" | 9 #include "base/auto_reset.h" |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
(...skipping 25 matching lines...) Expand all Loading... |
36 | 36 |
37 using views::LabelButtonBorder; | 37 using views::LabelButtonBorder; |
38 | 38 |
39 namespace { | 39 namespace { |
40 | 40 |
41 // Toolbar action buttons have no insets because the badges are drawn right at | 41 // Toolbar action buttons have no insets because the badges are drawn right at |
42 // the edge of the view's area. Other badding (such as centering the icon) is | 42 // the edge of the view's area. Other badding (such as centering the icon) is |
43 // handled directly by the Image. | 43 // handled directly by the Image. |
44 const int kBorderInset = 0; | 44 const int kBorderInset = 0; |
45 | 45 |
46 // The callback to call directly before showing the context menu. | |
47 ToolbarActionView::ContextMenuCallback* context_menu_callback_for_test = | |
48 nullptr; | |
49 | |
50 } // namespace | 46 } // namespace |
51 | 47 |
52 //////////////////////////////////////////////////////////////////////////////// | 48 //////////////////////////////////////////////////////////////////////////////// |
53 // ToolbarActionView | 49 // ToolbarActionView |
54 | 50 |
55 ToolbarActionView::ToolbarActionView( | 51 ToolbarActionView::ToolbarActionView( |
56 ToolbarActionViewController* view_controller, | 52 ToolbarActionViewController* view_controller, |
57 ToolbarActionView::Delegate* delegate) | 53 ToolbarActionView::Delegate* delegate) |
58 : MenuButton(base::string16(), this, false), | 54 : MenuButton(base::string16(), this, false), |
59 view_controller_(view_controller), | 55 view_controller_(view_controller), |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
171 menu_runner_.reset(); | 167 menu_runner_.reset(); |
172 menu_ = nullptr; | 168 menu_ = nullptr; |
173 view_controller_->OnContextMenuClosed(); | 169 view_controller_->OnContextMenuClosed(); |
174 menu_adapter_.reset(); | 170 menu_adapter_.reset(); |
175 } | 171 } |
176 | 172 |
177 gfx::ImageSkia ToolbarActionView::GetIconForTest() { | 173 gfx::ImageSkia ToolbarActionView::GetIconForTest() { |
178 return GetImage(views::Button::STATE_NORMAL); | 174 return GetImage(views::Button::STATE_NORMAL); |
179 } | 175 } |
180 | 176 |
181 void ToolbarActionView::set_context_menu_callback_for_testing( | |
182 base::Callback<void(ToolbarActionView*)>* callback) { | |
183 context_menu_callback_for_test = callback; | |
184 } | |
185 | |
186 gfx::Size ToolbarActionView::GetPreferredSize() const { | 177 gfx::Size ToolbarActionView::GetPreferredSize() const { |
187 return gfx::Size(ToolbarActionsBar::IconWidth(false), | 178 return gfx::Size(ToolbarActionsBar::IconWidth(false), |
188 ToolbarActionsBar::IconHeight()); | 179 ToolbarActionsBar::IconHeight()); |
189 } | 180 } |
190 | 181 |
191 bool ToolbarActionView::OnMousePressed(const ui::MouseEvent& event) { | 182 bool ToolbarActionView::OnMousePressed(const ui::MouseEvent& event) { |
192 // views::MenuButton actions are only triggered by left mouse clicks. | 183 // views::MenuButton actions are only triggered by left mouse clicks. |
193 if (event.IsOnlyLeftMouseButton() && !pressed_lock_) { | 184 if (event.IsOnlyLeftMouseButton() && !pressed_lock_) { |
194 // TODO(bruthig): The ACTION_PENDING triggering logic should be in | 185 // TODO(bruthig): The ACTION_PENDING triggering logic should be in |
195 // MenuButton::OnPressed() however there is a bug with the pressed state | 186 // MenuButton::OnPressed() however there is a bug with the pressed state |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
295 | 286 |
296 // Unretained() is safe here as ToolbarActionView will always outlive the | 287 // Unretained() is safe here as ToolbarActionView will always outlive the |
297 // menu. Any action that would lead to the deletion of |this| first triggers | 288 // menu. Any action that would lead to the deletion of |this| first triggers |
298 // the closing of the menu through lost capture. | 289 // the closing of the menu through lost capture. |
299 menu_adapter_.reset(new views::MenuModelAdapter( | 290 menu_adapter_.reset(new views::MenuModelAdapter( |
300 context_menu_model, | 291 context_menu_model, |
301 base::Bind(&ToolbarActionView::OnMenuClosed, base::Unretained(this)))); | 292 base::Bind(&ToolbarActionView::OnMenuClosed, base::Unretained(this)))); |
302 menu_ = menu_adapter_->CreateMenu(); | 293 menu_ = menu_adapter_->CreateMenu(); |
303 menu_runner_.reset(new views::MenuRunner(menu_, run_types)); | 294 menu_runner_.reset(new views::MenuRunner(menu_, run_types)); |
304 | 295 |
305 if (context_menu_callback_for_test) | |
306 context_menu_callback_for_test->Run(this); | |
307 ignore_result( | 296 ignore_result( |
308 menu_runner_->RunMenuAt(parent, this, gfx::Rect(screen_loc, size()), | 297 menu_runner_->RunMenuAt(parent, this, gfx::Rect(screen_loc, size()), |
309 views::MENU_ANCHOR_TOPLEFT, source_type)); | 298 views::MENU_ANCHOR_TOPLEFT, source_type)); |
310 } | 299 } |
311 | 300 |
312 bool ToolbarActionView::CloseActiveMenuIfNeeded() { | 301 bool ToolbarActionView::CloseActiveMenuIfNeeded() { |
313 // If this view is shown inside another menu, there's a possibility that there | 302 // If this view is shown inside another menu, there's a possibility that there |
314 // is another context menu showing that we have to close before we can | 303 // is another context menu showing that we have to close before we can |
315 // activate a different menu. | 304 // activate a different menu. |
316 if (delegate_->ShownInsideMenu()) { | 305 if (delegate_->ShownInsideMenu()) { |
317 views::MenuController* menu_controller = | 306 views::MenuController* menu_controller = |
318 views::MenuController::GetActiveInstance(); | 307 views::MenuController::GetActiveInstance(); |
319 // If this is shown inside a menu, then there should always be an active | 308 // If this is shown inside a menu, then there should always be an active |
320 // menu controller. | 309 // menu controller. |
321 DCHECK(menu_controller); | 310 DCHECK(menu_controller); |
322 if (menu_controller->in_nested_run()) { | 311 if (menu_controller->in_nested_run()) { |
323 // There is another menu showing. Close the outermost menu (since we are | 312 // There is another menu showing. Close the outermost menu (since we are |
324 // shown in the same menu, we don't want to close the whole thing). | 313 // shown in the same menu, we don't want to close the whole thing). |
325 menu_controller->Cancel(views::MenuController::EXIT_OUTERMOST); | 314 menu_controller->Cancel(views::MenuController::EXIT_OUTERMOST); |
326 return true; | 315 return true; |
327 } | 316 } |
328 } | 317 } |
329 | 318 |
330 return false; | 319 return false; |
331 } | 320 } |
OLD | NEW |