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
content / browser / renderer_host / virtual_keyboard_controller_win.cc [blame]
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/browser/renderer_host/virtual_keyboard_controller_win.h"
#include "base/trace_event/trace_event.h"
#include "content/browser/renderer_host/render_widget_host_view_aura.h"
#include "ui/base/ime/input_method.h"
#include "ui/base/ime/mojom/text_input_state.mojom.h"
#include "ui/base/ime/virtual_keyboard_controller.h"
#include "ui/events/event_utils.h"
#include "ui/gfx/geometry/rect.h"
namespace content {
VirtualKeyboardControllerWin::VirtualKeyboardControllerWin(
RenderWidgetHostViewAura* host_view,
ui::InputMethod* input_method)
: host_view_(host_view), input_method_(input_method) {
host_view_->SetInsets(gfx::Insets());
}
VirtualKeyboardControllerWin::~VirtualKeyboardControllerWin() {
if (observers_registered_) {
// De-register the input pane observers.
if (auto* controller = input_method_->GetVirtualKeyboardController())
controller->RemoveObserver(this);
}
}
void VirtualKeyboardControllerWin::HideAndNotifyKeyboardInset() {
if (auto* controller = input_method_->GetVirtualKeyboardController()) {
if (virtual_keyboard_shown_) {
// If the VK is already showing, then dismiss it first.
controller->DismissVirtualKeyboard();
// Should also scroll the content into view after the VK dismisses.
OnKeyboardHidden();
}
}
}
void VirtualKeyboardControllerWin::OnKeyboardVisible(
const gfx::Rect& keyboard_rect) {
TRACE_EVENT0("vk", "VirtualKeyboardControllerWin::OnKeyboardVisible");
// If the software input panel (SIP) is manually raised by the user, the flag
// should be set so we don't call TryShow API again.
virtual_keyboard_shown_ = true;
if (host_view_->GetVirtualKeyboardMode() !=
ui::mojom::VirtualKeyboardMode::kOverlaysContent) {
host_view_->SetInsets(gfx::Insets::TLBR(
0, 0, keyboard_rect.IsEmpty() ? 0 : keyboard_rect.height(), 0));
} else {
host_view_->NotifyVirtualKeyboardOverlayRect(keyboard_rect);
}
}
void VirtualKeyboardControllerWin::OnKeyboardHidden() {
TRACE_EVENT0("vk", "VirtualKeyboardControllerWin::OnKeyboardHidden");
// If the software input panel (SIP) is manually closed by the user, the flag
// should be reset so we don't call TryHide API again. Also,
// next time user taps on an editable element after manually dismissing the
// keyboard, this flag is used to determine whether TryShow needs to be
// called or not. Calling TryShow/TryHide multiple times leads to SIP
// flickering.
virtual_keyboard_shown_ = false;
if (host_view_->GetVirtualKeyboardMode() !=
ui::mojom::VirtualKeyboardMode::kOverlaysContent) {
// Restore the viewport.
host_view_->SetInsets(gfx::Insets());
} else {
host_view_->NotifyVirtualKeyboardOverlayRect(gfx::Rect());
}
}
void VirtualKeyboardControllerWin::ShowVirtualKeyboard() {
TRACE_EVENT0("vk", "VirtualKeyboardControllerWin::ShowVirtualKeyboard");
if (input_method_->GetVirtualKeyboardController()) {
if (!virtual_keyboard_shown_) {
virtual_keyboard_shown_ = true;
input_method_->SetVirtualKeyboardVisibilityIfEnabled(true);
}
}
}
void VirtualKeyboardControllerWin::HideVirtualKeyboard() {
TRACE_EVENT0("vk", "VirtualKeyboardControllerWin::HideVirtualKeyboard");
if (auto* controller = input_method_->GetVirtualKeyboardController()) {
if (virtual_keyboard_shown_) {
virtual_keyboard_shown_ = false;
controller->DismissVirtualKeyboard();
}
}
}
void VirtualKeyboardControllerWin::UpdateTextInputState(
const ui::mojom::TextInputState* state) {
// Conditions to show the VK:
// 1. User has to interact with the editable element.
// 2. Pointer type has to be either touch or pen.
// 3. Accessibility has set focus on an editable element.
// 4. If virtualkeyboardpolicy is manual, leave the SIP in its current state -
// script authors need to call show() or hide() explicitly to trigger SIP
// actions.
// 5. If virtualkeyboardpolicy is auto, show the SIP.
// If there are no keyboard controllers or the pointer type is neither pen or
// touch or accessibility has not set focus into an editable element, then
// don't change the state of the keyboard.
auto* controller = input_method_->GetVirtualKeyboardController();
is_manual_policy_ =
state->vk_policy == ui::mojom::VirtualKeyboardPolicy::MANUAL;
if (!controller ||
!(IsPointerTypeValidForVirtualKeyboard() || is_manual_policy_) ||
!host_view_->host()->GetView() || !host_view_->host()->delegate()) {
return;
}
// Register the observers if the pointer type is pen/touch.
if (!observers_registered_) {
controller->AddObserver(this);
observers_registered_ = true;
}
if (state->show_ime_if_needed &&
state->vk_policy == ui::mojom::VirtualKeyboardPolicy::AUTO) {
ShowVirtualKeyboard();
return;
}
if (is_manual_policy_) {
switch (state->last_vk_visibility_request) {
case ui::mojom::VirtualKeyboardVisibilityRequest::SHOW:
if (host_view_->FocusedFrameHasStickyActivation())
ShowVirtualKeyboard();
break;
case ui::mojom::VirtualKeyboardVisibilityRequest::HIDE:
HideVirtualKeyboard();
break;
default:
// Don't change the state of the VK.
break;
}
}
}
void VirtualKeyboardControllerWin::FocusedNodeChanged(bool is_editable) {
if (!is_editable) {
HideVirtualKeyboard();
return;
}
}
bool VirtualKeyboardControllerWin::IsPointerTypeValidForVirtualKeyboard()
const {
return (host_view_->GetLastPointerType() == ui::EventPointerType::kTouch ||
host_view_->GetLastPointerType() == ui::EventPointerType::kPen);
}
} // namespace content