File: text_input_target.h

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 (114 lines) | stat: -rw-r--r-- 4,525 bytes parent folder | download | duplicates (8)
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
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef UI_BASE_IME_ASH_TEXT_INPUT_TARGET_H_
#define UI_BASE_IME_ASH_TEXT_INPUT_TARGET_H_

#include <stdint.h>

#include <string>
#include <string_view>

#include "base/component_export.h"
#include "base/functional/callback.h"
#include "ui/base/ime/composition_text.h"
#include "ui/base/ime/input_method.h"
#include "ui/base/ime/text_input_client.h"
#include "ui/events/event.h"

namespace ash {

struct SurroundingTextInfo {
  std::u16string surrounding_text;

  // This is relative to the beginning of |surrounding_text|.
  gfx::Range selection_range;

  // Offset of the surrounding_text in the field in UTF-16.
  size_t offset;
};

// An interface representing an input target that supports text editing via a
// TextInputMethod. Applications like Chrome browser, Android apps, Linux apps
// should all implement this interface in order to support TextInputMethods.
// All strings related to IME operations should be UTF-16 encoded and all
// indices/ranges relative to those strings should be UTF-16 code units.
class COMPONENT_EXPORT(UI_BASE_IME_ASH) TextInputTarget {
 public:
  using SetAutocorrectRangeDoneCallback = base::OnceCallback<void(bool)>;

  // Called when the engine commit a text.
  virtual void CommitText(
      const std::u16string& text,
      ui::TextInputClient::InsertTextCursorBehavior cursor_behavior) = 0;

  // Called when the engine changes the composition range.
  // Returns true if the operation was successful.
  // If |text_spans| is empty, then this function uses a default span that
  // spans across the new composition range.
  virtual bool SetCompositionRange(
      uint32_t before,
      uint32_t after,
      const std::vector<ui::ImeTextSpan>& text_spans) = 0;
  virtual bool SetComposingRange(
      uint32_t start,
      uint32_t end,
      const std::vector<ui::ImeTextSpan>& text_spans) = 0;
  virtual gfx::Range GetAutocorrectRange() = 0;

  // Sets the autocorrect range to be `range`.
  // Actual implementation must call |callback| and notify if the autocorrect
  // range is set successfully.
  virtual void SetAutocorrectRange(
      const gfx::Range& range,
      SetAutocorrectRangeDoneCallback callback) = 0;
  virtual std::optional<ui::GrammarFragment> GetGrammarFragmentAtCursor() = 0;
  virtual bool ClearGrammarFragments(const gfx::Range& range) = 0;
  virtual bool AddGrammarFragments(
      const std::vector<ui::GrammarFragment>& fragements) = 0;

  // Called when the engine updates composition text.
  virtual void UpdateCompositionText(const ui::CompositionText& text,
                                     uint32_t cursor_pos,
                                     bool visible) = 0;

  // Called when the engine request deleting surrounding string.
  virtual void DeleteSurroundingText(uint32_t num_char16s_before_cursor,
                                     uint32_t num_char16s_after_cursor) = 0;

  // Deletes any active composition, and the current selection plus the
  // specified number of char16 values before and after the selection, and
  // replaces it with |replacement_string|.
  // Places the cursor at the end of |replacement_string|.
  virtual void ReplaceSurroundingText(uint32_t length_before_selection,
                                      uint32_t length_after_selection,
                                      std::u16string_view replacement_text) = 0;

  // Called from the extension API.
  // WARNING: This could return a stale cache that doesn't reflect reality, due
  // to async-ness between browser-process IMF and render-process
  // `ui::TextInputClient`.
  // TODO(crbug/1194424): Ensure this always returns accurate result.
  virtual SurroundingTextInfo GetSurroundingTextInfo() = 0;

  // Called when the engine sends a key event.
  virtual void SendKeyEvent(ui::KeyEvent* event) = 0;

  // Gets the input method pointer.
  virtual ui::InputMethod* GetInputMethod() = 0;

  // Commits the current composition and keeps the selection unchanged.
  // Set |reset_engine| to false if this was triggered from the extension.
  virtual void ConfirmComposition(bool reset_engine) = 0;

  // Returns true if there is any composition text.
  virtual bool HasCompositionText() = 0;

  // Returns the ukm::SourceId that identifies the currently focused client.
  virtual ukm::SourceId GetClientSourceForMetrics() = 0;
};

}  // namespace ash

#endif  // UI_BASE_IME_ASH_TEXT_INPUT_TARGET_H_