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

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"
11 #include "base/single_thread_task_runner.h" 11 #include "base/single_thread_task_runner.h"
12 #include "base/threading/thread_task_runner_handle.h" 12 #include "base/threading/thread_task_runner_handle.h"
13 #include "base/time/time.h" 13 #include "base/time/time.h"
14 #include "cc/paint/paint_flags.h" 14 #include "cc/paint/paint_flags.h"
15 #include "chrome/app/vector_icons/vector_icons.h" 15 #include "chrome/app/vector_icons/vector_icons.h"
16 #include "chrome/browser/themes/theme_properties.h" 16 #include "chrome/browser/themes/theme_properties.h"
17 #include "chrome/browser/ui/browser.h" 17 #include "chrome/browser/ui/browser.h"
18 #include "chrome/browser/ui/browser_otr_state.h" 18 #include "chrome/browser/ui/browser_otr_state.h"
19 #include "chrome/browser/ui/layout_constants.h" 19 #include "chrome/browser/ui/layout_constants.h"
20 #include "chrome/browser/ui/tabs/tab_strip_model.h" 20 #include "chrome/browser/ui/tabs/tab_strip_model.h"
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"
25 #include "chrome/browser/ui/views/toolbar/toolbar_button.h" 24 #include "chrome/browser/ui/views/toolbar/toolbar_button.h"
26 #include "chrome/browser/ui/views/toolbar/toolbar_view.h" 25 #include "chrome/browser/ui/views/toolbar/toolbar_view.h"
27 #include "chrome/common/chrome_switches.h" 26 #include "chrome/common/chrome_switches.h"
28 #include "chrome/grit/theme_resources.h" 27 #include "chrome/grit/theme_resources.h"
29 #include "ui/base/resource/resource_bundle.h" 28 #include "ui/base/resource/resource_bundle.h"
30 #include "ui/base/theme_provider.h" 29 #include "ui/base/theme_provider.h"
30 #include "ui/gfx/canvas.h"
31 #include "ui/gfx/color_palette.h" 31 #include "ui/gfx/color_palette.h"
32 #include "ui/gfx/paint_vector_icon.h" 32 #include "ui/gfx/paint_vector_icon.h"
33 #include "ui/gfx/scoped_canvas.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;
40 } // namespace 41 } // namespace
41 42
43 namespace internal {
44
45 class VectorIconAnimator {
46 public:
47 explicit VectorIconAnimator(const gfx::VectorIcon& icon)
48 : icon_(icon), duration_(gfx::GetDurationOfAnimation(icon)) {}
49 ~VectorIconAnimator() {}
50
51 void Start(const base::Closure& animation_callback) {
52 start_time_ = base::TimeTicks::Now();
53 timer_.Start(FROM_HERE, base::TimeDelta::FromMilliseconds(17), this,
54 &VectorIconAnimator::TimerFired);
55 callback_ = animation_callback;
56 }
57
58 base::TimeDelta GetElapsedTime() {
59 return base::TimeTicks::Now() - start_time_;
60 }
61
62 void PaintIcon(gfx::Canvas* canvas,
63 SkColor color,
64 const gfx::Point& position) {
65 if (timer_.IsRunning()) {
66 gfx::ScopedCanvas scoped(canvas);
67 canvas->Translate(position.OffsetFromOrigin());
68 base::TimeDelta elapsed = base::TimeTicks::Now() - start_time_;
69 gfx::PaintVectorIcon(canvas, icon_, color, &elapsed);
70 } else {
71 canvas->DrawImageInt(gfx::CreateVectorIcon(icon_, color), position.x(),
72 position.y());
73 }
74 }
75
76 private:
77 void TimerFired() {
78 if (duration_ > base::TimeTicks::Now() - start_time_)
79 timer_.Stop();
80
81 callback_.Run();
82 }
83
84 const gfx::VectorIcon& icon_;
85 base::Closure callback_;
86 base::TimeTicks start_time_;
87 base::RepeatingTimer timer_;
88 const base::TimeDelta duration_;
89
90 DISALLOW_COPY_AND_ASSIGN(VectorIconAnimator);
91 };
92 }
93
42 // static 94 // static
43 bool AppMenuButton::g_open_app_immediately_for_testing = false; 95 bool AppMenuButton::g_open_app_immediately_for_testing = false;
44 96
45 AppMenuButton::AppMenuButton(ToolbarView* toolbar_view) 97 AppMenuButton::AppMenuButton(ToolbarView* toolbar_view)
46 : views::MenuButton(base::string16(), toolbar_view, false), 98 : views::MenuButton(base::string16(), toolbar_view, false),
47 severity_(AppMenuIconController::Severity::NONE), 99 severity_(AppMenuIconController::Severity::NONE),
48 type_(AppMenuIconController::IconType::NONE), 100 type_(AppMenuIconController::IconType::NONE),
49 toolbar_view_(toolbar_view), 101 toolbar_view_(toolbar_view),
50 should_use_new_icon_(false), 102 should_use_new_icon_(false),
51 margin_trailing_(0), 103 margin_trailing_(0),
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
124 menu_listeners_.RemoveObserver(listener); 176 menu_listeners_.RemoveObserver(listener);
125 } 177 }
126 178
127 gfx::Size AppMenuButton::GetPreferredSize() const { 179 gfx::Size AppMenuButton::GetPreferredSize() const {
128 gfx::Rect rect(gfx::Size(kIconSize, kIconSize)); 180 gfx::Rect rect(gfx::Size(kIconSize, kIconSize));
129 rect.Inset(gfx::Insets(-ToolbarButton::kInteriorPadding)); 181 rect.Inset(gfx::Insets(-ToolbarButton::kInteriorPadding));
130 return rect.size(); 182 return rect.size();
131 } 183 }
132 184
133 void AppMenuButton::Layout() { 185 void AppMenuButton::Layout() {
134 if (animation_) { 186 if (animator_) {
135 ink_drop_container()->SetBoundsRect(GetLocalBounds()); 187 ink_drop_container()->SetBoundsRect(GetLocalBounds());
136 image()->SetBoundsRect(GetLocalBounds()); 188 image()->SetBoundsRect(GetLocalBounds());
137 return; 189 return;
138 } 190 }
139 191
140 views::MenuButton::Layout(); 192 views::MenuButton::Layout();
141 } 193 }
142 194
143 void AppMenuButton::OnPaint(gfx::Canvas* canvas) { 195 void AppMenuButton::OnPaint(gfx::Canvas* canvas) {
144 if (!animation_) { 196 if (!animator_) {
145 views::MenuButton::OnPaint(canvas); 197 views::MenuButton::OnPaint(canvas);
146 return; 198 return;
147 } 199 }
148 200
149 gfx::Rect bounds = GetLocalBounds(); 201 animator_->PaintIcon(canvas, SK_ColorRED,
150 bounds.Inset(gfx::Insets(ToolbarButton::kInteriorPadding)); 202 gfx::Point(ToolbarButton::kInteriorPadding,
151 animation_->PaintAppMenu(canvas, bounds); 203 ToolbarButton::kInteriorPadding));
152 } 204 }
153 205
154 void AppMenuButton::TabInsertedAt(TabStripModel* tab_strip_model, 206 void AppMenuButton::TabInsertedAt(TabStripModel* tab_strip_model,
155 content::WebContents* contents, 207 content::WebContents* contents,
156 int index, 208 int index,
157 bool foreground) { 209 bool foreground) {
158 AnimateIconIfPossible(); 210 AnimateIconIfPossible();
159 } 211 }
160 212
161 void AppMenuButton::UpdateIcon(bool should_animate) { 213 void AppMenuButton::UpdateIcon(bool should_animate) {
(...skipping 13 matching lines...) Expand all
175 severity_color = native_theme->GetSystemColor( 227 severity_color = native_theme->GetSystemColor(
176 ui::NativeTheme::kColorId_AlertSeverityMedium); 228 ui::NativeTheme::kColorId_AlertSeverityMedium);
177 break; 229 break;
178 case AppMenuIconController::Severity::HIGH: 230 case AppMenuIconController::Severity::HIGH:
179 severity_color = native_theme->GetSystemColor( 231 severity_color = native_theme->GetSystemColor(
180 ui::NativeTheme::kColorId_AlertSeverityHigh); 232 ui::NativeTheme::kColorId_AlertSeverityHigh);
181 break; 233 break;
182 } 234 }
183 235
184 if (should_use_new_icon_) { 236 if (should_use_new_icon_) {
185 if (!animation_) 237 if (!animator_)
186 animation_ = base::MakeUnique<AppMenuAnimation>(this, toolbar_icon_color); 238 animator_.reset(
239 new internal::VectorIconAnimator(kBrowserToolsAnimatedIcon));
187 240
188 animation_->set_target_color(severity_color);
189 if (should_animate) 241 if (should_animate)
190 AnimateIconIfPossible(); 242 AnimateIconIfPossible();
191
192 return; 243 return;
193 } 244 }
194 245
195 const gfx::VectorIcon* icon_id = nullptr; 246 const gfx::VectorIcon* icon_id = nullptr;
196 switch (type_) { 247 switch (type_) {
197 case AppMenuIconController::IconType::NONE: 248 case AppMenuIconController::IconType::NONE:
198 icon_id = &kBrowserToolsIcon; 249 icon_id = &kBrowserToolsIcon;
199 DCHECK_EQ(AppMenuIconController::Severity::NONE, severity_); 250 DCHECK_EQ(AppMenuIconController::Severity::NONE, severity_);
200 break; 251 break;
201 case AppMenuIconController::IconType::UPGRADE_NOTIFICATION: 252 case AppMenuIconController::IconType::UPGRADE_NOTIFICATION:
202 icon_id = &kBrowserToolsUpdateIcon; 253 icon_id = &kBrowserToolsUpdateIcon;
203 break; 254 break;
204 case AppMenuIconController::IconType::GLOBAL_ERROR: 255 case AppMenuIconController::IconType::GLOBAL_ERROR:
205 case AppMenuIconController::IconType::INCOMPATIBILITY_WARNING: 256 case AppMenuIconController::IconType::INCOMPATIBILITY_WARNING:
206 icon_id = &kBrowserToolsErrorIcon; 257 icon_id = &kBrowserToolsErrorIcon;
207 break; 258 break;
208 } 259 }
209 260
210 SetImage(views::Button::STATE_NORMAL, 261 SetImage(views::Button::STATE_NORMAL,
211 gfx::CreateVectorIcon(*icon_id, severity_color)); 262 gfx::CreateVectorIcon(*icon_id, severity_color));
212 } 263 }
213 264
214 void AppMenuButton::SetTrailingMargin(int margin) { 265 void AppMenuButton::SetTrailingMargin(int margin) {
215 margin_trailing_ = margin; 266 margin_trailing_ = margin;
216 UpdateThemedBorder(); 267 UpdateThemedBorder();
217 InvalidateLayout(); 268 InvalidateLayout();
218 } 269 }
219 270
220 void AppMenuButton::AnimateIconIfPossible() { 271 void AppMenuButton::AnimateIconIfPossible() {
221 if (!animation_ || !should_use_new_icon_ || 272 if (!animator_ || !should_use_new_icon_ ||
222 severity_ == AppMenuIconController::Severity::NONE) { 273 severity_ == AppMenuIconController::Severity::NONE) {
223 return; 274 return;
224 } 275 }
225 276
226 animation_->StartAnimation(); 277 animator_->Start(
227 } 278 base::Bind(&AppMenuButton::SchedulePaint, base::Unretained(this)));
228
229 void AppMenuButton::AppMenuAnimationStarted() {
230 SetPaintToLayer();
231 layer()->SetFillsBoundsOpaquely(false);
232 }
233
234 void AppMenuButton::AppMenuAnimationEnded() {
235 DestroyLayer();
236 } 279 }
237 280
238 const char* AppMenuButton::GetClassName() const { 281 const char* AppMenuButton::GetClassName() const {
239 return "AppMenuButton"; 282 return "AppMenuButton";
240 } 283 }
241 284
242 std::unique_ptr<views::LabelButtonBorder> AppMenuButton::CreateDefaultBorder() 285 std::unique_ptr<views::LabelButtonBorder> AppMenuButton::CreateDefaultBorder()
243 const { 286 const {
244 std::unique_ptr<views::LabelButtonBorder> border = 287 std::unique_ptr<views::LabelButtonBorder> border =
245 MenuButton::CreateDefaultBorder(); 288 MenuButton::CreateDefaultBorder();
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
292 return ui::DragDropTypes::DRAG_MOVE; 335 return ui::DragDropTypes::DRAG_MOVE;
293 } 336 }
294 337
295 void AppMenuButton::OnDragExited() { 338 void AppMenuButton::OnDragExited() {
296 weak_factory_.InvalidateWeakPtrs(); 339 weak_factory_.InvalidateWeakPtrs();
297 } 340 }
298 341
299 int AppMenuButton::OnPerformDrop(const ui::DropTargetEvent& event) { 342 int AppMenuButton::OnPerformDrop(const ui::DropTargetEvent& event) {
300 return ui::DragDropTypes::DRAG_MOVE; 343 return ui::DragDropTypes::DRAG_MOVE;
301 } 344 }
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