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

Side by Side Diff: chrome/browser/ui/cocoa/toolbar/app_toolbar_button.mm

Issue 2859903003: [Mac] App Menu Animated Icon (Closed)
Patch Set: Switch to CanvasSkiaPaint 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
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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 #import "chrome/browser/ui/cocoa/toolbar/app_toolbar_button.h" 5 #import "chrome/browser/ui/cocoa/toolbar/app_toolbar_button.h"
6 6
7 #include "base/command_line.h"
7 #include "base/macros.h" 8 #include "base/macros.h"
8 #include "chrome/app/vector_icons/vector_icons.h" 9 #include "chrome/app/vector_icons/vector_icons.h"
9 #import "chrome/browser/ui/cocoa/themed_window.h" 10 #import "chrome/browser/ui/cocoa/themed_window.h"
10 #import "chrome/browser/ui/cocoa/view_id_util.h" 11 #import "chrome/browser/ui/cocoa/view_id_util.h"
12 #include "chrome/browser/ui/toolbar/app_menu_animation.h"
13 #include "chrome/common/chrome_switches.h"
11 #include "chrome/grit/chromium_strings.h" 14 #include "chrome/grit/chromium_strings.h"
15 #include "skia/ext/skia_utils_mac.h"
12 #include "ui/base/l10n/l10n_util_mac.h" 16 #include "ui/base/l10n/l10n_util_mac.h"
13 #include "ui/base/material_design/material_design_controller.h" 17 #include "ui/base/material_design/material_design_controller.h"
14 #include "ui/base/theme_provider.h" 18 #include "ui/base/theme_provider.h"
19 #include "ui/gfx/canvas_skia_paint.h"
15 #include "ui/gfx/color_palette.h" 20 #include "ui/gfx/color_palette.h"
21 #include "ui/gfx/skia_util.h"
22
23 namespace {
24
25 // The size of the icon.
26 constexpr float kIconSize = 16.0f;
27
28 } // namespace
29
30 class AppMenuBridgeCocoa : public AppMenuAnimationDelegate {
31 public:
32 AppMenuBridgeCocoa(AppToolbarButton* owner);
33
34 void AppMenuAnimationStarted() override;
35 void AppMenuAnimationEnded() override;
36 void InvalidateIcon() override;
37
38 private:
39 AppToolbarButton* owner_;
40 };
41
42 AppMenuBridgeCocoa::AppMenuBridgeCocoa(AppToolbarButton* owner)
43 : owner_(owner) {}
44
45 void AppMenuBridgeCocoa::AppMenuAnimationStarted() {}
46
47 void AppMenuBridgeCocoa::AppMenuAnimationEnded() {}
48
49 void AppMenuBridgeCocoa::InvalidateIcon() {
50 [owner_ setNeedsDisplay:YES];
51 }
16 52
17 @interface AppToolbarButton () 53 @interface AppToolbarButton ()
18 - (void)commonInit; 54 - (void)commonInit;
19 @end 55 @end
20 56
21 @implementation AppToolbarButton 57 @implementation AppToolbarButton
22 58
23 - (instancetype)initWithFrame:(NSRect)frame { 59 - (instancetype)initWithFrame:(NSRect)frame {
24 if ((self = [super initWithFrame:frame])) { 60 if ((self = [super initWithFrame:frame])) {
25 [self commonInit]; 61 [self commonInit];
62
63 base::CommandLine* commandLine = base::CommandLine::ForCurrentProcess();
64 if (commandLine->HasSwitch(switches::kEnableNewAppMenuIcon)) {
65 appMenuBridge_.reset(new AppMenuBridgeCocoa(self));
66 appMenuAnimation_.reset(new AppMenuAnimation(
67 appMenuBridge_.get(),
68 [self vectorIconColor:[[self window] hasDarkTheme]]));
69 }
26 } 70 }
27 return self; 71 return self;
28 } 72 }
29 73
30 - (void)awakeFromNib { 74 - (void)awakeFromNib {
31 [self commonInit]; 75 [self commonInit];
32 } 76 }
33 77
78 - (void)drawRect:(NSRect)frame {
79 [super drawRect:frame];
80
81 if (appMenuAnimation_) {
82 NSRect imageFrame = NSInsetRect(frame, (NSWidth(frame) - kIconSize) / 2,
83 (NSHeight(frame) - kIconSize) / 2);
84 gfx::CanvasSkiaPaint canvas(imageFrame, false);
85 canvas.set_composite_alpha(true);
86 appMenuAnimation_->PaintAppMenu(&canvas, gfx::Rect(imageFrame));
87 canvas.Restore();
88 }
89 }
90
34 - (void)commonInit { 91 - (void)commonInit {
35 view_id_util::SetID(self, VIEW_ID_APP_MENU); 92 view_id_util::SetID(self, VIEW_ID_APP_MENU);
36 severity_ = AppMenuIconController::Severity::NONE; 93 severity_ = AppMenuIconController::Severity::NONE;
37 type_ = AppMenuIconController::IconType::NONE; 94 type_ = AppMenuIconController::IconType::NONE;
38 [self setToolTip:l10n_util::GetNSString(IDS_APPMENU_TOOLTIP)]; 95 [self setToolTip:l10n_util::GetNSString(IDS_APPMENU_TOOLTIP)];
39 } 96 }
40 97
41 - (const gfx::VectorIcon*)vectorIcon { 98 - (const gfx::VectorIcon*)vectorIcon {
99 // The new app menu icon doesn't use vectors.
100 if (appMenuAnimation_)
101 return nullptr;
102
42 switch (type_) { 103 switch (type_) {
43 case AppMenuIconController::IconType::NONE: 104 case AppMenuIconController::IconType::NONE:
44 DCHECK_EQ(severity_, AppMenuIconController::Severity::NONE); 105 DCHECK_EQ(severity_, AppMenuIconController::Severity::NONE);
45 return &kBrowserToolsIcon; 106 return &kBrowserToolsIcon;
46 case AppMenuIconController::IconType::UPGRADE_NOTIFICATION: 107 case AppMenuIconController::IconType::UPGRADE_NOTIFICATION:
47 return &kBrowserToolsUpdateIcon; 108 return &kBrowserToolsUpdateIcon;
48 case AppMenuIconController::IconType::GLOBAL_ERROR: 109 case AppMenuIconController::IconType::GLOBAL_ERROR:
49 case AppMenuIconController::IconType::INCOMPATIBILITY_WARNING: 110 case AppMenuIconController::IconType::INCOMPATIBILITY_WARNING:
50 return &kBrowserToolsErrorIcon; 111 return &kBrowserToolsErrorIcon;
51 } 112 }
(...skipping 27 matching lines...) Expand all
79 break; 140 break;
80 } 141 }
81 } 142 }
82 143
83 - (void)setSeverity:(AppMenuIconController::Severity)severity 144 - (void)setSeverity:(AppMenuIconController::Severity)severity
84 iconType:(AppMenuIconController::IconType)type 145 iconType:(AppMenuIconController::IconType)type
85 shouldAnimate:(BOOL)shouldAnimate { 146 shouldAnimate:(BOOL)shouldAnimate {
86 if (severity != severity_ || type != type_) { 147 if (severity != severity_ || type != type_) {
87 severity_ = severity; 148 severity_ = severity;
88 type_ = type; 149 type_ = type;
89 // Update the button state images with the new severity color or icon type. 150
90 [self resetButtonStateImages]; 151 if (appMenuAnimation_) {
152 appMenuAnimation_->set_target_color(
153 [self vectorIconColor:[[self window] hasDarkTheme]]);
154 appMenuAnimation_->StartAnimation();
155 } else {
156 // Update the button state images with the new severity color or icon
157 // type.
158 [self resetButtonStateImages];
159 }
91 } 160 }
92 } 161 }
93 162
163 - (void)onTabInsertedInForeground {
164 if (appMenuAnimation_ && severity_ != AppMenuIconController::Severity::NONE)
165 appMenuAnimation_->StartAnimation();
166 }
167
168 - (void)willShowMenu {
169 if (appMenuAnimation_ && severity_ != AppMenuIconController::Severity::NONE)
170 appMenuAnimation_->StartAnimation();
171 }
172
94 @end 173 @end
OLDNEW
« no previous file with comments | « chrome/browser/ui/cocoa/toolbar/app_toolbar_button.h ('k') | chrome/browser/ui/toolbar/app_menu_animation.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698