Chromium Code Reviews| 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/bookmarks/bookmark_bar_instructions_view.h" | 5 #include "chrome/browser/ui/views/bookmarks/bookmark_bar_instructions_view.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
| 10 #include "chrome/browser/defaults.h" | 10 #include "chrome/browser/defaults.h" |
| 11 #include "chrome/browser/themes/theme_properties.h" | 11 #include "chrome/browser/themes/theme_properties.h" |
| 12 #include "chrome/browser/ui/bookmarks/bookmark_bar_instructions_delegate.h" | 12 #include "chrome/browser/ui/bookmarks/bookmark_bar_instructions_delegate.h" |
| 13 #include "chrome/grit/generated_resources.h" | 13 #include "chrome/grit/generated_resources.h" |
| 14 #include "ui/accessibility/ax_view_state.h" | 14 #include "ui/accessibility/ax_view_state.h" |
| 15 #include "ui/base/l10n/l10n_util.h" | 15 #include "ui/base/l10n/l10n_util.h" |
| 16 #include "ui/base/resource/material_design/material_design_controller.h" | |
| 16 #include "ui/base/theme_provider.h" | 17 #include "ui/base/theme_provider.h" |
| 18 #include "ui/native_theme/native_theme.h" | |
| 17 #include "ui/views/controls/label.h" | 19 #include "ui/views/controls/label.h" |
| 18 #include "ui/views/controls/link.h" | 20 #include "ui/views/controls/link.h" |
| 19 | 21 |
| 20 namespace { | 22 namespace { |
| 21 | 23 |
| 22 // Horizontal padding, in pixels, between the link and label. | 24 // Horizontal padding, in pixels, between the link and label. |
| 23 const int kViewPadding = 6; | 25 const int kViewPadding = 6; |
| 24 | 26 |
| 25 } // namespace | 27 } // namespace |
| 26 | 28 |
| 27 BookmarkBarInstructionsView::BookmarkBarInstructionsView( | 29 BookmarkBarInstructionsView::BookmarkBarInstructionsView( |
| 28 BookmarkBarInstructionsDelegate* delegate) | 30 BookmarkBarInstructionsDelegate* delegate) |
| 29 : delegate_(delegate), | 31 : delegate_(delegate), |
| 30 instructions_(NULL), | 32 instructions_(NULL), |
| 31 import_link_(NULL), | 33 import_link_(NULL), |
| 32 baseline_(-1), | 34 baseline_(-1), |
| 33 updated_colors_(false) { | 35 updated_colors_(false) { |
| 34 instructions_ = new views::Label( | 36 base::string16 instructions_text = |
| 35 l10n_util::GetStringUTF16(IDS_BOOKMARKS_NO_ITEMS)); | 37 l10n_util::GetStringUTF16(IDS_BOOKMARKS_NO_ITEMS); |
| 38 if (ui::MaterialDesignController::IsModeMaterial()) | |
| 39 instructions_text += base::ASCIIToUTF16(" "); | |
|
sky
2015/10/19 15:38:40
Why do you add a space for material? Might the spa
Evan Stade
2015/10/19 16:49:52
Previously this space was implemented with kViewPa
jungshik at Google
2015/10/23 06:30:19
Sorry for the late reply and thanks for testing in
| |
| 40 instructions_ = new views::Label(instructions_text); | |
| 36 instructions_->SetAutoColorReadabilityEnabled(false); | 41 instructions_->SetAutoColorReadabilityEnabled(false); |
| 37 AddChildView(instructions_); | 42 AddChildView(instructions_); |
| 38 | 43 |
| 39 if (browser_defaults::kShowImportOnBookmarkBar) { | 44 if (browser_defaults::kShowImportOnBookmarkBar) { |
| 40 import_link_ = new views::Link( | 45 import_link_ = new views::Link( |
| 41 l10n_util::GetStringUTF16(IDS_BOOKMARK_BAR_IMPORT_LINK)); | 46 l10n_util::GetStringUTF16(IDS_BOOKMARK_BAR_IMPORT_LINK)); |
| 42 // We don't want the link to alter tab navigation. | 47 // We don't want the link to alter tab navigation. |
| 43 import_link_->SetFocusable(false); | 48 import_link_->SetFocusable(false); |
| 44 import_link_->set_listener(this); | 49 import_link_->set_listener(this); |
| 45 import_link_->set_context_menu_controller(this); | 50 import_link_->set_context_menu_controller(this); |
| 46 import_link_->SetAutoColorReadabilityEnabled(false); | 51 import_link_->SetAutoColorReadabilityEnabled(false); |
| 47 AddChildView(import_link_); | 52 AddChildView(import_link_); |
| 48 } | 53 } |
| 49 } | 54 } |
| 50 | 55 |
| 51 gfx::Size BookmarkBarInstructionsView::GetPreferredSize() const { | 56 gfx::Size BookmarkBarInstructionsView::GetPreferredSize() const { |
| 52 int ascent = 0, descent = 0, height = 0, width = 0; | 57 int ascent = 0, descent = 0, height = 0, width = 0; |
| 53 for (int i = 0; i < child_count(); ++i) { | 58 for (int i = 0; i < child_count(); ++i) { |
| 54 const views::View* view = child_at(i); | 59 const views::View* view = child_at(i); |
| 55 gfx::Size pref = view->GetPreferredSize(); | 60 gfx::Size pref = view->GetPreferredSize(); |
| 56 int baseline = view->GetBaseline(); | 61 int baseline = view->GetBaseline(); |
| 57 if (baseline != -1) { | 62 if (baseline != -1) { |
| 58 ascent = std::max(ascent, baseline); | 63 ascent = std::max(ascent, baseline); |
| 59 descent = std::max(descent, pref.height() - baseline); | 64 descent = std::max(descent, pref.height() - baseline); |
| 60 } else { | 65 } else { |
| 61 height = std::max(pref.height(), height); | 66 height = std::max(pref.height(), height); |
| 62 } | 67 } |
| 63 width += pref.width(); | 68 width += pref.width(); |
| 64 } | 69 } |
| 65 width += (child_count() - 1) * kViewPadding; | 70 if (!ui::MaterialDesignController::IsModeMaterial()) |
| 71 width += (child_count() - 1) * kViewPadding; | |
| 66 if (ascent != 0) | 72 if (ascent != 0) |
| 67 height = std::max(ascent + descent, height); | 73 height = std::max(ascent + descent, height); |
| 68 return gfx::Size(width, height); | 74 return gfx::Size(width, height); |
| 69 } | 75 } |
| 70 | 76 |
| 71 void BookmarkBarInstructionsView::Layout() { | 77 void BookmarkBarInstructionsView::Layout() { |
| 72 int remaining_width = width(); | 78 int remaining_width = width(); |
| 73 int x = 0; | 79 int x = 0; |
| 74 for (int i = 0; i < child_count(); ++i) { | 80 for (int i = 0; i < child_count(); ++i) { |
| 75 views::View* view = child_at(i); | 81 views::View* view = child_at(i); |
| 76 gfx::Size pref = view->GetPreferredSize(); | 82 gfx::Size pref = view->GetPreferredSize(); |
| 77 int baseline = view->GetBaseline(); | 83 int baseline = view->GetBaseline(); |
| 78 int y; | 84 int y; |
| 79 if (baseline != -1 && baseline_ != -1) | 85 if (baseline != -1 && baseline_ != -1) |
| 80 y = baseline_ - baseline; | 86 y = baseline_ - baseline; |
| 81 else | 87 else |
| 82 y = (height() - pref.height()) / 2; | 88 y = (height() - pref.height()) / 2; |
| 83 int view_width = std::min(remaining_width, pref.width()); | 89 int view_width = std::min(remaining_width, pref.width()); |
| 84 view->SetBounds(x, y, view_width, pref.height()); | 90 view->SetBounds(x, y, view_width, pref.height()); |
| 85 x += view_width + kViewPadding; | 91 x += view_width; |
| 92 if (!ui::MaterialDesignController::IsModeMaterial()) | |
| 93 x += kViewPadding; | |
| 86 remaining_width = std::max(0, width() - x); | 94 remaining_width = std::max(0, width() - x); |
| 87 } | 95 } |
| 88 } | 96 } |
| 89 | 97 |
| 90 const char* BookmarkBarInstructionsView::GetClassName() const { | 98 const char* BookmarkBarInstructionsView::GetClassName() const { |
| 91 return "BookmarkBarInstructionsView"; | 99 return "BookmarkBarInstructionsView"; |
| 92 } | 100 } |
| 93 | 101 |
| 94 void BookmarkBarInstructionsView::OnThemeChanged() { | 102 void BookmarkBarInstructionsView::OnThemeChanged() { |
| 95 UpdateColors(); | 103 UpdateColors(); |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 121 | 129 |
| 122 void BookmarkBarInstructionsView::UpdateColors() { | 130 void BookmarkBarInstructionsView::UpdateColors() { |
| 123 // We don't always have a theme provider (ui tests, for example). | 131 // We don't always have a theme provider (ui tests, for example). |
| 124 const ui::ThemeProvider* theme_provider = GetThemeProvider(); | 132 const ui::ThemeProvider* theme_provider = GetThemeProvider(); |
| 125 if (!theme_provider) | 133 if (!theme_provider) |
| 126 return; | 134 return; |
| 127 updated_colors_ = true; | 135 updated_colors_ = true; |
| 128 SkColor text_color = | 136 SkColor text_color = |
| 129 theme_provider->GetColor(ThemeProperties::COLOR_BOOKMARK_TEXT); | 137 theme_provider->GetColor(ThemeProperties::COLOR_BOOKMARK_TEXT); |
| 130 instructions_->SetEnabledColor(text_color); | 138 instructions_->SetEnabledColor(text_color); |
| 131 if (import_link_) | 139 if (import_link_) { |
|
varkha
2015/10/19 15:01:29
nit: could return early on !import_link to simplif
Evan Stade
2015/10/19 16:49:52
Done.
| |
| 132 import_link_->SetEnabledColor(text_color); | 140 if (!ui::MaterialDesignController::IsModeMaterial()) { |
| 141 import_link_->SetEnabledColor(text_color); | |
| 142 } else { | |
| 143 // For MD, use the default link color if it provides enough contrast. If | |
| 144 // contrast is too low, fall back to the bookmark text color and use an | |
| 145 // underline to make it obvious it's a link. The default color readability | |
| 146 // code (which only adjusts luminance) doesn't work well in this case. | |
| 147 SkColor bg = theme_provider->GetColor( | |
| 148 ThemeProperties::ThemeProperties::COLOR_TOOLBAR); | |
| 149 SkColor link_color = GetNativeTheme()->GetSystemColor( | |
| 150 ui::NativeTheme::kColorId_LinkEnabled); | |
| 151 if (color_utils::GetContrastRatio(link_color, bg) >= | |
| 152 color_utils::kMinimumReadableContrastRatio) { | |
| 153 import_link_->SetUnderline(false); | |
| 154 import_link_->SetEnabledColor(link_color); | |
| 155 } else { | |
| 156 import_link_->SetUnderline(true); | |
| 157 import_link_->SetEnabledColor(text_color); | |
| 158 } | |
|
varkha
2015/10/19 15:01:29
nit: maybe shorten this similar to:
bool enough_c
Evan Stade
2015/10/19 16:49:52
Done.
| |
| 159 } | |
| 160 } | |
| 133 } | 161 } |
| OLD | NEW |