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 3// found in the LICENSE file. 4 5#include "chrome/browser/ui/views/bookmarks/bookmark_bar_instructions_view.h" 6 7#include <algorithm> 8 9#include "base/strings/utf_string_conversions.h" 10#include "chrome/browser/defaults.h" 11#include "chrome/browser/themes/theme_properties.h" 12#include "chrome/browser/ui/bookmarks/bookmark_bar_instructions_delegate.h" 13#include "grit/generated_resources.h" 14#include "ui/base/accessibility/accessible_view_state.h" 15#include "ui/base/l10n/l10n_util.h" 16#include "ui/base/theme_provider.h" 17#include "ui/views/controls/label.h" 18#include "ui/views/controls/link.h" 19 20namespace { 21 22// Horizontal padding, in pixels, between the link and label. 23const int kViewPadding = 6; 24 25} // namespace 26 27BookmarkBarInstructionsView::BookmarkBarInstructionsView( 28 chrome::BookmarkBarInstructionsDelegate* delegate) 29 : delegate_(delegate), 30 instructions_(NULL), 31 import_link_(NULL), 32 baseline_(-1), 33 updated_colors_(false) { 34 instructions_ = new views::Label( 35 l10n_util::GetStringUTF16(IDS_BOOKMARKS_NO_ITEMS)); 36 instructions_->SetAutoColorReadabilityEnabled(false); 37 AddChildView(instructions_); 38 39 if (browser_defaults::kShowImportOnBookmarkBar) { 40 import_link_ = new views::Link( 41 l10n_util::GetStringUTF16(IDS_BOOKMARK_BAR_IMPORT_LINK)); 42 // We don't want the link to alter tab navigation. 43 import_link_->set_focusable(false); 44 import_link_->set_listener(this); 45 import_link_->set_context_menu_controller(this); 46 import_link_->SetAutoColorReadabilityEnabled(false); 47 AddChildView(import_link_); 48 } 49} 50 51gfx::Size BookmarkBarInstructionsView::GetPreferredSize() { 52 int ascent = 0, descent = 0, height = 0, width = 0; 53 for (int i = 0; i < child_count(); ++i) { 54 views::View* view = child_at(i); 55 gfx::Size pref = view->GetPreferredSize(); 56 int baseline = view->GetBaseline(); 57 if (baseline != -1) { 58 ascent = std::max(ascent, baseline); 59 descent = std::max(descent, pref.height() - baseline); 60 } else { 61 height = std::max(pref.height(), height); 62 } 63 width += pref.width(); 64 } 65 width += (child_count() - 1) * kViewPadding; 66 if (ascent != 0) 67 height = std::max(ascent + descent, height); 68 return gfx::Size(width, height); 69} 70 71void BookmarkBarInstructionsView::Layout() { 72 int remaining_width = width(); 73 int x = 0; 74 for (int i = 0; i < child_count(); ++i) { 75 views::View* view = child_at(i); 76 gfx::Size pref = view->GetPreferredSize(); 77 int baseline = view->GetBaseline(); 78 int y; 79 if (baseline != -1 && baseline_ != -1) 80 y = baseline_ - baseline; 81 else 82 y = (height() - pref.height()) / 2; 83 int view_width = std::min(remaining_width, pref.width()); 84 view->SetBounds(x, y, view_width, pref.height()); 85 x += view_width + kViewPadding; 86 remaining_width = std::max(0, width() - x); 87 } 88} 89 90void BookmarkBarInstructionsView::OnThemeChanged() { 91 UpdateColors(); 92} 93 94void BookmarkBarInstructionsView::ViewHierarchyChanged( 95 const ViewHierarchyChangedDetails& details) { 96 if (!updated_colors_ && details.is_add && GetWidget()) 97 UpdateColors(); 98} 99 100void BookmarkBarInstructionsView::GetAccessibleState( 101 ui::AccessibleViewState* state) { 102 state->role = ui::AccessibilityTypes::ROLE_GROUPING; 103} 104 105void BookmarkBarInstructionsView::LinkClicked(views::Link* source, 106 int event_flags) { 107 delegate_->ShowImportDialog(); 108} 109 110void BookmarkBarInstructionsView::ShowContextMenuForView( 111 views::View* source, 112 const gfx::Point& point, 113 ui::MenuSourceType source_type) { 114 // Do nothing here, we don't want to show the Bookmarks context menu when 115 // the user right clicks on the "Import bookmarks now" link. 116} 117 118void BookmarkBarInstructionsView::UpdateColors() { 119 // We don't always have a theme provider (ui tests, for example). 120 const ui::ThemeProvider* theme_provider = GetThemeProvider(); 121 if (!theme_provider) 122 return; 123 updated_colors_ = true; 124 SkColor text_color = 125 theme_provider->GetColor(ThemeProperties::COLOR_BOOKMARK_TEXT); 126 instructions_->SetEnabledColor(text_color); 127 if (import_link_) 128 import_link_->SetEnabledColor(text_color); 129} 130