Commit a437dab3 authored by rdevlin.cronin's avatar rdevlin.cronin Committed by Commit bot

[Extensions UI] Add a placeholder icon for extensions in the toolbar

Create a placeholder icon source for extensions that don't have an icon, so that
they don't just look like puzzle pieces in the toolbar.
To start, only use this in the toolbar, but the class is flexible enough that
it could also be used, e.g., in the chrome://extensions page.

BUG=486206

Review URL: https://codereview.chromium.org/1131443006

Cr-Commit-Position: refs/heads/master@{#329932}
parent b80d3de9
...@@ -11,8 +11,10 @@ ...@@ -11,8 +11,10 @@
#include "chrome/common/badge_util.h" #include "chrome/common/badge_util.h"
#include "chrome/common/icon_with_badge_image_source.h" #include "chrome/common/icon_with_badge_image_source.h"
#include "extensions/browser/extension_icon_image.h" #include "extensions/browser/extension_icon_image.h"
#include "extensions/browser/extension_icon_placeholder.h"
#include "extensions/common/constants.h" #include "extensions/common/constants.h"
#include "extensions/common/extension_icon_set.h" #include "extensions/common/extension_icon_set.h"
#include "extensions/common/feature_switch.h"
#include "extensions/common/manifest_handlers/icons_handler.h" #include "extensions/common/manifest_handlers/icons_handler.h"
#include "grit/theme_resources.h" #include "grit/theme_resources.h"
#include "grit/ui_resources.h" #include "grit/ui_resources.h"
...@@ -103,7 +105,9 @@ const int ExtensionAction::kPageActionIconMaxSize = ...@@ -103,7 +105,9 @@ const int ExtensionAction::kPageActionIconMaxSize =
ExtensionAction::ExtensionAction(const extensions::Extension& extension, ExtensionAction::ExtensionAction(const extensions::Extension& extension,
extensions::ActionInfo::Type action_type, extensions::ActionInfo::Type action_type,
const extensions::ActionInfo& manifest_data) const extensions::ActionInfo& manifest_data)
: extension_id_(extension.id()), action_type_(action_type) { : extension_id_(extension.id()),
extension_name_(extension.name()),
action_type_(action_type) {
// Page/script actions are hidden/disabled by default, and browser actions are // Page/script actions are hidden/disabled by default, and browser actions are
// visible/enabled by default. // visible/enabled by default.
SetIsVisible(kDefaultTabId, SetIsVisible(kDefaultTabId,
...@@ -282,7 +286,18 @@ extensions::IconImage* ExtensionAction::LoadDefaultIconImage( ...@@ -282,7 +286,18 @@ extensions::IconImage* ExtensionAction::LoadDefaultIconImage(
gfx::Image ExtensionAction::GetDefaultIconImage() const { gfx::Image ExtensionAction::GetDefaultIconImage() const {
// If we have a default icon, it should be loaded before trying to use it. // If we have a default icon, it should be loaded before trying to use it.
DCHECK(!default_icon_image_ == !default_icon_); DCHECK(!default_icon_image_ == !default_icon_);
return default_icon_image_ ? default_icon_image_->image() : GetDefaultIcon(); if (default_icon_image_)
return default_icon_image_->image();
// If the extension action redesign is enabled, we use a special placeholder
// icon (with the first letter of the extension name) rather than the default
// (puzzle piece).
if (extensions::FeatureSwitch::extension_action_redesign()->IsEnabled()) {
return extensions::ExtensionIconPlaceholder::CreateImage(
extension_misc::EXTENSION_ICON_ACTION, extension_name_);
}
return GetDefaultIcon();
} }
bool ExtensionAction::HasPopupUrl(int tab_id) const { bool ExtensionAction::HasPopupUrl(int tab_id) const {
......
...@@ -257,6 +257,9 @@ class ExtensionAction { ...@@ -257,6 +257,9 @@ class ExtensionAction {
// extension manifest). // extension manifest).
const std::string extension_id_; const std::string extension_id_;
// The name of the extension.
const std::string extension_name_;
const extensions::ActionInfo::Type action_type_; const extensions::ActionInfo::Type action_type_;
// Each of these data items can have both a global state (stored with the key // Each of these data items can have both a global state (stored with the key
......
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "extensions/browser/extension_icon_placeholder.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "extensions/grit/extensions_browser_resources.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/gfx/canvas.h"
#include "ui/gfx/codec/png_codec.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/geometry/size.h"
#include "ui/gfx/image/canvas_image_source.h"
#include "ui/gfx/image/image_skia.h"
namespace extensions {
namespace {
// Returns the FontStyle to use for the given icon |size|.
ui::ResourceBundle::FontStyle GetFontStyleForIconSize(
extension_misc::ExtensionIcons size) {
switch (size) {
case extension_misc::EXTENSION_ICON_INVALID:
case extension_misc::EXTENSION_ICON_BITTY:
return ui::ResourceBundle::SmallFont;
case extension_misc::EXTENSION_ICON_ACTION:
case extension_misc::EXTENSION_ICON_SMALLISH:
case extension_misc::EXTENSION_ICON_SMALL:
return ui::ResourceBundle::MediumFont;
case extension_misc::EXTENSION_ICON_MEDIUM:
case extension_misc::EXTENSION_ICON_LARGE:
case extension_misc::EXTENSION_ICON_EXTRA_LARGE:
case extension_misc::EXTENSION_ICON_GIGANTOR:
return ui::ResourceBundle::LargeFont;
}
NOTREACHED();
return ui::ResourceBundle::MediumFont;
}
// Returns the background image to use for the given icon |size|.
gfx::Image GetBackgroundImageForIconSize(extension_misc::ExtensionIcons size) {
int resource_id = 0;
// Right now, we have resources for a 19x19 (action) and a 48x48 (extensions
// page icon). The implementation of the placeholder scales these correctly,
// so it's not a big deal to use these for other sizes, but if it's something
// that will be done frequently, we should probably make a devoted asset for
// that size.
switch (size) {
case extension_misc::EXTENSION_ICON_INVALID:
case extension_misc::EXTENSION_ICON_BITTY:
case extension_misc::EXTENSION_ICON_ACTION:
case extension_misc::EXTENSION_ICON_SMALLISH:
case extension_misc::EXTENSION_ICON_SMALL:
resource_id = IDR_EXTENSION_ACTION_PLAIN_BACKGROUND;
break;
case extension_misc::EXTENSION_ICON_MEDIUM:
case extension_misc::EXTENSION_ICON_LARGE:
case extension_misc::EXTENSION_ICON_EXTRA_LARGE:
case extension_misc::EXTENSION_ICON_GIGANTOR:
resource_id = IDR_EXTENSION_ICON_PLAIN_BACKGROUND;
break;
}
return ui::ResourceBundle::GetSharedInstance().GetImageNamed(resource_id);
}
} // namespace
ExtensionIconPlaceholder::ExtensionIconPlaceholder(
extension_misc::ExtensionIcons size,
const std::string& letter)
: gfx::CanvasImageSource(gfx::Size(size, size), false),
icon_size_(size),
letter_(base::UTF8ToUTF16(letter.substr(0, 1))),
base_image_(GetBackgroundImageForIconSize(size)) {
}
ExtensionIconPlaceholder::~ExtensionIconPlaceholder() {
}
gfx::Image ExtensionIconPlaceholder::CreateImage(
extension_misc::ExtensionIcons size,
const std::string& name) {
return gfx::Image(gfx::ImageSkia(new ExtensionIconPlaceholder(size, name),
gfx::Size(size, size)));
}
void ExtensionIconPlaceholder::Draw(gfx::Canvas* canvas) {
// Draw the background image, correctly scaled.
canvas->DrawImageInt(*base_image_.ToImageSkia(), 0, 0,
base_image_.Size().width(), base_image_.Size().height(),
0, 0, size().width(), size().height(), true);
gfx::Rect bounds(size().width(), size().height());
// Draw the letter on top.
canvas->DrawStringRectWithFlags(
letter_, ui::ResourceBundle::GetSharedInstance().GetFontList(
GetFontStyleForIconSize(icon_size_)),
SK_ColorWHITE, bounds, gfx::Canvas::TEXT_ALIGN_CENTER);
}
} // namespace extensions
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef EXTENSIONS_BROWSER_EXTENSION_ICON_PLACEHOLDER_H_
#define EXTENSIONS_BROWSER_EXTENSION_ICON_PLACEHOLDER_H_
#include <string>
#include "base/macros.h"
#include "base/strings/string16.h"
#include "extensions/common/constants.h"
#include "ui/gfx/image/canvas_image_source.h"
#include "ui/gfx/image/image.h"
namespace gfx {
class Canvas;
class Size;
}
namespace extensions {
// An extension icon image with a gray background and the first letter of the
// extension name, so that not all extensions without an icon look the same.
class ExtensionIconPlaceholder : public gfx::CanvasImageSource {
public:
ExtensionIconPlaceholder(extension_misc::ExtensionIcons size,
const std::string& name);
~ExtensionIconPlaceholder() override;
// Creates an image backed by an ImageSkia with the ExtensionIconPlaceholder
// as its image source.
static gfx::Image CreateImage(extension_misc::ExtensionIcons size,
const std::string& name);
private:
// gfx::CanvasImageSource:
void Draw(gfx::Canvas* canvas) override;
// The size this placeholder is filling.
extension_misc::ExtensionIcons icon_size_;
// The first letter of the extension's name.
base::string16 letter_;
// The gray background image, on top of which the letter is drawn.
gfx::Image base_image_;
DISALLOW_COPY_AND_ASSIGN(ExtensionIconPlaceholder);
};
} // namespace extensions
#endif // EXTENSIONS_BROWSER_EXTENSION_ICON_PLACEHOLDER_H_
...@@ -13,6 +13,8 @@ ...@@ -13,6 +13,8 @@
<structures fallback_to_low_resolution="true"> <structures fallback_to_low_resolution="true">
<structure type="chrome_scaled_image" name="IDR_APP_DEFAULT_ICON" file="app_default_icon.png" /> <structure type="chrome_scaled_image" name="IDR_APP_DEFAULT_ICON" file="app_default_icon.png" />
<structure type="chrome_scaled_image" name="IDR_EXTENSION_DEFAULT_ICON" file="extension_default_icon.png" /> <structure type="chrome_scaled_image" name="IDR_EXTENSION_DEFAULT_ICON" file="extension_default_icon.png" />
<structure type="chrome_scaled_image" name="IDR_EXTENSION_ACTION_PLAIN_BACKGROUND" file="extension_action_plain_background.png"/>
<structure type="chrome_scaled_image" name="IDR_EXTENSION_ICON_PLAIN_BACKGROUND" file="extension_icon_plain_background.png"/>
</structures> </structures>
</release> </release>
</grit> </grit>
...@@ -576,6 +576,8 @@ ...@@ -576,6 +576,8 @@
'browser/extension_host_queue.h', 'browser/extension_host_queue.h',
'browser/extension_icon_image.cc', 'browser/extension_icon_image.cc',
'browser/extension_icon_image.h', 'browser/extension_icon_image.h',
'browser/extension_icon_placeholder.cc',
'browser/extension_icon_placeholder.h',
'browser/extension_message_filter.cc', 'browser/extension_message_filter.cc',
'browser/extension_message_filter.h', 'browser/extension_message_filter.h',
'browser/extension_pref_store.cc', 'browser/extension_pref_store.cc',
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment