Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(342)

Side by Side Diff: chrome/browser/ui/views/toolbar/app_menu_button.cc

Issue 2860163002: WIP - allow vector icons to specify motion.
Patch Set: checkpoint Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « chrome/browser/ui/views/toolbar/app_menu_button.h ('k') | testing/gmock/BUILD.gn » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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/toolbar/app_menu_button.h" 5 #include "chrome/browser/ui/views/toolbar/app_menu_button.h"
6 6
7 #include "base/command_line.h" 7 #include "base/command_line.h"
8 #include "base/location.h" 8 #include "base/location.h"
9 #include "base/memory/ptr_util.h" 9 #include "base/memory/ptr_util.h"
10 #include "base/metrics/histogram_macros.h" 10 #include "base/metrics/histogram_macros.h"
(...skipping 10 matching lines...) Expand all
21 #include "chrome/browser/ui/toolbar/app_menu_model.h" 21 #include "chrome/browser/ui/toolbar/app_menu_model.h"
22 #include "chrome/browser/ui/views/extensions/browser_action_drag_data.h" 22 #include "chrome/browser/ui/views/extensions/browser_action_drag_data.h"
23 #include "chrome/browser/ui/views/toolbar/app_menu.h" 23 #include "chrome/browser/ui/views/toolbar/app_menu.h"
24 #include "chrome/browser/ui/views/toolbar/app_menu_animation.h" 24 #include "chrome/browser/ui/views/toolbar/app_menu_animation.h"
25 #include "chrome/browser/ui/views/toolbar/toolbar_button.h" 25 #include "chrome/browser/ui/views/toolbar/toolbar_button.h"
26 #include "chrome/browser/ui/views/toolbar/toolbar_view.h" 26 #include "chrome/browser/ui/views/toolbar/toolbar_view.h"
27 #include "chrome/common/chrome_switches.h" 27 #include "chrome/common/chrome_switches.h"
28 #include "chrome/grit/theme_resources.h" 28 #include "chrome/grit/theme_resources.h"
29 #include "ui/base/resource/resource_bundle.h" 29 #include "ui/base/resource/resource_bundle.h"
30 #include "ui/base/theme_provider.h" 30 #include "ui/base/theme_provider.h"
31 #include "ui/gfx/canvas.h"
31 #include "ui/gfx/color_palette.h" 32 #include "ui/gfx/color_palette.h"
32 #include "ui/gfx/paint_vector_icon.h" 33 #include "ui/gfx/paint_vector_icon.h"
33 #include "ui/keyboard/keyboard_controller.h" 34 #include "ui/keyboard/keyboard_controller.h"
34 #include "ui/views/controls/button/label_button_border.h" 35 #include "ui/views/controls/button/label_button_border.h"
35 #include "ui/views/controls/menu/menu_listener.h" 36 #include "ui/views/controls/menu/menu_listener.h"
36 #include "ui/views/metrics.h" 37 #include "ui/views/metrics.h"
37 38
38 namespace { 39 namespace {
39 const float kIconSize = 16; 40 const float kIconSize = 16;
41
42 class VectorIconAnimator {
43 public:
44 explicit VectorIconAnimator(const gfx::VectorIcon& icon)
45 : icon_(icon), duration_(gfx::GetDurationOfAnimation(icon)) {}
46 ~VectorIconAnimator() {}
47
48 void Start(const base::Closure& animation_callback) {
49 start_time_ = base::TimeTicks::Now();
50 timer_.Start(FROM_HERE, base::TimeDelta::FromMilliseconds(17), this,
51 &VectorIconAnimator::TimerFired);
52 callback_ = animation_callback;
53 }
54
55 base::TimeDelta GetElapsedTime() {
56 return base::TimeTicks::Now() - start_time_;
57 }
58
59 void PaintIcon(gfx::Canvas* canvas,
60 SkColor color,
61 const gfx::Point& position) {
62 if (timer_.IsRunning()) {
63 gfx::ScopedCanvas scoped(canvas);
64 canvas->Translate(position.OffsetFromOrigin());
65 base::TimeDelta elapsed = base::TimeTicks::Now() - start_time_;
66 gfx::PaintVectorIcon(canvas, icon_, color, &elapsed);
67 } else {
68 canvas->DrawImageInt(gfx::CreateVectorIcon(icon_, color), position.x(),
69 position.y());
70 }
71 }
72
73 private:
74 void TimerFired() {
75 if (duration_ > base::TimeTicks::Now() - start_time_)
76 timer_.Stop();
77
78 callback_.Run();
79 }
80
81 const gfx::VectorIcon& icon_;
82 base::Closure callback_;
83 base::TimeTicks start_time_;
84 base::RepeatingTimer timer_;
85 const base::TimeDelta duration_;
86
87 DISALLOW_COPY_AND_ASSIGN(VectorIconAnimator);
88 };
89
40 } // namespace 90 } // namespace
41 91
42 // static 92 // static
43 bool AppMenuButton::g_open_app_immediately_for_testing = false; 93 bool AppMenuButton::g_open_app_immediately_for_testing = false;
44 94
45 AppMenuButton::AppMenuButton(ToolbarView* toolbar_view) 95 AppMenuButton::AppMenuButton(ToolbarView* toolbar_view)
46 : views::MenuButton(base::string16(), toolbar_view, false), 96 : views::MenuButton(base::string16(), toolbar_view, false),
47 severity_(AppMenuIconController::Severity::NONE), 97 severity_(AppMenuIconController::Severity::NONE),
48 type_(AppMenuIconController::IconType::NONE), 98 type_(AppMenuIconController::IconType::NONE),
49 toolbar_view_(toolbar_view), 99 toolbar_view_(toolbar_view),
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
124 menu_listeners_.RemoveObserver(listener); 174 menu_listeners_.RemoveObserver(listener);
125 } 175 }
126 176
127 gfx::Size AppMenuButton::GetPreferredSize() const { 177 gfx::Size AppMenuButton::GetPreferredSize() const {
128 gfx::Rect rect(gfx::Size(kIconSize, kIconSize)); 178 gfx::Rect rect(gfx::Size(kIconSize, kIconSize));
129 rect.Inset(gfx::Insets(-ToolbarButton::kInteriorPadding)); 179 rect.Inset(gfx::Insets(-ToolbarButton::kInteriorPadding));
130 return rect.size(); 180 return rect.size();
131 } 181 }
132 182
133 void AppMenuButton::Layout() { 183 void AppMenuButton::Layout() {
134 if (animation_) { 184 if (animator_) {
135 ink_drop_container()->SetBoundsRect(GetLocalBounds()); 185 ink_drop_container()->SetBoundsRect(GetLocalBounds());
136 image()->SetBoundsRect(GetLocalBounds()); 186 image()->SetBoundsRect(GetLocalBounds());
137 return; 187 return;
138 } 188 }
139 189
140 views::MenuButton::Layout(); 190 views::MenuButton::Layout();
141 } 191 }
142 192
143 void AppMenuButton::OnPaint(gfx::Canvas* canvas) { 193 void AppMenuButton::OnPaint(gfx::Canvas* canvas) {
144 if (!animation_) { 194 if (!animator_) {
145 views::MenuButton::OnPaint(canvas); 195 views::MenuButton::OnPaint(canvas);
146 return; 196 return;
147 } 197 }
148 198
149 gfx::Rect bounds = GetLocalBounds(); 199 animator_->PaintIcon(canvas, SK_ColorRED,
150 bounds.Inset(gfx::Insets(ToolbarButton::kInteriorPadding)); 200 gfx::Vector2d(ToolbarButton::kInteriorPadding,
151 animation_->PaintAppMenu(canvas, bounds); 201 ToolbarButton::kInteriorPadding));
152 } 202 }
153 203
154 void AppMenuButton::TabInsertedAt(TabStripModel* tab_strip_model, 204 void AppMenuButton::TabInsertedAt(TabStripModel* tab_strip_model,
155 content::WebContents* contents, 205 content::WebContents* contents,
156 int index, 206 int index,
157 bool foreground) { 207 bool foreground) {
158 AnimateIconIfPossible(); 208 AnimateIconIfPossible();
159 } 209 }
160 210
161 void AppMenuButton::UpdateIcon(bool should_animate) { 211 void AppMenuButton::UpdateIcon(bool should_animate) {
(...skipping 13 matching lines...) Expand all
175 severity_color = native_theme->GetSystemColor( 225 severity_color = native_theme->GetSystemColor(
176 ui::NativeTheme::kColorId_AlertSeverityMedium); 226 ui::NativeTheme::kColorId_AlertSeverityMedium);
177 break; 227 break;
178 case AppMenuIconController::Severity::HIGH: 228 case AppMenuIconController::Severity::HIGH:
179 severity_color = native_theme->GetSystemColor( 229 severity_color = native_theme->GetSystemColor(
180 ui::NativeTheme::kColorId_AlertSeverityHigh); 230 ui::NativeTheme::kColorId_AlertSeverityHigh);
181 break; 231 break;
182 } 232 }
183 233
184 if (should_use_new_icon_) { 234 if (should_use_new_icon_) {
185 if (!animation_) 235 if (!animator_)
186 animation_ = base::MakeUnique<AppMenuAnimation>(this, toolbar_icon_color); 236 animator_.reset(new VectorIconAnimator(kBrowserToolsAnimatedIcon));
187 237
188 animation_->set_target_color(severity_color); 238 // animation_->set_target_color(severity_color);
189 if (should_animate) 239 if (should_animate)
190 AnimateIconIfPossible(); 240 AnimateIconIfPossible();
191
192 return; 241 return;
193 } 242 }
194 243
195 const gfx::VectorIcon* icon_id = nullptr; 244 const gfx::VectorIcon* icon_id = nullptr;
196 switch (type_) { 245 switch (type_) {
197 case AppMenuIconController::IconType::NONE: 246 case AppMenuIconController::IconType::NONE:
198 icon_id = &kBrowserToolsIcon; 247 icon_id = &kBrowserToolsIcon;
199 DCHECK_EQ(AppMenuIconController::Severity::NONE, severity_); 248 DCHECK_EQ(AppMenuIconController::Severity::NONE, severity_);
200 break; 249 break;
201 case AppMenuIconController::IconType::UPGRADE_NOTIFICATION: 250 case AppMenuIconController::IconType::UPGRADE_NOTIFICATION:
202 icon_id = &kBrowserToolsUpdateIcon; 251 icon_id = &kBrowserToolsUpdateIcon;
203 break; 252 break;
204 case AppMenuIconController::IconType::GLOBAL_ERROR: 253 case AppMenuIconController::IconType::GLOBAL_ERROR:
205 case AppMenuIconController::IconType::INCOMPATIBILITY_WARNING: 254 case AppMenuIconController::IconType::INCOMPATIBILITY_WARNING:
206 icon_id = &kBrowserToolsErrorIcon; 255 icon_id = &kBrowserToolsErrorIcon;
207 break; 256 break;
208 } 257 }
209 258
210 SetImage(views::Button::STATE_NORMAL, 259 SetImage(views::Button::STATE_NORMAL,
211 gfx::CreateVectorIcon(*icon_id, severity_color)); 260 gfx::CreateVectorIcon(*icon_id, severity_color));
212 } 261 }
213 262
214 void AppMenuButton::SetTrailingMargin(int margin) { 263 void AppMenuButton::SetTrailingMargin(int margin) {
215 margin_trailing_ = margin; 264 margin_trailing_ = margin;
216 UpdateThemedBorder(); 265 UpdateThemedBorder();
217 InvalidateLayout(); 266 InvalidateLayout();
218 } 267 }
219 268
220 void AppMenuButton::AnimateIconIfPossible() { 269 void AppMenuButton::AnimateIconIfPossible() {
221 if (!animation_ || !should_use_new_icon_ || 270 if (/*!animation_ ||*/ !should_use_new_icon_ ||
222 severity_ == AppMenuIconController::Severity::NONE) { 271 severity_ == AppMenuIconController::Severity::NONE) {
223 return; 272 return;
224 } 273 }
225 274
226 animation_->StartAnimation(); 275 // animation_->StartAnimation();
276 animation_start_time_ = base::TimeTicks::Now();
277 animation_timer_.Start(FROM_HERE, base::TimeDelta::FromMilliseconds(17),
278 static_cast<views::View*>(this),
279 &views::View::SchedulePaint);
227 } 280 }
228 281
229 void AppMenuButton::AppMenuAnimationStarted() { 282 void AppMenuButton::AppMenuAnimationStarted() {
230 SetPaintToLayer(); 283 SetPaintToLayer();
231 layer()->SetFillsBoundsOpaquely(false); 284 layer()->SetFillsBoundsOpaquely(false);
232 } 285 }
233 286
234 void AppMenuButton::AppMenuAnimationEnded() { 287 void AppMenuButton::AppMenuAnimationEnded() {
235 DestroyLayer(); 288 DestroyLayer();
236 } 289 }
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
292 return ui::DragDropTypes::DRAG_MOVE; 345 return ui::DragDropTypes::DRAG_MOVE;
293 } 346 }
294 347
295 void AppMenuButton::OnDragExited() { 348 void AppMenuButton::OnDragExited() {
296 weak_factory_.InvalidateWeakPtrs(); 349 weak_factory_.InvalidateWeakPtrs();
297 } 350 }
298 351
299 int AppMenuButton::OnPerformDrop(const ui::DropTargetEvent& event) { 352 int AppMenuButton::OnPerformDrop(const ui::DropTargetEvent& event) {
300 return ui::DragDropTypes::DRAG_MOVE; 353 return ui::DragDropTypes::DRAG_MOVE;
301 } 354 }
OLDNEW
« no previous file with comments | « chrome/browser/ui/views/toolbar/app_menu_button.h ('k') | testing/gmock/BUILD.gn » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698