File: fake_text_input_client.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (276 lines) | stat: -rw-r--r-- 7,705 bytes parent folder | download | duplicates (5)
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
// 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 "ui/base/ime/fake_text_input_client.h"

#include "base/check_op.h"
#include "build/build_config.h"
#include "ui/base/ime/input_method.h"
#include "ui/events/event_constants.h"
#include "ui/gfx/geometry/rect.h"

namespace ui {

FakeTextInputClient::FakeTextInputClient(TextInputType text_input_type)
    : text_input_type_(text_input_type) {}

FakeTextInputClient::FakeTextInputClient(Options options)
    : FakeTextInputClient(/*input_method=*/nullptr, std::move(options)) {}

FakeTextInputClient::FakeTextInputClient(InputMethod* input_method,
                                         Options options)
    : input_method_(input_method),
      text_input_type_(options.type),
      mode_(options.mode),
      flags_(options.flags),
      can_insert_image_(options.can_insert_image),
      caret_bounds_(options.caret_bounds),
      should_do_learning_(options.should_do_learning) {}

FakeTextInputClient::~FakeTextInputClient() {
  Blur();
}

void FakeTextInputClient::set_text_input_type(TextInputType text_input_type) {
  text_input_type_ = text_input_type;
}

void FakeTextInputClient::set_source_id(ukm::SourceId source_id) {
  source_id_ = source_id;
}

void FakeTextInputClient::SetTextAndSelection(const std::u16string& text,
                                              gfx::Range selection) {
  DCHECK_LE(selection_.end(), text.length());
  text_ = text;
  selection_ = selection;
}

void FakeTextInputClient::Focus() {
  if (input_method_ != nullptr) {
    input_method_->SetFocusedTextInputClient(this);
  }
}

void FakeTextInputClient::Blur() {
  if (input_method_ != nullptr) {
    input_method_->SetFocusedTextInputClient(nullptr);
  }
}

base::WeakPtr<ui::TextInputClient> FakeTextInputClient::AsWeakPtr() {
  return weak_ptr_factory_.GetWeakPtr();
}

void FakeTextInputClient::SetCompositionText(
    const CompositionText& composition) {
  text_.insert(selection_.start(), composition.text);
  const size_t new_cursor = selection_.start() + composition.text.length();
  composition_range_ = gfx::Range(selection_.start(), new_cursor);
  selection_ = gfx::Range(new_cursor, new_cursor);
}

size_t FakeTextInputClient::ConfirmCompositionText(bool keep_selection) {
  return std::numeric_limits<size_t>::max();
}

void FakeTextInputClient::ClearCompositionText() {}

void FakeTextInputClient::InsertText(
    const std::u16string& text,
    TextInputClient::InsertTextCursorBehavior cursor_behavior) {
  const gfx::Range replacement_range =
      composition_range_.is_empty() ? selection_ : composition_range_;

  text_.replace(replacement_range.start(), replacement_range.length(), text);

  if (cursor_behavior ==
      TextInputClient::InsertTextCursorBehavior::kMoveCursorAfterText) {
    selection_ = gfx::Range(replacement_range.start() + text.length(),
                            replacement_range.start() + text.length());
  }

  composition_range_ = gfx::Range();
}

void FakeTextInputClient::InsertChar(const KeyEvent& event) {}

bool FakeTextInputClient::CanInsertImage() {
  return can_insert_image_;
}

void FakeTextInputClient::InsertImage(const GURL& src) {
  last_inserted_image_url_ = src;
}

TextInputType FakeTextInputClient::GetTextInputType() const {
  return text_input_type_;
}

TextInputMode FakeTextInputClient::GetTextInputMode() const {
  return mode_;
}

base::i18n::TextDirection FakeTextInputClient::GetTextDirection() const {
  return base::i18n::UNKNOWN_DIRECTION;
}

int FakeTextInputClient::GetTextInputFlags() const {
  return flags_;
}

void FakeTextInputClient::SetFlags(const int flags) {
  flags_ = flags;
}

void FakeTextInputClient::SetUrl(const GURL& url) {
  url_ = url;
}

bool FakeTextInputClient::CanComposeInline() const {
  return false;
}

gfx::Rect FakeTextInputClient::GetCaretBounds() const {
  return caret_bounds_;
}

gfx::Rect FakeTextInputClient::GetSelectionBoundingBox() const {
  return {};
}

#if BUILDFLAG(IS_WIN)
std::optional<gfx::Rect> FakeTextInputClient::GetProximateCharacterBounds(
    const gfx::Range& range) const {
  return std::nullopt;
}

std::optional<size_t> FakeTextInputClient::GetProximateCharacterIndexFromPoint(
    const gfx::Point& screen_point_in_dips,
    IndexFromPointFlags flags) const {
  return std::nullopt;
}
#endif  // BUILDFLAG(IS_WIN)

bool FakeTextInputClient::GetCompositionCharacterBounds(size_t index,
                                                        gfx::Rect* rect) const {
  return false;
}

bool FakeTextInputClient::HasCompositionText() const {
  return !composition_range_.is_empty();
}

ui::TextInputClient::FocusReason FakeTextInputClient::GetFocusReason() const {
  return ui::TextInputClient::FOCUS_REASON_MOUSE;
}

bool FakeTextInputClient::GetTextRange(gfx::Range* range) const {
  *range = gfx::Range(0, text_.length());
  return true;
}

bool FakeTextInputClient::GetCompositionTextRange(gfx::Range* range) const {
  return false;
}

bool FakeTextInputClient::GetEditableSelectionRange(gfx::Range* range) const {
  *range = selection_;
  return true;
}

bool FakeTextInputClient::SetEditableSelectionRange(const gfx::Range& range) {
  return false;
}

#if BUILDFLAG(IS_MAC)
bool FakeTextInputClient::DeleteRange(const gfx::Range& range) {
  return false;
}
#endif

bool FakeTextInputClient::GetTextFromRange(const gfx::Range& range,
                                           std::u16string* text) const {
  if (range.GetMax() <= text_.length()) {
    *text = text_.substr(range.GetMin(), range.length());
    return true;
  }
  return false;
}

void FakeTextInputClient::OnInputMethodChanged() {}

bool FakeTextInputClient::ChangeTextDirectionAndLayoutAlignment(
    base::i18n::TextDirection direction) {
  return false;
}

void FakeTextInputClient::ExtendSelectionAndDelete(size_t before,
                                                   size_t after) {}

void FakeTextInputClient::EnsureCaretNotInRect(const gfx::Rect& rect) {}

bool FakeTextInputClient::IsTextEditCommandEnabled(
    TextEditCommand command) const {
  return false;
}

void FakeTextInputClient::SetTextEditCommandForNextKeyEvent(
    TextEditCommand command) {}

ukm::SourceId FakeTextInputClient::GetClientSourceForMetrics() const {
  return source_id_;
}

bool FakeTextInputClient::ShouldDoLearning() {
  return should_do_learning_;
}

#if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
bool FakeTextInputClient::SetCompositionFromExistingText(
    const gfx::Range& range,
    const std::vector<ui::ImeTextSpan>& ui_ime_text_spans) {
  if (range.start() < 0 || range.end() > text_.length())
    return false;

  composition_range_ = range;
  ime_text_spans_ = ui_ime_text_spans;
  return true;
}
#endif

#if BUILDFLAG(IS_CHROMEOS)
gfx::Range FakeTextInputClient::GetAutocorrectRange() const {
  return autocorrect_range_;
}

gfx::Rect FakeTextInputClient::GetAutocorrectCharacterBounds() const {
  return {};
}

bool FakeTextInputClient::SetAutocorrectRange(const gfx::Range& range) {
  autocorrect_range_ = range;
  return true;
}
#endif

#if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_CHROMEOS)
void FakeTextInputClient::GetActiveTextInputControlLayoutBounds(
    std::optional<gfx::Rect>* control_bounds,
    std::optional<gfx::Rect>* selection_bounds) {}

ui::TextInputClient::EditingContext
FakeTextInputClient::GetTextEditingContext() {
  return EditingContext{.page_url = url_};
}
#endif

#if BUILDFLAG(IS_WIN)
void FakeTextInputClient::SetActiveCompositionForAccessibility(
    const gfx::Range& range,
    const std::u16string& active_composition_text,
    bool is_composition_committed) {}
#endif

}  // namespace ui