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

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

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

Powered by Google App Engine
This is Rietveld 408576698