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