notification_manager.cc 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // Copyright 2017 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/keyboard/ui/notification_manager.h"
  5. #include "ash/public/cpp/keyboard/keyboard_controller_observer.h"
  6. #include "base/observer_list.h"
  7. #include "ui/gfx/geometry/rect.h"
  8. namespace keyboard {
  9. template <typename T>
  10. ValueNotificationConsolidator<T>::ValueNotificationConsolidator(
  11. const T& initial_value)
  12. : value_(initial_value) {}
  13. template <typename T>
  14. bool ValueNotificationConsolidator<T>::ShouldSendNotification(
  15. const T& new_value) {
  16. const bool value_changed = new_value != value_;
  17. if (value_changed) {
  18. value_ = new_value;
  19. }
  20. return value_changed;
  21. }
  22. NotificationManager::NotificationManager()
  23. : visibility_(false),
  24. visual_bounds_(gfx::Rect()),
  25. occluded_bounds_(gfx::Rect()),
  26. workspace_displaced_bounds_(gfx::Rect()) {}
  27. void NotificationManager::SendNotifications(
  28. bool does_occluded_bounds_affect_layout,
  29. const gfx::Rect& visual_bounds,
  30. const gfx::Rect& occluded_bounds,
  31. const base::ObserverList<ash::KeyboardControllerObserver>::Unchecked&
  32. observers) {
  33. bool is_visible = !visual_bounds.IsEmpty();
  34. bool send_visibility_notification =
  35. ShouldSendVisibilityNotification(is_visible);
  36. bool send_visual_bounds_notification =
  37. ShouldSendVisualBoundsNotification(visual_bounds);
  38. bool send_occluded_bounds_notification =
  39. ShouldSendOccludedBoundsNotification(occluded_bounds);
  40. const gfx::Rect workspace_layout_offset_region =
  41. does_occluded_bounds_affect_layout ? occluded_bounds : gfx::Rect();
  42. bool send_displaced_bounds_notification =
  43. ShouldSendWorkspaceDisplacementBoundsNotification(
  44. workspace_layout_offset_region);
  45. ash::KeyboardStateDescriptor state;
  46. state.is_visible = is_visible;
  47. state.visual_bounds = visual_bounds;
  48. state.occluded_bounds_in_screen = occluded_bounds;
  49. state.displaced_bounds_in_screen = workspace_layout_offset_region;
  50. for (auto& observer : observers) {
  51. if (send_visibility_notification)
  52. observer.OnKeyboardVisibilityChanged(is_visible);
  53. if (send_visual_bounds_notification)
  54. observer.OnKeyboardVisibleBoundsChanged(visual_bounds);
  55. if (send_occluded_bounds_notification)
  56. observer.OnKeyboardOccludedBoundsChanged(occluded_bounds);
  57. if (send_displaced_bounds_notification) {
  58. observer.OnKeyboardDisplacingBoundsChanged(
  59. workspace_layout_offset_region);
  60. }
  61. observer.OnKeyboardAppearanceChanged(state);
  62. }
  63. }
  64. bool NotificationManager::ShouldSendVisibilityNotification(
  65. bool current_visibility) {
  66. return visibility_.ShouldSendNotification(current_visibility);
  67. }
  68. bool NotificationManager::ShouldSendVisualBoundsNotification(
  69. const gfx::Rect& new_bounds) {
  70. return visual_bounds_.ShouldSendNotification(
  71. CanonicalizeEmptyRectangles(new_bounds));
  72. }
  73. bool NotificationManager::ShouldSendOccludedBoundsNotification(
  74. const gfx::Rect& new_bounds) {
  75. return occluded_bounds_.ShouldSendNotification(
  76. CanonicalizeEmptyRectangles(new_bounds));
  77. }
  78. bool NotificationManager::ShouldSendWorkspaceDisplacementBoundsNotification(
  79. const gfx::Rect& new_bounds) {
  80. return workspace_displaced_bounds_.ShouldSendNotification(
  81. CanonicalizeEmptyRectangles(new_bounds));
  82. }
  83. gfx::Rect NotificationManager::CanonicalizeEmptyRectangles(
  84. const gfx::Rect& rect) const {
  85. if (rect.IsEmpty()) {
  86. return gfx::Rect();
  87. }
  88. return rect;
  89. }
  90. } // namespace keyboard