color_provider.h 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. #ifndef UI_COLOR_COLOR_PROVIDER_H_
  5. #define UI_COLOR_COLOR_PROVIDER_H_
  6. #include <forward_list>
  7. #include <memory>
  8. #include "base/component_export.h"
  9. #include "base/containers/flat_map.h"
  10. #include "third_party/abseil-cpp/absl/types/optional.h"
  11. #include "third_party/skia/include/core/SkColor.h"
  12. #include "ui/color/color_id.h"
  13. #include "ui/color/color_mixer.h"
  14. namespace ui {
  15. // A ColorProvider holds the complete pipeline of ColorMixers that compute
  16. // result colors for UI elements. ColorProvider is meant to be a long-lived
  17. // object whose internal list of mixers does not change after initial
  18. // construction. Separate ColorProviders should be instantiated for e.g.
  19. // windows with different themes.
  20. // TODO(pkasting): Figure out ownership model and lifetime.
  21. class COMPONENT_EXPORT(COLOR) ColorProvider {
  22. public:
  23. using ColorMap = base::flat_map<ColorId, SkColor>;
  24. ColorProvider();
  25. ColorProvider(const ColorProvider&) = delete;
  26. ColorProvider& operator=(const ColorProvider&) = delete;
  27. ColorProvider(ColorProvider&&);
  28. ColorProvider& operator=(ColorProvider&&);
  29. ~ColorProvider();
  30. // Adds a mixer to the current color pipeline after all other
  31. // non-"postprocessing" mixers. Returns a reference to the added mixer so
  32. // callers can subsequently add sets and/or recipes.
  33. ColorMixer& AddMixer();
  34. // Like AddMixer(), but adds at the very end of the color pipeline.
  35. // "Postprocessing" mixers are meant to run after all other mixers and are
  36. // skipped when calling GetUnprocessedColor().
  37. ColorMixer& AddPostprocessingMixer();
  38. // Returns the result color for |id| by applying the effects of each mixer in
  39. // order. Returns gfx::kPlaceholderColor if no mixer knows how to construct
  40. // |id|.
  41. SkColor GetColor(ColorId id) const;
  42. // Generates the `color_map_` used by this provider for all ColorIds defined
  43. // by attached mixers. After the map is generated attached mixers and their
  44. // associated objects are discarded. Mixers should not be added to the
  45. // provider after this has been called.
  46. void GenerateColorMap();
  47. void SetColorForTesting(ColorId id, SkColor color);
  48. const ColorMap& color_map_for_testing() { return *color_map_; }
  49. private:
  50. using Mixers = std::forward_list<ColorMixer>;
  51. // Returns the last mixer in the chain that is not a "postprocessing" mixer,
  52. // or nullptr.
  53. const ColorMixer* GetLastNonPostprocessingMixer() const;
  54. // The entire color pipeline, in reverse order (that is, the "last" mixer is
  55. // at the front).
  56. Mixers mixers_;
  57. // The first mixer in the chain that is a "postprocessing" mixer.
  58. Mixers::iterator first_postprocessing_mixer_ = mixers_.before_begin();
  59. // A cached map of ColorId => SkColor mappings for this provider. This will be
  60. // generated in the call to `GenerateColorMap()`.
  61. absl::optional<ColorMap> color_map_;
  62. };
  63. } // namespace ui
  64. #endif // UI_COLOR_COLOR_PROVIDER_H_