summaryrefslogtreecommitdiff
path: root/chromium/ui/keyboard/notification_manager.cc
blob: 4838d2b3fc256985c76befa6458a4be6d5658e49 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ui/keyboard/notification_manager.h"
#include "base/observer_list.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/keyboard/keyboard_controller_observer.h"

namespace keyboard {

template <typename T>
bool ValueNotificationConsolidator<T>::ShouldSendNotification(
    const T new_value) {
  if (never_sent_) {
    value_ = new_value;
    never_sent_ = false;
    return true;
  }
  const bool value_changed = new_value != value_;
  if (value_changed) {
    value_ = new_value;
  }
  return value_changed;
}

NotificationManager::NotificationManager() {}

void NotificationManager::SendNotifications(
    bool bounds_obscure_usable_region,
    bool bounds_affect_layout,
    bool is_locked,
    const gfx::Rect& bounds,
    const base::ObserverList<KeyboardControllerObserver>& observers) {
  bool is_available = !bounds.IsEmpty();
  bool send_availability_notification =
      ShouldSendAvailabilityNotification(is_available);

  bool send_visual_bounds_notification =
      ShouldSendVisualBoundsNotification(bounds);

  const gfx::Rect occluded_region =
      bounds_obscure_usable_region ? bounds : gfx::Rect();
  bool send_occluded_bounds_notification =
      ShouldSendOccludedBoundsNotification(occluded_region);

  const gfx::Rect workspace_layout_offset_region =
      bounds_affect_layout ? bounds : gfx::Rect();
  bool send_displaced_bounds_notification =
      ShouldSendWorkspaceDisplacementBoundsNotification(
          workspace_layout_offset_region);

  KeyboardStateDescriptor state;
  state.is_available = is_available;
  state.is_locked = is_locked;
  state.visual_bounds = bounds;
  state.occluded_bounds = occluded_region;
  state.displaced_bounds = workspace_layout_offset_region;

  for (KeyboardControllerObserver& observer : observers) {
    if (send_availability_notification)
      observer.OnKeyboardAvailabilityChanged(is_available);

    if (send_visual_bounds_notification)
      observer.OnKeyboardVisibleBoundsChanged(bounds);

    if (send_occluded_bounds_notification)
      observer.OnKeyboardWorkspaceOccludedBoundsChanged(occluded_region);

    if (send_displaced_bounds_notification) {
      observer.OnKeyboardWorkspaceDisplacingBoundsChanged(
          workspace_layout_offset_region);
    }

    observer.OnKeyboardAppearanceChanged(state);
  }
}

bool NotificationManager::ShouldSendAvailabilityNotification(
    bool current_availability) {
  return availability_.ShouldSendNotification(current_availability);
}

bool NotificationManager::ShouldSendVisualBoundsNotification(
    const gfx::Rect& new_bounds) {
  return visual_bounds_.ShouldSendNotification(
      CanonicalizeEmptyRectangles(new_bounds));
}

bool NotificationManager::ShouldSendOccludedBoundsNotification(
    const gfx::Rect& new_bounds) {
  return occluded_bounds_.ShouldSendNotification(
      CanonicalizeEmptyRectangles(new_bounds));
}

bool NotificationManager::ShouldSendWorkspaceDisplacementBoundsNotification(
    const gfx::Rect& new_bounds) {
  return workspace_displaced_bounds_.ShouldSendNotification(
      CanonicalizeEmptyRectangles(new_bounds));
}

const gfx::Rect NotificationManager::CanonicalizeEmptyRectangles(
    const gfx::Rect rect) const {
  if (rect.IsEmpty()) {
    return gfx::Rect();
  }
  return rect;
}

}  // namespace keyboard