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.
5 #include "chrome/browser/ui/views/bookmarks/bookmark_bar_instructions_view.h"
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"
22 // Horizontal padding, in pixels, between the link and label.
23 const int kViewPadding
= 6;
27 BookmarkBarInstructionsView::BookmarkBarInstructionsView(
28 chrome::BookmarkBarInstructionsDelegate
* delegate
)
29 : delegate_(delegate
),
33 updated_colors_(false) {
34 instructions_
= new views::Label(
35 l10n_util::GetStringUTF16(IDS_BOOKMARKS_NO_ITEMS
));
36 instructions_
->SetAutoColorReadabilityEnabled(false);
37 AddChildView(instructions_
);
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_
);
51 gfx::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();
58 ascent
= std::max(ascent
, baseline
);
59 descent
= std::max(descent
, pref
.height() - baseline
);
61 height
= std::max(pref
.height(), height
);
63 width
+= pref
.width();
65 width
+= (child_count() - 1) * kViewPadding
;
67 height
= std::max(ascent
+ descent
, height
);
68 return gfx::Size(width
, height
);
71 void BookmarkBarInstructionsView::Layout() {
72 int remaining_width
= width();
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();
79 if (baseline
!= -1 && baseline_
!= -1)
80 y
= baseline_
- baseline
;
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
);
90 void BookmarkBarInstructionsView::OnThemeChanged() {
94 void BookmarkBarInstructionsView::ViewHierarchyChanged(
95 const ViewHierarchyChangedDetails
& details
) {
96 if (!updated_colors_
&& details
.is_add
&& GetWidget())
100 void BookmarkBarInstructionsView::GetAccessibleState(
101 ui::AccessibleViewState
* state
) {
102 state
->role
= ui::AccessibilityTypes::ROLE_GROUPING
;
105 void BookmarkBarInstructionsView::LinkClicked(views::Link
* source
,
107 delegate_
->ShowImportDialog();
110 void BookmarkBarInstructionsView::ShowContextMenuForView(
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.
118 void BookmarkBarInstructionsView::UpdateColors() {
119 // We don't always have a theme provider (ui tests, for example).
120 const ui::ThemeProvider
* theme_provider
= GetThemeProvider();
123 updated_colors_
= true;
125 theme_provider
->GetColor(ThemeProperties::COLOR_BOOKMARK_TEXT
);
126 instructions_
->SetEnabledColor(text_color
);
128 import_link_
->SetEnabledColor(text_color
);