| 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/app_menu.h" | 5 #include "chrome/browser/ui/views/toolbar/app_menu.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include <algorithm> | 9 #include <algorithm> |
| 10 #include <cmath> | 10 #include <cmath> |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 104 | 104 |
| 105 // Returns true if |command_id| identifies a recent tabs menu item. | 105 // Returns true if |command_id| identifies a recent tabs menu item. |
| 106 bool IsRecentTabsCommand(int command_id) { | 106 bool IsRecentTabsCommand(int command_id) { |
| 107 return command_id >= AppMenuModel::kMinRecentTabsCommandId && | 107 return command_id >= AppMenuModel::kMinRecentTabsCommandId && |
| 108 command_id <= AppMenuModel::kMaxRecentTabsCommandId; | 108 command_id <= AppMenuModel::kMaxRecentTabsCommandId; |
| 109 } | 109 } |
| 110 | 110 |
| 111 // Subclass of ImageButton whose preferred size includes the size of the border. | 111 // Subclass of ImageButton whose preferred size includes the size of the border. |
| 112 class FullscreenButton : public ImageButton { | 112 class FullscreenButton : public ImageButton { |
| 113 public: | 113 public: |
| 114 explicit FullscreenButton(views::ButtonListener* listener) | 114 explicit FullscreenButton(views::ImageButtonDelegate* listener) |
| 115 : ImageButton(listener) { } | 115 : ImageButton(listener) {} |
| 116 | 116 |
| 117 // Overridden from ImageButton. | 117 // views::ImageButton: |
| 118 gfx::Size GetPreferredSize() const override { | 118 gfx::Size GetPreferredSize() const override { |
| 119 gfx::Size pref = ImageButton::GetPreferredSize(); | 119 gfx::Size pref = ImageButton::GetPreferredSize(); |
| 120 if (border()) { | 120 if (border()) { |
| 121 gfx::Insets insets = border()->GetInsets(); | 121 gfx::Insets insets = border()->GetInsets(); |
| 122 pref.Enlarge(insets.width(), insets.height()); | 122 pref.Enlarge(insets.width(), insets.height()); |
| 123 } | 123 } |
| 124 return pref; | 124 return pref; |
| 125 } | 125 } |
| 126 | 126 |
| 127 void GetAccessibleNodeData(ui::AXNodeData* node_data) override { | 127 void GetAccessibleNodeData(ui::AXNodeData* node_data) override { |
| (...skipping 16 matching lines...) Expand all Loading... |
| 144 | 144 |
| 145 // A rectangular button with a border drawn along the leading (left) side. | 145 // A rectangular button with a border drawn along the leading (left) side. |
| 146 LEADING_BORDER, | 146 LEADING_BORDER, |
| 147 | 147 |
| 148 // A button with no drawn border and a rounded background. | 148 // A button with no drawn border and a rounded background. |
| 149 ROUNDED_BUTTON, | 149 ROUNDED_BUTTON, |
| 150 }; | 150 }; |
| 151 | 151 |
| 152 explicit InMenuButtonBackground(ButtonType type) : type_(type) {} | 152 explicit InMenuButtonBackground(ButtonType type) : type_(type) {} |
| 153 | 153 |
| 154 // Overridden from views::Background. | 154 // views::Background: |
| 155 void Paint(gfx::Canvas* canvas, View* view) const override { | 155 void Paint(gfx::Canvas* canvas, View* view) const override { |
| 156 CustomButton* button = CustomButton::AsCustomButton(view); | 156 CustomButton* button = CustomButton::AsCustomButton(view); |
| 157 views::Button::ButtonState state = | 157 views::Button::ButtonState state = |
| 158 button ? button->state() : views::Button::STATE_NORMAL; | 158 button ? button->state() : views::Button::STATE_NORMAL; |
| 159 int h = view->height(); | 159 int h = view->height(); |
| 160 | 160 |
| 161 // Draw leading border if desired. | 161 // Draw leading border if desired. |
| 162 gfx::Rect bounds(view->GetLocalBounds()); | 162 gfx::Rect bounds(view->GetLocalBounds()); |
| 163 if (type_ == LEADING_BORDER) { | 163 if (type_ == LEADING_BORDER) { |
| 164 // We need to flip the canvas for RTL iff the button is not auto-flipping | 164 // We need to flip the canvas for RTL iff the button is not auto-flipping |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 244 menu_accelerator.modifiers()).GetShortcutText(); | 244 menu_accelerator.modifiers()).GetShortcutText(); |
| 245 } | 245 } |
| 246 | 246 |
| 247 return MenuItemView::GetAccessibleNameForMenuItem( | 247 return MenuItemView::GetAccessibleNameForMenuItem( |
| 248 accessible_name, accelerator_text); | 248 accessible_name, accelerator_text); |
| 249 } | 249 } |
| 250 | 250 |
| 251 // A button that lives inside a menu item. | 251 // A button that lives inside a menu item. |
| 252 class InMenuButton : public LabelButton { | 252 class InMenuButton : public LabelButton { |
| 253 public: | 253 public: |
| 254 InMenuButton(views::ButtonListener* listener, const base::string16& text) | 254 InMenuButton(views::ImageButtonDelegate* listener, const base::string16& text) |
| 255 : LabelButton(listener, text), in_menu_background_(NULL) {} | 255 : LabelButton(listener, text), in_menu_background_(NULL) {} |
| 256 ~InMenuButton() override {} | 256 ~InMenuButton() override {} |
| 257 | 257 |
| 258 void Init(InMenuButtonBackground::ButtonType type) { | 258 void Init(InMenuButtonBackground::ButtonType type) { |
| 259 // An InMenuButton should always be focusable regardless of the platform. | 259 // An InMenuButton should always be focusable regardless of the platform. |
| 260 // Hence we don't use SetFocusForPlatform(). | 260 // Hence we don't use SetFocusForPlatform(). |
| 261 SetFocusBehavior(FocusBehavior::ALWAYS); | 261 SetFocusBehavior(FocusBehavior::ALWAYS); |
| 262 SetHorizontalAlignment(gfx::ALIGN_CENTER); | 262 SetHorizontalAlignment(gfx::ALIGN_CENTER); |
| 263 | 263 |
| 264 in_menu_background_ = new InMenuButtonBackground(type); | 264 in_menu_background_ = new InMenuButtonBackground(type); |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 296 } | 296 } |
| 297 | 297 |
| 298 private: | 298 private: |
| 299 InMenuButtonBackground* in_menu_background_; | 299 InMenuButtonBackground* in_menu_background_; |
| 300 | 300 |
| 301 DISALLOW_COPY_AND_ASSIGN(InMenuButton); | 301 DISALLOW_COPY_AND_ASSIGN(InMenuButton); |
| 302 }; | 302 }; |
| 303 | 303 |
| 304 // AppMenuView is a view that can contain label buttons. | 304 // AppMenuView is a view that can contain label buttons. |
| 305 class AppMenuView : public views::View, | 305 class AppMenuView : public views::View, |
| 306 public views::ButtonListener, | 306 public views::ImageButtonDelegate, |
| 307 public AppMenuObserver { | 307 public AppMenuObserver { |
| 308 public: | 308 public: |
| 309 AppMenuView(AppMenu* menu, ButtonMenuItemModel* menu_model) | 309 AppMenuView(AppMenu* menu, ButtonMenuItemModel* menu_model) |
| 310 : menu_(menu), | 310 : menu_(menu), |
| 311 menu_model_(menu_model) { | 311 menu_model_(menu_model) { |
| 312 menu_->AddObserver(this); | 312 menu_->AddObserver(this); |
| 313 } | 313 } |
| 314 | 314 |
| 315 ~AppMenuView() override { | 315 ~AppMenuView() override { |
| 316 if (menu_) | 316 if (menu_) |
| 317 menu_->RemoveObserver(this); | 317 menu_->RemoveObserver(this); |
| 318 } | 318 } |
| 319 | 319 |
| 320 // Overridden from views::View. | 320 // views::View: |
| 321 void SchedulePaintInRect(const gfx::Rect& r) override { | 321 void SchedulePaintInRect(const gfx::Rect& r) override { |
| 322 // Normally when the mouse enters/exits a button the buttons invokes | 322 // Normally when the mouse enters/exits a button the buttons invokes |
| 323 // SchedulePaint. As part of the button border (InMenuButtonBackground) is | 323 // SchedulePaint. As part of the button border (InMenuButtonBackground) is |
| 324 // rendered by the button to the left/right of it SchedulePaint on the the | 324 // rendered by the button to the left/right of it SchedulePaint on the the |
| 325 // button may not be enough, so this forces a paint all. | 325 // button may not be enough, so this forces a paint all. |
| 326 View::SchedulePaintInRect(gfx::Rect(size())); | 326 View::SchedulePaintInRect(gfx::Rect(size())); |
| 327 } | 327 } |
| 328 | 328 |
| 329 InMenuButton* CreateAndConfigureButton( | 329 InMenuButton* CreateAndConfigureButton( |
| 330 int string_id, | 330 int string_id, |
| (...skipping 18 matching lines...) Expand all Loading... |
| 349 button->set_tag(index); | 349 button->set_tag(index); |
| 350 button->SetEnabled(menu_model_->IsEnabledAt(index)); | 350 button->SetEnabled(menu_model_->IsEnabledAt(index)); |
| 351 | 351 |
| 352 AddChildView(button); | 352 AddChildView(button); |
| 353 // all buttons on menu should must be a custom button in order for | 353 // all buttons on menu should must be a custom button in order for |
| 354 // the keyboard nativigation work. | 354 // the keyboard nativigation work. |
| 355 DCHECK(CustomButton::AsCustomButton(button)); | 355 DCHECK(CustomButton::AsCustomButton(button)); |
| 356 return button; | 356 return button; |
| 357 } | 357 } |
| 358 | 358 |
| 359 // Overridden from AppMenuObserver: | 359 // AppMenuObserver: |
| 360 void AppMenuDestroyed() override { | 360 void AppMenuDestroyed() override { |
| 361 menu_->RemoveObserver(this); | 361 menu_->RemoveObserver(this); |
| 362 menu_ = NULL; | 362 menu_ = NULL; |
| 363 menu_model_ = NULL; | 363 menu_model_ = NULL; |
| 364 } | 364 } |
| 365 | 365 |
| 366 protected: | 366 protected: |
| 367 AppMenu* menu() { return menu_; } | 367 AppMenu* menu() { return menu_; } |
| 368 const AppMenu* menu() const { return menu_; } | 368 const AppMenu* menu() const { return menu_; } |
| 369 ButtonMenuItemModel* menu_model() { return menu_model_; } | 369 ButtonMenuItemModel* menu_model() { return menu_model_; } |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 429 int paste_index) | 429 int paste_index) |
| 430 : AppMenuView(menu, menu_model) { | 430 : AppMenuView(menu, menu_model) { |
| 431 CreateAndConfigureButton(IDS_CUT, InMenuButtonBackground::LEADING_BORDER, | 431 CreateAndConfigureButton(IDS_CUT, InMenuButtonBackground::LEADING_BORDER, |
| 432 cut_index); | 432 cut_index); |
| 433 CreateAndConfigureButton(IDS_COPY, InMenuButtonBackground::LEADING_BORDER, | 433 CreateAndConfigureButton(IDS_COPY, InMenuButtonBackground::LEADING_BORDER, |
| 434 copy_index); | 434 copy_index); |
| 435 CreateAndConfigureButton(IDS_PASTE, InMenuButtonBackground::LEADING_BORDER, | 435 CreateAndConfigureButton(IDS_PASTE, InMenuButtonBackground::LEADING_BORDER, |
| 436 paste_index); | 436 paste_index); |
| 437 } | 437 } |
| 438 | 438 |
| 439 // Overridden from View. | 439 // views::View: |
| 440 gfx::Size GetPreferredSize() const override { | 440 gfx::Size GetPreferredSize() const override { |
| 441 // Returned height doesn't matter as MenuItemView forces everything to the | 441 // Returned height doesn't matter as MenuItemView forces everything to the |
| 442 // height of the menuitemview. | 442 // height of the menuitemview. |
| 443 return gfx::Size(GetMaxChildViewPreferredWidth() * child_count(), 0); | 443 return gfx::Size(GetMaxChildViewPreferredWidth() * child_count(), 0); |
| 444 } | 444 } |
| 445 | 445 |
| 446 void Layout() override { | 446 void Layout() override { |
| 447 // All buttons are given the same width. | 447 // All buttons are given the same width. |
| 448 int width = GetMaxChildViewPreferredWidth(); | 448 int width = GetMaxChildViewPreferredWidth(); |
| 449 for (int i = 0; i < child_count(); ++i) | 449 for (int i = 0; i < child_count(); ++i) |
| 450 child_at(i)->SetBounds(i * width, 0, width, height()); | 450 child_at(i)->SetBounds(i * width, 0, width, height()); |
| 451 } | 451 } |
| 452 | 452 |
| 453 // Overridden from ButtonListener. | 453 // views::ImageButtonDelegate: |
| 454 void ButtonPressed(views::Button* sender, const ui::Event& event) override { | 454 void ButtonPressed(views::Button* sender, const ui::Event& event) override { |
| 455 menu()->CancelAndEvaluate(menu_model(), sender->tag()); | 455 menu()->CancelAndEvaluate(menu_model(), sender->tag()); |
| 456 } | 456 } |
| 457 | 457 |
| 458 private: | 458 private: |
| 459 // Returns the max preferred width of all the children. | 459 // Returns the max preferred width of all the children. |
| 460 int GetMaxChildViewPreferredWidth() const { | 460 int GetMaxChildViewPreferredWidth() const { |
| 461 int width = 0; | 461 int width = 0; |
| 462 for (int i = 0; i < child_count(); ++i) | 462 for (int i = 0; i < child_count(); ++i) |
| 463 width = std::max(width, child_at(i)->GetPreferredSize().width()); | 463 width = std::max(width, child_at(i)->GetPreferredSize().width()); |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 534 menu_model, fullscreen_index, IDS_ACCNAME_FULLSCREEN)); | 534 menu_model, fullscreen_index, IDS_ACCNAME_FULLSCREEN)); |
| 535 AddChildView(fullscreen_button_); | 535 AddChildView(fullscreen_button_); |
| 536 | 536 |
| 537 // Need to set a font list for the zoom label width calculations. | 537 // Need to set a font list for the zoom label width calculations. |
| 538 OnNativeThemeChanged(NULL); | 538 OnNativeThemeChanged(NULL); |
| 539 UpdateZoomControls(); | 539 UpdateZoomControls(); |
| 540 } | 540 } |
| 541 | 541 |
| 542 ~ZoomView() override {} | 542 ~ZoomView() override {} |
| 543 | 543 |
| 544 // Overridden from View. | 544 // views::View: |
| 545 gfx::Size GetPreferredSize() const override { | 545 gfx::Size GetPreferredSize() const override { |
| 546 // The increment/decrement button are forced to the same width. | 546 // The increment/decrement button are forced to the same width. |
| 547 int button_width = std::max(increment_button_->GetPreferredSize().width(), | 547 int button_width = std::max(increment_button_->GetPreferredSize().width(), |
| 548 decrement_button_->GetPreferredSize().width()); | 548 decrement_button_->GetPreferredSize().width()); |
| 549 int fullscreen_width = | 549 int fullscreen_width = |
| 550 fullscreen_button_->GetPreferredSize().width() + kFullscreenPadding; | 550 fullscreen_button_->GetPreferredSize().width() + kFullscreenPadding; |
| 551 // Returned height doesn't matter as MenuItemView forces everything to the | 551 // Returned height doesn't matter as MenuItemView forces everything to the |
| 552 // height of the menuitemview. Note that we have overridden the height when | 552 // height of the menuitemview. Note that we have overridden the height when |
| 553 // constructing the menu. | 553 // constructing the menu. |
| 554 return gfx::Size( | 554 return gfx::Size( |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 600 gfx::ImageSkia hovered_fullscreen_image( | 600 gfx::ImageSkia hovered_fullscreen_image( |
| 601 new HoveredImageSource(*full_screen_image, fg_color), | 601 new HoveredImageSource(*full_screen_image, fg_color), |
| 602 full_screen_image->size()); | 602 full_screen_image->size()); |
| 603 fullscreen_button_->SetImage( | 603 fullscreen_button_->SetImage( |
| 604 ImageButton::STATE_HOVERED, &hovered_fullscreen_image); | 604 ImageButton::STATE_HOVERED, &hovered_fullscreen_image); |
| 605 fullscreen_button_->SetImage( | 605 fullscreen_button_->SetImage( |
| 606 ImageButton::STATE_PRESSED, &hovered_fullscreen_image); | 606 ImageButton::STATE_PRESSED, &hovered_fullscreen_image); |
| 607 } | 607 } |
| 608 } | 608 } |
| 609 | 609 |
| 610 // Overridden from ButtonListener. | 610 // views::ImageButtonDelegate: |
| 611 void ButtonPressed(views::Button* sender, const ui::Event& event) override { | 611 void ButtonPressed(views::Button* sender, const ui::Event& event) override { |
| 612 if (sender->tag() == fullscreen_index_) { | 612 if (sender->tag() == fullscreen_index_) { |
| 613 menu()->CancelAndEvaluate(menu_model(), sender->tag()); | 613 menu()->CancelAndEvaluate(menu_model(), sender->tag()); |
| 614 } else { | 614 } else { |
| 615 // Zoom buttons don't close the menu. | 615 // Zoom buttons don't close the menu. |
| 616 menu_model()->ActivatedAt(sender->tag()); | 616 menu_model()->ActivatedAt(sender->tag()); |
| 617 } | 617 } |
| 618 } | 618 } |
| 619 | 619 |
| 620 private: | 620 private: |
| (...skipping 615 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1236 0, | 1236 0, |
| 1237 BookmarkMenuDelegate::SHOW_PERMANENT_FOLDERS, | 1237 BookmarkMenuDelegate::SHOW_PERMANENT_FOLDERS, |
| 1238 BOOKMARK_LAUNCH_LOCATION_APP_MENU); | 1238 BOOKMARK_LAUNCH_LOCATION_APP_MENU); |
| 1239 } | 1239 } |
| 1240 | 1240 |
| 1241 int AppMenu::ModelIndexFromCommandId(int command_id) const { | 1241 int AppMenu::ModelIndexFromCommandId(int command_id) const { |
| 1242 CommandIDToEntry::const_iterator ix = command_id_to_entry_.find(command_id); | 1242 CommandIDToEntry::const_iterator ix = command_id_to_entry_.find(command_id); |
| 1243 DCHECK(ix != command_id_to_entry_.end()); | 1243 DCHECK(ix != command_id_to_entry_.end()); |
| 1244 return ix->second.second; | 1244 return ix->second.second; |
| 1245 } | 1245 } |
| OLD | NEW |