OLD | NEW |
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 29 matching lines...) Expand all Loading... |
40 } // namespace | 40 } // namespace |
41 | 41 |
42 // static | 42 // static |
43 bool AppMenuButton::g_open_app_immediately_for_testing = false; | 43 bool AppMenuButton::g_open_app_immediately_for_testing = false; |
44 | 44 |
45 AppMenuButton::AppMenuButton(ToolbarView* toolbar_view) | 45 AppMenuButton::AppMenuButton(ToolbarView* toolbar_view) |
46 : views::MenuButton(base::string16(), toolbar_view, false), | 46 : views::MenuButton(base::string16(), toolbar_view, false), |
47 severity_(AppMenuIconController::Severity::NONE), | 47 severity_(AppMenuIconController::Severity::NONE), |
48 type_(AppMenuIconController::IconType::NONE), | 48 type_(AppMenuIconController::IconType::NONE), |
49 toolbar_view_(toolbar_view), | 49 toolbar_view_(toolbar_view), |
| 50 should_use_new_icon_(false), |
50 margin_trailing_(0), | 51 margin_trailing_(0), |
51 weak_factory_(this) { | 52 weak_factory_(this) { |
52 SetInkDropMode(InkDropMode::ON); | 53 SetInkDropMode(InkDropMode::ON); |
53 SetFocusPainter(nullptr); | 54 SetFocusPainter(nullptr); |
54 | 55 |
55 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); | 56 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); |
56 if (command_line->HasSwitch(switches::kAppMenuIcon)) { | 57 if (command_line->HasSwitch(switches::kEnableNewAppMenuIcon)) { |
57 std::string flag = | 58 Browser* browser = toolbar_view_->browser(); |
58 command_line->GetSwitchValueASCII(switches::kAppMenuIcon); | 59 browser->tab_strip_model()->AddObserver(this); |
59 if (flag == switches::kAppMenuIconPersistentClosedState) { | 60 should_use_new_icon_ = true; |
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 } | 61 } |
67 } | 62 } |
68 | 63 |
69 AppMenuButton::~AppMenuButton() {} | 64 AppMenuButton::~AppMenuButton() {} |
70 | 65 |
71 void AppMenuButton::SetSeverity(AppMenuIconController::IconType type, | 66 void AppMenuButton::SetSeverity(AppMenuIconController::IconType type, |
72 AppMenuIconController::Severity severity, | 67 AppMenuIconController::Severity severity, |
73 bool animate) { | 68 bool animate) { |
74 type_ = type; | 69 type_ = type; |
75 severity_ = severity; | 70 severity_ = severity; |
(...skipping 26 matching lines...) Expand all Loading... |
102 menu_->RunMenu(this); | 97 menu_->RunMenu(this); |
103 | 98 |
104 if (!for_drop) { | 99 if (!for_drop) { |
105 // Record the time-to-action for the menu. We don't record in the case of a | 100 // Record the time-to-action for the menu. We don't record in the case of a |
106 // drag-and-drop command because menus opened for drag-and-drop don't block | 101 // drag-and-drop command because menus opened for drag-and-drop don't block |
107 // the message loop. | 102 // the message loop. |
108 UMA_HISTOGRAM_TIMES("Toolbar.AppMenuTimeToAction", | 103 UMA_HISTOGRAM_TIMES("Toolbar.AppMenuTimeToAction", |
109 base::TimeTicks::Now() - menu_open_time); | 104 base::TimeTicks::Now() - menu_open_time); |
110 } | 105 } |
111 | 106 |
112 if (animation_ && severity_ != AppMenuIconController::Severity::NONE) | 107 AnimateIconIfPossible(); |
113 animation_->StartAnimation(); | |
114 } | 108 } |
115 | 109 |
116 void AppMenuButton::CloseMenu() { | 110 void AppMenuButton::CloseMenu() { |
117 if (menu_) | 111 if (menu_) |
118 menu_->CloseMenu(); | 112 menu_->CloseMenu(); |
119 menu_.reset(); | 113 menu_.reset(); |
120 } | 114 } |
121 | 115 |
122 bool AppMenuButton::IsMenuShowing() const { | 116 bool AppMenuButton::IsMenuShowing() const { |
123 return menu_ && menu_->IsShowing(); | 117 return menu_ && menu_->IsShowing(); |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
155 | 149 |
156 gfx::Rect bounds = GetLocalBounds(); | 150 gfx::Rect bounds = GetLocalBounds(); |
157 bounds.Inset(gfx::Insets(ToolbarButton::kInteriorPadding)); | 151 bounds.Inset(gfx::Insets(ToolbarButton::kInteriorPadding)); |
158 animation_->PaintAppMenu(canvas, bounds); | 152 animation_->PaintAppMenu(canvas, bounds); |
159 } | 153 } |
160 | 154 |
161 void AppMenuButton::TabInsertedAt(TabStripModel* tab_strip_model, | 155 void AppMenuButton::TabInsertedAt(TabStripModel* tab_strip_model, |
162 content::WebContents* contents, | 156 content::WebContents* contents, |
163 int index, | 157 int index, |
164 bool foreground) { | 158 bool foreground) { |
165 if (severity_ != AppMenuIconController::Severity::NONE) | 159 AnimateIconIfPossible(); |
166 animation_->StartAnimation(); | |
167 } | 160 } |
168 | 161 |
169 void AppMenuButton::UpdateIcon(bool should_animate) { | 162 void AppMenuButton::UpdateIcon(bool should_animate) { |
170 SkColor severity_color = gfx::kPlaceholderColor; | 163 SkColor severity_color = gfx::kPlaceholderColor; |
171 SkColor toolbar_icon_color = | 164 SkColor toolbar_icon_color = |
172 GetThemeProvider()->GetColor(ThemeProperties::COLOR_TOOLBAR_BUTTON_ICON); | 165 GetThemeProvider()->GetColor(ThemeProperties::COLOR_TOOLBAR_BUTTON_ICON); |
173 const ui::NativeTheme* native_theme = GetNativeTheme(); | 166 const ui::NativeTheme* native_theme = GetNativeTheme(); |
174 switch (severity_) { | 167 switch (severity_) { |
175 case AppMenuIconController::Severity::NONE: | 168 case AppMenuIconController::Severity::NONE: |
176 severity_color = toolbar_icon_color; | 169 severity_color = toolbar_icon_color; |
177 break; | 170 break; |
178 case AppMenuIconController::Severity::LOW: | 171 case AppMenuIconController::Severity::LOW: |
179 severity_color = native_theme->GetSystemColor( | 172 severity_color = native_theme->GetSystemColor( |
180 ui::NativeTheme::kColorId_AlertSeverityLow); | 173 ui::NativeTheme::kColorId_AlertSeverityLow); |
181 break; | 174 break; |
182 case AppMenuIconController::Severity::MEDIUM: | 175 case AppMenuIconController::Severity::MEDIUM: |
183 severity_color = native_theme->GetSystemColor( | 176 severity_color = native_theme->GetSystemColor( |
184 ui::NativeTheme::kColorId_AlertSeverityMedium); | 177 ui::NativeTheme::kColorId_AlertSeverityMedium); |
185 break; | 178 break; |
186 case AppMenuIconController::Severity::HIGH: | 179 case AppMenuIconController::Severity::HIGH: |
187 severity_color = native_theme->GetSystemColor( | 180 severity_color = native_theme->GetSystemColor( |
188 ui::NativeTheme::kColorId_AlertSeverityHigh); | 181 ui::NativeTheme::kColorId_AlertSeverityHigh); |
189 break; | 182 break; |
190 } | 183 } |
191 | 184 |
192 if (animation_) { | 185 if (should_use_new_icon_) { |
193 animation_->SetIconColors(toolbar_icon_color, severity_color); | 186 // |animation_| is created here instead of the constructor because the |
| 187 // theme provider is necessary to provide the correct initial toolbar |
| 188 // icon color. |
| 189 if (!animation_) { |
| 190 animation_ = base::MakeUnique<AppMenuAnimation>(this, toolbar_icon_color); |
| 191 } |
| 192 animation_->set_severity_color(severity_color); |
194 if (should_animate) | 193 if (should_animate) |
195 animation_->StartAnimation(); | 194 AnimateIconIfPossible(); |
| 195 |
196 return; | 196 return; |
197 } | 197 } |
198 | 198 |
199 const gfx::VectorIcon* icon_id = nullptr; | 199 const gfx::VectorIcon* icon_id = nullptr; |
200 switch (type_) { | 200 switch (type_) { |
201 case AppMenuIconController::IconType::NONE: | 201 case AppMenuIconController::IconType::NONE: |
202 icon_id = &kBrowserToolsIcon; | 202 icon_id = &kBrowserToolsIcon; |
203 DCHECK_EQ(AppMenuIconController::Severity::NONE, severity_); | 203 DCHECK_EQ(AppMenuIconController::Severity::NONE, severity_); |
204 break; | 204 break; |
205 case AppMenuIconController::IconType::UPGRADE_NOTIFICATION: | 205 case AppMenuIconController::IconType::UPGRADE_NOTIFICATION: |
206 icon_id = &kBrowserToolsUpdateIcon; | 206 icon_id = &kBrowserToolsUpdateIcon; |
207 break; | 207 break; |
208 case AppMenuIconController::IconType::GLOBAL_ERROR: | 208 case AppMenuIconController::IconType::GLOBAL_ERROR: |
209 case AppMenuIconController::IconType::INCOMPATIBILITY_WARNING: | 209 case AppMenuIconController::IconType::INCOMPATIBILITY_WARNING: |
210 icon_id = &kBrowserToolsErrorIcon; | 210 icon_id = &kBrowserToolsErrorIcon; |
211 break; | 211 break; |
212 } | 212 } |
213 | 213 |
214 SetImage(views::Button::STATE_NORMAL, | 214 SetImage(views::Button::STATE_NORMAL, |
215 gfx::CreateVectorIcon(*icon_id, severity_color)); | 215 gfx::CreateVectorIcon(*icon_id, severity_color)); |
216 } | 216 } |
217 | 217 |
218 void AppMenuButton::SetTrailingMargin(int margin) { | 218 void AppMenuButton::SetTrailingMargin(int margin) { |
219 margin_trailing_ = margin; | 219 margin_trailing_ = margin; |
220 UpdateThemedBorder(); | 220 UpdateThemedBorder(); |
221 InvalidateLayout(); | 221 InvalidateLayout(); |
222 } | 222 } |
223 | 223 |
| 224 void AppMenuButton::AnimateIconIfPossible() { |
| 225 if (!should_use_new_icon_) |
| 226 return; |
| 227 |
| 228 if (severity_ == AppMenuIconController::Severity::NONE) |
| 229 return; |
| 230 |
| 231 if (animation_) |
| 232 animation_->StartAnimation(); |
| 233 } |
| 234 |
224 void AppMenuButton::AppMenuAnimationStarted() { | 235 void AppMenuButton::AppMenuAnimationStarted() { |
225 SetPaintToLayer(); | 236 SetPaintToLayer(); |
226 layer()->SetFillsBoundsOpaquely(false); | 237 layer()->SetFillsBoundsOpaquely(false); |
227 } | 238 } |
228 | 239 |
229 void AppMenuButton::AppMenuAnimationEnded() { | 240 void AppMenuButton::AppMenuAnimationEnded() { |
230 DestroyLayer(); | 241 DestroyLayer(); |
231 } | 242 } |
232 | 243 |
233 const char* AppMenuButton::GetClassName() const { | 244 const char* AppMenuButton::GetClassName() const { |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
287 return ui::DragDropTypes::DRAG_MOVE; | 298 return ui::DragDropTypes::DRAG_MOVE; |
288 } | 299 } |
289 | 300 |
290 void AppMenuButton::OnDragExited() { | 301 void AppMenuButton::OnDragExited() { |
291 weak_factory_.InvalidateWeakPtrs(); | 302 weak_factory_.InvalidateWeakPtrs(); |
292 } | 303 } |
293 | 304 |
294 int AppMenuButton::OnPerformDrop(const ui::DropTargetEvent& event) { | 305 int AppMenuButton::OnPerformDrop(const ui::DropTargetEvent& event) { |
295 return ui::DragDropTypes::DRAG_MOVE; | 306 return ui::DragDropTypes::DRAG_MOVE; |
296 } | 307 } |
OLD | NEW |