persistent_window_controller.h 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // Copyright 2018 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_DISPLAY_PERSISTENT_WINDOW_CONTROLLER_H_
  5. #define ASH_DISPLAY_PERSISTENT_WINDOW_CONTROLLER_H_
  6. #include <unordered_map>
  7. #include "ash/ash_export.h"
  8. #include "ash/public/cpp/session/session_observer.h"
  9. #include "base/callback.h"
  10. #include "ui/aura/window_tracker.h"
  11. #include "ui/display/display_observer.h"
  12. namespace ash {
  13. // Observes display changes and saves/restores window bounds persistently in
  14. // multi-displays scenario. It will observe and restore window bounds
  15. // persistently on screen rotation as well.
  16. class ASH_EXPORT PersistentWindowController : public display::DisplayObserver,
  17. public SessionObserver {
  18. public:
  19. // Public so it can be used by unit tests.
  20. constexpr static char kNumOfWindowsRestoredOnDisplayAdded[] =
  21. "Ash.PersistentWindow.NumOfWindowsRestoredOnDisplayAdded";
  22. constexpr static char kNumOfWindowsRestoredOnScreenRotation[] =
  23. "Ash.PersistentWindow.NumOfWindowsRestoredOnScreenRotation";
  24. PersistentWindowController();
  25. PersistentWindowController(const PersistentWindowController&) = delete;
  26. PersistentWindowController& operator=(const PersistentWindowController&) =
  27. delete;
  28. ~PersistentWindowController() override;
  29. private:
  30. // display::DisplayObserver:
  31. void OnWillProcessDisplayChanges() override;
  32. void OnDisplayAdded(const display::Display& new_display) override;
  33. void OnDisplayRemoved(const display::Display& old_display) override;
  34. void OnDisplayMetricsChanged(const display::Display& display,
  35. uint32_t changed_metrics) override;
  36. void OnDidProcessDisplayChanges() override;
  37. // SessionObserver:
  38. void OnFirstSessionStarted() override;
  39. // Called when restoring persistent window placement on display added.
  40. void MaybeRestorePersistentWindowBoundsOnDisplayAdded();
  41. // Called when restoring persistent window placement on screen rotation.
  42. void MaybeRestorePersistentWindowBoundsOnScreenRotation();
  43. // Callback binded on display added and run on display changes are processed.
  44. base::OnceClosure display_added_restore_callback_;
  45. // Callback binded on display rotation happens and run on display changes are
  46. // processed.
  47. base::OnceClosure screen_rotation_restore_callback_;
  48. // Temporary storage that stores windows that may need persistent info
  49. // stored on display removal. Cleared when display changes are processed.
  50. aura::WindowTracker need_persistent_info_windows_;
  51. // Tracking the screen orientation of each display before screen rotation
  52. // take effect. Key is the display id, value is true if the display is in
  53. // the landscape orientation, otherwise false. This is used to help restore
  54. // windows' bounds on screen rotation. It is needed since the target rotation
  55. // already changed even inside OnWillProcessDisplayChanges, which means the
  56. // screen orientation checked there will be the updated orientation when
  57. // screen rotation happens. So we get the initial screen orientation
  58. // OnFirstSessionStarted and store the updated ones inside
  59. // OnDidProcessDisplayChanges.
  60. std::unordered_map<int64_t, bool> is_landscape_orientation_map_;
  61. // Register for DisplayObserver callbacks.
  62. display::ScopedDisplayObserver display_observer_{this};
  63. };
  64. } // namespace ash
  65. #endif // ASH_DISPLAY_PERSISTENT_WINDOW_CONTROLLER_H_