Flutter Engine Uber Docs
Docs for the entire Flutter Engine repo.
 
Loading...
Searching...
No Matches
text_delegate.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/// Text editing functionality delegated from |PlatformView|.
6/// See |TextDelegate| for details.
7
8#ifndef FLUTTER_SHELL_PLATFORM_FUCHSIA_FLUTTER_TEXT_DELEGATE_H_
9#define FLUTTER_SHELL_PLATFORM_FUCHSIA_FLUTTER_TEXT_DELEGATE_H_
10
11#include <memory>
12
13#include <fuchsia/ui/input/cpp/fidl.h>
14#include <fuchsia/ui/input3/cpp/fidl.h>
15#include <fuchsia/ui/views/cpp/fidl.h>
16#include <lib/fidl/cpp/binding.h>
17
21
22namespace flutter_runner {
23
24/// The channel name used for text editing platofrm messages.
25constexpr char kTextInputChannel[] = "flutter/textinput";
26
27/// The channel name used for key event platform messages.
28constexpr char kKeyEventChannel[] = "flutter/keyevent";
29
30/// TextDelegate handles keyboard inpout and text editing.
31///
32/// It mediates between Fuchsia's input and Flutter's platform messages. When it
33/// is initialized, it contacts `fuchsia.ui.input.Keyboard` to register itself
34/// as listener of key events.
35///
36/// Whenever a text editing request comes from the
37/// Flutter app, it will activate Fuchsia's input method editor, and will send
38/// text edit actions coming from the Fuchsia platform over to the Flutter app,
39/// by converting FIDL messages (`fuchsia.ui.input.InputMethodEditorClient`
40/// calls) to appropriate text editing Flutter platform messages.
41///
42/// For details refer to:
43/// * Flutter side:
44/// https://api.flutter.dev/javadoc/io/flutter/embedding/engine/systemchannels/TextInputChannel.html
45/// * Fuchsia side: https://fuchsia.dev/reference/fidl/fuchsia.ui.input
46class TextDelegate : public fuchsia::ui::input3::KeyboardListener,
47 public fuchsia::ui::input::InputMethodEditorClient {
48 public:
49 /// Creates a new TextDelegate.
50 ///
51 /// Args:
52 /// view_ref: the reference to the app's view. Required for registration
53 /// with Fuchsia.
54 /// ime_service: a handle to Fuchsia's input method service.
55 /// keyboard: the keyboard listener, gets notified of key presses and
56 /// releases.
57 /// dispatch_callback: a function used to send a Flutter platform message.
58 TextDelegate(fuchsia::ui::views::ViewRef view_ref,
59 fuchsia::ui::input::ImeServiceHandle ime_service,
60 fuchsia::ui::input3::KeyboardHandle keyboard,
61 std::function<void(std::unique_ptr<flutter::PlatformMessage>)>
62 dispatch_callback);
63
64 /// |fuchsia.ui.input3.KeyboardListener|
65 /// Called by the embedder every time there is a key event to process.
66 void OnKeyEvent(fuchsia::ui::input3::KeyEvent key_event,
67 fuchsia::ui::input3::KeyboardListener::OnKeyEventCallback
68 callback) override;
69
70 /// |fuchsia::ui::input::InputMethodEditorClient|
71 /// Called by the embedder every time the edit state is updated.
72 void DidUpdateState(
73 fuchsia::ui::input::TextInputState state,
74 std::unique_ptr<fuchsia::ui::input::InputEvent> event) override;
75
76 /// |fuchsia::ui::input::InputMethodEditorClient|
77 /// Called by the embedder when the action key is pressed, and the requested
78 /// action is supplied to Flutter.
79 void OnAction(fuchsia::ui::input::InputMethodAction action) override;
80
81 /// Gets a new input method editor from the input connection. Run when both
82 /// Scenic has focus and Flutter has requested input with setClient.
83 void ActivateIme();
84
85 /// Detaches the input method editor connection, ending the edit session and
86 /// closing the onscreen keyboard. Call when input is no longer desired,
87 /// either because Scenic says we lost focus or when Flutter no longer has a
88 /// text field focused.
89 void DeactivateIme();
90
91 /// Channel handler for kTextInputChannel
93 std::unique_ptr<flutter::PlatformMessage> message);
94
95 /// Returns true if there is a text state (i.e. if some text editing is in
96 /// progress).
97 bool HasTextState() { return last_text_state_.has_value(); }
98
99 private:
100 // Activates the input method editor, assigning |action| to the "enter" key.
101 // This action will be reported by |OnAction| above when the "enter" key is
102 // pressed. Note that in the case of multi-line text editors, |OnAction| will
103 // never be called: instead, the text editor will insert a newline into the
104 // edited text.
105 void ActivateIme(fuchsia::ui::input::InputMethodAction action);
106
107 // Converts Fuchsia platform key codes into Flutter key codes.
108 Keyboard keyboard_translator_;
109
110 // A callback for sending a single Flutter platform message.
111 std::function<void(std::unique_ptr<flutter::PlatformMessage>)>
112 dispatch_callback_;
113
114 // TextDelegate server-side binding. Methods called when the text edit state
115 // is updated.
116 fidl::Binding<fuchsia::ui::input::InputMethodEditorClient> ime_client_;
117
118 // An interface for interacting with a text input control.
119 fuchsia::ui::input::InputMethodEditorPtr ime_;
120
121 // An interface for requesting the InputMethodEditor.
122 fuchsia::ui::input::ImeServicePtr text_sync_service_;
123
124 // The locally-unique identifier of the text input currently in use. Flutter
125 // usually uses only one at a time.
126 int current_text_input_client_ = 0;
127
128 // TextDelegate server side binding. Methods called when a key is pressed.
129 fidl::Binding<fuchsia::ui::input3::KeyboardListener>
130 keyboard_listener_binding_;
131
132 // The client-side stub for calling the Keyboard protocol.
133 fuchsia::ui::input3::KeyboardPtr keyboard_;
134
135 // last_text_state_ is the last state of the text input as reported by the IME
136 // or initialized by Flutter. We set it to null if Flutter doesn't want any
137 // input, since then there is no text input state at all.
138 // If nullptr, then no editing is in progress.
139 std::optional<fuchsia::ui::input::TextInputState> last_text_state_;
140
141 // The action that Flutter expects to happen when the user presses the "enter"
142 // key. For example, it could be `InputMethodAction::DONE` which would cause
143 // text editing to stop and the current text to be accepted.
144 // If set to std::nullopt, then no editing is in progress.
145 std::optional<fuchsia::ui::input::InputMethodAction> requested_text_action_;
146
148};
149
150} // namespace flutter_runner
151
152#endif // FLUTTER_SHELL_PLATFORM_FUCHSIA_FLUTTER_TEXT_DELEGATE_H_
void DidUpdateState(fuchsia::ui::input::TextInputState state, std::unique_ptr< fuchsia::ui::input::InputEvent > event) override
void OnKeyEvent(fuchsia::ui::input3::KeyEvent key_event, fuchsia::ui::input3::KeyboardListener::OnKeyEventCallback callback) override
void OnAction(fuchsia::ui::input::InputMethodAction action) override
bool HandleFlutterTextInputChannelPlatformMessage(std::unique_ptr< flutter::PlatformMessage > message)
Channel handler for kTextInputChannel.
const char * message
FlutterDesktopBinaryReply callback
#define FML_DISALLOW_COPY_AND_ASSIGN(TypeName)
Definition macros.h:27
FlutterKeyEvent key_event
constexpr char kTextInputChannel[]
The channel name used for text editing platofrm messages.
constexpr char kKeyEventChannel[]
The channel name used for key event platform messages.