File: notification_manager.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (113 lines) | stat: -rw-r--r-- 3,606 bytes parent folder | download | duplicates (6)
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
111
112
113
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

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

namespace keyboard {

template <typename T>
ValueNotificationConsolidator<T>::ValueNotificationConsolidator(
    const T& initial_value)
    : value_(initial_value) {}

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

NotificationManager::NotificationManager()
    : visibility_(false),
      visual_bounds_(gfx::Rect()),
      occluded_bounds_(gfx::Rect()),
      workspace_displaced_bounds_(gfx::Rect()) {}

void NotificationManager::SendNotifications(
    bool does_occluded_bounds_affect_layout,
    const gfx::Rect& visual_bounds,
    const gfx::Rect& occluded_bounds,
    bool is_temporary,
    const base::ObserverList<ash::KeyboardControllerObserver>::Unchecked&
        observers) {
  bool is_visible = !visual_bounds.IsEmpty();
  bool send_visibility_notification =
      ShouldSendVisibilityNotification(is_visible);

  bool send_visual_bounds_notification =
      ShouldSendVisualBoundsNotification(visual_bounds);

  bool send_occluded_bounds_notification =
      ShouldSendOccludedBoundsNotification(occluded_bounds);

  const gfx::Rect workspace_layout_offset_region =
      does_occluded_bounds_affect_layout ? occluded_bounds : gfx::Rect();
  bool send_displaced_bounds_notification =
      ShouldSendWorkspaceDisplacementBoundsNotification(
          workspace_layout_offset_region);

  ash::KeyboardStateDescriptor state;
  state.is_visible = is_visible;
  state.is_temporary = is_temporary;
  state.visual_bounds = visual_bounds;
  state.occluded_bounds_in_screen = occluded_bounds;
  state.displaced_bounds_in_screen = workspace_layout_offset_region;

  for (auto& observer : observers) {
    if (send_visibility_notification)
      observer.OnKeyboardVisibilityChanged(is_visible);

    if (send_visual_bounds_notification)
      observer.OnKeyboardVisibleBoundsChanged(visual_bounds);

    if (send_occluded_bounds_notification)
      observer.OnKeyboardOccludedBoundsChanged(occluded_bounds);

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

    observer.OnKeyboardAppearanceChanged(state);
  }
}

bool NotificationManager::ShouldSendVisibilityNotification(
    bool current_visibility) {
  return visibility_.ShouldSendNotification(current_visibility);
}

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));
}

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

}  // namespace keyboard