-
Notifications
You must be signed in to change notification settings - Fork 6.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
personalization: set keyboard backlight color upon login
Create KeyboardBacklightColorController to hold the logic of setting the user's selected keyboard backlight color after login. Also observe when the wallpaper extracted color changes to set the keyboard color when appropriate. The pref registration is moved from personalization_app_prefs to this controller for logic isolation. The color sequence from booting up would be: 1. Use the wallpaper extracted color when available. 2. After the user logs in, use their saved pref. BUG=b/230519831 TEST=ash_unittests --gtest_filter="*KeyboardBacklightColorControllerTest*" TEST=unit_tests --gtest_filter="*PersonalizationAppKeyboardBacklightProviderImplTest.*" Cq-Include-Trybots: luci.chrome.try:linux-chromeos-chrome Change-Id: I702163077ad54424b1411022d548e0af05f4c275 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3631712 Commit-Queue: Jason Thai <jasontt@chromium.org> Reviewed-by: James Cook <jamescook@chromium.org> Reviewed-by: Xiaohui Chen <xiaohuic@chromium.org> Reviewed-by: Scott Violet <sky@chromium.org> Cr-Commit-Position: refs/heads/main@{#1001666}
- Loading branch information
Jason Thai
authored and
Chromium LUCI CQ
committed
May 10, 2022
1 parent
173a31e
commit e367085
Showing
13 changed files
with
287 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
116 changes: 116 additions & 0 deletions
116
ash/system/keyboard_brightness/keyboard_backlight_color_controller.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
// Copyright 2022 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 "ash/system/keyboard_brightness/keyboard_backlight_color_controller.h" | ||
|
||
#include "ash/constants/ash_pref_names.h" | ||
#include "ash/public/cpp/wallpaper/wallpaper_controller.h" | ||
#include "ash/rgb_keyboard/rgb_keyboard_manager.h" | ||
#include "ash/rgb_keyboard/rgb_keyboard_util.h" | ||
#include "ash/session/session_controller_impl.h" | ||
#include "ash/shell.h" | ||
#include "ash/wallpaper/wallpaper_controller_impl.h" | ||
#include "ash/webui/personalization_app/mojom/personalization_app.mojom.h" | ||
#include "base/check_op.h" | ||
#include "components/prefs/pref_registry_simple.h" | ||
#include "components/prefs/pref_service.h" | ||
#include "third_party/skia/include/core/SkColor.h" | ||
#include "ui/gfx/color_analysis.h" | ||
|
||
namespace ash { | ||
|
||
namespace { | ||
|
||
PrefService* GetActivePrefService() { | ||
return Shell::Get()->session_controller()->GetActivePrefService(); | ||
} | ||
|
||
} // namespace | ||
|
||
KeyboardBacklightColorController::KeyboardBacklightColorController() { | ||
wallpaper_controller_observation_.Observe(WallpaperController::Get()); | ||
} | ||
|
||
KeyboardBacklightColorController::~KeyboardBacklightColorController() = default; | ||
|
||
// static | ||
void KeyboardBacklightColorController::RegisterProfilePrefs( | ||
PrefRegistrySimple* registry) { | ||
registry->RegisterIntegerPref( | ||
prefs::kPersonalizationKeyboardBacklightColor, | ||
static_cast<int>(personalization_app::mojom::BacklightColor::kWallpaper)); | ||
} | ||
|
||
void KeyboardBacklightColorController::SetBacklightColor( | ||
personalization_app::mojom::BacklightColor backlight_color) { | ||
auto* rgb_keyboard_manager = Shell::Get()->rgb_keyboard_manager(); | ||
DCHECK(rgb_keyboard_manager); | ||
DVLOG(3) << __func__ << " backlight_color=" << backlight_color; | ||
switch (backlight_color) { | ||
case personalization_app::mojom::BacklightColor::kWallpaper: { | ||
auto* wallpaper_controller = Shell::Get()->wallpaper_controller(); | ||
DCHECK(wallpaper_controller); | ||
SkColor color = wallpaper_controller->GetProminentColor( | ||
color_utils::ColorProfile(color_utils::LumaRange::NORMAL, | ||
color_utils::SaturationRange::VIBRANT)); | ||
rgb_keyboard_manager->SetStaticBackgroundColor( | ||
SkColorGetR(color), SkColorGetG(color), SkColorGetB(color)); | ||
break; | ||
} | ||
case personalization_app::mojom::BacklightColor::kWhite: | ||
case personalization_app::mojom::BacklightColor::kRed: | ||
case personalization_app::mojom::BacklightColor::kYellow: | ||
case personalization_app::mojom::BacklightColor::kGreen: | ||
case personalization_app::mojom::BacklightColor::kBlue: | ||
case personalization_app::mojom::BacklightColor::kIndigo: | ||
case personalization_app::mojom::BacklightColor::kPurple: { | ||
SkColor color = ConvertBacklightColorToSkColor(backlight_color); | ||
rgb_keyboard_manager->SetStaticBackgroundColor( | ||
SkColorGetR(color), SkColorGetG(color), SkColorGetB(color)); | ||
break; | ||
} | ||
case personalization_app::mojom::BacklightColor::kRainbow: | ||
rgb_keyboard_manager->SetRainbowMode(); | ||
break; | ||
} | ||
|
||
GetActivePrefService()->SetInteger( | ||
prefs::kPersonalizationKeyboardBacklightColor, | ||
static_cast<int>(backlight_color)); | ||
} | ||
|
||
personalization_app::mojom::BacklightColor | ||
KeyboardBacklightColorController::GetBacklightColor() { | ||
return static_cast<personalization_app::mojom::BacklightColor>( | ||
GetActivePrefService()->GetInteger( | ||
prefs::kPersonalizationKeyboardBacklightColor)); | ||
} | ||
|
||
void KeyboardBacklightColorController::OnActiveUserSessionChanged( | ||
const AccountId& account_id) { | ||
auto* session_controller = Shell::Get()->session_controller(); | ||
DCHECK(session_controller); | ||
PrefService* pref_service = | ||
session_controller->GetUserPrefServiceForUser(account_id); | ||
DCHECK(pref_service); | ||
auto backlight_color = | ||
static_cast<personalization_app::mojom::BacklightColor>( | ||
pref_service->GetInteger( | ||
prefs::kPersonalizationKeyboardBacklightColor)); | ||
SetBacklightColor(backlight_color); | ||
} | ||
|
||
void KeyboardBacklightColorController::OnWallpaperColorsChanged() { | ||
auto* wallpaper_controller = Shell::Get()->wallpaper_controller(); | ||
DCHECK(wallpaper_controller); | ||
auto backlight_color = | ||
static_cast<personalization_app::mojom::BacklightColor>( | ||
GetActivePrefService()->GetInteger( | ||
prefs::kPersonalizationKeyboardBacklightColor)); | ||
if (backlight_color != personalization_app::mojom::BacklightColor::kWallpaper) | ||
return; | ||
SetBacklightColor(personalization_app::mojom::BacklightColor::kWallpaper); | ||
} | ||
|
||
} // namespace ash |
57 changes: 57 additions & 0 deletions
57
ash/system/keyboard_brightness/keyboard_backlight_color_controller.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// Copyright 2022 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 ASH_SYSTEM_KEYBOARD_BRIGHTNESS_KEYBOARD_BACKLIGHT_COLOR_CONTROLLER_H_ | ||
#define ASH_SYSTEM_KEYBOARD_BRIGHTNESS_KEYBOARD_BACKLIGHT_COLOR_CONTROLLER_H_ | ||
|
||
#include "ash/ash_export.h" | ||
#include "ash/public/cpp/session/session_observer.h" | ||
#include "ash/public/cpp/wallpaper/wallpaper_controller.h" | ||
#include "ash/public/cpp/wallpaper/wallpaper_controller_observer.h" | ||
#include "ash/webui/personalization_app/mojom/personalization_app.mojom-shared.h" | ||
#include "base/scoped_observation.h" | ||
|
||
class PrefRegistrySimple; | ||
|
||
namespace ash { | ||
|
||
// Controller to manage keyboard backlight colors. | ||
class ASH_EXPORT KeyboardBacklightColorController | ||
: public SessionObserver, | ||
public WallpaperControllerObserver { | ||
public: | ||
KeyboardBacklightColorController(); | ||
|
||
KeyboardBacklightColorController(const KeyboardBacklightColorController&) = | ||
delete; | ||
KeyboardBacklightColorController& operator=( | ||
const KeyboardBacklightColorController&) = delete; | ||
|
||
~KeyboardBacklightColorController() override; | ||
|
||
static void RegisterProfilePrefs(PrefRegistrySimple* registry); | ||
|
||
// Sets the keyboard backlight color. | ||
void SetBacklightColor( | ||
personalization_app::mojom::BacklightColor backlight_color); | ||
|
||
// Returns the currently set backlight color. | ||
personalization_app::mojom::BacklightColor GetBacklightColor(); | ||
|
||
// SessionObserver: | ||
void OnActiveUserSessionChanged(const AccountId& account_id) override; | ||
|
||
// WallpaperControllerObserver: | ||
void OnWallpaperColorsChanged() override; | ||
|
||
private: | ||
ScopedSessionObserver scoped_session_observer_{this}; | ||
|
||
base::ScopedObservation<WallpaperController, WallpaperControllerObserver> | ||
wallpaper_controller_observation_{this}; | ||
}; | ||
|
||
} // namespace ash | ||
|
||
#endif // ASH_SYSTEM_KEYBOARD_BRIGHTNESS_KEYBOARD_BACKLIGHT_COLOR_CONTROLLER_H_ |
82 changes: 82 additions & 0 deletions
82
ash/system/keyboard_brightness/keyboard_backlight_color_controller_unittest.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
// Copyright 2022 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 "ash/system/keyboard_brightness/keyboard_backlight_color_controller.h" | ||
#include "ash/constants/ash_features.h" | ||
#include "ash/constants/ash_pref_names.h" | ||
#include "ash/session/session_controller_impl.h" | ||
#include "ash/shell.h" | ||
#include "ash/test/ash_test_base.h" | ||
#include "base/test/scoped_feature_list.h" | ||
#include "testing/gtest/include/gtest/gtest.h" | ||
|
||
namespace ash { | ||
|
||
namespace { | ||
constexpr char kUser1[] = "user1@test.com"; | ||
const AccountId account_id_1 = AccountId::FromUserEmailGaiaId(kUser1, kUser1); | ||
|
||
constexpr char kUser2[] = "user2@test.com"; | ||
const AccountId account_id_2 = AccountId::FromUserEmailGaiaId(kUser2, kUser2); | ||
} // namespace | ||
|
||
class KeyboardBacklightColorControllerTest : public AshTestBase { | ||
public: | ||
KeyboardBacklightColorControllerTest() | ||
: scoped_feature_list_(features::kRgbKeyboard) {} | ||
|
||
KeyboardBacklightColorControllerTest( | ||
const KeyboardBacklightColorControllerTest&) = delete; | ||
KeyboardBacklightColorControllerTest& operator=( | ||
const KeyboardBacklightColorControllerTest&) = delete; | ||
|
||
~KeyboardBacklightColorControllerTest() override = default; | ||
|
||
// testing::Test: | ||
void SetUp() override { | ||
AshTestBase::SetUp(); | ||
|
||
controller_ = Shell::Get()->keyboard_backlight_color_controller(); | ||
} | ||
|
||
protected: | ||
KeyboardBacklightColorController* controller_ = nullptr; | ||
|
||
private: | ||
base::test::ScopedFeatureList scoped_feature_list_; | ||
}; | ||
|
||
TEST_F(KeyboardBacklightColorControllerTest, SetBacklightColorUpdatesPref) { | ||
controller_->SetBacklightColor( | ||
personalization_app::mojom::BacklightColor::kBlue); | ||
|
||
PrefService* prefs_service = | ||
Shell::Get()->session_controller()->GetActivePrefService(); | ||
EXPECT_EQ(personalization_app::mojom::BacklightColor::kBlue, | ||
static_cast<personalization_app::mojom::BacklightColor>( | ||
prefs_service->GetInteger( | ||
prefs::kPersonalizationKeyboardBacklightColor))); | ||
} | ||
|
||
TEST_F(KeyboardBacklightColorControllerTest, SetBacklightColorAfterSignin) { | ||
// Verify the user starts with wallpaper-extracted color. | ||
SimulateUserLogin(account_id_1); | ||
EXPECT_EQ(personalization_app::mojom::BacklightColor::kWallpaper, | ||
controller_->GetBacklightColor()); | ||
controller_->SetBacklightColor( | ||
personalization_app::mojom::BacklightColor::kRainbow); | ||
EXPECT_EQ(personalization_app::mojom::BacklightColor::kRainbow, | ||
controller_->GetBacklightColor()); | ||
|
||
// Simulate login for user2. | ||
SimulateUserLogin(account_id_2); | ||
EXPECT_EQ(personalization_app::mojom::BacklightColor::kWallpaper, | ||
controller_->GetBacklightColor()); | ||
|
||
SimulateUserLogin(account_id_1); | ||
EXPECT_EQ(personalization_app::mojom::BacklightColor::kRainbow, | ||
controller_->GetBacklightColor()); | ||
} | ||
|
||
} // namespace ash |
Oops, something went wrong.