OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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/views/bookmark_bar_view.h" | 5 #include "chrome/browser/views/bookmark_bar_view.h" |
6 | 6 |
7 #include <limits> | 7 #include <limits> |
8 | 8 |
9 #include "base/string_util.h" | 9 #include "base/string_util.h" |
10 #include "base/base_drag_source.h" | 10 #include "base/base_drag_source.h" |
11 #include "chrome/browser/bookmarks/bookmark_context_menu.h" | 11 #include "chrome/browser/bookmarks/bookmark_context_menu.h" |
12 #include "chrome/browser/bookmarks/bookmark_utils.h" | 12 #include "chrome/browser/bookmarks/bookmark_utils.h" |
13 #include "chrome/browser/browser.h" | 13 #include "chrome/browser/browser.h" |
14 #include "chrome/browser/browser_list.h" | 14 #include "chrome/browser/browser_list.h" |
15 #include "chrome/browser/browser_process.h" | 15 #include "chrome/browser/browser_process.h" |
16 #include "chrome/browser/browser_window.h" | 16 #include "chrome/browser/browser_window.h" |
17 #include "chrome/browser/drag_utils.h" | 17 #include "chrome/browser/drag_utils.h" |
18 #include "chrome/browser/download/download_util.h" | 18 #include "chrome/browser/download/download_util.h" |
| 19 #include "chrome/browser/extensions/extension.h" |
| 20 #include "chrome/browser/extensions/extension_view.h" |
| 21 #include "chrome/browser/extensions/extensions_service.h" |
19 #include "chrome/browser/history/history.h" | 22 #include "chrome/browser/history/history.h" |
20 #include "chrome/browser/metrics/user_metrics.h" | 23 #include "chrome/browser/metrics/user_metrics.h" |
21 #include "chrome/browser/profile.h" | 24 #include "chrome/browser/profile.h" |
22 #include "chrome/browser/tab_contents/page_navigator.h" | 25 #include "chrome/browser/tab_contents/page_navigator.h" |
23 #include "chrome/browser/tab_contents/tab_contents.h" | 26 #include "chrome/browser/tab_contents/tab_contents.h" |
24 #include "chrome/browser/view_ids.h" | 27 #include "chrome/browser/view_ids.h" |
25 #include "chrome/browser/views/bookmark_editor_view.h" | 28 #include "chrome/browser/views/bookmark_editor_view.h" |
26 #include "chrome/browser/views/event_utils.h" | 29 #include "chrome/browser/views/event_utils.h" |
27 #include "chrome/browser/views/frame/browser_view.h" | 30 #include "chrome/browser/views/frame/browser_view.h" |
28 #include "chrome/browser/views/input_window.h" | 31 #include "chrome/browser/views/input_window.h" |
(...skipping 279 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
308 BookmarkButton::PaintAnimation(this, canvas, | 311 BookmarkButton::PaintAnimation(this, canvas, |
309 show_animation_->GetCurrentValue()); | 312 show_animation_->GetCurrentValue()); |
310 } | 313 } |
311 | 314 |
312 private: | 315 private: |
313 scoped_ptr<SlideAnimation> show_animation_; | 316 scoped_ptr<SlideAnimation> show_animation_; |
314 | 317 |
315 DISALLOW_COPY_AND_ASSIGN(BookmarkFolderButton); | 318 DISALLOW_COPY_AND_ASSIGN(BookmarkFolderButton); |
316 }; | 319 }; |
317 | 320 |
| 321 // ExtensionToolstrip ------------------------------------------------------- |
| 322 |
| 323 // A simple container with a border for an ExtensionView. |
| 324 class ExtensionToolstrip : public views::View { |
| 325 public: |
| 326 static const int kPadding = 2; |
| 327 |
| 328 ExtensionToolstrip(const GURL& url, Profile* profile) |
| 329 : view_(new ExtensionView(url, profile)) { |
| 330 AddChildView(view_); |
| 331 set_border(views::Border::CreateEmptyBorder( |
| 332 kPadding, kPadding, kPadding, kPadding)); |
| 333 } |
| 334 |
| 335 virtual gfx::Size GetPreferredSize() { |
| 336 gfx::Size size = view_->GetPreferredSize(); |
| 337 size.Enlarge(kPadding*2, kPadding*2); |
| 338 return size; |
| 339 } |
| 340 |
| 341 virtual void DidChangeBounds(const gfx::Rect& previous, |
| 342 const gfx::Rect& current) { |
| 343 view_->SetBounds(GetLocalBounds(false)); |
| 344 } |
| 345 |
| 346 private: |
| 347 ExtensionView* view_; |
| 348 |
| 349 DISALLOW_COPY_AND_ASSIGN(ExtensionToolstrip); |
| 350 }; |
| 351 |
318 // DropInfo ------------------------------------------------------------------- | 352 // DropInfo ------------------------------------------------------------------- |
319 | 353 |
320 // Tracks drops on the BookmarkBarView. | 354 // Tracks drops on the BookmarkBarView. |
321 | 355 |
322 struct DropInfo { | 356 struct DropInfo { |
323 DropInfo() : drop_index(-1), is_menu_showing(false), valid(false) {} | 357 DropInfo() : drop_index(-1), is_menu_showing(false), valid(false) {} |
324 | 358 |
325 // Whether the data is valid. | 359 // Whether the data is valid. |
326 bool valid; | 360 bool valid; |
327 | 361 |
(...skipping 354 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
682 : profile_(NULL), | 716 : profile_(NULL), |
683 browser_(browser), | 717 browser_(browser), |
684 page_navigator_(NULL), | 718 page_navigator_(NULL), |
685 model_(NULL), | 719 model_(NULL), |
686 other_bookmarked_button_(NULL), | 720 other_bookmarked_button_(NULL), |
687 model_changed_listener_(NULL), | 721 model_changed_listener_(NULL), |
688 show_folder_drop_menu_task_(NULL), | 722 show_folder_drop_menu_task_(NULL), |
689 overflow_button_(NULL), | 723 overflow_button_(NULL), |
690 instructions_(NULL), | 724 instructions_(NULL), |
691 bookmarks_separator_view_(NULL), | 725 bookmarks_separator_view_(NULL), |
692 throbbing_view_(NULL) { | 726 throbbing_view_(NULL), |
| 727 num_extension_toolstrips_(0) { |
693 SetID(VIEW_ID_BOOKMARK_BAR); | 728 SetID(VIEW_ID_BOOKMARK_BAR); |
694 Init(); | 729 Init(); |
695 SetProfile(profile); | 730 SetProfile(profile); |
696 | 731 |
697 | 732 |
698 if (IsAlwaysShown()) { | 733 if (IsAlwaysShown()) { |
699 size_animation_->Reset(1); | 734 size_animation_->Reset(1); |
700 } else { | 735 } else { |
701 size_animation_->Reset(0); | 736 size_animation_->Reset(0); |
702 } | 737 } |
(...skipping 10 matching lines...) Expand all Loading... |
713 void BookmarkBarView::SetProfile(Profile* profile) { | 748 void BookmarkBarView::SetProfile(Profile* profile) { |
714 DCHECK(profile); | 749 DCHECK(profile); |
715 if (profile_ == profile) | 750 if (profile_ == profile) |
716 return; | 751 return; |
717 | 752 |
718 StopThrobbing(true); | 753 StopThrobbing(true); |
719 | 754 |
720 // Cancels the current cancelable. | 755 // Cancels the current cancelable. |
721 NotifyModelChanged(); | 756 NotifyModelChanged(); |
722 | 757 |
723 // Remove the current buttons. | 758 // Remove the current buttons and extension toolstrips. |
724 for (int i = GetBookmarkButtonCount() - 1; i >= 0; --i) { | 759 for (int i = GetBookmarkButtonCount() + num_extension_toolstrips_ - 1; |
| 760 i >= 0; --i) { |
725 View* child = GetChildViewAt(i); | 761 View* child = GetChildViewAt(i); |
726 RemoveChildView(child); | 762 RemoveChildView(child); |
727 delete child; | 763 delete child; |
728 } | 764 } |
729 | 765 |
730 profile_ = profile; | 766 profile_ = profile; |
731 | 767 |
732 if (model_) | 768 if (model_) |
733 model_->RemoveObserver(this); | 769 model_->RemoveObserver(this); |
734 | 770 |
735 // Disable the other bookmarked button, we'll re-enable when the model is | 771 // Disable the other bookmarked button, we'll re-enable when the model is |
736 // loaded. | 772 // loaded. |
737 other_bookmarked_button_->SetEnabled(false); | 773 other_bookmarked_button_->SetEnabled(false); |
738 | 774 |
739 NotificationService* ns = NotificationService::current(); | 775 NotificationService* ns = NotificationService::current(); |
740 Source<Profile> ns_source(profile_->GetOriginalProfile()); | 776 Source<Profile> ns_source(profile_->GetOriginalProfile()); |
741 ns->AddObserver(this, NotificationType::BOOKMARK_BUBBLE_SHOWN, ns_source); | 777 ns->AddObserver(this, NotificationType::BOOKMARK_BUBBLE_SHOWN, ns_source); |
742 ns->AddObserver(this, NotificationType::BOOKMARK_BUBBLE_HIDDEN, ns_source); | 778 ns->AddObserver(this, NotificationType::BOOKMARK_BUBBLE_HIDDEN, ns_source); |
743 ns->AddObserver(this, NotificationType::BOOKMARK_BAR_VISIBILITY_PREF_CHANGED, | 779 ns->AddObserver(this, NotificationType::BOOKMARK_BAR_VISIBILITY_PREF_CHANGED, |
744 NotificationService::AllSources()); | 780 NotificationService::AllSources()); |
| 781 ns->AddObserver(this, NotificationType::EXTENSIONS_LOADED, |
| 782 NotificationService::AllSources()); |
745 | 783 |
746 model_ = profile_->GetBookmarkModel(); | 784 model_ = profile_->GetBookmarkModel(); |
747 model_->AddObserver(this); | 785 model_->AddObserver(this); |
748 if (model_->IsLoaded()) | 786 if (model_->IsLoaded()) |
749 Loaded(model_); | 787 Loaded(model_); |
750 // else case: we'll receive notification back from the BookmarkModel when done | 788 // else case: we'll receive notification back from the BookmarkModel when done |
751 // loading, then we'll populate the bar. | 789 // loading, then we'll populate the bar. |
752 } | 790 } |
753 | 791 |
754 void BookmarkBarView::SetPageNavigator(PageNavigator* navigator) { | 792 void BookmarkBarView::SetPageNavigator(PageNavigator* navigator) { |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
828 for (int i = 0; i < GetBookmarkButtonCount(); ++i) { | 866 for (int i = 0; i < GetBookmarkButtonCount(); ++i) { |
829 views::View* child = GetChildViewAt(i); | 867 views::View* child = GetChildViewAt(i); |
830 gfx::Size pref = child->GetPreferredSize(); | 868 gfx::Size pref = child->GetPreferredSize(); |
831 int next_x = x + pref.width() + kButtonPadding; | 869 int next_x = x + pref.width() + kButtonPadding; |
832 child->SetVisible(next_x < max_x); | 870 child->SetVisible(next_x < max_x); |
833 child->SetBounds(x, y, pref.width(), height); | 871 child->SetBounds(x, y, pref.width(), height); |
834 x = next_x; | 872 x = next_x; |
835 } | 873 } |
836 } | 874 } |
837 | 875 |
| 876 // Extension toolstrips. |
| 877 for (int i = GetBookmarkButtonCount(); |
| 878 i < GetBookmarkButtonCount() + num_extension_toolstrips_; ++i) { |
| 879 views::View* child = GetChildViewAt(i); |
| 880 gfx::Size pref = child->GetPreferredSize(); |
| 881 int next_x = x + pref.width() + kButtonPadding; |
| 882 child->SetVisible(next_x < max_x); |
| 883 child->SetBounds(x, y, pref.width(), height); |
| 884 x = next_x; |
| 885 } |
| 886 |
838 // Layout the right side of the bar. | 887 // Layout the right side of the bar. |
839 const bool all_visible = | 888 const bool all_visible = |
840 (GetBookmarkButtonCount() == 0 || | 889 (GetBookmarkButtonCount() == 0 || |
841 GetChildViewAt(GetBookmarkButtonCount() - 1)->IsVisible()); | 890 GetChildViewAt(GetBookmarkButtonCount() - 1)->IsVisible()); |
842 | 891 |
843 // Layout the right side buttons. | 892 // Layout the right side buttons. |
844 x = max_x + kButtonPadding; | 893 x = max_x + kButtonPadding; |
845 | 894 |
846 // The overflow button. | 895 // The overflow button. |
847 overflow_button_->SetBounds(x, y, overflow_pref.width(), height); | 896 overflow_button_->SetBounds(x, y, overflow_pref.width(), height); |
(...skipping 363 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1211 // text and flipping the ChromeCanvas object will break text rendering. Since | 1260 // text and flipping the ChromeCanvas object will break text rendering. Since |
1212 // the overflow button does not contain text, we can safely flip it. | 1261 // the overflow button does not contain text, we can safely flip it. |
1213 button->EnableCanvasFlippingForRTLUI(true); | 1262 button->EnableCanvasFlippingForRTLUI(true); |
1214 | 1263 |
1215 // Make visible as necessary. | 1264 // Make visible as necessary. |
1216 button->SetVisible(false); | 1265 button->SetVisible(false); |
1217 return button; | 1266 return button; |
1218 } | 1267 } |
1219 | 1268 |
1220 int BookmarkBarView::GetBookmarkButtonCount() { | 1269 int BookmarkBarView::GetBookmarkButtonCount() { |
1221 // We contain four non-bookmark button views: recently bookmarked, | 1270 // We contain at least four non-bookmark button views: recently bookmarked, |
1222 // bookmarks separator, chevrons (for overflow) and the instruction | 1271 // bookmarks separator, chevrons (for overflow), the instruction |
1223 // label. | 1272 // label, as well as any ExtensionViews displaying toolstrips. |
1224 return GetChildViewCount() - 4; | 1273 return GetChildViewCount() - 4 - num_extension_toolstrips_; |
1225 } | 1274 } |
1226 | 1275 |
1227 void BookmarkBarView::Loaded(BookmarkModel* model) { | 1276 void BookmarkBarView::Loaded(BookmarkModel* model) { |
1228 BookmarkNode* node = model_->GetBookmarkBarNode(); | 1277 BookmarkNode* node = model_->GetBookmarkBarNode(); |
1229 DCHECK(node && model_->other_node()); | 1278 DCHECK(node && model_->other_node()); |
1230 // Create a button for each of the children on the bookmark bar. | 1279 // Create a button for each of the children on the bookmark bar. |
1231 for (int i = 0; i < node->GetChildCount(); ++i) | 1280 for (int i = 0; i < node->GetChildCount(); ++i) |
1232 AddChildView(i, CreateBookmarkButton(node->GetChild(i))); | 1281 AddChildView(i, CreateBookmarkButton(node->GetChild(i))); |
1233 other_bookmarked_button_->SetEnabled(true); | 1282 other_bookmarked_button_->SetEnabled(true); |
| 1283 |
| 1284 AddExtensionToolstrips(profile_->GetExtensionsService()->extensions()); |
| 1285 |
1234 Layout(); | 1286 Layout(); |
1235 SchedulePaint(); | 1287 SchedulePaint(); |
1236 } | 1288 } |
1237 | 1289 |
1238 void BookmarkBarView::BookmarkModelBeingDeleted(BookmarkModel* model) { | 1290 void BookmarkBarView::BookmarkModelBeingDeleted(BookmarkModel* model) { |
1239 // The bookmark model should never be deleted before us. This code exists | 1291 // The bookmark model should never be deleted before us. This code exists |
1240 // to check for regressions in shutdown code and not crash. | 1292 // to check for regressions in shutdown code and not crash. |
1241 NOTREACHED(); | 1293 NOTREACHED(); |
1242 | 1294 |
1243 // Do minimal cleanup, presumably we'll be deleted shortly. | 1295 // Do minimal cleanup, presumably we'll be deleted shortly. |
(...skipping 331 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1575 case NotificationType::BOOKMARK_BUBBLE_SHOWN: | 1627 case NotificationType::BOOKMARK_BUBBLE_SHOWN: |
1576 StopThrobbing(true); | 1628 StopThrobbing(true); |
1577 bubble_url_ = *(Details<GURL>(details).ptr()); | 1629 bubble_url_ = *(Details<GURL>(details).ptr()); |
1578 StartThrobbing(); | 1630 StartThrobbing(); |
1579 break; | 1631 break; |
1580 | 1632 |
1581 case NotificationType::BOOKMARK_BUBBLE_HIDDEN: | 1633 case NotificationType::BOOKMARK_BUBBLE_HIDDEN: |
1582 StopThrobbing(false); | 1634 StopThrobbing(false); |
1583 bubble_url_ = GURL(); | 1635 bubble_url_ = GURL(); |
1584 break; | 1636 break; |
| 1637 |
| 1638 case NotificationType::EXTENSIONS_LOADED: { |
| 1639 const ExtensionList* extensions = Details<ExtensionList>(details).ptr(); |
| 1640 if (AddExtensionToolstrips(extensions)) { |
| 1641 Layout(); |
| 1642 SchedulePaint(); |
| 1643 } |
| 1644 break; |
| 1645 } |
1585 } | 1646 } |
1586 } | 1647 } |
1587 | 1648 |
| 1649 bool BookmarkBarView::AddExtensionToolstrips(const ExtensionList* extensions) { |
| 1650 bool added_toolstrip = false; |
| 1651 for (ExtensionList::const_iterator extension = extensions->begin(); |
| 1652 extension != extensions->end(); ++extension) { |
| 1653 if (!(*extension)->toolstrip_url().is_empty()) { |
| 1654 ExtensionToolstrip* view = |
| 1655 new ExtensionToolstrip((*extension)->toolstrip_url(), profile_); |
| 1656 int index = GetBookmarkButtonCount() + num_extension_toolstrips_; |
| 1657 AddChildView(index, view); |
| 1658 added_toolstrip = true; |
| 1659 ++num_extension_toolstrips_; |
| 1660 } |
| 1661 } |
| 1662 |
| 1663 return added_toolstrip; |
| 1664 } |
| 1665 |
1588 void BookmarkBarView::RemoveNotificationObservers() { | 1666 void BookmarkBarView::RemoveNotificationObservers() { |
1589 NotificationService* ns = NotificationService::current(); | 1667 NotificationService* ns = NotificationService::current(); |
1590 Source<Profile> ns_source(profile_->GetOriginalProfile()); | 1668 Source<Profile> ns_source(profile_->GetOriginalProfile()); |
1591 ns->RemoveObserver(this, NotificationType::BOOKMARK_BUBBLE_SHOWN, ns_source); | 1669 ns->RemoveObserver(this, NotificationType::BOOKMARK_BUBBLE_SHOWN, ns_source); |
1592 ns->RemoveObserver(this, NotificationType::BOOKMARK_BUBBLE_HIDDEN, ns_source); | 1670 ns->RemoveObserver(this, NotificationType::BOOKMARK_BUBBLE_HIDDEN, ns_source); |
1593 ns->RemoveObserver(this, | 1671 ns->RemoveObserver(this, |
1594 NotificationType::BOOKMARK_BAR_VISIBILITY_PREF_CHANGED, | 1672 NotificationType::BOOKMARK_BAR_VISIBILITY_PREF_CHANGED, |
1595 NotificationService::AllSources()); | 1673 NotificationService::AllSources()); |
| 1674 ns->RemoveObserver(this, NotificationType::EXTENSIONS_LOADED, |
| 1675 NotificationService::AllSources()); |
1596 } | 1676 } |
1597 | 1677 |
1598 void BookmarkBarView::NotifyModelChanged() { | 1678 void BookmarkBarView::NotifyModelChanged() { |
1599 if (model_changed_listener_) | 1679 if (model_changed_listener_) |
1600 model_changed_listener_->ModelChanged(); | 1680 model_changed_listener_->ModelChanged(); |
1601 } | 1681 } |
1602 | 1682 |
1603 void BookmarkBarView::ShowDropFolderForNode(BookmarkNode* node) { | 1683 void BookmarkBarView::ShowDropFolderForNode(BookmarkNode* node) { |
1604 if (drop_menu_runner_.get() && drop_menu_runner_->GetNode() == node) { | 1684 if (drop_menu_runner_.get() && drop_menu_runner_->GetNode() == node) { |
1605 // Already showing for the specified node. | 1685 // Already showing for the specified node. |
(...skipping 279 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1885 } | 1965 } |
1886 | 1966 |
1887 void BookmarkBarView::StopThrobbing(bool immediate) { | 1967 void BookmarkBarView::StopThrobbing(bool immediate) { |
1888 if (!throbbing_view_) | 1968 if (!throbbing_view_) |
1889 return; | 1969 return; |
1890 | 1970 |
1891 // If not immediate, cycle through 2 more complete cycles. | 1971 // If not immediate, cycle through 2 more complete cycles. |
1892 throbbing_view_->StartThrobbing(immediate ? 0 : 4); | 1972 throbbing_view_->StartThrobbing(immediate ? 0 : 4); |
1893 throbbing_view_ = NULL; | 1973 throbbing_view_ = NULL; |
1894 } | 1974 } |
OLD | NEW |