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

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

Issue 2789203003: [Views] App Menu Animated Icon (Closed)
Patch Set: Cleaned up Created 3 years, 8 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
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/location.h" 8 #include "base/location.h"
8 #include "base/metrics/histogram_macros.h" 9 #include "base/metrics/histogram_macros.h"
9 #include "base/single_thread_task_runner.h" 10 #include "base/single_thread_task_runner.h"
10 #include "base/threading/thread_task_runner_handle.h" 11 #include "base/threading/thread_task_runner_handle.h"
11 #include "base/time/time.h" 12 #include "base/time/time.h"
13 #include "cc/paint/paint_flags.h"
12 #include "chrome/app/vector_icons/vector_icons.h" 14 #include "chrome/app/vector_icons/vector_icons.h"
13 #include "chrome/browser/themes/theme_properties.h" 15 #include "chrome/browser/themes/theme_properties.h"
14 #include "chrome/browser/ui/browser.h" 16 #include "chrome/browser/ui/browser.h"
15 #include "chrome/browser/ui/browser_otr_state.h" 17 #include "chrome/browser/ui/browser_otr_state.h"
16 #include "chrome/browser/ui/layout_constants.h" 18 #include "chrome/browser/ui/layout_constants.h"
19 #include "chrome/browser/ui/tabs/tab_strip_model.h"
17 #include "chrome/browser/ui/toolbar/app_menu_model.h" 20 #include "chrome/browser/ui/toolbar/app_menu_model.h"
18 #include "chrome/browser/ui/views/extensions/browser_action_drag_data.h" 21 #include "chrome/browser/ui/views/extensions/browser_action_drag_data.h"
19 #include "chrome/browser/ui/views/toolbar/app_menu.h" 22 #include "chrome/browser/ui/views/toolbar/app_menu.h"
23 #include "chrome/browser/ui/views/toolbar/app_menu_animation.h"
20 #include "chrome/browser/ui/views/toolbar/toolbar_button.h" 24 #include "chrome/browser/ui/views/toolbar/toolbar_button.h"
21 #include "chrome/browser/ui/views/toolbar/toolbar_view.h" 25 #include "chrome/browser/ui/views/toolbar/toolbar_view.h"
26 #include "chrome/common/chrome_switches.h"
22 #include "chrome/grit/theme_resources.h" 27 #include "chrome/grit/theme_resources.h"
23 #include "ui/base/resource/resource_bundle.h" 28 #include "ui/base/resource/resource_bundle.h"
24 #include "ui/base/theme_provider.h" 29 #include "ui/base/theme_provider.h"
25 #include "ui/gfx/color_palette.h" 30 #include "ui/gfx/color_palette.h"
26 #include "ui/gfx/paint_vector_icon.h" 31 #include "ui/gfx/paint_vector_icon.h"
27 #include "ui/keyboard/keyboard_controller.h" 32 #include "ui/keyboard/keyboard_controller.h"
28 #include "ui/views/controls/button/label_button_border.h" 33 #include "ui/views/controls/button/label_button_border.h"
29 #include "ui/views/controls/menu/menu_listener.h" 34 #include "ui/views/controls/menu/menu_listener.h"
30 #include "ui/views/metrics.h" 35 #include "ui/views/metrics.h"
31 36
37 namespace {
38 const float kIconSize = 16;
39 } // namespace
40
32 // static 41 // static
33 bool AppMenuButton::g_open_app_immediately_for_testing = false; 42 bool AppMenuButton::g_open_app_immediately_for_testing = false;
34 43
35 AppMenuButton::AppMenuButton(ToolbarView* toolbar_view) 44 AppMenuButton::AppMenuButton(ToolbarView* toolbar_view)
36 : views::MenuButton(base::string16(), toolbar_view, false), 45 : views::MenuButton(base::string16(), toolbar_view, false),
37 severity_(AppMenuIconController::Severity::NONE), 46 severity_(AppMenuIconController::Severity::NONE),
38 type_(AppMenuIconController::IconType::NONE), 47 type_(AppMenuIconController::IconType::NONE),
39 toolbar_view_(toolbar_view), 48 toolbar_view_(toolbar_view),
40 margin_trailing_(0), 49 margin_trailing_(0),
41 weak_factory_(this) { 50 weak_factory_(this) {
42 SetInkDropMode(InkDropMode::ON); 51 SetInkDropMode(InkDropMode::ON);
43 SetFocusPainter(nullptr); 52 SetFocusPainter(nullptr);
53
54 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
55 if (command_line->HasSwitch(switches::kGlobalErrorMenuIcon)) {
56 std::string flag =
57 command_line->GetSwitchValueASCII(switches::kGlobalErrorMenuIcon);
58 if (flag != switches::kGlobalErrorMenuIconOldBehavior) {
59 if (flag == switches::kGlobalErrorMenuIconPersistentClosedState) {
60 Browser* browser = toolbar_view_->browser();
61 browser->tab_strip_model()->AddObserver(this);
62 animation_.reset(new AppMenuAnimation(this, true));
msw 2017/04/11 17:34:14 nit: = base::MakeUnique here and below
spqchan 2017/04/12 19:42:10 Done.
63 } else {
64 animation_.reset(new AppMenuAnimation(this, false));
65 }
66 }
67 }
44 } 68 }
45 69
46 AppMenuButton::~AppMenuButton() {} 70 AppMenuButton::~AppMenuButton() {}
47 71
48 void AppMenuButton::SetSeverity(AppMenuIconController::IconType type, 72 void AppMenuButton::SetSeverity(AppMenuIconController::IconType type,
49 AppMenuIconController::Severity severity, 73 AppMenuIconController::Severity severity,
50 bool animate) { 74 bool animate) {
51 type_ = type; 75 type_ = type;
52 severity_ = severity; 76 severity_ = severity;
53 UpdateIcon(); 77 UpdateIcon(animate);
54 } 78 }
55 79
56 void AppMenuButton::ShowMenu(bool for_drop) { 80 void AppMenuButton::ShowMenu(bool for_drop) {
57 if (menu_ && menu_->IsShowing()) 81 if (menu_ && menu_->IsShowing())
58 return; 82 return;
59 83
60 #if defined(USE_AURA) 84 #if defined(USE_AURA)
61 keyboard::KeyboardController* keyboard_controller = 85 keyboard::KeyboardController* keyboard_controller =
62 keyboard::KeyboardController::GetInstance(); 86 keyboard::KeyboardController::GetInstance();
63 if (keyboard_controller && keyboard_controller->keyboard_visible()) { 87 if (keyboard_controller && keyboard_controller->keyboard_visible()) {
(...skipping 14 matching lines...) Expand all
78 base::TimeTicks menu_open_time = base::TimeTicks::Now(); 102 base::TimeTicks menu_open_time = base::TimeTicks::Now();
79 menu_->RunMenu(this); 103 menu_->RunMenu(this);
80 104
81 if (!for_drop) { 105 if (!for_drop) {
82 // Record the time-to-action for the menu. We don't record in the case of a 106 // Record the time-to-action for the menu. We don't record in the case of a
83 // drag-and-drop command because menus opened for drag-and-drop don't block 107 // drag-and-drop command because menus opened for drag-and-drop don't block
84 // the message loop. 108 // the message loop.
85 UMA_HISTOGRAM_TIMES("Toolbar.AppMenuTimeToAction", 109 UMA_HISTOGRAM_TIMES("Toolbar.AppMenuTimeToAction",
86 base::TimeTicks::Now() - menu_open_time); 110 base::TimeTicks::Now() - menu_open_time);
87 } 111 }
112
113 if (severity_ != AppMenuIconController::Severity::NONE)
114 animation_->StartAnimation();
88 } 115 }
89 116
90 void AppMenuButton::CloseMenu() { 117 void AppMenuButton::CloseMenu() {
91 if (menu_) 118 if (menu_)
92 menu_->CloseMenu(); 119 menu_->CloseMenu();
93 menu_.reset(); 120 menu_.reset();
94 } 121 }
95 122
96 bool AppMenuButton::IsMenuShowing() const { 123 bool AppMenuButton::IsMenuShowing() const {
97 return menu_ && menu_->IsShowing(); 124 return menu_ && menu_->IsShowing();
98 } 125 }
99 126
100 void AppMenuButton::AddMenuListener(views::MenuListener* listener) { 127 void AppMenuButton::AddMenuListener(views::MenuListener* listener) {
101 menu_listeners_.AddObserver(listener); 128 menu_listeners_.AddObserver(listener);
102 } 129 }
103 130
104 void AppMenuButton::RemoveMenuListener(views::MenuListener* listener) { 131 void AppMenuButton::RemoveMenuListener(views::MenuListener* listener) {
105 menu_listeners_.RemoveObserver(listener); 132 menu_listeners_.RemoveObserver(listener);
106 } 133 }
107 134
108 gfx::Size AppMenuButton::GetPreferredSize() const { 135 gfx::Size AppMenuButton::GetPreferredSize() const {
109 gfx::Rect rect(image()->GetPreferredSize()); 136 gfx::Rect rect(gfx::Size(kIconSize, kIconSize));
110 rect.Inset(gfx::Insets(-ToolbarButton::kInteriorPadding)); 137 rect.Inset(gfx::Insets(-ToolbarButton::kInteriorPadding));
111 return rect.size(); 138 return rect.size();
112 } 139 }
113 140
114 void AppMenuButton::UpdateIcon() { 141 void AppMenuButton::Layout() {
115 SkColor color = gfx::kPlaceholderColor; 142 if (animation_.get()) {
msw 2017/04/11 17:34:14 nit: remove |.get()|, you can just check |if (anim
spqchan 2017/04/12 19:42:10 Done.
143 ink_drop_container()->SetBoundsRect(GetLocalBounds());
144 image()->SetBoundsRect(GetLocalBounds());
145 return;
146 }
147
148 views::MenuButton::Layout();
149 }
150
151 void AppMenuButton::OnPaint(gfx::Canvas* canvas) {
152 if (!animation_.get()) {
153 views::MenuButton::OnPaint(canvas);
154 return;
155 }
156
157 gfx::Rect bounds = GetLocalBounds();
158 bounds.Inset(gfx::Insets(ToolbarButton::kInteriorPadding));
159 animation_->PaintAppMenu(canvas, bounds);
160 }
161
162 void AppMenuButton::TabInsertedAt(TabStripModel* tab_strip_model,
163 content::WebContents* contents,
164 int index,
165 bool foreground) {
166 if (severity_ != AppMenuIconController::Severity::NONE)
167 animation_->StartAnimation();
168 }
169
170 void AppMenuButton::UpdateIcon(bool should_animate) {
171 SkColor severity_color = gfx::kPlaceholderColor;
172 SkColor toolbar_icon_color =
173 GetThemeProvider()->GetColor(ThemeProperties::COLOR_TOOLBAR_BUTTON_ICON);
116 const ui::NativeTheme* native_theme = GetNativeTheme(); 174 const ui::NativeTheme* native_theme = GetNativeTheme();
117 switch (severity_) { 175 switch (severity_) {
118 case AppMenuIconController::Severity::NONE: 176 case AppMenuIconController::Severity::NONE:
119 color = GetThemeProvider()->GetColor( 177 severity_color = toolbar_icon_color;
120 ThemeProperties::COLOR_TOOLBAR_BUTTON_ICON);
121 break; 178 break;
122 case AppMenuIconController::Severity::LOW: 179 case AppMenuIconController::Severity::LOW:
123 color = native_theme->GetSystemColor( 180 severity_color = native_theme->GetSystemColor(
124 ui::NativeTheme::kColorId_AlertSeverityLow); 181 ui::NativeTheme::kColorId_AlertSeverityLow);
125 break; 182 break;
126 case AppMenuIconController::Severity::MEDIUM: 183 case AppMenuIconController::Severity::MEDIUM:
127 color = native_theme->GetSystemColor( 184 severity_color = native_theme->GetSystemColor(
128 ui::NativeTheme::kColorId_AlertSeverityMedium); 185 ui::NativeTheme::kColorId_AlertSeverityMedium);
129 break; 186 break;
130 case AppMenuIconController::Severity::HIGH: 187 case AppMenuIconController::Severity::HIGH:
131 color = native_theme->GetSystemColor( 188 severity_color = native_theme->GetSystemColor(
132 ui::NativeTheme::kColorId_AlertSeverityHigh); 189 ui::NativeTheme::kColorId_AlertSeverityHigh);
133 break; 190 break;
134 } 191 }
135 192
193 if (animation_.get()) {
194 animation_->UpdateIconColor(toolbar_icon_color, severity_color);
195 if (should_animate)
196 animation_->StartAnimation();
197 return;
198 }
199
136 const gfx::VectorIcon* icon_id = nullptr; 200 const gfx::VectorIcon* icon_id = nullptr;
137 switch (type_) { 201 switch (type_) {
138 case AppMenuIconController::IconType::NONE: 202 case AppMenuIconController::IconType::NONE:
139 icon_id = &kBrowserToolsIcon; 203 icon_id = &kBrowserToolsIcon;
140 DCHECK_EQ(AppMenuIconController::Severity::NONE, severity_); 204 DCHECK_EQ(AppMenuIconController::Severity::NONE, severity_);
141 break; 205 break;
142 case AppMenuIconController::IconType::UPGRADE_NOTIFICATION: 206 case AppMenuIconController::IconType::UPGRADE_NOTIFICATION:
143 icon_id = &kBrowserToolsUpdateIcon; 207 icon_id = &kBrowserToolsUpdateIcon;
144 break; 208 break;
145 case AppMenuIconController::IconType::GLOBAL_ERROR: 209 case AppMenuIconController::IconType::GLOBAL_ERROR:
146 case AppMenuIconController::IconType::INCOMPATIBILITY_WARNING: 210 case AppMenuIconController::IconType::INCOMPATIBILITY_WARNING:
147 icon_id = &kBrowserToolsErrorIcon; 211 icon_id = &kBrowserToolsErrorIcon;
148 break; 212 break;
149 } 213 }
150 214
151 SetImage(views::Button::STATE_NORMAL, gfx::CreateVectorIcon(*icon_id, color)); 215 SetImage(views::Button::STATE_NORMAL,
216 gfx::CreateVectorIcon(*icon_id, severity_color));
152 } 217 }
153 218
154 void AppMenuButton::SetTrailingMargin(int margin) { 219 void AppMenuButton::SetTrailingMargin(int margin) {
155 margin_trailing_ = margin; 220 margin_trailing_ = margin;
156 UpdateThemedBorder(); 221 UpdateThemedBorder();
157 InvalidateLayout(); 222 InvalidateLayout();
158 } 223 }
159 224
160 const char* AppMenuButton::GetClassName() const { 225 const char* AppMenuButton::GetClassName() const {
161 return "AppMenuButton"; 226 return "AppMenuButton";
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
213 return ui::DragDropTypes::DRAG_MOVE; 278 return ui::DragDropTypes::DRAG_MOVE;
214 } 279 }
215 280
216 void AppMenuButton::OnDragExited() { 281 void AppMenuButton::OnDragExited() {
217 weak_factory_.InvalidateWeakPtrs(); 282 weak_factory_.InvalidateWeakPtrs();
218 } 283 }
219 284
220 int AppMenuButton::OnPerformDrop(const ui::DropTargetEvent& event) { 285 int AppMenuButton::OnPerformDrop(const ui::DropTargetEvent& event) {
221 return ui::DragDropTypes::DRAG_MOVE; 286 return ui::DragDropTypes::DRAG_MOVE;
222 } 287 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698