#include "ash/accessibility/flash_screen_controller.h"
#include "ash/color_enhancement/color_enhancement_controller.h"
#include "ash/shell.h"
#include "ui/accessibility/accessibility_features.h"
#include "ui/message_center/message_center_types.h"
#include "ui/message_center/public/cpp/notification_types.h"
namespace ash {
namespace {
constexpr auto kNotificationTimerDelay = base::Milliseconds(300);
constexpr int kNumFlashesPerNotification = 2;
}
FlashScreenController::FlashScreenController() : throb_animation_{this} {
notification_observer_.Observe(message_center::MessageCenter::Get());
throb_animation_.SetThrobDuration(kNotificationTimerDelay);
}
FlashScreenController::~FlashScreenController() = default;
void FlashScreenController::OnNotificationDisplayed(
const std::string& notification_id,
const message_center::DisplaySource display_source) {
if (display_source == message_center::DISPLAY_SOURCE_POPUP) {
MaybeFlashOn(notification_id);
}
}
void FlashScreenController::OnNotificationAdded(
const std::string& notification_id) {
MaybeFlashOn(notification_id);
}
void FlashScreenController::AnimationEnded(const gfx::Animation* animation) {
if (!throb_animation_.IsShowing()) {
FlashOff();
}
}
void FlashScreenController::AnimationProgressed(
const gfx::Animation* animation) {
const double percent = 1 - animation->GetCurrentValue();
if (percent == 0) {
FlashOff();
return;
}
float r = SkColorGetR(color_);
float g = SkColorGetG(color_);
float b = SkColorGetB(color_);
r = r + (255 - r) * percent;
g = g + (255 - g) * percent;
b = b + (255 - b) * percent;
if (r > 255) {
r = 255;
}
if (g > 255) {
g = 255;
}
if (b > 255) {
b = 255;
}
SkColor color = SkColorSetRGB(r, g, b);
auto* color_enhancement_controller =
Shell::Get()->color_enhancement_controller();
color_enhancement_controller->FlashScreenForNotification(true,
color);
}
void FlashScreenController::AnimationCanceled(const gfx::Animation* animation) {
FlashOff();
}
void FlashScreenController::MaybeFlashOn(const std::string& notification_id) {
auto* message_center = message_center::MessageCenter::Get();
if (!message_center || message_center->IsQuietMode()) {
return;
}
message_center::Notification* notification =
message_center->FindNotificationById(notification_id);
if (!notification || notification->silent()) {
return;
}
if (notification->group_parent()) {
return;
}
if (notification->priority() < message_center::DEFAULT_PRIORITY) {
return;
}
FlashOn();
}
void FlashScreenController::PreviewFlash() {
FlashOn();
}
void FlashScreenController::FlashOn() {
if (!::features::IsAccessibilityFlashScreenFeatureEnabled()) {
return;
}
if (!enabled_) {
return;
}
if (throb_animation_.is_animating()) {
return;
}
throb_animation_.StartThrobbing(kNumFlashesPerNotification * 2);
}
void FlashScreenController::FlashOff() {
auto* color_enhancement_controller =
Shell::Get()->color_enhancement_controller();
color_enhancement_controller->FlashScreenForNotification(
false, color_);
}
}