Skip to content

Commit

Permalink
Remove sundry IsModeMaterial checks.
Browse files Browse the repository at this point in the history
BUG=648281
[email protected]

Review-Url: https://codereview.chromium.org/2401363005
Cr-Commit-Position: refs/heads/master@{#424633}
  • Loading branch information
estade authored and Commit bot committed Oct 12, 2016
1 parent c03addf commit 944999f
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 236 deletions.
162 changes: 19 additions & 143 deletions chrome/browser/ui/extensions/icon_with_badge_image_source.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,90 +8,28 @@
#include <cmath>
#include <utility>

#include "base/logging.h"
#include "base/strings/utf_string_conversions.h"
#include "build/build_config.h"
#include "chrome/browser/extensions/extension_action.h"
#include "chrome/grit/theme_resources.h"
#include "third_party/skia/include/core/SkPaint.h"
#include "third_party/skia/include/core/SkRefCnt.h"
#include "third_party/skia/include/core/SkTypeface.h"
#include "ui/base/material_design/material_design_controller.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/gfx/canvas.h"
#include "ui/gfx/color_palette.h"
#include "ui/gfx/font.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/geometry/size.h"
#include "ui/gfx/image/image_skia_operations.h"
#include "ui/resources/grit/ui_resources.h"

namespace {

// Different platforms need slightly different constants to look good.
// TODO(devlin): Comb through these and see if they are all still needed/
// appropriate.
#if defined(OS_WIN)
const float kTextSize = 10;
// The padding between the top of the badge and the top of the text.
const int kTopTextPadding = -1;
#elif defined(OS_MACOSX)
const float kTextSize = 9.0;
const int kTopTextPadding = 0;
#elif defined(OS_CHROMEOS)
const float kTextSize = 8.0;
const int kTopTextPadding = 1;
#elif defined(OS_POSIX)
const float kTextSize = 9.0;
const int kTopTextPadding = 0;
#endif

const int kPadding = 2;
const int kBadgeHeight = 11;
const int kMaxTextWidth = 23;

// The minimum width for center-aligning the badge.
const int kCenterAlignThreshold = 20;

// Helper routine that returns a singleton SkPaint object configured for
// rendering badge overlay text (correct font, typeface, etc).
SkPaint* GetBadgeTextPaintSingleton() {
#if defined(OS_MACOSX)
const char kPreferredTypeface[] = "Helvetica Bold";
#else
const char kPreferredTypeface[] = "Arial";
#endif

static SkPaint* text_paint = NULL;
if (!text_paint) {
text_paint = new SkPaint;
text_paint->setAntiAlias(true);
text_paint->setTextAlign(SkPaint::kLeft_Align);

sk_sp<SkTypeface> typeface(
SkTypeface::MakeFromName(kPreferredTypeface,
SkFontStyle::FromOldStyle(SkTypeface::kBold)));
// Skia doesn't do any font fallback---if the user is missing the font then
// typeface will be NULL. If we don't do manual fallback then we'll crash.
if (typeface) {
text_paint->setFakeBoldText(true);
} else {
// Fall back to the system font. We don't bold it because we aren't sure
// how it will look.
// For the most part this code path will only be hit on Linux systems
// that don't have Arial.
ResourceBundle& rb = ResourceBundle::GetSharedInstance();
const gfx::Font& base_font = rb.GetFont(ResourceBundle::BaseFont);
typeface = SkTypeface::MakeFromName(base_font.GetFontName().c_str(),
SkFontStyle());
DCHECK(typeface);
}

text_paint->setTypeface(std::move(typeface));
}
return text_paint;
}

gfx::ImageSkiaRep ScaleImageSkiaRep(const gfx::ImageSkiaRep& rep,
int target_width_dp,
float target_scale) {
Expand Down Expand Up @@ -166,19 +104,12 @@ void IconWithBadgeImageSource::PaintBadge(gfx::Canvas* canvas) {
? SK_ColorWHITE
: badge_->text_color;

SkColor background_color = ui::MaterialDesignController::IsModeMaterial()
? gfx::kGoogleBlue500
: SkColorSetRGB(218, 0, 24);
if (SkColorGetA(badge_->background_color) != SK_AlphaTRANSPARENT)
background_color = badge_->background_color;
// Make sure the background color is opaque. See http://crbug.com/619499
if (ui::MaterialDesignController::IsModeMaterial())
background_color = SkColorSetA(background_color, SK_AlphaOPAQUE);

canvas->Save();
SkColor background_color =
SkColorGetA(badge_->background_color) == SK_AlphaTRANSPARENT
? gfx::kGoogleBlue500
: SkColorSetA(badge_->background_color, SK_AlphaOPAQUE);

SkPaint* text_paint = nullptr;
int text_width = 0;
ResourceBundle* rb = &ResourceBundle::GetSharedInstance();
gfx::FontList base_font = rb->GetFontList(ResourceBundle::BaseFont)
.DeriveWithHeightUpperBound(kBadgeHeight);
Expand All @@ -199,30 +130,8 @@ void IconWithBadgeImageSource::PaintBadge(gfx::Canvas* canvas) {
base_font = bigger_font;
}

if (ui::MaterialDesignController::IsModeMaterial()) {
text_width =
const int text_width =
std::min(kMaxTextWidth, canvas->GetStringWidth(utf16_text, base_font));
} else {
text_paint = GetBadgeTextPaintSingleton();
text_paint->setColor(text_color);
float scale = canvas->image_scale();

// Calculate text width. Font width may not be linear with respect to the
// scale factor (e.g. when hinting is applied), so we need to use the font
// size that canvas actually uses when drawing a text.
text_paint->setTextSize(SkFloatToScalar(kTextSize) * scale);
SkScalar sk_text_width_in_pixel =
text_paint->measureText(badge_->text.c_str(), badge_->text.size());
text_paint->setTextSize(SkFloatToScalar(kTextSize));

// We clamp the width to a max size. SkPaint::measureText returns the width
// in pixel (as a result of scale multiplier), so convert
// sk_text_width_in_pixel back to DIP (density independent pixel) first.
text_width = std::min(
kMaxTextWidth, static_cast<int>(std::ceil(
SkScalarToFloat(sk_text_width_in_pixel) / scale)));
}

// Calculate badge size. It is clamped to a min width just because it looks
// silly if it is too skinny.
int badge_width = text_width + kPadding * 2;
Expand All @@ -243,53 +152,20 @@ void IconWithBadgeImageSource::PaintBadge(gfx::Canvas* canvas) {
rect_paint.setAntiAlias(true);
rect_paint.setColor(background_color);

if (ui::MaterialDesignController::IsModeMaterial()) {
// Clear part of the background icon.
gfx::Rect cutout_rect(rect);
cutout_rect.Inset(-1, -1);
SkPaint cutout_paint = rect_paint;
cutout_paint.setBlendMode(SkBlendMode::kClear);
canvas->DrawRoundRect(cutout_rect, 2, cutout_paint);

// Paint the backdrop.
canvas->DrawRoundRect(rect, 1, rect_paint);

// Paint the text.
rect.Inset(std::max(kPadding, (rect.width() - text_width) / 2),
kBadgeHeight - base_font.GetHeight(), kPadding, 0);
canvas->DrawStringRect(utf16_text, base_font, text_color, rect);
} else {
// Paint the backdrop.
canvas->DrawRoundRect(rect, 2, rect_paint);

// Overlay the gradient. It is stretchy, so we do this in three parts.
ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
gfx::ImageSkia* gradient_left =
rb.GetImageSkiaNamed(IDR_BROWSER_ACTION_BADGE_LEFT);
gfx::ImageSkia* gradient_right =
rb.GetImageSkiaNamed(IDR_BROWSER_ACTION_BADGE_RIGHT);
gfx::ImageSkia* gradient_center =
rb.GetImageSkiaNamed(IDR_BROWSER_ACTION_BADGE_CENTER);

canvas->DrawImageInt(*gradient_left, rect.x(), rect.y());
canvas->TileImageInt(
*gradient_center, rect.x() + gradient_left->width(), rect.y(),
rect.width() - gradient_left->width() - gradient_right->width(),
rect.height());
canvas->DrawImageInt(*gradient_right,
rect.right() - gradient_right->width(), rect.y());

// Finally, draw the text centered within the badge. We set a clip in case
// the text was too large.
rect.Inset(kPadding, 0);
canvas->ClipRect(rect);
canvas->sk_canvas()->drawText(
badge_->text.c_str(), badge_->text.size(),
SkFloatToScalar(rect.x() +
static_cast<float>(rect.width() - text_width) / 2),
SkFloatToScalar(rect.y() + kTextSize + kTopTextPadding), *text_paint);
}
canvas->Restore();
// Clear part of the background icon.
gfx::Rect cutout_rect(rect);
cutout_rect.Inset(-1, -1);
SkPaint cutout_paint = rect_paint;
cutout_paint.setBlendMode(SkBlendMode::kClear);
canvas->DrawRoundRect(cutout_rect, 2, cutout_paint);

// Paint the backdrop.
canvas->DrawRoundRect(rect, 1, rect_paint);

// Paint the text.
rect.Inset(std::max(kPadding, (rect.width() - text_width) / 2),
kBadgeHeight - base_font.GetHeight(), kPadding, 0);
canvas->DrawStringRect(utf16_text, base_font, text_color, rect);
}

void IconWithBadgeImageSource::PaintPageActionDecoration(gfx::Canvas* canvas) {
Expand Down
47 changes: 2 additions & 45 deletions chrome/browser/ui/libgtk2ui/gtk2_ui.cc
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
#include "third_party/skia/include/core/SkCanvas.h"
#include "third_party/skia/include/core/SkColor.h"
#include "third_party/skia/include/core/SkShader.h"
#include "ui/base/material_design/material_design_controller.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/display/display.h"
#include "ui/gfx/canvas.h"
Expand Down Expand Up @@ -228,11 +227,6 @@ int indicators_count;
// The unknown content type.
const char* kUnknownContentType = "application/octet-stream";

// Values used as the new luminance and saturation values in the inactive tab
// text color.
const double kInactiveLuminance = 0.15;
const double kInactiveSaturation = 0.3;

// TODO(erg): ThemeService has a whole interface just for reading default
// constants. Figure out what to do with that more long term; for now, just
// copy the constants themselves here.
Expand Down Expand Up @@ -467,10 +461,6 @@ void Gtk2UI::Initialize() {
Gtk2EventLoop::GetInstance();
}

void Gtk2UI::MaterialDesignControllerReady() {
UpdateMaterialDesignColors();
}

bool Gtk2UI::GetTint(int id, color_utils::HSL* tint) const {
switch (id) {
// Tints for which the cross-platform default is fine. Before adding new
Expand Down Expand Up @@ -825,6 +815,8 @@ void Gtk2UI::LoadGtkValues() {

colors_[ThemeProperties::COLOR_TAB_TEXT] = label_color;
colors_[ThemeProperties::COLOR_BOOKMARK_TEXT] = label_color;
colors_[ThemeProperties::COLOR_BACKGROUND_TAB_TEXT] =
color_utils::BlendTowardOppositeLuma(label_color, 50);

UpdateDefaultFont();

Expand All @@ -833,21 +825,6 @@ void Gtk2UI::LoadGtkValues() {
GetNormalEntryForegroundHSL(&entry_tint_);
GetSelectedEntryForegroundHSL(&selected_entry_tint_);

// The inactive frame color never occurs naturally in the theme, as it is a
// tinted version of the normal frame color. We generate another color based
// on the background tab color, with the lightness and saturation moved in the
// opposite direction. (We don't touch the hue, since there should be subtle
// hints of the color in the text.)
color_utils::HSL inactive_tab_text_hsl;
color_utils::SkColorToHSL(
theme->GetSystemColor(ui::NativeTheme::kColorId_WindowBackground),
&inactive_tab_text_hsl);
inactive_tab_text_hsl.s = kInactiveLuminance;
inactive_tab_text_hsl.l = kInactiveSaturation;

colors_[ThemeProperties::COLOR_BACKGROUND_TAB_TEXT] =
color_utils::HSLToSkColor(inactive_tab_text_hsl, 255);

// We pick the text and background colors for the NTP out of the colors for a
// GtkEntry. We do this because GtkEntries background color is never the same
// as |toolbar_color|, is usually a white, and when it isn't a white,
Expand Down Expand Up @@ -927,23 +904,6 @@ void Gtk2UI::LoadCursorTheme() {
g_free(theme);
}

void Gtk2UI::UpdateMaterialDesignColors() {
// TODO(varkha): This should be merged back into LoadGtkValues() once Material
// Design is on unconditionally.
// Early return when Material Design Controller is not initialized yet. This
// is harmless and the colors will get updated when this method is called
// again after the initialization. See http://crbug.com/622234.
if (!ui::MaterialDesignController::is_mode_initialized() ||
!ui::MaterialDesignController::IsModeMaterial()) {
return;
}
NativeThemeGtk2* theme = NativeThemeGtk2::instance();
SkColor label_color =
theme->GetSystemColor(ui::NativeTheme::kColorId_LabelEnabledColor);
colors_[ThemeProperties::COLOR_BACKGROUND_TAB_TEXT] =
color_utils::BlendTowardOppositeLuma(label_color, 50);
}

void Gtk2UI::BuildFrameColors() {
#if GTK_MAJOR_VERSION == 2
NativeThemeGtk2* theme = NativeThemeGtk2::instance();
Expand Down Expand Up @@ -1111,9 +1071,6 @@ void Gtk2UI::UpdateDefaultFont() {

void Gtk2UI::ResetStyle() {
LoadGtkValues();
// TODO(varkha): There will be no need to call UpdateMaterialDesignColors()
// once Material Design is on unconditionally.
UpdateMaterialDesignColors();
NativeThemeGtk2::instance()->NotifyObservers();
}

Expand Down
9 changes: 0 additions & 9 deletions chrome/browser/ui/libgtk2ui/gtk2_ui.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
#include "base/observer_list.h"
#include "chrome/browser/ui/libgtk2ui/gtk2_signal.h"
#include "chrome/browser/ui/libgtk2ui/libgtk2ui_export.h"
#include "ui/base/ime/linux/text_edit_key_bindings_delegate_auralinux.h"
#include "ui/gfx/color_utils.h"
#include "ui/views/linux_ui/linux_ui.h"
#include "ui/views/window/frame_buttons.h"
Expand Down Expand Up @@ -68,9 +67,6 @@ class Gtk2UI : public views::LinuxUI {

// ui::LinuxUI:
void Initialize() override;
// TODO(varkha): This should not be necessary once Material Design is on
// unconditionally.
void MaterialDesignControllerReady() override;
bool GetTint(int id, color_utils::HSL* tint) const override;
bool GetColor(int id, SkColor* color) const override;
SkColor GetFocusRingColor() const override;
Expand Down Expand Up @@ -128,11 +124,6 @@ class Gtk2UI : public views::LinuxUI {
// Initialize the Xcursor theme and size with the GTK theme and size.
void LoadCursorTheme();

// Updates colors if necessary after possible modification of command line.
// TODO(varkha): This should not be necessary once Material Design is on
// unconditionally.
void UpdateMaterialDesignColors();

// Reads in explicit theme frame colors from the ChromeGtkFrame style class
// or generates them per our fallback algorithm.
void BuildFrameColors();
Expand Down
24 changes: 0 additions & 24 deletions chrome/browser/ui/tabs/tab_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -177,30 +177,6 @@ TabAlertState GetTabAlertStateForContents(content::WebContents* contents) {

gfx::Image GetTabAlertIndicatorImage(TabAlertState alert_state,
SkColor button_color) {
#if defined(OS_MACOSX)
if (!ui::MaterialDesignController::IsModeMaterial()) {
ui::ResourceBundle* rb = &ui::ResourceBundle::GetSharedInstance();
switch (alert_state) {
case TabAlertState::AUDIO_PLAYING:
return rb->GetNativeImageNamed(IDR_TAB_AUDIO_INDICATOR);
case TabAlertState::AUDIO_MUTING:
return rb->GetNativeImageNamed(IDR_TAB_AUDIO_MUTING_INDICATOR);
case TabAlertState::MEDIA_RECORDING:
return rb->GetNativeImageNamed(IDR_TAB_RECORDING_INDICATOR);
case TabAlertState::TAB_CAPTURING:
return rb->GetNativeImageNamed(IDR_TAB_CAPTURE_INDICATOR);
case TabAlertState::BLUETOOTH_CONNECTED:
return rb->GetNativeImageNamed(IDR_TAB_BLUETOOTH_INDICATOR);
case TabAlertState::USB_CONNECTED:
return rb->GetNativeImageNamed(IDR_TAB_USB_INDICATOR);
case TabAlertState::NONE:
break;
}
NOTREACHED();
return gfx::Image();
}
#endif

gfx::VectorIconId icon_id = gfx::VectorIconId::VECTOR_ICON_NONE;
switch (alert_state) {
case TabAlertState::AUDIO_PLAYING:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,6 @@ void ChromeBrowserMainExtraPartsViewsLinux::ToolkitInitialized() {

void ChromeBrowserMainExtraPartsViewsLinux::PreCreateThreads() {
ChromeBrowserMainExtraPartsViews::PreCreateThreads();
// TODO(varkha): The next call should not be necessary once Material Design is
// on unconditionally.
views::LinuxUI::instance()->MaterialDesignControllerReady();
views::LinuxUI::instance()->UpdateDeviceScaleFactor(
display::Screen::GetScreen()->GetPrimaryDisplay().device_scale_factor());

Expand Down
11 changes: 3 additions & 8 deletions chrome/browser/ui/views/tabs/tab_strip_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -329,14 +329,9 @@ TEST_F(TabStripTest, ImmersiveMode) {
// Creates a tab strip in stacked layout mode and verifies the correctness
// of hit tests against the visible/occluded regions of a tab and the tab
// close button of the active tab.
TEST_F(TabStripTest, TabHitTestMaskWhenStacked) {
// TODO(varkha,pkasting): Update the test for Material Design layout.
// crbug.com/575327
if (ui::MaterialDesignController::IsModeMaterial()) {
VLOG(1) << "Test is DISABLED for material design layouts.";
return;
}

// TODO(pkasting): Update the test for Material Design layout.
// crbug.com/575327
TEST_F(TabStripTest, DISABLED_TabHitTestMaskWhenStacked) {
tab_strip_->SetBounds(0, 0, 300, 20);

controller_->AddTab(0, false);
Expand Down
Loading

0 comments on commit 944999f

Please sign in to comment.