Flutter Windows Embedder
text_input_manager.h
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef FLUTTER_SHELL_PLATFORM_WINDOWS_TEXT_INPUT_MANAGER_H_
6 #define FLUTTER_SHELL_PLATFORM_WINDOWS_TEXT_INPUT_MANAGER_H_
7 
8 #include <Windows.h>
9 #include <Windowsx.h>
10 
11 #include <optional>
12 #include <string>
13 
14 #include "flutter/fml/macros.h"
16 
17 namespace flutter {
18 
19 // Management interface for IME-based text input on Windows.
20 //
21 // When inputting text in CJK languages, text is entered via a multi-step
22 // process, where direct keyboard input is buffered into a composing string,
23 // which is then converted into the desired characters by selecting from a
24 // candidates list and committing the change to the string.
25 //
26 // This implementation wraps the Win32 IMM32 APIs and provides a mechanism for
27 // creating and positioning the IME window, a system caret, and the candidates
28 // list as well as for accessing composing and results string contents.
30  public:
31  TextInputManager() noexcept = default;
32  virtual ~TextInputManager() = default;
33 
34  // Sets the window handle with which the IME is associated.
35  void SetWindowHandle(HWND window_handle);
36 
37  // Creates a new IME window and system caret.
38  //
39  // This method should be invoked in response to the WM_IME_SETCONTEXT and
40  // WM_IME_STARTCOMPOSITION events.
41  void CreateImeWindow();
42 
43  // Destroys the current IME window and system caret.
44  //
45  // This method should be invoked in response to the WM_IME_ENDCOMPOSITION
46  // event.
47  void DestroyImeWindow();
48 
49  // Updates the current IME window and system caret position.
50  //
51  // This method should be invoked when handling user input via
52  // WM_IME_COMPOSITION events.
53  void UpdateImeWindow();
54 
55  // Updates the current IME window and system caret position.
56  //
57  // This method should be invoked when handling cursor position/size updates.
58  void UpdateCaretRect(const Rect& rect);
59 
60  // Returns the cursor position relative to the start of the composing range.
61  virtual long GetComposingCursorPosition() const;
62 
63  // Returns the contents of the composing string.
64  //
65  // This may be called in response to WM_IME_COMPOSITION events where the
66  // GCS_COMPSTR flag is set in the lparam. In some IMEs, this string may also
67  // be set in events where the GCS_RESULTSTR flag is set. This contains the
68  // in-progress composing string.
69  virtual std::optional<std::u16string> GetComposingString() const;
70 
71  // Returns the contents of the result string.
72  //
73  // This may be called in response to WM_IME_COMPOSITION events where the
74  // GCS_RESULTSTR flag is set in the lparam. This contains the final string to
75  // be committed in the composing region when composition is ended.
76  virtual std::optional<std::u16string> GetResultString() const;
77 
78  /// Aborts IME composing.
79  ///
80  /// Aborts composing, closes the candidates window, and clears the contents
81  /// of the composing string.
82  void AbortComposing();
83 
84  private:
85  // Returns either the composing string or result string based on the value of
86  // the |type| parameter.
87  std::optional<std::u16string> GetString(int type) const;
88 
89  // Moves the IME composing and candidates windows to the current caret
90  // position.
91  void MoveImeWindow(HIMC imm_context);
92 
93  // The window with which the IME windows are associated.
94  HWND window_handle_ = nullptr;
95 
96  // True if IME-based composing is active.
97  bool ime_active_ = false;
98 
99  // The system caret rect.
100  Rect caret_rect_ = {{0, 0}, {0, 0}};
101 
102  FML_DISALLOW_COPY_AND_ASSIGN(TextInputManager);
103 };
104 
105 } // namespace flutter
106 
107 #endif // FLUTTER_SHELL_PLATFORM_WINDOWS_TEXT_INPUT_MANAGER_H_
flutter::TextInputManager::GetResultString
virtual std::optional< std::u16string > GetResultString() const
Definition: text_input_manager.cc:122
geometry.h
flutter::TextInputManager::CreateImeWindow
void CreateImeWindow()
Definition: text_input_manager.cc:51
flutter::TextInputManager::UpdateCaretRect
void UpdateCaretRect(const Rect &rect)
Definition: text_input_manager.cc:91
flutter::TextInputManager::~TextInputManager
virtual ~TextInputManager()=default
flutter::Rect
Definition: geometry.h:56
flutter::TextInputManager::GetComposingCursorPosition
virtual long GetComposingCursorPosition() const
Definition: text_input_manager.cc:104
flutter::TextInputManager
Definition: text_input_manager.h:29
flutter
Definition: accessibility_bridge_windows.cc:11
type
enum flutter::testing::@88::KeyboardChange::Type type
flutter::TextInputManager::AbortComposing
void AbortComposing()
Definition: text_input_manager.cc:126
flutter::TextInputManager::GetComposingString
virtual std::optional< std::u16string > GetComposingString() const
Definition: text_input_manager.cc:118
flutter::TextInputManager::TextInputManager
TextInputManager() noexcept=default
flutter::TextInputManager::DestroyImeWindow
void DestroyImeWindow()
Definition: text_input_manager.cc:68
flutter::TextInputManager::SetWindowHandle
void SetWindowHandle(HWND window_handle)
Definition: text_input_manager.cc:47
flutter::TextInputManager::UpdateImeWindow
void UpdateImeWindow()
Definition: text_input_manager.cc:80