Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(58)

Side by Side Diff: chrome/browser/ui/views/toolbar/wrench_menu.cc

Issue 1252133002: Optimizing the time taken to build and show the WrenchMenu. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressed pkasting@'s comments from patch set 3. Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/wrench_menu.h" 5 #include "chrome/browser/ui/views/toolbar/wrench_menu.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <cmath> 8 #include <cmath>
9 #include <set> 9 #include <set>
10 10
(...skipping 353 matching lines...) Expand 10 before | Expand all | Expand 10 after
364 364
365 // Overridden from WrenchMenuObserver: 365 // Overridden from WrenchMenuObserver:
366 void WrenchMenuDestroyed() override { 366 void WrenchMenuDestroyed() override {
367 menu_->RemoveObserver(this); 367 menu_->RemoveObserver(this);
368 menu_ = NULL; 368 menu_ = NULL;
369 menu_model_ = NULL; 369 menu_model_ = NULL;
370 } 370 }
371 371
372 protected: 372 protected:
373 WrenchMenu* menu() { return menu_; } 373 WrenchMenu* menu() { return menu_; }
374 const WrenchMenu* menu() const { return menu_; }
374 ButtonMenuItemModel* menu_model() { return menu_model_; } 375 ButtonMenuItemModel* menu_model() { return menu_model_; }
375 376
376 private: 377 private:
377 // Hosting WrenchMenu. 378 // Hosting WrenchMenu.
378 // WARNING: this may be NULL during shutdown. 379 // WARNING: this may be NULL during shutdown.
379 WrenchMenu* menu_; 380 WrenchMenu* menu_;
380 381
381 // The menu model containing the increment/decrement/reset items. 382 // The menu model containing the increment/decrement/reset items.
382 // WARNING: this may be NULL during shutdown. 383 // WARNING: this may be NULL during shutdown.
383 ButtonMenuItemModel* menu_model_; 384 ButtonMenuItemModel* menu_model_;
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after
485 ButtonMenuItemModel* menu_model, 486 ButtonMenuItemModel* menu_model,
486 int decrement_index, 487 int decrement_index,
487 int increment_index, 488 int increment_index,
488 int fullscreen_index) 489 int fullscreen_index)
489 : WrenchMenuView(menu, menu_model), 490 : WrenchMenuView(menu, menu_model),
490 fullscreen_index_(fullscreen_index), 491 fullscreen_index_(fullscreen_index),
491 increment_button_(NULL), 492 increment_button_(NULL),
492 zoom_label_(NULL), 493 zoom_label_(NULL),
493 decrement_button_(NULL), 494 decrement_button_(NULL),
494 fullscreen_button_(NULL), 495 fullscreen_button_(NULL),
495 zoom_label_width_(0) { 496 zoom_label_max_width_(0),
497 zoom_label_max_width_valid_(false) {
496 browser_zoom_subscription_ = 498 browser_zoom_subscription_ =
497 ui_zoom::ZoomEventManager::GetForBrowserContext( 499 ui_zoom::ZoomEventManager::GetForBrowserContext(
498 menu->browser_->profile()) 500 menu->browser_->profile())
499 ->AddZoomLevelChangedCallback( 501 ->AddZoomLevelChangedCallback(
500 base::Bind(&WrenchMenu::ZoomView::OnZoomLevelChanged, 502 base::Bind(&WrenchMenu::ZoomView::OnZoomLevelChanged,
501 base::Unretained(this))); 503 base::Unretained(this)));
502 504
503 decrement_button_ = CreateButtonWithAccName( 505 decrement_button_ = CreateButtonWithAccName(
504 IDS_ZOOM_MINUS2, InMenuButtonBackground::LEFT_BUTTON, 506 IDS_ZOOM_MINUS2, InMenuButtonBackground::LEFT_BUTTON,
505 decrement_index, IDS_ACCNAME_ZOOM_MINUS2); 507 decrement_index, IDS_ACCNAME_ZOOM_MINUS2);
506 508
507 zoom_label_ = new Label( 509 zoom_label_ = new Label(
508 l10n_util::GetStringFUTF16Int(IDS_ZOOM_PERCENT, 100)); 510 l10n_util::GetStringFUTF16Int(IDS_ZOOM_PERCENT, 100));
509 zoom_label_->SetAutoColorReadabilityEnabled(false); 511 zoom_label_->SetAutoColorReadabilityEnabled(false);
510 zoom_label_->SetHorizontalAlignment(gfx::ALIGN_RIGHT); 512 zoom_label_->SetHorizontalAlignment(gfx::ALIGN_RIGHT);
511 513
512 InMenuButtonBackground* center_bg = 514 InMenuButtonBackground* center_bg =
513 new InMenuButtonBackground(InMenuButtonBackground::RIGHT_BUTTON); 515 new InMenuButtonBackground(InMenuButtonBackground::RIGHT_BUTTON);
514 zoom_label_->set_background(center_bg); 516 zoom_label_->set_background(center_bg);
515 517
516 AddChildView(zoom_label_); 518 AddChildView(zoom_label_);
517 zoom_label_width_ = MaxWidthForZoomLabel(); 519 zoom_label_max_width_valid_ = false;
518 520
519 increment_button_ = CreateButtonWithAccName( 521 increment_button_ = CreateButtonWithAccName(
520 IDS_ZOOM_PLUS2, InMenuButtonBackground::RIGHT_BUTTON, 522 IDS_ZOOM_PLUS2, InMenuButtonBackground::RIGHT_BUTTON,
521 increment_index, IDS_ACCNAME_ZOOM_PLUS2); 523 increment_index, IDS_ACCNAME_ZOOM_PLUS2);
522 524
523 center_bg->SetOtherButtons(decrement_button_, increment_button_); 525 center_bg->SetOtherButtons(decrement_button_, increment_button_);
524 526
525 fullscreen_button_ = new FullscreenButton(this); 527 fullscreen_button_ = new FullscreenButton(this);
526 // all buttons on menu should must be a custom button in order for 528 // all buttons on menu should must be a custom button in order for
527 // the keyboard nativigation work. 529 // the keyboard nativigation work.
(...skipping 25 matching lines...) Expand all
553 // Overridden from View. 555 // Overridden from View.
554 gfx::Size GetPreferredSize() const override { 556 gfx::Size GetPreferredSize() const override {
555 // The increment/decrement button are forced to the same width. 557 // The increment/decrement button are forced to the same width.
556 int button_width = std::max(increment_button_->GetPreferredSize().width(), 558 int button_width = std::max(increment_button_->GetPreferredSize().width(),
557 decrement_button_->GetPreferredSize().width()); 559 decrement_button_->GetPreferredSize().width());
558 int fullscreen_width = 560 int fullscreen_width =
559 fullscreen_button_->GetPreferredSize().width() + kFullscreenPadding; 561 fullscreen_button_->GetPreferredSize().width() + kFullscreenPadding;
560 // Returned height doesn't matter as MenuItemView forces everything to the 562 // Returned height doesn't matter as MenuItemView forces everything to the
561 // height of the menuitemview. Note that we have overridden the height when 563 // height of the menuitemview. Note that we have overridden the height when
562 // constructing the menu. 564 // constructing the menu.
563 return gfx::Size(button_width + zoom_label_width_ + button_width + 565 return gfx::Size(
564 fullscreen_width, 0); 566 button_width + ZoomLabelMaxWidth() + button_width + fullscreen_width,
567 0);
565 } 568 }
566 569
567 void Layout() override { 570 void Layout() override {
568 int x = 0; 571 int x = 0;
569 int button_width = std::max(increment_button_->GetPreferredSize().width(), 572 int button_width = std::max(increment_button_->GetPreferredSize().width(),
570 decrement_button_->GetPreferredSize().width()); 573 decrement_button_->GetPreferredSize().width());
571 gfx::Rect bounds(0, 0, button_width, height()); 574 gfx::Rect bounds(0, 0, button_width, height());
572 575
573 decrement_button_->SetBoundsRect(bounds); 576 decrement_button_->SetBoundsRect(bounds);
574 577
575 x += bounds.width(); 578 x += bounds.width();
576 bounds.set_x(x); 579 bounds.set_x(x);
577 bounds.set_width(zoom_label_width_); 580 bounds.set_width(ZoomLabelMaxWidth());
578 zoom_label_->SetBoundsRect(bounds); 581 zoom_label_->SetBoundsRect(bounds);
579 582
580 x += bounds.width(); 583 x += bounds.width();
581 bounds.set_x(x); 584 bounds.set_x(x);
582 bounds.set_width(button_width); 585 bounds.set_width(button_width);
583 increment_button_->SetBoundsRect(bounds); 586 increment_button_->SetBoundsRect(bounds);
584 587
585 x += bounds.width(); 588 x += bounds.width();
586 bounds.set_x(x); 589 bounds.set_x(x);
587 bounds.set_width(fullscreen_button_->GetPreferredSize().width() + 590 bounds.set_width(fullscreen_button_->GetPreferredSize().width() +
588 kFullscreenPadding); 591 kFullscreenPadding);
589 fullscreen_button_->SetBoundsRect(bounds); 592 fullscreen_button_->SetBoundsRect(bounds);
590 } 593 }
591 594
592 void OnNativeThemeChanged(const ui::NativeTheme* theme) override { 595 void OnNativeThemeChanged(const ui::NativeTheme* theme) override {
593 WrenchMenuView::OnNativeThemeChanged(theme); 596 WrenchMenuView::OnNativeThemeChanged(theme);
594 597
595 const MenuConfig& menu_config = MenuConfig::instance(theme); 598 const MenuConfig& menu_config = MenuConfig::instance(theme);
596 zoom_label_->SetBorder(views::Border::CreateEmptyBorder( 599 zoom_label_->SetBorder(views::Border::CreateEmptyBorder(
597 0, kZoomLabelHorizontalPadding, 0, kZoomLabelHorizontalPadding)); 600 0, kZoomLabelHorizontalPadding, 0, kZoomLabelHorizontalPadding));
598 zoom_label_->SetFontList(menu_config.font_list); 601 zoom_label_->SetFontList(menu_config.font_list);
599 zoom_label_width_ = MaxWidthForZoomLabel(); 602 zoom_label_max_width_valid_ = false;
600 603
601 if (theme) { 604 if (theme) {
602 zoom_label_->SetEnabledColor(theme->GetSystemColor( 605 zoom_label_->SetEnabledColor(theme->GetSystemColor(
603 ui::NativeTheme::kColorId_EnabledMenuItemForegroundColor)); 606 ui::NativeTheme::kColorId_EnabledMenuItemForegroundColor));
604 gfx::ImageSkia* full_screen_image = 607 gfx::ImageSkia* full_screen_image =
605 ui::ResourceBundle::GetSharedInstance().GetImageSkiaNamed( 608 ui::ResourceBundle::GetSharedInstance().GetImageSkiaNamed(
606 IDR_FULLSCREEN_MENU_BUTTON); 609 IDR_FULLSCREEN_MENU_BUTTON);
607 SkColor fg_color = theme->GetSystemColor( 610 SkColor fg_color = theme->GetSystemColor(
608 ui::NativeTheme::kColorId_SelectedMenuItemForegroundColor); 611 ui::NativeTheme::kColorId_SelectedMenuItemForegroundColor);
609 gfx::ImageSkia hovered_fullscreen_image( 612 gfx::ImageSkia hovered_fullscreen_image(
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
644 if (zoom_controller) 647 if (zoom_controller)
645 zoom = zoom_controller->GetZoomPercent(); 648 zoom = zoom_controller->GetZoomPercent();
646 increment_button_->SetEnabled(zoom < 649 increment_button_->SetEnabled(zoom <
647 selected_tab->GetMaximumZoomPercent()); 650 selected_tab->GetMaximumZoomPercent());
648 decrement_button_->SetEnabled(zoom > 651 decrement_button_->SetEnabled(zoom >
649 selected_tab->GetMinimumZoomPercent()); 652 selected_tab->GetMinimumZoomPercent());
650 } 653 }
651 zoom_label_->SetText( 654 zoom_label_->SetText(
652 l10n_util::GetStringFUTF16Int(IDS_ZOOM_PERCENT, zoom)); 655 l10n_util::GetStringFUTF16Int(IDS_ZOOM_PERCENT, zoom));
653 656
654 zoom_label_width_ = MaxWidthForZoomLabel(); 657 zoom_label_max_width_valid_ = false;
655 } 658 }
656 659
657 // Calculates the max width the zoom string can be. 660 // Returns the max width the zoom string can be.
658 int MaxWidthForZoomLabel() { 661 int ZoomLabelMaxWidth() const {
659 const gfx::FontList& font_list = zoom_label_->font_list(); 662 if (!zoom_label_max_width_valid_) {
660 int border_width = 663 const gfx::FontList& font_list = zoom_label_->font_list();
661 zoom_label_->border() ? zoom_label_->border()->GetInsets().width() : 0; 664 int border_width = zoom_label_->border()
665 ? zoom_label_->border()->GetInsets().width()
666 : 0;
662 667
663 int max_w = 0; 668 int max_w = 0;
664 669
665 WebContents* selected_tab = 670 WebContents* selected_tab =
666 menu()->browser_->tab_strip_model()->GetActiveWebContents(); 671 menu()->browser_->tab_strip_model()->GetActiveWebContents();
667 if (selected_tab) { 672 if (selected_tab) {
668 int min_percent = selected_tab->GetMinimumZoomPercent(); 673 int min_percent = selected_tab->GetMinimumZoomPercent();
669 int max_percent = selected_tab->GetMaximumZoomPercent(); 674 int max_percent = selected_tab->GetMaximumZoomPercent();
670 675
671 int step = (max_percent - min_percent) / 10; 676 int step = (max_percent - min_percent) / 10;
672 for (int i = min_percent; i <= max_percent; i += step) { 677 for (int i = min_percent; i <= max_percent; i += step) {
673 int w = gfx::GetStringWidth( 678 int w = gfx::GetStringWidth(
674 l10n_util::GetStringFUTF16Int(IDS_ZOOM_PERCENT, i), font_list); 679 l10n_util::GetStringFUTF16Int(IDS_ZOOM_PERCENT, i), font_list);
675 max_w = std::max(w, max_w); 680 max_w = std::max(w, max_w);
681 }
682 } else {
683 max_w = gfx::GetStringWidth(
684 l10n_util::GetStringFUTF16Int(IDS_ZOOM_PERCENT, 100), font_list);
676 } 685 }
677 } else { 686 zoom_label_max_width_ = max_w + border_width;
678 max_w = gfx::GetStringWidth( 687
679 l10n_util::GetStringFUTF16Int(IDS_ZOOM_PERCENT, 100), font_list); 688 zoom_label_max_width_valid_ = true;
680 } 689 }
681 690 return zoom_label_max_width_;
682 return max_w + border_width;
683 } 691 }
684 692
685 // Index of the fullscreen menu item in the model. 693 // Index of the fullscreen menu item in the model.
686 const int fullscreen_index_; 694 const int fullscreen_index_;
687 695
688 scoped_ptr<content::HostZoomMap::Subscription> browser_zoom_subscription_; 696 scoped_ptr<content::HostZoomMap::Subscription> browser_zoom_subscription_;
689 content::NotificationRegistrar registrar_; 697 content::NotificationRegistrar registrar_;
690 698
691 // Button for incrementing the zoom. 699 // Button for incrementing the zoom.
692 LabelButton* increment_button_; 700 LabelButton* increment_button_;
693 701
694 // Label showing zoom as a percent. 702 // Label showing zoom as a percent.
695 Label* zoom_label_; 703 Label* zoom_label_;
696 704
697 // Button for decrementing the zoom. 705 // Button for decrementing the zoom.
698 LabelButton* decrement_button_; 706 LabelButton* decrement_button_;
699 707
700 ImageButton* fullscreen_button_; 708 ImageButton* fullscreen_button_;
701 709
702 // Width given to |zoom_label_|. This is the width at 100%. 710 // Cached width of how wide the zoom label string can be. This is the width at
703 int zoom_label_width_; 711 // 100%. This should not be accessed directly, use ZoomLabelMaxWidth()
712 // instead. This value is cached because is depends on multiple calls to
713 // gfx::GetStringWidth(...) which are expensive.
714 mutable int zoom_label_max_width_;
715
716 // Flag tracking whether calls to ZoomLabelMaxWidth() need to re-calculate
717 // the label width, because the cached value may no longer be correct.
718 mutable bool zoom_label_max_width_valid_;
704 719
705 DISALLOW_COPY_AND_ASSIGN(ZoomView); 720 DISALLOW_COPY_AND_ASSIGN(ZoomView);
706 }; 721 };
707 722
708 // RecentTabsMenuModelDelegate ------------------------------------------------ 723 // RecentTabsMenuModelDelegate ------------------------------------------------
709 724
710 // Provides the ui::MenuModelDelegate implementation for RecentTabsSubMenuModel 725 // Provides the ui::MenuModelDelegate implementation for RecentTabsSubMenuModel
711 // items. 726 // items.
712 class WrenchMenu::RecentTabsMenuModelDelegate : public ui::MenuModelDelegate { 727 class WrenchMenu::RecentTabsMenuModelDelegate : public ui::MenuModelDelegate {
713 public: 728 public:
(...skipping 535 matching lines...) Expand 10 before | Expand all | Expand 10 after
1249 0, 1264 0,
1250 BookmarkMenuDelegate::SHOW_PERMANENT_FOLDERS, 1265 BookmarkMenuDelegate::SHOW_PERMANENT_FOLDERS,
1251 BOOKMARK_LAUNCH_LOCATION_WRENCH_MENU); 1266 BOOKMARK_LAUNCH_LOCATION_WRENCH_MENU);
1252 } 1267 }
1253 1268
1254 int WrenchMenu::ModelIndexFromCommandId(int command_id) const { 1269 int WrenchMenu::ModelIndexFromCommandId(int command_id) const {
1255 CommandIDToEntry::const_iterator ix = command_id_to_entry_.find(command_id); 1270 CommandIDToEntry::const_iterator ix = command_id_to_entry_.find(command_id);
1256 DCHECK(ix != command_id_to_entry_.end()); 1271 DCHECK(ix != command_id_to_entry_.end());
1257 return ix->second.second; 1272 return ix->second.second;
1258 } 1273 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698