| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/download/download_shelf_context_menu_view.h" | 5 #include "chrome/browser/ui/views/download/download_shelf_context_menu_view.h" |
| 6 | 6 |
| 7 #include "base/i18n/rtl.h" | 7 #include "base/i18n/rtl.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "chrome/browser/download/download_item_model.h" | 9 #include "chrome/browser/download/download_item_model.h" |
| 10 #include "content/public/browser/download_item.h" | 10 #include "content/public/browser/download_item.h" |
| 11 #include "content/public/browser/page_navigator.h" | 11 #include "content/public/browser/page_navigator.h" |
| 12 #include "ui/gfx/geometry/point.h" | 12 #include "ui/gfx/geometry/point.h" |
| 13 #include "ui/views/controls/menu/menu_model_adapter.h" | 13 #include "ui/views/controls/menu/menu_model_adapter.h" |
| 14 #include "ui/views/controls/menu/menu_runner.h" | 14 #include "ui/views/controls/menu/menu_runner.h" |
| 15 | 15 |
| 16 DownloadShelfContextMenuView::DownloadShelfContextMenuView( | 16 DownloadShelfContextMenuView::DownloadShelfContextMenuView( |
| 17 content::DownloadItem* download_item) | 17 content::DownloadItem* download_item) |
| 18 : DownloadShelfContextMenu(download_item) { | 18 : DownloadShelfContextMenu(download_item) { |
| 19 } | 19 } |
| 20 | 20 |
| 21 DownloadShelfContextMenuView::~DownloadShelfContextMenuView() {} | 21 DownloadShelfContextMenuView::~DownloadShelfContextMenuView() {} |
| 22 | 22 |
| 23 void DownloadShelfContextMenuView::Run(views::Widget* parent_widget, | 23 void DownloadShelfContextMenuView::Run( |
| 24 const gfx::Rect& rect, | 24 views::Widget* parent_widget, |
| 25 ui::MenuSourceType source_type) { | 25 const gfx::Rect& rect, |
| 26 ui::MenuSourceType source_type, |
| 27 const base::Closure& on_menu_closed_callback) { |
| 26 ui::MenuModel* menu_model = GetMenuModel(); | 28 ui::MenuModel* menu_model = GetMenuModel(); |
| 27 // Run() should not be getting called if the DownloadItem was destroyed. | 29 // Run() should not be getting called if the DownloadItem was destroyed. |
| 28 DCHECK(menu_model); | 30 DCHECK(menu_model); |
| 29 | 31 |
| 30 menu_model_adapter_.reset(new views::MenuModelAdapter( | 32 menu_model_adapter_.reset(new views::MenuModelAdapter( |
| 31 menu_model, base::Bind(&DownloadShelfContextMenuView::OnMenuClosed, | 33 menu_model, base::Bind(&DownloadShelfContextMenuView::OnMenuClosed, |
| 32 base::Unretained(this)))); | 34 base::Unretained(this), on_menu_closed_callback))); |
| 33 | 35 |
| 34 menu_runner_.reset(new views::MenuRunner(menu_model_adapter_->CreateMenu(), | 36 menu_runner_.reset(new views::MenuRunner(menu_model_adapter_->CreateMenu(), |
| 35 views::MenuRunner::HAS_MNEMONICS | | 37 views::MenuRunner::HAS_MNEMONICS | |
| 36 views::MenuRunner::CONTEXT_MENU | | 38 views::MenuRunner::CONTEXT_MENU | |
| 37 views::MenuRunner::ASYNC)); | 39 views::MenuRunner::ASYNC)); |
| 38 | 40 |
| 39 // The menu's alignment is determined based on the UI layout. | 41 // The menu's alignment is determined based on the UI layout. |
| 40 views::MenuAnchorPosition position; | 42 views::MenuAnchorPosition position; |
| 41 if (base::i18n::IsRTL()) | 43 if (base::i18n::IsRTL()) |
| 42 position = views::MENU_ANCHOR_TOPRIGHT; | 44 position = views::MENU_ANCHOR_TOPRIGHT; |
| 43 else | 45 else |
| 44 position = views::MENU_ANCHOR_TOPLEFT; | 46 position = views::MENU_ANCHOR_TOPLEFT; |
| 45 | 47 |
| 46 menu_runner_->RunMenuAt(parent_widget, NULL, rect, position, source_type); | 48 menu_runner_->RunMenuAt(parent_widget, NULL, rect, position, source_type); |
| 47 } | 49 } |
| 48 | 50 |
| 49 void DownloadShelfContextMenuView::OnMenuClosed() { | 51 void DownloadShelfContextMenuView::OnMenuClosed( |
| 52 const base::Closure& on_menu_closed_callback) { |
| 50 close_time_ = base::TimeTicks::Now(); | 53 close_time_ = base::TimeTicks::Now(); |
| 54 |
| 55 // This must be ran before clearing |menu_model_adapter_| who owns the |
| 56 // reference. |
| 57 if (!on_menu_closed_callback.is_null()) |
| 58 on_menu_closed_callback.Run(); |
| 59 |
| 51 menu_model_adapter_.reset(); | 60 menu_model_adapter_.reset(); |
| 52 menu_runner_.reset(); | 61 menu_runner_.reset(); |
| 53 } | 62 } |
| OLD | NEW |