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