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 | |
Peter Kasting
2014/06/11 18:20:48
Nit: Refactor (I don't think "factorize" is a word
benwells
2014/06/12 03:36:34
Done. There goes my attempt to mint a new word...
| |
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); | |
Peter Kasting
2014/06/11 18:20:48
nIT: Indent 4, not even (2 places)
benwells
2014/06/12 03:36:35
Done.
| |
39 } | |
40 | |
41 int GetTotalSpacingWhileAnimating() { | |
42 return GetBubbleOuterPadding(true) + LocationBarView::kItemPadding + | |
43 GetBubbleOuterPadding(false); | |
44 } | |
45 | |
46 } // namespace | |
47 | |
48 AddToAppLauncherView::AddToAppLauncherView(LocationBarView* parent, | |
49 const gfx::FontList& font_list, | |
50 SkColor text_color, | |
51 SkColor parent_background_color) | |
52 : parent_(parent), | |
53 background_painter_( | |
54 views::Painter::CreateImageGridPainter(kBackgroundImages)), | |
55 icon_(new views::ImageView), | |
56 text_label_(new views::Label(base::string16(), font_list)), | |
57 slide_animator_(this) { | |
58 icon_->SetHorizontalAlignment(views::ImageView::LEADING); | |
59 icon_->SetImage(ui::ResourceBundle::GetSharedInstance().GetImageSkiaNamed( | |
60 IDR_BOOKMARK_BAR_APPS_SHORTCUT)); | |
61 icon_->SetTooltipText( | |
62 base::UTF8ToUTF16(l10n_util::GetStringUTF8(IDS_ADD_TO_APP_LIST_HINT))); | |
63 AddChildView(icon_); | |
64 | |
65 text_label_->SetVisible(false); | |
66 text_label_->SetEnabledColor(text_color); | |
67 // Calculate the actual background color for the label. The background images | |
68 // are painted atop |parent_background_color|. We grab the color of the | |
69 // middle pixel of the middle image of the background, which we treat as the | |
70 // representative color of the entire background (reasonable, given the | |
71 // current appearance of these images). Then we alpha-blend it over the | |
72 // parent background color to determine the actual color the label text will | |
73 // sit atop. | |
74 const SkBitmap& bitmap(ui::ResourceBundle::GetSharedInstance() | |
75 .GetImageSkiaNamed(kBackgroundImages[4]) | |
76 ->GetRepresentation(1.0f) | |
77 .sk_bitmap()); | |
Peter Kasting
2014/06/11 18:20:48
Nit: Can you wrap this like the code in content_se
benwells
2014/06/12 03:36:35
Done.
| |
78 SkAutoLockPixels pixel_lock(bitmap); | |
79 SkColor background_image_color = | |
80 bitmap.getColor(bitmap.width() / 2, bitmap.height() / 2); | |
81 // Tricky bit: We alpha blend an opaque version of |background_image_color| | |
82 // against |parent_background_color| using the original image grid color's | |
83 // alpha. This is because AlphaBlend(a, b, 255) always returns |a| unchanged | |
84 // even if |a| is a color with non-255 alpha. | |
85 text_label_->SetBackgroundColor( | |
86 color_utils::AlphaBlend(SkColorSetA(background_image_color, 255), | |
87 parent_background_color, | |
88 SkColorGetA(background_image_color))); | |
89 text_label_->SetHorizontalAlignment(gfx::ALIGN_LEFT); | |
90 text_label_->SetElideBehavior(gfx::TRUNCATE); | |
91 text_label_->SetText(base::UTF8ToUTF16( | |
92 l10n_util::GetStringUTF8(IDS_ADD_TO_APP_LIST_NOTIFICATION_TEXT))); | |
93 AddChildView(text_label_); | |
94 | |
95 slide_animator_.SetSlideDuration(kAnimationDurationMS); | |
96 slide_animator_.SetTweenType(gfx::Tween::LINEAR); | |
97 } | |
98 | |
99 AddToAppLauncherView::~AddToAppLauncherView() { | |
100 } | |
101 | |
102 void AddToAppLauncherView::Update(content::WebContents* web_contents) { | |
103 SetVisible(false); | |
Peter Kasting
2014/06/11 18:20:48
In the cases where we ultimately remain invisible,
benwells
2014/06/12 03:36:34
Yep good catch. Done.
I think it will require fur
| |
104 | |
105 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); | |
106 if (!command_line->HasSwitch(switches::kEnableProminentURLAppFlow) || | |
107 !command_line->HasSwitch(switches::kEnableStreamlinedHostedApps)) { | |
Peter Kasting
2014/06/11 18:20:48
Nit: No {} (since you don't use {} on one-line bod
benwells
2014/06/12 03:36:34
There isn't a single statement if anymore, so kept
| |
108 return; | |
109 } | |
110 | |
111 if (!web_contents || web_contents->IsLoading() || | |
Peter Kasting
2014/06/11 18:20:48
Nit: Personally I'd just combine this with the con
benwells
2014/06/12 03:36:35
Done.
| |
112 !web_contents->GetLastCommittedURL().SchemeIsHTTPOrHTTPS()) { | |
113 return; | |
114 } | |
115 | |
116 SetVisible(true); | |
117 | |
118 if (!slide_animator_.is_animating()) { | |
119 text_label_->SetVisible(true); | |
120 slide_animator_.Show(); | |
121 } | |
122 } | |
123 | |
124 void AddToAppLauncherView::AnimationEnded(const gfx::Animation* animation) { | |
125 slide_animator_.Reset(); | |
126 text_label_->SetVisible(false); | |
127 parent_->Layout(); | |
Peter Kasting
2014/06/11 18:20:48
Nit: I might replace these last two lines with "An
benwells
2014/06/12 03:36:35
Done.
| |
128 parent_->SchedulePaint(); | |
129 } | |
130 | |
131 void AddToAppLauncherView::AnimationProgressed( | |
132 const gfx::Animation* animation) { | |
133 parent_->Layout(); | |
134 parent_->SchedulePaint(); | |
135 } | |
136 | |
137 void AddToAppLauncherView::AnimationCanceled(const gfx::Animation* animation) { | |
138 AnimationEnded(animation); | |
139 } | |
140 | |
141 gfx::Size AddToAppLauncherView::GetPreferredSize() const { | |
142 // Height will be ignored by the LocationBarView. | |
143 gfx::Size size(icon_->GetPreferredSize()); | |
144 if (slide_animator_.is_animating()) { | |
145 double state = slide_animator_.GetCurrentValue(); | |
146 // The fraction of the animation we'll spend animating the string into view, | |
147 // which is also the fraction we'll spend animating it closed; total | |
148 // animation (slide out, show, then slide in) is 1.0. | |
149 const double kOpenFraction = | |
150 static_cast<double>(kOpenTimeMS) / kAnimationDurationMS; | |
151 double size_fraction = 1.0; | |
152 if (state < kOpenFraction) | |
153 size_fraction = state / kOpenFraction; | |
154 if (state > (1.0 - kOpenFraction)) | |
155 size_fraction = (1.0 - state) / kOpenFraction; | |
156 size.Enlarge(size_fraction * (text_label_->GetPreferredSize().width() + | |
157 GetTotalSpacingWhileAnimating()), | |
Peter Kasting
2014/06/11 18:20:48
Nit: Slightly strange indenting, the original inde
benwells
2014/06/12 03:36:34
That's clang format. I prefer this (the indenting
| |
158 0); | |
159 size.SetToMax(background_painter_->GetMinimumSize()); | |
160 } | |
161 return size; | |
162 } | |
163 | |
164 void AddToAppLauncherView::Layout() { | |
165 const int icon_width = icon_->GetPreferredSize().width(); | |
166 icon_->SetBounds( | |
167 std::min((width() - icon_width) / 2, GetBubbleOuterPadding(true)), | |
168 0, | |
169 icon_width, | |
170 height()); | |
171 text_label_->SetBounds( | |
172 icon_->bounds().right() + LocationBarView::kItemPadding, | |
173 0, | |
174 std::max(width() - GetTotalSpacingWhileAnimating() - icon_width, 0), | |
175 height()); | |
176 } | |
177 | |
178 void AddToAppLauncherView::OnPaintBackground(gfx::Canvas* canvas) { | |
179 if (slide_animator_.is_animating()) | |
180 background_painter_->Paint(canvas, size()); | |
181 } | |
OLD | NEW |