OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 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/location_bar/add_to_app_launcher_view.h" |
| 6 |
| 7 #include "base/command_line.h" |
| 8 #include "base/strings/utf_string_conversions.h" |
| 9 #include "chrome/browser/ui/views/location_bar/location_bar_view.h" |
| 10 #include "chrome/common/chrome_switches.h" |
| 11 #include "content/public/browser/web_contents.h" |
| 12 #include "grit/generated_resources.h" |
| 13 #include "grit/theme_resources.h" |
| 14 #include "ui/base/l10n/l10n_util.h" |
| 15 #include "ui/base/resource/resource_bundle.h" |
| 16 #include "ui/gfx/color_utils.h" |
| 17 #include "ui/views/controls/image_view.h" |
| 18 #include "ui/views/controls/label.h" |
| 19 #include "url/gurl.h" |
| 20 |
| 21 namespace { |
| 22 |
| 23 // TODO(benwells): Factorize common code between this and |
| 24 // ContentSettingImageView. |
| 25 const int kBackgroundImages[] = IMAGE_GRID(IDR_OMNIBOX_CONTENT_SETTING_BUBBLE); |
| 26 const int kStayOpenTimeMS = 3200; // Time spent with animation fully open. |
| 27 |
| 28 const int kOpenTimeMS = 150; |
| 29 const int kAnimationDurationMS = (kOpenTimeMS * 2) + kStayOpenTimeMS; |
| 30 |
| 31 // Amount of padding at the edges of the bubble. If |by_icon| is true, this |
| 32 // is the padding next to the icon; otherwise it's the padding next to the |
| 33 // label. (We increase padding next to the label by the amount of padding |
| 34 // "built in" to the icon in order to make the bubble appear to have |
| 35 // symmetrical padding.) |
| 36 int GetBubbleOuterPadding(bool by_icon) { |
| 37 return LocationBarView::kItemPadding - LocationBarView::kBubblePadding + |
| 38 (by_icon ? 0 : LocationBarView::kIconInternalPadding); |
| 39 } |
| 40 |
| 41 } // namespace |
| 42 |
| 43 AddToAppLauncherView::AddToAppLauncherView(LocationBarView* parent, |
| 44 const gfx::FontList& font_list, |
| 45 SkColor text_color, |
| 46 SkColor parent_background_color) |
| 47 : parent_(parent), |
| 48 background_painter_( |
| 49 views::Painter::CreateImageGridPainter(kBackgroundImages)), |
| 50 icon_(new views::ImageView), |
| 51 text_label_(new views::Label(base::string16(), font_list)), |
| 52 slide_animator_(this) { |
| 53 icon_->SetHorizontalAlignment(views::ImageView::LEADING); |
| 54 icon_->SetImage(ui::ResourceBundle::GetSharedInstance().GetImageSkiaNamed( |
| 55 IDR_BOOKMARK_BAR_APPS_SHORTCUT)); |
| 56 icon_->SetTooltipText( |
| 57 base::UTF8ToUTF16(l10n_util::GetStringUTF8(IDS_ADD_TO_APP_LIST_HINT))); |
| 58 AddChildView(icon_); |
| 59 |
| 60 text_label_->SetVisible(false); |
| 61 text_label_->SetEnabledColor(text_color); |
| 62 // Calculate the actual background color for the label. The background images |
| 63 // are painted atop |parent_background_color|. We grab the color of the |
| 64 // middle pixel of the middle image of the background, which we treat as the |
| 65 // representative color of the entire background (reasonable, given the |
| 66 // current appearance of these images). Then we alpha-blend it over the |
| 67 // parent background color to determine the actual color the label text will |
| 68 // sit atop. |
| 69 const SkBitmap& bitmap(ui::ResourceBundle::GetSharedInstance() |
| 70 .GetImageSkiaNamed(kBackgroundImages[4]) |
| 71 ->GetRepresentation(1.0f) |
| 72 .sk_bitmap()); |
| 73 SkAutoLockPixels pixel_lock(bitmap); |
| 74 SkColor background_image_color = |
| 75 bitmap.getColor(bitmap.width() / 2, bitmap.height() / 2); |
| 76 // Tricky bit: We alpha blend an opaque version of |background_image_color| |
| 77 // against |parent_background_color| using the original image grid color's |
| 78 // alpha. This is because AlphaBlend(a, b, 255) always returns |a| unchanged |
| 79 // even if |a| is a color with non-255 alpha. |
| 80 text_label_->SetBackgroundColor( |
| 81 color_utils::AlphaBlend(SkColorSetA(background_image_color, 255), |
| 82 parent_background_color, |
| 83 SkColorGetA(background_image_color))); |
| 84 text_label_->SetHorizontalAlignment(gfx::ALIGN_LEFT); |
| 85 text_label_->SetElideBehavior(gfx::TRUNCATE); |
| 86 text_label_->SetText(base::UTF8ToUTF16( |
| 87 l10n_util::GetStringUTF8(IDS_ADD_TO_APP_LIST_NOTIFICATION_TEXT))); |
| 88 AddChildView(text_label_); |
| 89 |
| 90 slide_animator_.SetSlideDuration(kAnimationDurationMS); |
| 91 slide_animator_.SetTweenType(gfx::Tween::LINEAR); |
| 92 } |
| 93 |
| 94 AddToAppLauncherView::~AddToAppLauncherView() { |
| 95 } |
| 96 |
| 97 void AddToAppLauncherView::Update(content::WebContents* web_contents) { |
| 98 SetVisible(false); |
| 99 |
| 100 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); |
| 101 if (!command_line->HasSwitch(switches::kEnableProminentURLAppFlow) || |
| 102 !command_line->HasSwitch(switches::kEnableStreamlinedHostedApps)) { |
| 103 return; |
| 104 } |
| 105 |
| 106 if (!web_contents || web_contents->IsLoading() || |
| 107 !web_contents->GetLastCommittedURL().SchemeIsHTTPOrHTTPS()) { |
| 108 return; |
| 109 } |
| 110 |
| 111 SetVisible(true); |
| 112 |
| 113 if (!background_showing()) { |
| 114 text_label_->SetVisible(true); |
| 115 slide_animator_.Show(); |
| 116 } |
| 117 } |
| 118 |
| 119 void AddToAppLauncherView::AnimationEnded(const gfx::Animation* animation) { |
| 120 slide_animator_.Reset(); |
| 121 text_label_->SetVisible(false); |
| 122 parent_->Layout(); |
| 123 parent_->SchedulePaint(); |
| 124 } |
| 125 |
| 126 void AddToAppLauncherView::AnimationProgressed( |
| 127 const gfx::Animation* animation) { |
| 128 parent_->Layout(); |
| 129 parent_->SchedulePaint(); |
| 130 } |
| 131 |
| 132 void AddToAppLauncherView::AnimationCanceled(const gfx::Animation* animation) { |
| 133 AnimationEnded(animation); |
| 134 } |
| 135 |
| 136 gfx::Size AddToAppLauncherView::GetPreferredSize() const { |
| 137 // Height will be ignored by the LocationBarView. |
| 138 gfx::Size size(icon_->GetPreferredSize()); |
| 139 if (background_showing()) { |
| 140 double state = slide_animator_.GetCurrentValue(); |
| 141 // The fraction of the animation we'll spend animating the string into view, |
| 142 // which is also the fraction we'll spend animating it closed; total |
| 143 // animation (slide out, show, then slide in) is 1.0. |
| 144 const double kOpenFraction = |
| 145 static_cast<double>(kOpenTimeMS) / kAnimationDurationMS; |
| 146 double size_fraction = 1.0; |
| 147 if (state < kOpenFraction) |
| 148 size_fraction = state / kOpenFraction; |
| 149 if (state > (1.0 - kOpenFraction)) |
| 150 size_fraction = (1.0 - state) / kOpenFraction; |
| 151 size.Enlarge(size_fraction * (text_label_->GetPreferredSize().width() + |
| 152 GetTotalSpacingWhileAnimating()), |
| 153 0); |
| 154 size.SetToMax(background_painter_->GetMinimumSize()); |
| 155 } |
| 156 return size; |
| 157 } |
| 158 |
| 159 void AddToAppLauncherView::Layout() { |
| 160 const int icon_width = icon_->GetPreferredSize().width(); |
| 161 icon_->SetBounds( |
| 162 std::min((width() - icon_width) / 2, GetBubbleOuterPadding(true)), |
| 163 0, |
| 164 icon_width, |
| 165 height()); |
| 166 text_label_->SetBounds( |
| 167 icon_->bounds().right() + LocationBarView::kItemPadding, |
| 168 0, |
| 169 std::max(width() - GetTotalSpacingWhileAnimating() - icon_width, 0), |
| 170 height()); |
| 171 } |
| 172 |
| 173 void AddToAppLauncherView::OnPaintBackground(gfx::Canvas* canvas) { |
| 174 if (background_showing()) |
| 175 background_painter_->Paint(canvas, size()); |
| 176 } |
| 177 |
| 178 int AddToAppLauncherView::GetTotalSpacingWhileAnimating() const { |
| 179 return GetBubbleOuterPadding(true) + LocationBarView::kItemPadding + |
| 180 GetBubbleOuterPadding(false); |
| 181 } |
OLD | NEW |