ambient_util.cc 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // Copyright 2019 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "ash/ambient/util/ambient_util.h"
  5. #include "ash/ambient/ambient_constants.h"
  6. #include "ash/constants/ash_features.h"
  7. #include "ash/public/cpp/ambient/ambient_backend_controller.h"
  8. #include "ash/public/cpp/ambient/ambient_client.h"
  9. #include "ash/public/cpp/ambient/proto/photo_cache_entry.pb.h"
  10. #include "ash/style/dark_light_mode_controller_impl.h"
  11. #include "ash/utility/lottie_util.h"
  12. #include "base/no_destructor.h"
  13. #include "base/strings/strcat.h"
  14. #include "third_party/re2/src/re2/re2.h"
  15. #include "third_party/skia/include/core/SkColor.h"
  16. #include "ui/color/color_id.h"
  17. #include "ui/color/color_provider.h"
  18. #include "ui/gfx/color_palette.h"
  19. #include "ui/gfx/shadow_value.h"
  20. namespace ash {
  21. namespace ambient {
  22. namespace util {
  23. bool IsShowing(LockScreen::ScreenType type) {
  24. return LockScreen::HasInstance() && LockScreen::Get()->screen_type() == type;
  25. }
  26. SkColor GetContentLayerColor(
  27. AshColorProvider::ContentLayerType content_layer_type) {
  28. return GetContentLayerColor(
  29. content_layer_type,
  30. DarkLightModeControllerImpl::Get()->IsDarkModeEnabled());
  31. }
  32. SkColor GetContentLayerColor(
  33. AshColorProvider::ContentLayerType content_layer_type,
  34. bool dark_mode_enable) {
  35. auto* ash_color_provider = AshColorProvider::Get();
  36. switch (content_layer_type) {
  37. case AshColorProvider::ContentLayerType::kTextColorPrimary:
  38. case AshColorProvider::ContentLayerType::kTextColorSecondary:
  39. case AshColorProvider::ContentLayerType::kIconColorPrimary:
  40. case AshColorProvider::ContentLayerType::kIconColorSecondary:
  41. return dark_mode_enable
  42. ? ash_color_provider->GetContentLayerColor(content_layer_type)
  43. : SK_ColorWHITE;
  44. default:
  45. NOTREACHED() << "Unsupported content layer type";
  46. // Return a very bright color so it's obvious there is a mistake.
  47. return gfx::kPlaceholderColor;
  48. }
  49. }
  50. const gfx::FontList& GetDefaultFontlist() {
  51. static const base::NoDestructor<gfx::FontList> font_list("Google Sans, 64px");
  52. return *font_list;
  53. }
  54. gfx::ShadowValues GetTextShadowValues(const ui::ColorProvider* color_provider,
  55. int elevation) {
  56. // If `color_provider` does not exist the shadow values are being created in
  57. // order to calculate margins. In that case the color plays no role so set it
  58. // to gfx::kPlaceholderColor.
  59. // Currently an elevation of 2 falls back to MakeMdShadowValues so use
  60. // ui::kColorShadowBase, which is the base shadow color for MdShadowValues,
  61. // until MakeMdShadowValues is refactored to take in it’s own
  62. // |key_shadow_color| and |ambient_shadow_color|.
  63. // TODO(elainechien): crbug.com/1056950
  64. SkColor shadow_base_color =
  65. color_provider ? color_provider->GetColor(ui::kColorShadowBase)
  66. : gfx::kPlaceholderColor;
  67. return gfx::ShadowValue::MakeShadowValues(elevation, shadow_base_color,
  68. shadow_base_color);
  69. }
  70. bool IsAmbientModeTopicTypeAllowed(::ambient::TopicType topic_type) {
  71. switch (topic_type) {
  72. case ::ambient::TopicType::kCurated:
  73. return chromeos::features::kAmbientModeDefaultFeedEnabled.Get();
  74. case ::ambient::TopicType::kCapturedOnPixel:
  75. return chromeos::features::kAmbientModeCapturedOnPixelPhotosEnabled.Get();
  76. case ::ambient::TopicType::kCulturalInstitute:
  77. return chromeos::features::kAmbientModeCulturalInstitutePhotosEnabled
  78. .Get();
  79. case ::ambient::TopicType::kFeatured:
  80. return chromeos::features::kAmbientModeFeaturedPhotosEnabled.Get();
  81. case ::ambient::TopicType::kGeo:
  82. return chromeos::features::kAmbientModeGeoPhotosEnabled.Get();
  83. case ::ambient::TopicType::kPersonal:
  84. return chromeos::features::kAmbientModePersonalPhotosEnabled.Get();
  85. case ::ambient::TopicType::kRss:
  86. return chromeos::features::kAmbientModeRssPhotosEnabled.Get();
  87. case ::ambient::TopicType::kOther:
  88. return false;
  89. }
  90. }
  91. bool ParsedDynamicAssetId::operator<(const ParsedDynamicAssetId& other) const {
  92. return idx == other.idx ? position_id < other.position_id : idx < other.idx;
  93. }
  94. bool ParseDynamicLottieAssetId(base::StringPiece asset_id,
  95. ParsedDynamicAssetId& parsed_output) {
  96. static const base::NoDestructor<std::string> kAssetIdPatternStr(
  97. base::StrCat({kLottieCustomizableIdPrefix,
  98. R"(_Photo_Position([[:alnum:]]+)_([[:digit:]]+).*)"}));
  99. static const base::NoDestructor<RE2> kAssetIdPattern(
  100. kAssetIdPatternStr->data());
  101. return RE2::FullMatch(asset_id.data(), *kAssetIdPattern,
  102. &parsed_output.position_id, &parsed_output.idx);
  103. }
  104. } // namespace util
  105. } // namespace ambient
  106. } // namespace ash