Skip to content

Commit

Permalink
Feature param for custom ad notification dark mode background color.
Browse files Browse the repository at this point in the history
  • Loading branch information
aseren committed Sep 10, 2021
1 parent 688ff69 commit 1e54f66
Show file tree
Hide file tree
Showing 8 changed files with 127 additions and 5 deletions.
2 changes: 2 additions & 0 deletions browser/ui/views/brave_ads/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ source_set("brave_ads") {
"ad_notification_view_factory.h",
"bounds_util.cc",
"bounds_util.h",
"color_util.cc",
"color_util.h",
"insets_util.cc",
"insets_util.h",
"padded_image_button.cc",
Expand Down
26 changes: 21 additions & 5 deletions browser/ui/views/brave_ads/ad_notification_popup.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "brave/browser/ui/views/brave_ads/ad_notification_view.h"
#include "brave/browser/ui/views/brave_ads/ad_notification_view_factory.h"
#include "brave/browser/ui/views/brave_ads/bounds_util.h"
#include "brave/browser/ui/views/brave_ads/color_util.h"
#include "brave/components/brave_ads/common/features.h"
#include "brave/components/brave_ads/common/pref_names.h"
#include "brave/grit/brave_generated_resources.h"
Expand Down Expand Up @@ -52,9 +53,6 @@ std::map<std::string, AdNotificationPopup* /* NOT OWNED */>

bool g_disable_fade_in_animation_for_testing = false;

constexpr SkColor kLightModeBackgroundColor = SkColorSetRGB(0xed, 0xf0, 0xf2);
constexpr SkColor kDarkModeBackgroundColor = SkColorSetRGB(0x20, 0x23, 0x27);

constexpr SkColor kLightModeBorderColor = SkColorSetRGB(0xd5, 0xdb, 0xe2);
constexpr SkColor kDarkModeBorderColor = SkColorSetRGB(0x3f, 0x41, 0x45);
constexpr int kBorderThickness = 1;
Expand Down Expand Up @@ -82,6 +80,23 @@ class DefaultPopupInstanceFactory
}
};

SkColor GetLightModeBackgroundColor() {
return SkColorSetRGB(0xed, 0xf0, 0xf2);
}

SkColor GetDarkModeBackgroundColor() {
const std::string color_param =
features::AdNotificationDarkModeBackgroundColor();

SkColor bg_color;
if (!RgbStringToSkColor(color_param, &bg_color)) {
NOTREACHED();
return SkColorSetRGB(0x20, 0x23, 0x27);
}

return bg_color;
}

} // namespace

AdNotificationPopup::PopupInstanceFactory::~PopupInstanceFactory() = default;
Expand Down Expand Up @@ -257,8 +272,9 @@ void AdNotificationPopup::OnPaintBackground(gfx::Canvas* canvas) {
// Draw background
cc::PaintFlags background_flags;
background_flags.setAntiAlias(true);
background_flags.setColor(should_use_dark_colors ? kDarkModeBackgroundColor
: kLightModeBackgroundColor);
background_flags.setColor(should_use_dark_colors
? GetDarkModeBackgroundColor()
: GetLightModeBackgroundColor());
canvas->DrawRoundRect(bounds, kCornerRadius, background_flags);
}

Expand Down
39 changes: 39 additions & 0 deletions browser/ui/views/brave_ads/color_util.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/* Copyright (c) 2021 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "brave/browser/ui/views/brave_ads/color_util.h"

#include "base/strings/string_number_conversions.h"
#include "base/strings/string_piece.h"

namespace brave_ads {

bool RgbStringToSkColor(base::StringPiece rgb, SkColor* color) {
DCHECK(color);

// Expect three RGB color components with length == 2, e.g. 42fe4c.
constexpr size_t kColorComponentsCount = 3;
constexpr size_t kColorComponentLen = 2;

if (rgb.size() != kColorComponentsCount * kColorComponentLen) {
return false;
}

uint32_t components[kColorComponentsCount];
for (size_t i = 0; i < kColorComponentsCount; ++i) {
const size_t beg = kColorComponentLen * i;
uint32_t component = 0;
if (!base::HexStringToUInt(rgb.substr(beg, kColorComponentLen),
&component)) {
return false;
}
components[i] = component;
}

*color = SkColorSetRGB(components[0], components[1], components[2]);
return true;
}

} // namespace brave_ads
18 changes: 18 additions & 0 deletions browser/ui/views/brave_ads/color_util.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/* Copyright (c) 2021 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef BRAVE_BROWSER_UI_VIEWS_BRAVE_ADS_COLOR_UTIL_H_
#define BRAVE_BROWSER_UI_VIEWS_BRAVE_ADS_COLOR_UTIL_H_

#include "base/strings/string_piece_forward.h"
#include "third_party/skia/include/core/SkColor.h"

namespace brave_ads {

bool RgbStringToSkColor(base::StringPiece rgb, SkColor* color);

} // namespace brave_ads

#endif // BRAVE_BROWSER_UI_VIEWS_BRAVE_ADS_COLOR_UTIL_H_
21 changes: 21 additions & 0 deletions browser/ui/views/brave_ads/color_util_unittest.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright (c) 2021 The Brave Authors. All rights reserved.
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// you can obtain one at http://mozilla.org/MPL/2.0/.

#include "brave/browser/ui/views/brave_ads/color_util.h"
#include "base/strings/string_piece.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/skia/include/core/SkColor.h"

// npm run test -- brave_unit_tests --filter=ColorUtilTest.*

TEST(ColorUtilTest, CheckRgbStringToSkColor) {
SkColor color;
EXPECT_TRUE(brave_ads::RgbStringToSkColor("42fe4c", &color));
EXPECT_EQ(SkColorSetRGB(0x42, 0xfe, 0x4c), color);

EXPECT_FALSE(brave_ads::RgbStringToSkColor("", &color));
EXPECT_FALSE(brave_ads::RgbStringToSkColor("42fe4", &color));
EXPECT_FALSE(brave_ads::RgbStringToSkColor("h2fe4c", &color));
}
18 changes: 18 additions & 0 deletions components/brave_ads/common/features.cc
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ const char kFieldTrialParameterAdNotificationFadeDuration[] =
"ad_notification_fade_duration";
const int kDefaultAdNotificationFadeDuration = 200;

// Ad notification dark mode background color
const char kFieldTrialParameterAdNotificationDarkModeBackgroundColor[] =
"ad_notification_dark_mode_background_color";
// Default color value is SkColorSetRGB(0x20, 0x23, 0x27);
const char kDefaultAdNotificationDarkModeBackgroundColor[] = "202327";

// Ad notification normalized display coordinate for the x component should be
// between 0.0 and 1.0; coordinates outside this range will be adjusted to fit
// the work area. Set to 0.0 for left, 0.5 for center or 1.0 for right
Expand Down Expand Up @@ -124,6 +130,18 @@ int AdNotificationFadeDuration() {
kDefaultAdNotificationFadeDuration);
}

std::string AdNotificationDarkModeBackgroundColor() {
const std::string param_value = GetFieldTrialParamValueByFeature(
kCustomAdNotifications,
kFieldTrialParameterAdNotificationDarkModeBackgroundColor);

if (param_value.empty()) {
return kDefaultAdNotificationDarkModeBackgroundColor;
}

return param_value;
}

double AdNotificationNormalizedDisplayCoordinateX() {
return GetFieldTrialParamByFeatureAsDouble(
kCustomAdNotifications,
Expand Down
3 changes: 3 additions & 0 deletions components/brave_ads/common/features.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#ifndef BRAVE_COMPONENTS_BRAVE_ADS_COMMON_FEATURES_H_
#define BRAVE_COMPONENTS_BRAVE_ADS_COMMON_FEATURES_H_

#include <string>

#include "build/build_config.h"

namespace base {
Expand All @@ -27,6 +29,7 @@ bool IsCustomAdNotificationsEnabled();
bool CanFallbackToCustomAdNotifications();
#if !defined(OS_ANDROID)
int AdNotificationFadeDuration();
std::string AdNotificationDarkModeBackgroundColor();
double AdNotificationNormalizedDisplayCoordinateX();
int AdNotificationInsetX();
double AdNotificationNormalizedDisplayCoordinateY();
Expand Down
5 changes: 5 additions & 0 deletions test/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,11 @@ test("brave_unit_tests") {
]
}

if (toolkit_views) {
sources += [ "//brave/browser/ui/views/brave_ads/color_util_unittest.cc" ]
deps += [ "//brave/browser/ui/views/brave_ads" ]
}

if (binance_enabled) {
sources +=
[ "//brave/components/binance/browser/binance_json_parser_unittest.cc" ]
Expand Down

0 comments on commit 1e54f66

Please sign in to comment.