OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/browser_actions_container.h" | 5 #include "chrome/browser/ui/views/browser_actions_container.h" |
6 | 6 |
7 #include "base/stl_util.h" | 7 #include "base/stl_util.h" |
8 #include "chrome/browser/extensions/extension_service.h" | 8 #include "chrome/browser/extensions/extension_service.h" |
9 #include "chrome/browser/prefs/pref_service.h" | 9 #include "chrome/browser/prefs/pref_service.h" |
10 #include "chrome/browser/profiles/profile.h" | 10 #include "chrome/browser/profiles/profile.h" |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
121 // TODO(mpcomplete): remove this after users are upgraded to 5.0. | 121 // TODO(mpcomplete): remove this after users are upgraded to 5.0. |
122 int predefined_width = | 122 int predefined_width = |
123 profile_->GetPrefs()->GetInteger(prefs::kBrowserActionContainerWidth); | 123 profile_->GetPrefs()->GetInteger(prefs::kBrowserActionContainerWidth); |
124 if (predefined_width != 0) | 124 if (predefined_width != 0) |
125 model_->SetVisibleIconCount(WidthToIconCount(predefined_width)); | 125 model_->SetVisibleIconCount(WidthToIconCount(predefined_width)); |
126 } | 126 } |
127 if (model_ && model_->extensions_initialized()) | 127 if (model_ && model_->extensions_initialized()) |
128 SetContainerWidth(); | 128 SetContainerWidth(); |
129 } | 129 } |
130 | 130 |
131 int BrowserActionsContainer::GetCurrentTabId() const { | |
132 TabContents* tab = chrome::GetActiveTabContents(browser_); | |
133 return tab ? tab->restore_tab_helper()->session_id().id() : -1; | |
134 } | |
135 | |
136 BrowserActionView* BrowserActionsContainer::GetBrowserActionView( | 131 BrowserActionView* BrowserActionsContainer::GetBrowserActionView( |
137 ExtensionAction* action) { | 132 ExtensionAction* action) { |
138 for (BrowserActionViews::iterator i(browser_action_views_.begin()); | 133 for (BrowserActionViews::iterator i(browser_action_views_.begin()); |
139 i != browser_action_views_.end(); ++i) { | 134 i != browser_action_views_.end(); ++i) { |
140 if ((*i)->button()->browser_action() == action) | 135 if ((*i)->button()->browser_action() == action) |
141 return *i; | 136 return *i; |
142 } | 137 } |
143 return NULL; | 138 return NULL; |
144 } | 139 } |
145 | 140 |
146 void BrowserActionsContainer::RefreshBrowserActionViews() { | 141 void BrowserActionsContainer::RefreshBrowserActionViews() { |
147 for (size_t i = 0; i < browser_action_views_.size(); ++i) | 142 for (size_t i = 0; i < browser_action_views_.size(); ++i) |
148 browser_action_views_[i]->button()->UpdateState(); | 143 browser_action_views_[i]->button()->UpdateState(); |
149 } | 144 } |
150 | 145 |
151 void BrowserActionsContainer::CreateBrowserActionViews() { | 146 void BrowserActionsContainer::CreateBrowserActionViews() { |
152 DCHECK(browser_action_views_.empty()); | 147 DCHECK(browser_action_views_.empty()); |
153 if (!model_) | 148 if (!model_) |
154 return; | 149 return; |
155 | 150 |
156 for (extensions::ExtensionList::iterator i(model_->begin()); | 151 const extensions::ExtensionList& toolbar_items = model_->toolbar_items(); |
157 i != model_->end(); ++i) { | 152 for (extensions::ExtensionList::const_iterator i(toolbar_items.begin()); |
| 153 i != toolbar_items.end(); ++i) { |
158 if (!ShouldDisplayBrowserAction(*i)) | 154 if (!ShouldDisplayBrowserAction(*i)) |
159 continue; | 155 continue; |
160 | 156 |
161 BrowserActionView* view = new BrowserActionView(*i, this); | 157 BrowserActionView* view = new BrowserActionView(*i, browser_, this); |
162 browser_action_views_.push_back(view); | 158 browser_action_views_.push_back(view); |
163 AddChildView(view); | 159 AddChildView(view); |
164 } | 160 } |
165 } | 161 } |
166 | 162 |
167 void BrowserActionsContainer::DeleteBrowserActionViews() { | 163 void BrowserActionsContainer::DeleteBrowserActionViews() { |
168 HidePopup(); | 164 HidePopup(); |
169 STLDeleteElements(&browser_action_views_); | 165 STLDeleteElements(&browser_action_views_); |
170 } | 166 } |
171 | 167 |
172 void BrowserActionsContainer::OnBrowserActionVisibilityChanged() { | |
173 SetVisible(!browser_action_views_.empty()); | |
174 owner_view_->Layout(); | |
175 owner_view_->SchedulePaint(); | |
176 } | |
177 | |
178 size_t BrowserActionsContainer::VisibleBrowserActions() const { | 168 size_t BrowserActionsContainer::VisibleBrowserActions() const { |
179 size_t visible_actions = 0; | 169 size_t visible_actions = 0; |
180 for (size_t i = 0; i < browser_action_views_.size(); ++i) { | 170 for (size_t i = 0; i < browser_action_views_.size(); ++i) { |
181 if (browser_action_views_[i]->visible()) | 171 if (browser_action_views_[i]->visible()) |
182 ++visible_actions; | 172 ++visible_actions; |
183 } | 173 } |
184 return visible_actions; | 174 return visible_actions; |
185 } | 175 } |
186 | 176 |
187 void BrowserActionsContainer::OnBrowserActionExecuted( | |
188 BrowserActionButton* button) { | |
189 const Extension* extension = button->extension(); | |
190 GURL popup_url; | |
191 switch (model_->ExecuteBrowserAction(extension, browser_, &popup_url)) { | |
192 case ExtensionToolbarModel::ACTION_NONE: | |
193 break; | |
194 case ExtensionToolbarModel::ACTION_SHOW_POPUP: | |
195 ShowPopup(button, popup_url); | |
196 break; | |
197 } | |
198 } | |
199 | |
200 gfx::Size BrowserActionsContainer::GetPreferredSize() { | 177 gfx::Size BrowserActionsContainer::GetPreferredSize() { |
201 if (browser_action_views_.empty()) | 178 if (browser_action_views_.empty()) |
202 return gfx::Size(ToolbarView::kStandardSpacing, 0); | 179 return gfx::Size(ToolbarView::kStandardSpacing, 0); |
203 | 180 |
204 // We calculate the size of the view by taking the current width and | 181 // We calculate the size of the view by taking the current width and |
205 // subtracting resize_amount_ (the latter represents how far the user is | 182 // subtracting resize_amount_ (the latter represents how far the user is |
206 // resizing the view or, if animating the snapping, how far to animate it). | 183 // resizing the view or, if animating the snapping, how far to animate it). |
207 // But we also clamp it to a minimum size and the maximum size, so that the | 184 // But we also clamp it to a minimum size and the maximum size, so that the |
208 // container can never shrink too far or take up more space than it needs. In | 185 // container can never shrink too far or take up more space than it needs. In |
209 // other words: ContainerMinSize() < width() - resize < ClampTo(MAX). | 186 // other words: ContainerMinSize() < width() - resize < ClampTo(MAX). |
(...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
381 void BrowserActionsContainer::GetAccessibleState( | 358 void BrowserActionsContainer::GetAccessibleState( |
382 ui::AccessibleViewState* state) { | 359 ui::AccessibleViewState* state) { |
383 state->role = ui::AccessibilityTypes::ROLE_GROUPING; | 360 state->role = ui::AccessibilityTypes::ROLE_GROUPING; |
384 state->name = l10n_util::GetStringUTF16(IDS_ACCNAME_EXTENSIONS); | 361 state->name = l10n_util::GetStringUTF16(IDS_ACCNAME_EXTENSIONS); |
385 } | 362 } |
386 | 363 |
387 void BrowserActionsContainer::OnMenuButtonClicked(views::View* source, | 364 void BrowserActionsContainer::OnMenuButtonClicked(views::View* source, |
388 const gfx::Point& point) { | 365 const gfx::Point& point) { |
389 if (source == chevron_) { | 366 if (source == chevron_) { |
390 overflow_menu_ = new BrowserActionOverflowMenuController( | 367 overflow_menu_ = new BrowserActionOverflowMenuController( |
391 this, chevron_, browser_action_views_, VisibleBrowserActions()); | 368 this, browser_, chevron_, browser_action_views_, |
| 369 VisibleBrowserActions()); |
392 overflow_menu_->set_observer(this); | 370 overflow_menu_->set_observer(this); |
393 overflow_menu_->RunMenu(GetWidget(), false); | 371 overflow_menu_->RunMenu(GetWidget(), false); |
394 } | 372 } |
395 } | 373 } |
396 | 374 |
397 void BrowserActionsContainer::WriteDragDataForView(View* sender, | 375 void BrowserActionsContainer::WriteDragDataForView(View* sender, |
398 const gfx::Point& press_pt, | 376 const gfx::Point& press_pt, |
399 OSExchangeData* data) { | 377 OSExchangeData* data) { |
400 DCHECK(data); | 378 DCHECK(data); |
401 | 379 |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
471 DCHECK_EQ(popup_->GetWidget(), widget); | 449 DCHECK_EQ(popup_->GetWidget(), widget); |
472 popup_->GetWidget()->RemoveObserver(this); | 450 popup_->GetWidget()->RemoveObserver(this); |
473 popup_ = NULL; | 451 popup_ = NULL; |
474 // |popup_button_| is NULL if the extension has been removed. | 452 // |popup_button_| is NULL if the extension has been removed. |
475 if (popup_button_) { | 453 if (popup_button_) { |
476 popup_button_->SetButtonNotPushed(); | 454 popup_button_->SetButtonNotPushed(); |
477 popup_button_ = NULL; | 455 popup_button_ = NULL; |
478 } | 456 } |
479 } | 457 } |
480 | 458 |
| 459 int BrowserActionsContainer::GetCurrentTabId() const { |
| 460 TabContents* tab = chrome::GetActiveTabContents(browser_); |
| 461 return tab ? tab->restore_tab_helper()->session_id().id() : -1; |
| 462 } |
| 463 |
| 464 void BrowserActionsContainer::OnBrowserActionExecuted( |
| 465 BrowserActionButton* button) { |
| 466 const Extension* extension = button->extension(); |
| 467 GURL popup_url; |
| 468 if (model_->ExecuteBrowserAction(extension, browser_, &popup_url) == |
| 469 ExtensionToolbarModel::ACTION_SHOW_POPUP) |
| 470 ShowPopup(button, popup_url); |
| 471 } |
| 472 |
| 473 void BrowserActionsContainer::OnBrowserActionVisibilityChanged() { |
| 474 SetVisible(!browser_action_views_.empty()); |
| 475 owner_view_->Layout(); |
| 476 owner_view_->SchedulePaint(); |
| 477 } |
| 478 |
| 479 gfx::Point BrowserActionsContainer::GetViewContentOffset() const { |
| 480 return gfx::Point(0, ToolbarView::kVertSpacing); |
| 481 } |
| 482 |
481 void BrowserActionsContainer::MoveBrowserAction(const std::string& extension_id, | 483 void BrowserActionsContainer::MoveBrowserAction(const std::string& extension_id, |
482 size_t new_index) { | 484 size_t new_index) { |
483 ExtensionService* service = profile_->GetExtensionService(); | 485 ExtensionService* service = profile_->GetExtensionService(); |
484 if (service) { | 486 if (service) { |
485 const Extension* extension = service->GetExtensionById(extension_id, false); | 487 const Extension* extension = service->GetExtensionById(extension_id, false); |
486 model_->MoveBrowserAction(extension, new_index); | 488 model_->MoveBrowserAction(extension, new_index); |
487 SchedulePaint(); | 489 SchedulePaint(); |
488 } | 490 } |
489 } | 491 } |
490 | 492 |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
587 CloseOverflowMenu(); | 589 CloseOverflowMenu(); |
588 | 590 |
589 if (!ShouldDisplayBrowserAction(extension)) | 591 if (!ShouldDisplayBrowserAction(extension)) |
590 return; | 592 return; |
591 | 593 |
592 size_t visible_actions = VisibleBrowserActions(); | 594 size_t visible_actions = VisibleBrowserActions(); |
593 | 595 |
594 // Add the new browser action to the vector and the view hierarchy. | 596 // Add the new browser action to the vector and the view hierarchy. |
595 if (profile_->IsOffTheRecord()) | 597 if (profile_->IsOffTheRecord()) |
596 index = model_->OriginalIndexToIncognito(index); | 598 index = model_->OriginalIndexToIncognito(index); |
597 BrowserActionView* view = new BrowserActionView(extension, this); | 599 BrowserActionView* view = new BrowserActionView(extension, browser_, this); |
598 browser_action_views_.insert(browser_action_views_.begin() + index, view); | 600 browser_action_views_.insert(browser_action_views_.begin() + index, view); |
599 AddChildViewAt(view, index); | 601 AddChildViewAt(view, index); |
600 | 602 |
601 // If we are still initializing the container, don't bother animating. | 603 // If we are still initializing the container, don't bother animating. |
602 if (!model_->extensions_initialized()) | 604 if (!model_->extensions_initialized()) |
603 return; | 605 return; |
604 | 606 |
605 // Enlarge the container if it was already at maximum size and we're not in | 607 // Enlarge the container if it was already at maximum size and we're not in |
606 // the middle of upgrading. | 608 // the middle of upgrading. |
607 if ((model_->GetVisibleIconCount() < 0) && | 609 if ((model_->GetVisibleIconCount() < 0) && |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
673 void BrowserActionsContainer::LoadImages() { | 675 void BrowserActionsContainer::LoadImages() { |
674 ui::ThemeProvider* tp = GetThemeProvider(); | 676 ui::ThemeProvider* tp = GetThemeProvider(); |
675 chevron_->SetIcon(*tp->GetBitmapNamed(IDR_BROWSER_ACTIONS_OVERFLOW)); | 677 chevron_->SetIcon(*tp->GetBitmapNamed(IDR_BROWSER_ACTIONS_OVERFLOW)); |
676 chevron_->SetHoverIcon(*tp->GetBitmapNamed(IDR_BROWSER_ACTIONS_OVERFLOW_H)); | 678 chevron_->SetHoverIcon(*tp->GetBitmapNamed(IDR_BROWSER_ACTIONS_OVERFLOW_H)); |
677 chevron_->SetPushedIcon(*tp->GetBitmapNamed(IDR_BROWSER_ACTIONS_OVERFLOW_P)); | 679 chevron_->SetPushedIcon(*tp->GetBitmapNamed(IDR_BROWSER_ACTIONS_OVERFLOW_P)); |
678 } | 680 } |
679 | 681 |
680 void BrowserActionsContainer::SetContainerWidth() { | 682 void BrowserActionsContainer::SetContainerWidth() { |
681 int visible_actions = model_->GetVisibleIconCount(); | 683 int visible_actions = model_->GetVisibleIconCount(); |
682 if (visible_actions < 0) // All icons should be visible. | 684 if (visible_actions < 0) // All icons should be visible. |
683 visible_actions = model_->size(); | 685 visible_actions = model_->toolbar_items().size(); |
684 chevron_->SetVisible(static_cast<size_t>(visible_actions) < model_->size()); | 686 chevron_->SetVisible( |
| 687 static_cast<size_t>(visible_actions) < model_->toolbar_items().size()); |
685 container_width_ = IconCountToWidth(visible_actions, chevron_->visible()); | 688 container_width_ = IconCountToWidth(visible_actions, chevron_->visible()); |
686 } | 689 } |
687 | 690 |
688 void BrowserActionsContainer::CloseOverflowMenu() { | 691 void BrowserActionsContainer::CloseOverflowMenu() { |
689 if (overflow_menu_) | 692 if (overflow_menu_) |
690 overflow_menu_->CancelMenu(); | 693 overflow_menu_->CancelMenu(); |
691 } | 694 } |
692 | 695 |
693 void BrowserActionsContainer::StopShowFolderDropMenuTimer() { | 696 void BrowserActionsContainer::StopShowFolderDropMenuTimer() { |
694 show_menu_task_factory_.InvalidateWeakPtrs(); | 697 show_menu_task_factory_.InvalidateWeakPtrs(); |
695 } | 698 } |
696 | 699 |
697 void BrowserActionsContainer::StartShowFolderDropMenuTimer() { | 700 void BrowserActionsContainer::StartShowFolderDropMenuTimer() { |
698 MessageLoop::current()->PostDelayedTask( | 701 MessageLoop::current()->PostDelayedTask( |
699 FROM_HERE, | 702 FROM_HERE, |
700 base::Bind(&BrowserActionsContainer::ShowDropFolder, | 703 base::Bind(&BrowserActionsContainer::ShowDropFolder, |
701 show_menu_task_factory_.GetWeakPtr()), | 704 show_menu_task_factory_.GetWeakPtr()), |
702 base::TimeDelta::FromMilliseconds(views::GetMenuShowDelay())); | 705 base::TimeDelta::FromMilliseconds(views::GetMenuShowDelay())); |
703 } | 706 } |
704 | 707 |
705 void BrowserActionsContainer::ShowDropFolder() { | 708 void BrowserActionsContainer::ShowDropFolder() { |
706 DCHECK(!overflow_menu_); | 709 DCHECK(!overflow_menu_); |
707 SetDropIndicator(-1); | 710 SetDropIndicator(-1); |
708 overflow_menu_ = new BrowserActionOverflowMenuController( | 711 overflow_menu_ = new BrowserActionOverflowMenuController( |
709 this, chevron_, browser_action_views_, VisibleBrowserActions()); | 712 this, browser_, chevron_, browser_action_views_, VisibleBrowserActions()); |
710 overflow_menu_->set_observer(this); | 713 overflow_menu_->set_observer(this); |
711 overflow_menu_->RunMenu(GetWidget(), true); | 714 overflow_menu_->RunMenu(GetWidget(), true); |
712 } | 715 } |
713 | 716 |
714 void BrowserActionsContainer::SetDropIndicator(int x_pos) { | 717 void BrowserActionsContainer::SetDropIndicator(int x_pos) { |
715 if (drop_indicator_position_ != x_pos) { | 718 if (drop_indicator_position_ != x_pos) { |
716 drop_indicator_position_ = x_pos; | 719 drop_indicator_position_ = x_pos; |
717 SchedulePaint(); | 720 SchedulePaint(); |
718 } | 721 } |
719 } | 722 } |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
807 views::BubbleBorder::ArrowLocation arrow_location = base::i18n::IsRTL() ? | 810 views::BubbleBorder::ArrowLocation arrow_location = base::i18n::IsRTL() ? |
808 views::BubbleBorder::TOP_LEFT : views::BubbleBorder::TOP_RIGHT; | 811 views::BubbleBorder::TOP_LEFT : views::BubbleBorder::TOP_RIGHT; |
809 popup_ = ExtensionPopup::ShowPopup(popup_url, | 812 popup_ = ExtensionPopup::ShowPopup(popup_url, |
810 browser_, | 813 browser_, |
811 reference_view, | 814 reference_view, |
812 arrow_location); | 815 arrow_location); |
813 popup_->GetWidget()->AddObserver(this); | 816 popup_->GetWidget()->AddObserver(this); |
814 popup_button_ = button; | 817 popup_button_ = button; |
815 popup_button_->SetButtonPushed(); | 818 popup_button_->SetButtonPushed(); |
816 } | 819 } |
OLD | NEW |