OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/bookmarks/bookmark_bar_view.h" | 5 #include "chrome/browser/ui/views/bookmarks/bookmark_bar_view.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <limits> | 8 #include <limits> |
9 #include <set> | 9 #include <set> |
10 #include <vector> | 10 #include <vector> |
(...skipping 416 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
427 // Disable the other bookmarked button, we'll re-enable when the model is | 427 // Disable the other bookmarked button, we'll re-enable when the model is |
428 // loaded. | 428 // loaded. |
429 other_bookmarked_button_->SetEnabled(false); | 429 other_bookmarked_button_->SetEnabled(false); |
430 | 430 |
431 Source<Profile> ns_source(profile_->GetOriginalProfile()); | 431 Source<Profile> ns_source(profile_->GetOriginalProfile()); |
432 registrar_.Add(this, chrome::NOTIFICATION_BOOKMARK_BUBBLE_SHOWN, ns_source); | 432 registrar_.Add(this, chrome::NOTIFICATION_BOOKMARK_BUBBLE_SHOWN, ns_source); |
433 registrar_.Add(this, chrome::NOTIFICATION_BOOKMARK_BUBBLE_HIDDEN, ns_source); | 433 registrar_.Add(this, chrome::NOTIFICATION_BOOKMARK_BUBBLE_HIDDEN, ns_source); |
434 | 434 |
435 // Remove any existing bookmark buttons. | 435 // Remove any existing bookmark buttons. |
436 while (GetBookmarkButtonCount()) | 436 while (GetBookmarkButtonCount()) |
437 delete GetChildViewAt(0); | 437 delete child_at(0); |
438 | 438 |
439 model_ = profile_->GetBookmarkModel(); | 439 model_ = profile_->GetBookmarkModel(); |
440 if (model_) { | 440 if (model_) { |
441 model_->AddObserver(this); | 441 model_->AddObserver(this); |
442 if (model_->IsLoaded()) | 442 if (model_->IsLoaded()) |
443 Loaded(model_); | 443 Loaded(model_); |
444 // else case: we'll receive notification back from the BookmarkModel when | 444 // else case: we'll receive notification back from the BookmarkModel when |
445 // done loading, then we'll populate the bar. | 445 // done loading, then we'll populate the bar. |
446 } | 446 } |
447 } | 447 } |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
489 int* model_start_index) { | 489 int* model_start_index) { |
490 *model_start_index = 0; | 490 *model_start_index = 0; |
491 | 491 |
492 if (loc.x() < 0 || loc.x() >= width() || loc.y() < 0 || loc.y() >= height()) | 492 if (loc.x() < 0 || loc.x() >= width() || loc.y() < 0 || loc.y() >= height()) |
493 return NULL; | 493 return NULL; |
494 | 494 |
495 gfx::Point adjusted_loc(GetMirroredXInView(loc.x()), loc.y()); | 495 gfx::Point adjusted_loc(GetMirroredXInView(loc.x()), loc.y()); |
496 | 496 |
497 // Check the buttons first. | 497 // Check the buttons first. |
498 for (int i = 0; i < GetBookmarkButtonCount(); ++i) { | 498 for (int i = 0; i < GetBookmarkButtonCount(); ++i) { |
499 views::View* child = GetChildViewAt(i); | 499 views::View* child = child_at(i); |
500 if (!child->IsVisible()) | 500 if (!child->IsVisible()) |
501 break; | 501 break; |
502 if (child->bounds().Contains(adjusted_loc)) | 502 if (child->bounds().Contains(adjusted_loc)) |
503 return model_->GetBookmarkBarNode()->GetChild(i); | 503 return model_->GetBookmarkBarNode()->GetChild(i); |
504 } | 504 } |
505 | 505 |
506 // Then the overflow button. | 506 // Then the overflow button. |
507 if (overflow_button_->IsVisible() && | 507 if (overflow_button_->IsVisible() && |
508 overflow_button_->bounds().Contains(adjusted_loc)) { | 508 overflow_button_->bounds().Contains(adjusted_loc)) { |
509 *model_start_index = GetFirstHiddenNodeIndex(); | 509 *model_start_index = GetFirstHiddenNodeIndex(); |
(...skipping 11 matching lines...) Expand all Loading... |
521 | 521 |
522 views::MenuButton* BookmarkBarView::GetMenuButtonForNode( | 522 views::MenuButton* BookmarkBarView::GetMenuButtonForNode( |
523 const BookmarkNode* node) { | 523 const BookmarkNode* node) { |
524 if (node == model_->other_node()) | 524 if (node == model_->other_node()) |
525 return other_bookmarked_button_; | 525 return other_bookmarked_button_; |
526 if (node == model_->GetBookmarkBarNode()) | 526 if (node == model_->GetBookmarkBarNode()) |
527 return overflow_button_; | 527 return overflow_button_; |
528 int index = model_->GetBookmarkBarNode()->GetIndexOf(node); | 528 int index = model_->GetBookmarkBarNode()->GetIndexOf(node); |
529 if (index == -1 || !node->is_folder()) | 529 if (index == -1 || !node->is_folder()) |
530 return NULL; | 530 return NULL; |
531 return static_cast<views::MenuButton*>(GetChildViewAt(index)); | 531 return static_cast<views::MenuButton*>(child_at(index)); |
532 } | 532 } |
533 | 533 |
534 void BookmarkBarView::GetAnchorPositionForButton( | 534 void BookmarkBarView::GetAnchorPositionForButton( |
535 views::MenuButton* button, | 535 views::MenuButton* button, |
536 MenuItemView::AnchorPosition* anchor) { | 536 MenuItemView::AnchorPosition* anchor) { |
537 if (button == other_bookmarked_button_ || button == overflow_button_) | 537 if (button == other_bookmarked_button_ || button == overflow_button_) |
538 *anchor = MenuItemView::TOPRIGHT; | 538 *anchor = MenuItemView::TOPRIGHT; |
539 else | 539 else |
540 *anchor = MenuItemView::TOPLEFT; | 540 *anchor = MenuItemView::TOPLEFT; |
541 } | 541 } |
(...skipping 417 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
959 BookmarkNodeChangedImpl(model, node); | 959 BookmarkNodeChangedImpl(model, node); |
960 } | 960 } |
961 | 961 |
962 void BookmarkBarView::BookmarkNodeChildrenReordered(BookmarkModel* model, | 962 void BookmarkBarView::BookmarkNodeChildrenReordered(BookmarkModel* model, |
963 const BookmarkNode* node) { | 963 const BookmarkNode* node) { |
964 if (node != model_->GetBookmarkBarNode()) | 964 if (node != model_->GetBookmarkBarNode()) |
965 return; // We only care about reordering of the bookmark bar node. | 965 return; // We only care about reordering of the bookmark bar node. |
966 | 966 |
967 // Remove the existing buttons. | 967 // Remove the existing buttons. |
968 while (GetBookmarkButtonCount()) { | 968 while (GetBookmarkButtonCount()) { |
969 views::View* button = GetChildViewAt(0); | 969 views::View* button = child_at(0); |
970 RemoveChildView(button); | 970 RemoveChildView(button); |
971 MessageLoop::current()->DeleteSoon(FROM_HERE, button); | 971 MessageLoop::current()->DeleteSoon(FROM_HERE, button); |
972 } | 972 } |
973 | 973 |
974 // Create the new buttons. | 974 // Create the new buttons. |
975 for (int i = 0, child_count = node->child_count(); i < child_count; ++i) | 975 for (int i = 0, child_count = node->child_count(); i < child_count; ++i) |
976 AddChildViewAt(CreateBookmarkButton(node->GetChild(i)), i); | 976 AddChildViewAt(CreateBookmarkButton(node->GetChild(i)), i); |
977 UpdateColors(); | 977 UpdateColors(); |
978 | 978 |
979 Layout(); | 979 Layout(); |
(...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1205 | 1205 |
1206 int BookmarkBarView::GetBookmarkButtonCount() { | 1206 int BookmarkBarView::GetBookmarkButtonCount() { |
1207 // We contain five non-bookmark button views: other bookmarks, bookmarks | 1207 // We contain five non-bookmark button views: other bookmarks, bookmarks |
1208 // separator, chevrons (for overflow), the instruction label and the sync | 1208 // separator, chevrons (for overflow), the instruction label and the sync |
1209 // error button. | 1209 // error button. |
1210 return child_count() - 5; | 1210 return child_count() - 5; |
1211 } | 1211 } |
1212 | 1212 |
1213 views::TextButton* BookmarkBarView::GetBookmarkButton(int index) { | 1213 views::TextButton* BookmarkBarView::GetBookmarkButton(int index) { |
1214 DCHECK(index >= 0 && index < GetBookmarkButtonCount()); | 1214 DCHECK(index >= 0 && index < GetBookmarkButtonCount()); |
1215 return static_cast<views::TextButton*>(GetChildViewAt(index)); | 1215 return static_cast<views::TextButton*>(child_at(index)); |
1216 } | 1216 } |
1217 | 1217 |
1218 int BookmarkBarView::GetFirstHiddenNodeIndex() { | 1218 int BookmarkBarView::GetFirstHiddenNodeIndex() { |
1219 const int bb_count = GetBookmarkButtonCount(); | 1219 const int bb_count = GetBookmarkButtonCount(); |
1220 for (int i = 0; i < bb_count; ++i) { | 1220 for (int i = 0; i < bb_count; ++i) { |
1221 if (!GetBookmarkButton(i)->IsVisible()) | 1221 if (!GetBookmarkButton(i)->IsVisible()) |
1222 return i; | 1222 return i; |
1223 } | 1223 } |
1224 return bb_count; | 1224 return bb_count; |
1225 } | 1225 } |
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1342 | 1342 |
1343 StopThrobbing(true); | 1343 StopThrobbing(true); |
1344 // No need to start throbbing again as the bookmark bubble can't be up at | 1344 // No need to start throbbing again as the bookmark bubble can't be up at |
1345 // the same time as the user reorders. | 1345 // the same time as the user reorders. |
1346 | 1346 |
1347 if (parent != model_->GetBookmarkBarNode()) { | 1347 if (parent != model_->GetBookmarkBarNode()) { |
1348 // We only care about nodes on the bookmark bar. | 1348 // We only care about nodes on the bookmark bar. |
1349 return; | 1349 return; |
1350 } | 1350 } |
1351 DCHECK(index >= 0 && index < GetBookmarkButtonCount()); | 1351 DCHECK(index >= 0 && index < GetBookmarkButtonCount()); |
1352 views::View* button = GetChildViewAt(index); | 1352 views::View* button = child_at(index); |
1353 RemoveChildView(button); | 1353 RemoveChildView(button); |
1354 MessageLoop::current()->DeleteSoon(FROM_HERE, button); | 1354 MessageLoop::current()->DeleteSoon(FROM_HERE, button); |
1355 Layout(); | 1355 Layout(); |
1356 SchedulePaint(); | 1356 SchedulePaint(); |
1357 } | 1357 } |
1358 | 1358 |
1359 void BookmarkBarView::BookmarkNodeChangedImpl(BookmarkModel* model, | 1359 void BookmarkBarView::BookmarkNodeChangedImpl(BookmarkModel* model, |
1360 const BookmarkNode* node) { | 1360 const BookmarkNode* node) { |
1361 if (node->parent() != model_->GetBookmarkBarNode()) { | 1361 if (node->parent() != model_->GetBookmarkBarNode()) { |
1362 // We only care about nodes on the bookmark bar. | 1362 // We only care about nodes on the bookmark bar. |
(...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1547 if (parent == bbn) | 1547 if (parent == bbn) |
1548 break; | 1548 break; |
1549 parent_on_bb = parent; | 1549 parent_on_bb = parent; |
1550 } | 1550 } |
1551 if (parent_on_bb) { | 1551 if (parent_on_bb) { |
1552 int index = bbn->GetIndexOf(parent_on_bb); | 1552 int index = bbn->GetIndexOf(parent_on_bb); |
1553 if (index >= GetFirstHiddenNodeIndex()) { | 1553 if (index >= GetFirstHiddenNodeIndex()) { |
1554 // Node is hidden, animate the overflow button. | 1554 // Node is hidden, animate the overflow button. |
1555 throbbing_view_ = overflow_button_; | 1555 throbbing_view_ = overflow_button_; |
1556 } else if (!overflow_only) { | 1556 } else if (!overflow_only) { |
1557 throbbing_view_ = static_cast<CustomButton*>(GetChildViewAt(index)); | 1557 throbbing_view_ = static_cast<CustomButton*>(child_at(index)); |
1558 } | 1558 } |
1559 } else if (!overflow_only) { | 1559 } else if (!overflow_only) { |
1560 throbbing_view_ = other_bookmarked_button_; | 1560 throbbing_view_ = other_bookmarked_button_; |
1561 } | 1561 } |
1562 | 1562 |
1563 // Use a large number so that the button continues to throb. | 1563 // Use a large number so that the button continues to throb. |
1564 if (throbbing_view_) | 1564 if (throbbing_view_) |
1565 throbbing_view_->StartThrobbing(std::numeric_limits<int>::max()); | 1565 throbbing_view_->StartThrobbing(std::numeric_limits<int>::max()); |
1566 } | 1566 } |
1567 | 1567 |
1568 views::CustomButton* BookmarkBarView::DetermineViewToThrobFromRemove( | 1568 views::CustomButton* BookmarkBarView::DetermineViewToThrobFromRemove( |
1569 const BookmarkNode* parent, | 1569 const BookmarkNode* parent, |
1570 int old_index) { | 1570 int old_index) { |
1571 const BookmarkNode* bbn = model_->GetBookmarkBarNode(); | 1571 const BookmarkNode* bbn = model_->GetBookmarkBarNode(); |
1572 const BookmarkNode* old_node = parent; | 1572 const BookmarkNode* old_node = parent; |
1573 int old_index_on_bb = old_index; | 1573 int old_index_on_bb = old_index; |
1574 while (old_node && old_node != bbn) { | 1574 while (old_node && old_node != bbn) { |
1575 const BookmarkNode* parent = old_node->parent(); | 1575 const BookmarkNode* parent = old_node->parent(); |
1576 if (parent == bbn) { | 1576 if (parent == bbn) { |
1577 old_index_on_bb = bbn->GetIndexOf(old_node); | 1577 old_index_on_bb = bbn->GetIndexOf(old_node); |
1578 break; | 1578 break; |
1579 } | 1579 } |
1580 old_node = parent; | 1580 old_node = parent; |
1581 } | 1581 } |
1582 if (old_node) { | 1582 if (old_node) { |
1583 if (old_index_on_bb >= GetFirstHiddenNodeIndex()) { | 1583 if (old_index_on_bb >= GetFirstHiddenNodeIndex()) { |
1584 // Node is hidden, animate the overflow button. | 1584 // Node is hidden, animate the overflow button. |
1585 return overflow_button_; | 1585 return overflow_button_; |
1586 } | 1586 } |
1587 return static_cast<CustomButton*>(GetChildViewAt(old_index_on_bb)); | 1587 return static_cast<CustomButton*>(child_at(old_index_on_bb)); |
1588 } | 1588 } |
1589 // Node wasn't on the bookmark bar, use the other bookmark button. | 1589 // Node wasn't on the bookmark bar, use the other bookmark button. |
1590 return other_bookmarked_button_; | 1590 return other_bookmarked_button_; |
1591 } | 1591 } |
1592 | 1592 |
1593 void BookmarkBarView::UpdateColors() { | 1593 void BookmarkBarView::UpdateColors() { |
1594 // We don't always have a theme provider (ui tests, for example). | 1594 // We don't always have a theme provider (ui tests, for example). |
1595 const ui::ThemeProvider* theme_provider = GetThemeProvider(); | 1595 const ui::ThemeProvider* theme_provider = GetThemeProvider(); |
1596 if (!theme_provider) | 1596 if (!theme_provider) |
1597 return; | 1597 return; |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1667 std::min(static_cast<int>(pref.width()), | 1667 std::min(static_cast<int>(pref.width()), |
1668 max_x - x), | 1668 max_x - x), |
1669 height); | 1669 height); |
1670 instructions_->SetVisible(true); | 1670 instructions_->SetVisible(true); |
1671 } | 1671 } |
1672 } else { | 1672 } else { |
1673 if (!compute_bounds_only) | 1673 if (!compute_bounds_only) |
1674 instructions_->SetVisible(false); | 1674 instructions_->SetVisible(false); |
1675 | 1675 |
1676 for (int i = 0; i < GetBookmarkButtonCount(); ++i) { | 1676 for (int i = 0; i < GetBookmarkButtonCount(); ++i) { |
1677 views::View* child = GetChildViewAt(i); | 1677 views::View* child = child_at(i); |
1678 gfx::Size pref = child->GetPreferredSize(); | 1678 gfx::Size pref = child->GetPreferredSize(); |
1679 int next_x = x + pref.width() + kButtonPadding; | 1679 int next_x = x + pref.width() + kButtonPadding; |
1680 if (!compute_bounds_only) { | 1680 if (!compute_bounds_only) { |
1681 child->SetVisible(next_x < max_x); | 1681 child->SetVisible(next_x < max_x); |
1682 child->SetBounds(x, y, pref.width(), height); | 1682 child->SetBounds(x, y, pref.width(), height); |
1683 } | 1683 } |
1684 x = next_x; | 1684 x = next_x; |
1685 } | 1685 } |
1686 } | 1686 } |
1687 | 1687 |
1688 // Layout the right side of the bar. | 1688 // Layout the right side of the bar. |
1689 const bool all_visible = | 1689 const bool all_visible = |
1690 (GetBookmarkButtonCount() == 0 || | 1690 (GetBookmarkButtonCount() == 0 || |
1691 GetChildViewAt(GetBookmarkButtonCount() - 1)->IsVisible()); | 1691 child_at(GetBookmarkButtonCount() - 1)->IsVisible()); |
1692 | 1692 |
1693 // Layout the right side buttons. | 1693 // Layout the right side buttons. |
1694 if (!compute_bounds_only) | 1694 if (!compute_bounds_only) |
1695 x = max_x + kButtonPadding; | 1695 x = max_x + kButtonPadding; |
1696 else | 1696 else |
1697 x += kButtonPadding; | 1697 x += kButtonPadding; |
1698 | 1698 |
1699 // The overflow button. | 1699 // The overflow button. |
1700 if (!compute_bounds_only) { | 1700 if (!compute_bounds_only) { |
1701 overflow_button_->SetBounds(x, y, overflow_pref.width(), height); | 1701 overflow_button_->SetBounds(x, y, overflow_pref.width(), height); |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1755 (1 - size_animation_->GetCurrentValue()))); | 1755 (1 - size_animation_->GetCurrentValue()))); |
1756 } else { | 1756 } else { |
1757 prefsize.set_height( | 1757 prefsize.set_height( |
1758 static_cast<int>( | 1758 static_cast<int>( |
1759 browser_defaults::kBookmarkBarHeight * | 1759 browser_defaults::kBookmarkBarHeight * |
1760 size_animation_->GetCurrentValue())); | 1760 size_animation_->GetCurrentValue())); |
1761 } | 1761 } |
1762 } | 1762 } |
1763 return prefsize; | 1763 return prefsize; |
1764 } | 1764 } |
OLD | NEW |