diff options
author | estade@chromium.org <estade@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-03-06 00:43:44 +0000 |
---|---|---|
committer | estade@chromium.org <estade@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-03-06 00:43:44 +0000 |
commit | 8e3a815d1cf67f0e14a1b33431c9c9961b43fa79 (patch) | |
tree | 7947a494bc26dff0213b2b44cbbed131bb08e04f /chrome/browser | |
parent | 94f923b86617c2c6e7fda321256a7523096e3c51 (diff) | |
download | chromium_src-8e3a815d1cf67f0e14a1b33431c9c9961b43fa79.zip chromium_src-8e3a815d1cf67f0e14a1b33431c9c9961b43fa79.tar.gz chromium_src-8e3a815d1cf67f0e14a1b33431c9c9961b43fa79.tar.bz2 |
Refactor the download shelf context menu in preparation for porting.
* Delete DownloadDestinationContextMenu, which is not used anywhere.
* Delete the unused download menu item types from the ContextMenuCommands enum.
* Merge BaseContextMenu and DownloadShelfContextMenu, move them from download_util.{h,cc} to download_shelf.{h,cc}
* Take the windows-specific code from the new DownloadShelfContextMenu and put it in DownloadShelfContextMenuWin in download_item_view.cc (the only place it's used). DownloadShelfContextMenuWin extends DownloadShelfContextMenu and Menu::Delegate.
Review URL: http://codereview.chromium.org/40184
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@11068 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser')
-rw-r--r-- | chrome/browser/download/download_shelf.cc | 102 | ||||
-rw-r--r-- | chrome/browser/download/download_shelf.h | 37 | ||||
-rw-r--r-- | chrome/browser/download/download_util.cc | 184 | ||||
-rw-r--r-- | chrome/browser/download/download_util.h | 68 | ||||
-rw-r--r-- | chrome/browser/views/download_item_view.cc | 67 |
5 files changed, 202 insertions, 256 deletions
diff --git a/chrome/browser/download/download_shelf.cc b/chrome/browser/download/download_shelf.cc index 826dc4e..3feca71 100644 --- a/chrome/browser/download/download_shelf.cc +++ b/chrome/browser/download/download_shelf.cc @@ -4,15 +4,23 @@ #include "chrome/browser/download/download_shelf.h" +#include "base/file_util.h" #include "chrome/browser/dom_ui/downloads_ui.h" +#include "chrome/browser/download/download_item_model.h" +#include "chrome/browser/download/download_manager.h" #include "chrome/browser/metrics/user_metrics.h" +#include "chrome/common/l10n_util.h" +#include "grit/generated_resources.h" #if defined(OS_WIN) +#include "chrome/browser/download/download_util.h" #include "chrome/browser/tab_contents/tab_contents.h" #elif defined(OS_POSIX) #include "chrome/common/temp_scaffolding_stubs.h" #endif +// DownloadShelf --------------------------------------------------------------- + void DownloadShelf::ShowAllDownloads() { #if defined(OS_WIN) Profile* profile = tab_contents_->profile(); @@ -32,3 +40,97 @@ void DownloadShelf::ChangeTabContents(TabContents* old_contents, DCHECK(old_contents == tab_contents_); tab_contents_ = new_contents; } + +// DownloadShelfContextMenu ---------------------------------------------------- + +DownloadShelfContextMenu::DownloadShelfContextMenu( + BaseDownloadItemModel* download_model) + : download_(download_model->download()), + model_(download_model) { +} + +DownloadShelfContextMenu::~DownloadShelfContextMenu() { +} + +bool DownloadShelfContextMenu::ItemIsChecked(int id) const { + switch (id) { + case OPEN_WHEN_COMPLETE: + return download_->open_when_complete(); + case ALWAYS_OPEN_TYPE: { + const FilePath::StringType extension = + file_util::GetFileExtensionFromPath(download_->full_path()); + return download_->manager()->ShouldOpenFileExtension(extension); + } + } + return false; +} + +bool DownloadShelfContextMenu::ItemIsDefault(int id) const { + return id == OPEN_WHEN_COMPLETE; +} + +std::wstring DownloadShelfContextMenu::GetItemLabel(int id) const { + switch (id) { + case SHOW_IN_FOLDER: + return l10n_util::GetString(IDS_DOWNLOAD_LINK_SHOW); + case OPEN_WHEN_COMPLETE: + if (download_->state() == DownloadItem::IN_PROGRESS) + return l10n_util::GetString(IDS_DOWNLOAD_MENU_OPEN_WHEN_COMPLETE); + return l10n_util::GetString(IDS_DOWNLOAD_MENU_OPEN); + case ALWAYS_OPEN_TYPE: + return l10n_util::GetString(IDS_DOWNLOAD_MENU_ALWAYS_OPEN_TYPE); + case CANCEL: + return l10n_util::GetString(IDS_DOWNLOAD_MENU_CANCEL); + default: + NOTREACHED(); + } + return std::wstring(); +} + +bool DownloadShelfContextMenu::IsItemCommandEnabled(int id) const { + switch (id) { + case SHOW_IN_FOLDER: + case OPEN_WHEN_COMPLETE: + return download_->state() != DownloadItem::CANCELLED; + case ALWAYS_OPEN_TYPE: +#if defined(OS_WIN) + return download_util::CanOpenDownload(download_); +#else + // TODO(port): port download_util + NOTIMPLEMENTED(); + return true; +#endif + case CANCEL: + return download_->state() == DownloadItem::IN_PROGRESS; + default: + return id > 0 && id < MENU_LAST; + } +} + +void DownloadShelfContextMenu::ExecuteItemCommand(int id) { + switch (id) { + case SHOW_IN_FOLDER: + download_->manager()->ShowDownloadInShell(download_); + break; + case OPEN_WHEN_COMPLETE: +#if defined(OS_WIN) + download_util::OpenDownload(download_); +#else + // TODO(port): port download_util + NOTIMPLEMENTED(); +#endif + break; + case ALWAYS_OPEN_TYPE: { + const FilePath::StringType extension = + file_util::GetFileExtensionFromPath(download_->full_path()); + download_->manager()->OpenFilesOfExtension( + extension, !ItemIsChecked(ALWAYS_OPEN_TYPE)); + break; + } + case CANCEL: + model_->CancelTask(); + break; + default: + NOTREACHED(); + } +} diff --git a/chrome/browser/download/download_shelf.h b/chrome/browser/download/download_shelf.h index d3b83a1..6692fb8 100644 --- a/chrome/browser/download/download_shelf.h +++ b/chrome/browser/download/download_shelf.h @@ -5,9 +5,12 @@ #ifndef CHROME_BROWSER_DOWNLOAD_DOWNLOAD_SHELF_H_ #define CHROME_BROWSER_DOWNLOAD_DOWNLOAD_SHELF_H_ +#include <string> + #include "base/basictypes.h" class BaseDownloadItemModel; +class DownloadItem; class TabContents; // DownloadShelf is an interface for platform-specific download shelves to @@ -47,5 +50,39 @@ class DownloadShelf { DISALLOW_COPY_AND_ASSIGN(DownloadShelf); }; +// Logic for the download shelf context menu. Platform specific subclasses are +// responsible for creating and running the menu. +class DownloadShelfContextMenu { + public: + virtual ~DownloadShelfContextMenu(); + + protected: + explicit DownloadShelfContextMenu(BaseDownloadItemModel* download_model); + + enum ContextMenuCommands { + SHOW_IN_FOLDER = 1, // Open a file explorer window with the item selected + OPEN_WHEN_COMPLETE, // Open the download when it's finished + ALWAYS_OPEN_TYPE, // Default this file extension to always open + CANCEL, // Cancel the download + MENU_LAST + }; + + protected: + bool ItemIsChecked(int id) const; + bool ItemIsDefault(int id) const; + std::wstring GetItemLabel(int id) const; + bool IsItemCommandEnabled(int id) const; + void ExecuteItemCommand(int id); + + // Information source. + DownloadItem* download_; + + // A model to control the cancel behavior. + BaseDownloadItemModel* model_; + + private: + DISALLOW_COPY_AND_ASSIGN(DownloadShelfContextMenu); +}; + #endif // CHROME_BROWSER_DOWNLOAD_DOWNLOAD_SHELF_H_ diff --git a/chrome/browser/download/download_util.cc b/chrome/browser/download/download_util.cc index 4933945..27e078e 100644 --- a/chrome/browser/download/download_util.cc +++ b/chrome/browser/download/download_util.cc @@ -31,194 +31,10 @@ namespace download_util { -// BaseContextMenu ------------------------------------------------------------- - -BaseContextMenu::BaseContextMenu(DownloadItem* download) : download_(download) { -} - -BaseContextMenu::~BaseContextMenu() { -} - // How many times to cycle the complete animation. This should be an odd number // so that the animation ends faded out. static const int kCompleteAnimationCycles = 5; -bool BaseContextMenu::IsItemChecked(int id) const { - switch (id) { - case OPEN_WHEN_COMPLETE: - return download_->open_when_complete(); - case ALWAYS_OPEN_TYPE: { - const FilePath::StringType extension = - file_util::GetFileExtensionFromPath(download_->full_path()); - return download_->manager()->ShouldOpenFileExtension(extension); - } - } - return false; -} - -bool BaseContextMenu::IsItemDefault(int id) const { - return false; -} - -std::wstring BaseContextMenu::GetLabel(int id) const { - switch (id) { - case SHOW_IN_FOLDER: - return l10n_util::GetString(IDS_DOWNLOAD_LINK_SHOW); - case COPY_LINK: - return l10n_util::GetString(IDS_CONTENT_CONTEXT_COPYLINKLOCATION); - case COPY_PATH: - return l10n_util::GetString(IDS_DOWNLOAD_MENU_COPY_PATH); - case COPY_FILE: - return l10n_util::GetString(IDS_DOWNLOAD_MENU_COPY_FILE); - case OPEN_WHEN_COMPLETE: - if (download_->state() == DownloadItem::IN_PROGRESS) - return l10n_util::GetString(IDS_DOWNLOAD_MENU_OPEN_WHEN_COMPLETE); - return l10n_util::GetString(IDS_DOWNLOAD_MENU_OPEN); - case ALWAYS_OPEN_TYPE: - return l10n_util::GetString(IDS_DOWNLOAD_MENU_ALWAYS_OPEN_TYPE); - case REMOVE_ITEM: - return l10n_util::GetString(IDS_DOWNLOAD_MENU_REMOVE_ITEM); - case CANCEL: - return l10n_util::GetString(IDS_DOWNLOAD_MENU_CANCEL); - default: - NOTREACHED(); - } - return std::wstring(); -} - -bool BaseContextMenu::SupportsCommand(int id) const { - return id > 0 && id < MENU_LAST; -} - -bool BaseContextMenu::IsCommandEnabled(int id) const { - switch (id) { - case SHOW_IN_FOLDER: - case COPY_PATH: - case COPY_FILE: - case OPEN_WHEN_COMPLETE: - return download_->state() != DownloadItem::CANCELLED; - case ALWAYS_OPEN_TYPE: - return CanOpenDownload(download_); - case CANCEL: - return download_->state() == DownloadItem::IN_PROGRESS; - default: - return id > 0 && id < MENU_LAST; - } -} - -void BaseContextMenu::ExecuteCommand(int id) { - ScopedClipboardWriter scw(g_browser_process->clipboard_service()); - switch (id) { - case SHOW_IN_FOLDER: - download_->manager()->ShowDownloadInShell(download_); - break; - case COPY_LINK: - scw.WriteText(UTF8ToUTF16(download_->url().spec())); - break; - case COPY_PATH: - scw.WriteText(WideToUTF16(download_->full_path().ToWStringHack())); - break; - case COPY_FILE: - // TODO(paulg): Move to OSExchangeData when implementing drag and drop? - scw.WriteFile(download_->full_path()); - break; - case OPEN_WHEN_COMPLETE: - OpenDownload(download_); - break; - case ALWAYS_OPEN_TYPE: { - const FilePath::StringType extension = - file_util::GetFileExtensionFromPath(download_->full_path()); - download_->manager()->OpenFilesOfExtension( - extension, !IsItemChecked(ALWAYS_OPEN_TYPE)); - break; - } - case REMOVE_ITEM: - download_->Remove(false); - break; - case CANCEL: - download_->Cancel(true); - break; - default: - NOTREACHED(); - } -} - -// DownloadShelfContextMenu ---------------------------------------------------- - -DownloadShelfContextMenu::DownloadShelfContextMenu( - DownloadItem* download, - HWND window, - BaseDownloadItemModel* model, - const CPoint& point) - : BaseContextMenu(download), - model_(model) { - DCHECK(model_); - - // The menu's anchor point is determined based on the UI layout. - Menu::AnchorPoint anchor_point; - if (l10n_util::GetTextDirection() == l10n_util::RIGHT_TO_LEFT) - anchor_point = Menu::TOPRIGHT; - else - anchor_point = Menu::TOPLEFT; - - Menu context_menu(this, anchor_point, window); - if (download->state() == DownloadItem::COMPLETE) - context_menu.AppendMenuItem(OPEN_WHEN_COMPLETE, L"", Menu::NORMAL); - else - context_menu.AppendMenuItem(OPEN_WHEN_COMPLETE, L"", Menu::CHECKBOX); - context_menu.AppendMenuItem(ALWAYS_OPEN_TYPE, L"", Menu::CHECKBOX); - context_menu.AppendSeparator(); - context_menu.AppendMenuItem(SHOW_IN_FOLDER, L"", Menu::NORMAL); - context_menu.AppendSeparator(); - context_menu.AppendMenuItem(CANCEL, L"", Menu::NORMAL); - context_menu.RunMenuAt(point.x, point.y); -} - -DownloadShelfContextMenu::~DownloadShelfContextMenu() { -} - -bool DownloadShelfContextMenu::IsItemDefault(int id) const { - return id == OPEN_WHEN_COMPLETE; -} - -void DownloadShelfContextMenu::ExecuteCommand(int id) { - if (id == CANCEL) - model_->CancelTask(); - else - BaseContextMenu::ExecuteCommand(id); -} - -// DownloadDestinationContextMenu ---------------------------------------------- - -DownloadDestinationContextMenu::DownloadDestinationContextMenu( - DownloadItem* download, - HWND window, - const CPoint& point) - : BaseContextMenu(download) { - // The menu's anchor point is determined based on the UI layout. - Menu::AnchorPoint anchor_point; - if (l10n_util::GetTextDirection() == l10n_util::RIGHT_TO_LEFT) - anchor_point = Menu::TOPRIGHT; - else - anchor_point = Menu::TOPLEFT; - - Menu context_menu(this, anchor_point, window); - context_menu.AppendMenuItem(SHOW_IN_FOLDER, L"", Menu::NORMAL); - context_menu.AppendSeparator(); - context_menu.AppendMenuItem(COPY_LINK, L"", Menu::NORMAL); - context_menu.AppendMenuItem(COPY_PATH, L"", Menu::NORMAL); - context_menu.AppendMenuItem(COPY_FILE, L"", Menu::NORMAL); - context_menu.AppendSeparator(); - context_menu.AppendMenuItem(OPEN_WHEN_COMPLETE, L"", Menu::CHECKBOX); - context_menu.AppendMenuItem(ALWAYS_OPEN_TYPE, L"", Menu::CHECKBOX); - context_menu.AppendSeparator(); - context_menu.AppendMenuItem(REMOVE_ITEM, L"", Menu::NORMAL); - context_menu.RunMenuAt(point.x, point.y); -} - -DownloadDestinationContextMenu::~DownloadDestinationContextMenu() { -} - // Download opening ------------------------------------------------------------ bool CanOpenDownload(DownloadItem* download) { diff --git a/chrome/browser/download/download_util.h b/chrome/browser/download/download_util.h index 7f1c236..dc3d74e 100644 --- a/chrome/browser/download/download_util.h +++ b/chrome/browser/download/download_util.h @@ -24,74 +24,6 @@ class SkBitmap; namespace download_util { -// DownloadContextMenu --------------------------------------------------------- - -// The base class of context menus that provides the various commands. -// Subclasses are responsible for creating and running the menu. -class BaseContextMenu : public Menu::Delegate { - public: - explicit BaseContextMenu(DownloadItem* download); - virtual ~BaseContextMenu(); - - enum ContextMenuCommands { - SHOW_IN_FOLDER = 1, // Open an Explorer window with the item highlighted - COPY_LINK, // Copy the download's URL to the clipboard - COPY_PATH, // Copy the download's full path to the clipboard - COPY_FILE, // Copy the downloaded file to the clipboard - OPEN_WHEN_COMPLETE, // Open the download when it's finished - ALWAYS_OPEN_TYPE, // Default this file extension to always open - REMOVE_ITEM, // Remove the download - CANCEL, // Cancel the download - MENU_LAST - }; - - // Menu::Delegate interface - virtual bool IsItemChecked(int id) const; - virtual bool IsItemDefault(int id) const; - virtual std::wstring GetLabel(int id) const; - virtual bool SupportsCommand(int id) const; - virtual bool IsCommandEnabled(int id) const; - virtual void ExecuteCommand(int id); - - protected: - // Information source. - DownloadItem* download_; - - private: - DISALLOW_COPY_AND_ASSIGN(BaseContextMenu); -}; - -// Menu for the download shelf. -class DownloadShelfContextMenu : public BaseContextMenu { - public: - DownloadShelfContextMenu(DownloadItem* download, - HWND window, - BaseDownloadItemModel* model, - const CPoint& point); - virtual ~DownloadShelfContextMenu(); - - virtual bool IsItemDefault(int id) const; - virtual void ExecuteCommand(int id); - - private: - // A model to control the cancel behavior. - BaseDownloadItemModel* model_; - - DISALLOW_COPY_AND_ASSIGN(DownloadShelfContextMenu); -}; - -// Menu for the download destination view. -class DownloadDestinationContextMenu : public BaseContextMenu { - public: - DownloadDestinationContextMenu(DownloadItem* download, - HWND window, - const CPoint& point); - virtual ~DownloadDestinationContextMenu(); - - private: - DISALLOW_COPY_AND_ASSIGN(DownloadDestinationContextMenu); -}; - // DownloadProgressTask -------------------------------------------------------- // A class for managing the timed progress animations for a download view. The diff --git a/chrome/browser/views/download_item_view.cc b/chrome/browser/views/download_item_view.cc index 8b08a3f..a0f83ec 100644 --- a/chrome/browser/views/download_item_view.cc +++ b/chrome/browser/views/download_item_view.cc @@ -17,6 +17,7 @@ #include "chrome/common/l10n_util.h" #include "chrome/common/resource_bundle.h" #include "chrome/common/win_util.h" +#include "chrome/views/menu.h" #include "chrome/views/native_button.h" #include "chrome/views/root_view.h" #include "chrome/views/widget.h" @@ -56,6 +57,65 @@ static const SkColor kStatusColor = SkColorSetRGB(123, 141, 174); // How long the 'download complete' animation should last for. static const int kCompleteAnimationDurationMs = 2500; +// DownloadShelfContextMenuWin ------------------------------------------------- + +class DownloadShelfContextMenuWin : public DownloadShelfContextMenu, + public Menu::Delegate { + public: + DownloadShelfContextMenuWin::DownloadShelfContextMenuWin( + BaseDownloadItemModel* model, + HWND window, + const gfx::Point& point) + : DownloadShelfContextMenu(model) { + DCHECK(model); + + // The menu's anchor point is determined based on the UI layout. + Menu::AnchorPoint anchor_point; + if (l10n_util::GetTextDirection() == l10n_util::RIGHT_TO_LEFT) + anchor_point = Menu::TOPRIGHT; + else + anchor_point = Menu::TOPLEFT; + + Menu context_menu(this, anchor_point, window); + if (download_->state() == DownloadItem::COMPLETE) + context_menu.AppendMenuItem(OPEN_WHEN_COMPLETE, L"", Menu::NORMAL); + else + context_menu.AppendMenuItem(OPEN_WHEN_COMPLETE, L"", Menu::CHECKBOX); + context_menu.AppendMenuItem(ALWAYS_OPEN_TYPE, L"", Menu::CHECKBOX); + context_menu.AppendSeparator(); + context_menu.AppendMenuItem(SHOW_IN_FOLDER, L"", Menu::NORMAL); + context_menu.AppendSeparator(); + context_menu.AppendMenuItem(CANCEL, L"", Menu::NORMAL); + context_menu.RunMenuAt(point.x(), point.y()); + } + + // Menu::Delegate implementation --------------------------------------------- + + virtual bool IsItemChecked(int id) const { + return ItemIsChecked(id); + } + + virtual bool IsItemDefault(int id) const { + return ItemIsDefault(id); + } + + virtual std::wstring GetLabel(int id) const { + return GetItemLabel(id); + } + + virtual bool SupportsCommand(int id) const { + return id > 0 && id < MENU_LAST; + } + + virtual bool IsCommandEnabled(int id) const { + return IsItemCommandEnabled(id); + } + + virtual void ExecuteCommand(int id) { + return ExecuteItemCommand(id); + } +}; + // DownloadItemView ------------------------------------------------------------ DownloadItemView::DownloadItemView(DownloadItem* download, @@ -658,10 +718,9 @@ bool DownloadItemView::OnMousePressed(const views::MouseEvent& event) { } views::View::ConvertPointToScreen(this, &point); - download_util::DownloadShelfContextMenu menu(download_, - GetWidget()->GetHWND(), - model_.get(), - point.ToPOINT()); + DownloadShelfContextMenuWin menu(model_.get(), + GetWidget()->GetHWND(), + point); drop_down_pressed_ = false; // Showing the menu blocks. Here we revert the state. SetState(NORMAL, NORMAL); |