dark_light_mode_controller_impl.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // Copyright 2022 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. #ifndef ASH_STYLE_DARK_LIGHT_MODE_CONTROLLER_IMPL_H_
  5. #define ASH_STYLE_DARK_LIGHT_MODE_CONTROLLER_IMPL_H_
  6. #include "ash/ash_export.h"
  7. #include "ash/login/ui/login_data_dispatcher.h"
  8. #include "ash/public/cpp/login_types.h"
  9. #include "ash/public/cpp/style/dark_light_mode_controller.h"
  10. #include "ash/public/cpp/wallpaper/wallpaper_controller_observer.h"
  11. #include "ash/system/scheduled_feature/scheduled_feature.h"
  12. #include "base/observer_list.h"
  13. class AccountId;
  14. class PrefChangeRegistrar;
  15. class PrefRegistrySimple;
  16. class PrefService;
  17. namespace ash {
  18. class ColorModeObserver;
  19. class DarkLightModeNudgeController;
  20. // Controls the behavior of dark/light mode. Turns on the dark mode at sunset
  21. // and off at sunrise if auto schedule is set (custom start and end for
  22. // scheduling is not supported). And determine whether to show the educational
  23. // nudge for users on login.
  24. class ASH_EXPORT DarkLightModeControllerImpl
  25. : public DarkLightModeController,
  26. public LoginDataDispatcher::Observer,
  27. public WallpaperControllerObserver,
  28. public ScheduledFeature {
  29. public:
  30. DarkLightModeControllerImpl();
  31. DarkLightModeControllerImpl(const DarkLightModeControllerImpl&) = delete;
  32. DarkLightModeControllerImpl& operator=(const DarkLightModeControllerImpl&) =
  33. delete;
  34. ~DarkLightModeControllerImpl() override;
  35. static DarkLightModeControllerImpl* Get();
  36. static void RegisterProfilePrefs(PrefRegistrySimple* registry);
  37. // Enables or disables auto scheduling on dark mode feature. When enabled,
  38. // the dark mode will automatically turn on during sunset to sunrise and off
  39. // outside that period.
  40. void SetAutoScheduleEnabled(bool enabled);
  41. // True if dark mode is automatically scheduled to turn on at sunset and off
  42. // at sunrise.
  43. bool GetAutoScheduleEnabled() const;
  44. // Toggles pref |kDarkModeEnabled|.
  45. void ToggleColorMode();
  46. // DarkLightModeController:
  47. void AddObserver(ColorModeObserver* observer) override;
  48. void RemoveObserver(ColorModeObserver* observer) override;
  49. bool IsDarkModeEnabled() const override;
  50. void SetDarkModeEnabledForTest(bool enabled) override;
  51. // LoginDataDispatcher::Observer:
  52. void OnOobeDialogStateChanged(OobeDialogState state) override;
  53. void OnFocusPod(const AccountId& account_id) override;
  54. // WallpaperControllerObserver:
  55. void OnWallpaperColorsChanged() override;
  56. // ScheduledFeature:
  57. void OnActiveUserPrefServiceChanged(PrefService* prefs) override;
  58. void OnSessionStateChanged(session_manager::SessionState state) override;
  59. void SetShowNudgeForTesting(bool value);
  60. protected:
  61. // ScheduledFeature:
  62. void RefreshFeatureState() override;
  63. private:
  64. friend class ScopedLightModeAsDefault;
  65. friend class ScopedAssistantLightModeAsDefault;
  66. // ScheduledFeature:
  67. const char* GetFeatureName() const override;
  68. // Notifies all the observers on color mode changes and refreshes the system's
  69. // colors on this change.
  70. void NotifyColorModeChanges();
  71. // Returns a closure which calls `NotifyIfDarkModeChanged` if the dark mode
  72. // changed between creation and getting out of scope.
  73. base::ScopedClosureRunner GetNotifyOnDarkModeChangeClosure();
  74. void NotifyIfDarkModeChanged(bool old_is_dark_mode_enabled);
  75. std::unique_ptr<DarkLightModeNudgeController> nudge_controller_;
  76. // The default color is DARK when the DarkLightMode feature is disabled. But
  77. // we can also override it to LIGHT through ScopedLightModeAsDefault. This is
  78. // done to help keeping some of the UI elements as LIGHT by default before
  79. // launching the DarkLightMode feature. Overriding only if the DarkLightMode
  80. // feature is disabled. This variable will be removed once fully launched the
  81. // DarkLightMode feature.
  82. bool override_light_mode_as_default_ = false;
  83. // Temporary field for testing purposes while OOBE WebUI is being migrated.
  84. absl::optional<bool> is_dark_mode_enabled_in_oobe_for_testing_;
  85. OobeDialogState oobe_state_ = OobeDialogState::HIDDEN;
  86. // absl::nullopt in case no user pod is focused.
  87. absl::optional<bool> is_dark_mode_enabled_for_focused_pod_;
  88. base::ObserverList<ColorModeObserver> observers_;
  89. std::unique_ptr<PrefChangeRegistrar> pref_change_registrar_;
  90. PrefService* active_user_pref_service_ = nullptr; // Not owned.
  91. };
  92. } // namespace ash
  93. #endif // ASH_STYLE_DARK_LIGHT_MODE_CONTROLLER_IMPL_H_