diff options
Diffstat (limited to 'chrome/browser/views/extensions')
4 files changed, 153 insertions, 0 deletions
diff --git a/chrome/browser/views/extensions/browser_action_overflow_menu_controller.cc b/chrome/browser/views/extensions/browser_action_overflow_menu_controller.cc new file mode 100644 index 0000000..66b209c --- /dev/null +++ b/chrome/browser/views/extensions/browser_action_overflow_menu_controller.cc @@ -0,0 +1,82 @@ +// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "chrome/browser/views/extensions/browser_action_overflow_menu_controller.h" + +#include "base/utf_string_conversions.h" +#include "chrome/browser/browser_list.h" +#include "chrome/browser/tab_contents/tab_contents.h" +#include "chrome/browser/views/browser_actions_container.h" +#include "chrome/common/extensions/extension.h" +#include "views/controls/menu/menu_item_view.h" + +BrowserActionOverflowMenuController::BrowserActionOverflowMenuController( + BrowserActionsContainer* owner, + views::MenuButton* menu_button, + const std::vector<BrowserActionView*>& views, + int start_index) + : owner_(owner), + menu_button_(menu_button), + views_(&views), + start_index_(start_index) { + menu_.reset(new views::MenuItemView(this)); + menu_->set_has_icons(true); + + TabContents* tab = BrowserList::GetLastActive()->GetSelectedTabContents(); + int tab_id = tab->controller().session_id().id(); + + size_t command_id = 0; + for (size_t i = start_index; i < views_->size(); ++i) { + BrowserActionView* view = (*views_)[i]; + menu_->AppendMenuItemWithIcon( + command_id, + UTF8ToWide(view->button()->extension()->name()), + view->button()->extension()->browser_action()->GetIcon(tab_id)); + ++command_id; + } +} + +BrowserActionOverflowMenuController::~BrowserActionOverflowMenuController() { +} + +bool BrowserActionOverflowMenuController::RunMenu(gfx::NativeWindow window) { + gfx::Rect bounds = menu_button_->GetBounds( + views::View::IGNORE_MIRRORING_TRANSFORMATION); + gfx::Point screen_loc; + views::View::ConvertPointToScreen(menu_button_, &screen_loc); + bounds.set_x(screen_loc.x()); + bounds.set_y(screen_loc.y()); + + views::MenuItemView::AnchorPosition anchor = + menu_button_->UILayoutIsRightToLeft() ? views::MenuItemView::TOPRIGHT : + views::MenuItemView::TOPLEFT; + menu_->RunMenuAt(window, menu_button_, bounds, anchor, false); + return true; +} + +void BrowserActionOverflowMenuController::CancelMenu() { + if (context_menu_.get()) + context_menu_->Cancel(); + menu_->Cancel(); +} + +void BrowserActionOverflowMenuController::ExecuteCommand(int id) { + BrowserActionView* view = (*views_)[start_index_ + id]; + owner_->OnBrowserActionExecuted(view->button()); +} + +bool BrowserActionOverflowMenuController::ShowContextMenu( + views::MenuItemView* source, int id, int x, int y, bool is_mouse_gesture) { + if (!context_menu_.get()) + context_menu_.reset(new ExtensionActionContextMenu()); + // This blocks until the user choses something or dismisses. + context_menu_->Run((*views_)[start_index_ + id]->button()->extension(), + gfx::Point(x, y)); + + // The user is done with the context menu, so we can close the underlying + // menu. + menu_->Cancel(); + + return true; +} diff --git a/chrome/browser/views/extensions/browser_action_overflow_menu_controller.h b/chrome/browser/views/extensions/browser_action_overflow_menu_controller.h new file mode 100644 index 0000000..02950a4 --- /dev/null +++ b/chrome/browser/views/extensions/browser_action_overflow_menu_controller.h @@ -0,0 +1,63 @@ +// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef CHROME_BROWSER_VIEWS_EXTENSIONS_BROWSER_ACTION_OVERFLOW_MENU_CONTROLLER_H_ +#define CHROME_BROWSER_VIEWS_EXTENSIONS_BROWSER_ACTION_OVERFLOW_MENU_CONTROLLER_H_ + +#include <vector> + +#include "views/controls/menu/menu_delegate.h" + +class BrowserActionsContainer; +class BrowserActionView; +class ExtensionActionContextMenu; + +class BrowserActionOverflowMenuController : public views::MenuDelegate { + public: + BrowserActionOverflowMenuController( + BrowserActionsContainer* owner, + views::MenuButton* menu_button, + const std::vector<BrowserActionView*>& views, + int start_index); + virtual ~BrowserActionOverflowMenuController(); + + // Shows the overflow menu. + bool RunMenu(gfx::NativeWindow window); + + // Closes the overflow menu (and its context menu if open as well). + void CancelMenu(); + + // Overridden from views::MenuDelegate: + virtual void ExecuteCommand(int id); + virtual bool ShowContextMenu(views::MenuItemView* source, + int id, + int x, + int y, + bool is_mouse_gesture); + + private: + // A pointer to the browser action container that owns the overflow menu. + BrowserActionsContainer* owner_; + + // A pointer to the overflow menu button that we are showing the menu for. + views::MenuButton* menu_button_; + + // The overflow menu for the menu button. + scoped_ptr<views::MenuItemView> menu_; + + // The context menu (when you right click a menu item in the overflow menu). + scoped_ptr<ExtensionActionContextMenu> context_menu_; + + // The views vector of all the browser actions the container knows about. We + // won't show all items, just the one starting at |start_index| and above. + const std::vector<BrowserActionView*>* views_; + + // The index into the BrowserActionView vector, indicating where to start + // picking browser actions to draw. + int start_index_; + + DISALLOW_COPY_AND_ASSIGN(BrowserActionOverflowMenuController); +}; + +#endif // CHROME_BROWSER_VIEWS_EXTENSIONS_BROWSER_ACTION_OVERFLOW_MENU_CONTROLLER_H_ diff --git a/chrome/browser/views/extensions/extension_action_context_menu.cc b/chrome/browser/views/extensions/extension_action_context_menu.cc index 47927fc..eafdf5c 100644 --- a/chrome/browser/views/extensions/extension_action_context_menu.cc +++ b/chrome/browser/views/extensions/extension_action_context_menu.cc @@ -25,3 +25,8 @@ void ExtensionActionContextMenu::Run(Extension* extension, context_menu_menu_.reset(new views::Menu2(context_menu_contents_.get())); context_menu_menu_->RunContextMenuAt(point); } + +void ExtensionActionContextMenu::Cancel() { + if (context_menu_menu_.get()) + context_menu_menu_->CancelMenu(); +} diff --git a/chrome/browser/views/extensions/extension_action_context_menu.h b/chrome/browser/views/extensions/extension_action_context_menu.h index 25d1949..5340a389 100644 --- a/chrome/browser/views/extensions/extension_action_context_menu.h +++ b/chrome/browser/views/extensions/extension_action_context_menu.h @@ -20,6 +20,9 @@ class ExtensionActionContextMenu { // Display the context menu at a given point. void Run(Extension* extension, const gfx::Point& point); + // Closes the context menu if open. + void Cancel(); + private: // The options menu. scoped_ptr<ExtensionActionContextMenuModel> context_menu_contents_; |