File: chrome_keyboard_bounds_observer.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 (246 lines) | stat: -rw-r--r-- 8,276 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ui/ash/keyboard/chrome_keyboard_bounds_observer.h"

#include "ash/constants/ash_features.h"
#include "ash/public/cpp/shell_window_ids.h"
#include "ash/root_window_controller.h"
#include "chrome/browser/apps/platform_apps/app_window_registry_util.h"
#include "content/public/browser/render_widget_host.h"
#include "content/public/browser/render_widget_host_iterator.h"
#include "content/public/browser/render_widget_host_view.h"
#include "extensions/browser/app_window/app_window.h"
#include "ui/aura/window.h"
#include "ui/base/ime/ash/ime_bridge.h"
#include "ui/base/ime/input_method.h"
#include "ui/base/ime/mojom/virtual_keyboard_types.mojom.h"
#include "ui/base/ime/text_input_client.h"
#include "ui/base/ui_base_features.h"
#include "ui/display/display.h"
#include "ui/display/screen.h"
#include "ui/gfx/geometry/insets.h"
#include "ui/views/view.h"
#include "ui/views/widget/widget.h"

namespace {

// Returns the RenderWidgetHostView contained by |window|.
content::RenderWidgetHostView* GetHostViewForWindow(aura::Window* window) {
  std::unique_ptr<content::RenderWidgetHostIterator> hosts(
      content::RenderWidgetHost::GetRenderWidgetHosts());
  while (content::RenderWidgetHost* host = hosts->GetNextHost()) {
    content::RenderWidgetHostView* view = host->GetView();
    if (view && window->Contains(view->GetNativeView())) {
      return view;
    }
  }
  return nullptr;
}

ui::InputMethod* GetCurrentInputMethod() {
  ash::IMEBridge* bridge = ash::IMEBridge::Get();
  if (bridge && bridge->GetInputContextHandler()) {
    return bridge->GetInputContextHandler()->GetInputMethod();
  }
  return nullptr;
}

}  // namespace

ChromeKeyboardBoundsObserver::ChromeKeyboardBoundsObserver(
    aura::Window* keyboard_window)
    : keyboard_window_(keyboard_window) {
  DCHECK(keyboard_window_);
  ChromeKeyboardControllerClient::Get()->AddObserver(this);
}

ChromeKeyboardBoundsObserver::~ChromeKeyboardBoundsObserver() {
  UpdateOccludedBounds(gfx::Rect());

  RemoveAllObservedWindows();

  ChromeKeyboardControllerClient::Get()->RemoveObserver(this);
  CHECK(!views::WidgetObserver::IsInObserverList());
}

void ChromeKeyboardBoundsObserver::OnKeyboardVisibleBoundsChanged(
    const gfx::Rect& screen_bounds) {
  std::unique_ptr<content::RenderWidgetHostIterator> hosts(
      content::RenderWidgetHost::GetRenderWidgetHosts());

  while (content::RenderWidgetHost* host = hosts->GetNextHost()) {
    content::RenderWidgetHostView* view = host->GetView();
    if (view) {
      view->NotifyVirtualKeyboardOverlayRect(screen_bounds);
    }
  }
}

void ChromeKeyboardBoundsObserver::OnKeyboardOccludedBoundsChanged(
    const gfx::Rect& screen_bounds) {
  DVLOG(1) << "OnKeyboardOccludedBoundsChanged: " << screen_bounds.ToString();
  UpdateOccludedBounds(
      ChromeKeyboardControllerClient::Get()->IsKeyboardOverscrollEnabled()
          ? screen_bounds
          : gfx::Rect());
}

void ChromeKeyboardBoundsObserver::UpdateOccludedBounds(
    const gfx::Rect& screen_bounds) {
  DVLOG(1) << "UpdateOccludedBounds: " << screen_bounds.ToString();
  occluded_bounds_in_screen_ = screen_bounds;

  std::unique_ptr<content::RenderWidgetHostIterator> hosts(
      content::RenderWidgetHost::GetRenderWidgetHosts());

  // If the keyboard is hidden or floating then reset the insets for all
  // RenderWidgetHosts and remove observers.
  if (occluded_bounds_in_screen_.IsEmpty()) {
    while (content::RenderWidgetHost* host = hosts->GetNextHost()) {
      content::RenderWidgetHostView* view = host->GetView();
      if (view) {
        view->SetInsets(gfx::Insets());
      }
    }
    RemoveAllObservedWindows();
    return;
  }

  // Adjust the height of the viewport for visible windows on the primary
  // display. TODO(kevers): Add EnvObserver to properly initialize insets if a
  // window is created while the keyboard is visible.
  while (content::RenderWidgetHost* host = hosts->GetNextHost()) {
    content::RenderWidgetHostView* view = host->GetView();
    // Can be null, e.g. if the RenderWidget is being destroyed or
    // the render process crashed.
    if (!view) {
      continue;
    }

    aura::Window* window = view->GetNativeView();
    // Added while we determine if RenderWidgetHostViewChildFrame can be
    // changed to always return a non-null value: https://crbug.com/644726.
    // If we cannot guarantee a non-null value, then this may need to stay.
    if (!window) {
      continue;
    }

    if (!ShouldWindowOverscroll(window)) {
      continue;
    }

    UpdateInsets(window, view);
    AddObservedWindow(window);
  }

  // Window reshape can race with the IME trying to keep the text input caret
  // visible. Do this here because the widget bounds change happens before the
  // occluded bounds are updated. https://crbug.com/937722
  ui::InputMethod* ime = GetCurrentInputMethod();
  if (ime && ime->GetTextInputClient()) {
    ime->GetTextInputClient()->EnsureCaretNotInRect(occluded_bounds_in_screen_);
  }
}

void ChromeKeyboardBoundsObserver::AddObservedWindow(aura::Window* window) {
  // Only observe top level widget.
  views::Widget* widget =
      views::Widget::GetWidgetForNativeView(window->GetToplevelWindow());
  if (!widget || widget->HasObserver(this)) {
    return;
  }

  widget->AddObserver(this);
  observed_widgets_.insert(widget);
}

void ChromeKeyboardBoundsObserver::RemoveAllObservedWindows() {
  for (views::Widget* widget : observed_widgets_) {
    widget->RemoveObserver(this);
  }
  observed_widgets_.clear();
}

void ChromeKeyboardBoundsObserver::OnWidgetBoundsChanged(
    views::Widget* widget,
    const gfx::Rect& new_bounds) {
  DVLOG(1) << "OnWidgetBoundsChanged: " << widget->GetName() << " "
           << new_bounds.ToString();

  aura::Window* window = widget->GetNativeView();
  if (!ShouldWindowOverscroll(window)) {
    return;
  }

  content::RenderWidgetHostView* host_view = GetHostViewForWindow(window);
  if (!host_view) {
    return;  // Transition edge case
  }

  UpdateInsets(window, host_view);
}

void ChromeKeyboardBoundsObserver::OnWidgetDestroying(views::Widget* widget) {
  if (widget->HasObserver(this)) {
    widget->RemoveObserver(this);
  }
  observed_widgets_.erase(widget);
}

void ChromeKeyboardBoundsObserver::UpdateInsets(
    aura::Window* window,
    content::RenderWidgetHostView* view) {
  if (view->GetVirtualKeyboardMode() ==
      ui::mojom::VirtualKeyboardMode::kOverlaysContent) {
    view->SetInsets(gfx::Insets());
    return;
  }
  gfx::Rect view_bounds_in_screen = view->GetViewBounds();
  if (!ShouldEnableInsets(window)) {
    DVLOG(2) << "ResetInsets: " << window->GetName()
             << " Bounds: " << view_bounds_in_screen.ToString();
    view->SetInsets(gfx::Insets());
    return;
  }
  gfx::Rect intersect =
      gfx::IntersectRects(view_bounds_in_screen, occluded_bounds_in_screen_);
  int overlap = intersect.height();
  DVLOG(2) << "SetInsets: " << window->GetName()
           << " Bounds: " << view_bounds_in_screen.ToString()
           << " Overlap: " << overlap;
  if (overlap > 0 && overlap < view_bounds_in_screen.height()) {
    view->SetInsets(gfx::Insets::TLBR(0, 0, overlap, 0));
  } else {
    view->SetInsets(gfx::Insets());
  }
}

bool ChromeKeyboardBoundsObserver::ShouldWindowOverscroll(
    aura::Window* window) {
  // The virtual keyboard should not overscroll.
  if (window->GetToplevelWindow() == keyboard_window_->GetToplevelWindow()) {
    return false;
  }

  // IME windows should not overscroll.
  extensions::AppWindow* app_window =
      AppWindowRegistryUtil::GetAppWindowForNativeWindowAnyProfile(
          window->GetToplevelWindow());
  if (app_window && app_window->is_ime_window()) {
    return false;
  }

  return true;
}

bool ChromeKeyboardBoundsObserver::ShouldEnableInsets(aura::Window* window) {
  if (!keyboard_window_->IsVisible() ||
      !ChromeKeyboardControllerClient::Get()->IsKeyboardOverscrollEnabled()) {
    return false;
  }
  const auto* screen = display::Screen::GetScreen();
  return screen->GetDisplayNearestWindow(window).id() ==
         screen->GetDisplayNearestWindow(keyboard_window_).id();
}