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

Side by Side Diff: chrome/browser/ui/app_list/extension_app_item.cc

Issue 2819413003: Refactor extension app icon. (Closed)
Patch Set: nits 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 (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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/app_list/extension_app_item.h" 5 #include "chrome/browser/ui/app_list/extension_app_item.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include "base/macros.h" 9 #include "base/macros.h"
10 #include "base/metrics/user_metrics.h" 10 #include "base/metrics/user_metrics.h"
11 #include "build/build_config.h" 11 #include "build/build_config.h"
12 #include "chrome/browser/extensions/chrome_app_icon.h"
13 #include "chrome/browser/extensions/chrome_app_icon_service.h"
12 #include "chrome/browser/extensions/extension_util.h" 14 #include "chrome/browser/extensions/extension_util.h"
13 #include "chrome/browser/extensions/launch_util.h" 15 #include "chrome/browser/extensions/launch_util.h"
14 #include "chrome/browser/profiles/profile.h" 16 #include "chrome/browser/profiles/profile.h"
15 #include "chrome/browser/ui/app_list/app_list_controller_delegate.h" 17 #include "chrome/browser/ui/app_list/app_list_controller_delegate.h"
16 #include "chrome/browser/ui/app_list/extension_app_context_menu.h" 18 #include "chrome/browser/ui/app_list/extension_app_context_menu.h"
17 #include "chrome/browser/ui/extensions/extension_enable_flow.h" 19 #include "chrome/browser/ui/extensions/extension_enable_flow.h"
18 #include "chrome/common/extensions/extension_constants.h" 20 #include "chrome/common/extensions/extension_constants.h"
19 #include "chrome/common/extensions/extension_metrics.h" 21 #include "chrome/common/extensions/extension_metrics.h"
20 #include "chrome/grit/theme_resources.h" 22 #include "chrome/grit/theme_resources.h"
21 #include "components/prefs/pref_service.h" 23 #include "components/prefs/pref_service.h"
22 #include "components/sync/model/string_ordinal.h" 24 #include "components/sync/model/string_ordinal.h"
23 #include "extensions/browser/app_sorting.h" 25 #include "extensions/browser/app_sorting.h"
24 #include "extensions/browser/extension_prefs.h" 26 #include "extensions/browser/extension_prefs.h"
25 #include "extensions/browser/extension_registry.h" 27 #include "extensions/browser/extension_registry.h"
26 #include "extensions/common/extension.h" 28 #include "extensions/common/extension.h"
27 #include "extensions/common/extension_icon_set.h" 29 #include "extensions/common/extension_icon_set.h"
28 #include "extensions/common/manifest_handlers/icons_handler.h" 30 #include "extensions/common/manifest_handlers/icons_handler.h"
29 #include "extensions/common/manifest_url_handlers.h" 31 #include "extensions/common/manifest_url_handlers.h"
30 #include "ui/base/resource/resource_bundle.h" 32 #include "ui/base/resource/resource_bundle.h"
31 #include "ui/events/event_constants.h" 33 #include "ui/events/event_constants.h"
32 #include "ui/gfx/canvas.h" 34 #include "ui/gfx/canvas.h"
33 #include "ui/gfx/geometry/rect.h" 35 #include "ui/gfx/geometry/rect.h"
34 #include "ui/gfx/image/canvas_image_source.h" 36 #include "ui/gfx/image/canvas_image_source.h"
35 #include "ui/gfx/skia_util.h" 37 #include "ui/gfx/skia_util.h"
36 #include "chrome/browser/chromeos/extensions/gfx_utils.h"
37 38
38 using extensions::Extension; 39 using extensions::Extension;
39 40
40 namespace {
41
42 // Overlays a shortcut icon over the bottom left corner of a given image.
43 class ShortcutOverlayImageSource : public gfx::CanvasImageSource {
44 public:
45 explicit ShortcutOverlayImageSource(const gfx::ImageSkia& icon)
46 : gfx::CanvasImageSource(icon.size(), false),
47 icon_(icon) {
48 }
49 ~ShortcutOverlayImageSource() override {}
50
51 private:
52 // gfx::CanvasImageSource overrides:
53 void Draw(gfx::Canvas* canvas) override {
54 canvas->DrawImageInt(icon_, 0, 0);
55
56 // Draw the overlay in the bottom left corner of the icon.
57 const gfx::ImageSkia& overlay = *ui::ResourceBundle::GetSharedInstance().
58 GetImageSkiaNamed(IDR_APP_LIST_TAB_OVERLAY);
59 canvas->DrawImageInt(overlay, 0, icon_.height() - overlay.height());
60 }
61
62 gfx::ImageSkia icon_;
63
64 DISALLOW_COPY_AND_ASSIGN(ShortcutOverlayImageSource);
65 };
66
67 // Rounds the corners of a given image.
68 class RoundedCornersImageSource : public gfx::CanvasImageSource {
69 public:
70 explicit RoundedCornersImageSource(const gfx::ImageSkia& icon)
71 : gfx::CanvasImageSource(icon.size(), false),
72 icon_(icon) {
73 }
74 ~RoundedCornersImageSource() override {}
75
76 private:
77 // gfx::CanvasImageSource overrides:
78 void Draw(gfx::Canvas* canvas) override {
79 // The radius used to round the app icon.
80 const size_t kRoundingRadius = 2;
81
82 canvas->DrawImageInt(icon_, 0, 0);
83
84 cc::PaintFlags masking_flags;
85 masking_flags.setBlendMode(SkBlendMode::kDstIn);
86 canvas->SaveLayerWithFlags(masking_flags);
87
88 cc::PaintFlags mask_flags;
89 mask_flags.setAntiAlias(true);
90 mask_flags.setColor(SK_ColorWHITE);
91 canvas->DrawRoundRect(gfx::Rect(icon_.width(), icon_.height()),
92 kRoundingRadius, mask_flags);
93
94 canvas->Restore();
95 }
96
97 gfx::ImageSkia icon_;
98
99 DISALLOW_COPY_AND_ASSIGN(RoundedCornersImageSource);
100 };
101
102 } // namespace
103
104 ExtensionAppItem::ExtensionAppItem( 41 ExtensionAppItem::ExtensionAppItem(
105 Profile* profile, 42 Profile* profile,
106 const app_list::AppListSyncableService::SyncItem* sync_item, 43 const app_list::AppListSyncableService::SyncItem* sync_item,
107 const std::string& extension_id, 44 const std::string& extension_id,
108 const std::string& extension_name, 45 const std::string& extension_name,
109 const gfx::ImageSkia& installing_icon, 46 const gfx::ImageSkia& installing_icon,
110 bool is_platform_app) 47 bool is_platform_app)
111 : ChromeAppListItem(profile, extension_id), 48 : ChromeAppListItem(profile, extension_id),
112 extension_enable_flow_controller_(NULL), 49 extension_enable_flow_controller_(NULL),
113 extension_name_(extension_name), 50 extension_name_(extension_name),
114 installing_icon_(CreateDisabledIcon(installing_icon)), 51 installing_icon_(CreateDisabledIcon(installing_icon)),
115 is_platform_app_(is_platform_app) { 52 is_platform_app_(is_platform_app) {
116 Reload(); 53 Reload();
117 if (sync_item && sync_item->item_ordinal.IsValid()) 54 if (sync_item && sync_item->item_ordinal.IsValid())
118 UpdateFromSync(sync_item); 55 UpdateFromSync(sync_item);
119 else 56 else
120 SetDefaultPositionIfApplicable(); 57 SetDefaultPositionIfApplicable();
121 } 58 }
122 59
123 ExtensionAppItem::~ExtensionAppItem() { 60 ExtensionAppItem::~ExtensionAppItem() {
124 } 61 }
125 62
126 void ExtensionAppItem::Reload() { 63 void ExtensionAppItem::Reload() {
127 const Extension* extension = GetExtension(); 64 const Extension* extension = GetExtension();
128 bool is_installing = !extension; 65 bool is_installing = !extension;
129 SetIsInstalling(is_installing); 66 SetIsInstalling(is_installing);
130 if (is_installing) { 67 if (is_installing) {
131 SetName(extension_name_); 68 SetName(extension_name_);
132 UpdateIcon(); 69 SetIcon(installing_icon_);
133 return; 70 return;
134 } 71 }
135 SetNameAndShortName(extension->name(), extension->short_name()); 72 SetNameAndShortName(extension->name(), extension->short_name());
136 LoadImage(extension); 73 if (!icon_) {
74 icon_ = extensions::ChromeAppIconService::Get(profile())->CreateIcon(
75 this, extension_id(), extension_misc::EXTENSION_ICON_MEDIUM);
76 } else {
77 icon_->Reload();
78 }
137 } 79 }
138 80
139 void ExtensionAppItem::UpdateIcon() { 81 void ExtensionAppItem::OnIconUpdated(extensions::ChromeAppIcon* icon) {
140 gfx::ImageSkia icon = installing_icon_; 82 SetIcon(icon->IsValid() ? icon->image_skia() : installing_icon_);
141
142 // Use the app icon if the app exists. Turn the image greyscale if the app is
143 // not launchable.
144 if (GetExtension() && icon_) {
145 icon = icon_->image_skia();
146 const bool enabled = extensions::util::IsAppLaunchable(extension_id(),
147 profile());
148 extensions::util::MaybeApplyChromeBadge(profile(), id(), &icon);
149
150 if (!enabled)
151 icon = CreateDisabledIcon(icon);
152
153 if (GetExtension()->from_bookmark())
154 icon = gfx::ImageSkia(new RoundedCornersImageSource(icon), icon.size());
155 }
156 SetIcon(icon);
157 } 83 }
158 84
159 const Extension* ExtensionAppItem::GetExtension() const { 85 const Extension* ExtensionAppItem::GetExtension() const {
160 const extensions::ExtensionRegistry* registry = 86 const extensions::ExtensionRegistry* registry =
161 extensions::ExtensionRegistry::Get(profile()); 87 extensions::ExtensionRegistry::Get(profile());
162 const Extension* extension = registry->GetInstalledExtension( 88 const Extension* extension = registry->GetInstalledExtension(
163 extension_id()); 89 extension_id());
164 return extension; 90 return extension;
165 } 91 }
166 92
167 void ExtensionAppItem::LoadImage(const Extension* extension) {
168 icon_.reset(new extensions::IconImage(
169 profile(),
170 extension,
171 extensions::IconsInfo::GetIcons(extension),
172 extension_misc::EXTENSION_ICON_MEDIUM,
173 extensions::util::GetDefaultAppIcon(),
174 this));
175 UpdateIcon();
176 }
177
178 bool ExtensionAppItem::RunExtensionEnableFlow() { 93 bool ExtensionAppItem::RunExtensionEnableFlow() {
179 if (extensions::util::IsAppLaunchableWithoutEnabling(extension_id(), 94 if (extensions::util::IsAppLaunchableWithoutEnabling(extension_id(),
180 profile())) 95 profile()))
181 return false; 96 return false;
182 97
183 if (!extension_enable_flow_) { 98 if (!extension_enable_flow_) {
184 extension_enable_flow_controller_ = GetController(); 99 extension_enable_flow_controller_ = GetController();
185 extension_enable_flow_controller_->OnShowChildDialog(); 100 extension_enable_flow_controller_->OnShowChildDialog();
186 101
187 extension_enable_flow_.reset(new ExtensionEnableFlow( 102 extension_enable_flow_.reset(new ExtensionEnableFlow(
(...skipping 16 matching lines...) Expand all
204 119
205 if (RunExtensionEnableFlow()) 120 if (RunExtensionEnableFlow())
206 return; 121 return;
207 122
208 GetController()->LaunchApp(profile(), 123 GetController()->LaunchApp(profile(),
209 extension, 124 extension,
210 AppListControllerDelegate::LAUNCH_FROM_APP_LIST, 125 AppListControllerDelegate::LAUNCH_FROM_APP_LIST,
211 event_flags); 126 event_flags);
212 } 127 }
213 128
214 void ExtensionAppItem::OnExtensionIconImageChanged(
215 extensions::IconImage* image) {
216 DCHECK(icon_.get() == image);
217 UpdateIcon();
218 }
219
220 void ExtensionAppItem::OnExtensionIconImageDestroyed(
221 extensions::IconImage* image) {
222 SetIcon(gfx::ImageSkia());
223 }
224
225 void ExtensionAppItem::ExtensionEnableFlowFinished() { 129 void ExtensionAppItem::ExtensionEnableFlowFinished() {
226 extension_enable_flow_.reset(); 130 extension_enable_flow_.reset();
227 extension_enable_flow_controller_->OnCloseChildDialog(); 131 extension_enable_flow_controller_->OnCloseChildDialog();
228 extension_enable_flow_controller_ = NULL; 132 extension_enable_flow_controller_ = NULL;
229 133
230 // Automatically launch app after enabling. 134 // Automatically launch app after enabling.
231 Launch(ui::EF_NONE); 135 Launch(ui::EF_NONE);
232 } 136 }
233 137
234 void ExtensionAppItem::ExtensionEnableFlowAborted(bool user_initiated) { 138 void ExtensionAppItem::ExtensionEnableFlowAborted(bool user_initiated) {
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
270 // static 174 // static
271 const char ExtensionAppItem::kItemType[] = "ExtensionAppItem"; 175 const char ExtensionAppItem::kItemType[] = "ExtensionAppItem";
272 176
273 const char* ExtensionAppItem::GetItemType() const { 177 const char* ExtensionAppItem::GetItemType() const {
274 return ExtensionAppItem::kItemType; 178 return ExtensionAppItem::kItemType;
275 } 179 }
276 180
277 void ExtensionAppItem::ExecuteLaunchCommand(int event_flags) { 181 void ExtensionAppItem::ExecuteLaunchCommand(int event_flags) {
278 Launch(event_flags); 182 Launch(event_flags);
279 } 183 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698