display_list.h 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // Copyright 2016 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_DISPLAY_DISPLAY_LIST_H_
  5. #define UI_DISPLAY_DISPLAY_LIST_H_
  6. #include <stdint.h>
  7. #include <vector>
  8. #include "base/observer_list.h"
  9. #include "ui/display/display.h"
  10. #include "ui/display/display_export.h"
  11. namespace display {
  12. class DisplayObserver;
  13. // Maintains an ordered list of Displays as well as operations to add, remove
  14. // and update said list. Additionally maintains DisplayObservers and updates
  15. // them as appropriate.
  16. class DISPLAY_EXPORT DisplayList {
  17. public:
  18. using Displays = std::vector<Display>;
  19. enum class Type {
  20. PRIMARY,
  21. NOT_PRIMARY,
  22. };
  23. DisplayList();
  24. ~DisplayList();
  25. DisplayList(const DisplayList&) = delete;
  26. DisplayList& operator=(const DisplayList&) = delete;
  27. void AddObserver(DisplayObserver* observer);
  28. void RemoveObserver(DisplayObserver* observer);
  29. const Displays& displays() const { return displays_; }
  30. Displays::const_iterator FindDisplayById(int64_t id) const;
  31. // Get an iterator for the primary display. This returns an invalid iterator
  32. // if no such display is available. Callers must check the returned value
  33. // against `displays().end()` before dereferencing.
  34. Displays::const_iterator GetPrimaryDisplayIterator() const;
  35. void AddOrUpdateDisplay(const Display& display, Type type);
  36. // Updates the cached display based on display.id(). This returns a bitmask
  37. // of the changed values suitable for passing to
  38. // DisplayObserver::OnDisplayMetricsChanged().
  39. uint32_t UpdateDisplay(const Display& display);
  40. // Updates the cached display based on display.id(). Also updates the primary
  41. // display if |type| indicates |display| is the primary display. See single
  42. // argument version for description of return value.
  43. uint32_t UpdateDisplay(const Display& display, Type type);
  44. // Adds a new Display.
  45. void AddDisplay(const Display& display, Type type);
  46. // Removes the Display with the specified id.
  47. void RemoveDisplay(int64_t id);
  48. // Checks for general struct validity. This permits empty lists, but non-empty
  49. // lists must specify a primary display and the displays must not use repeated
  50. // or invalid ids.
  51. bool IsValid() const;
  52. base::ObserverList<DisplayObserver>* observers() { return &observers_; }
  53. const base::ObserverList<DisplayObserver>* observers() const {
  54. return &observers_;
  55. }
  56. private:
  57. // A non-const version of FindDisplayById.
  58. Displays::iterator FindDisplayByIdInternal(int64_t id);
  59. // The list of displays tracked by the display::Screen or other client.
  60. std::vector<Display> displays_;
  61. // The id of the primary Display in `displays_` for the display::Screen.
  62. int64_t primary_id_ = kInvalidDisplayId;
  63. base::ObserverList<DisplayObserver> observers_;
  64. };
  65. } // namespace display
  66. #endif // UI_DISPLAY_DISPLAY_LIST_H_