11 #include "flutter/fml/string_conversion.h"
24 "TextInput.setEditableSizeAndTransform";
29 "TextInputClient.updateEditingState";
31 "TextInputClient.updateEditingStateWithDeltas";
41 static constexpr
char kViewId[] =
"viewId";
52 static constexpr
char kXKey[] =
"x";
53 static constexpr
char kYKey[] =
"y";
62 "Internal Consistency Error";
69 if (active_model_ ==
nullptr) {
72 std::u16string text_before_change =
73 fml::Utf8ToUtf16(active_model_->GetText());
74 TextRange selection_before_change = active_model_->selection();
75 active_model_->AddText(
text);
77 if (enable_delta_model) {
80 SendStateUpdateWithDelta(*active_model_, &delta);
82 SendStateUpdate(*active_model_);
92 if (active_model_ ==
nullptr) {
100 EnterPressed(active_model_.get());
115 active_model_(nullptr) {
116 channel_->SetMethodCallHandler(
120 HandleMethodCall(call, std::move(result));
127 if (active_model_ ==
nullptr) {
130 active_model_->BeginComposing();
131 if (enable_delta_model) {
132 std::string
text = active_model_->GetText();
133 TextRange selection = active_model_->selection();
135 SendStateUpdateWithDelta(*active_model_, &delta);
137 SendStateUpdate(*active_model_);
142 if (active_model_ ==
nullptr) {
145 std::string text_before_change = active_model_->GetText();
146 TextRange selection_before_change = active_model_->selection();
147 TextRange composing_before_change = active_model_->composing_range();
148 std::string composing_text_before_change = text_before_change.substr(
149 composing_before_change.
start(), composing_before_change.
length());
150 active_model_->CommitComposing();
177 if (active_model_ ==
nullptr) {
180 std::string text_before_change = active_model_->GetText();
181 TextRange selection_before_change = active_model_->selection();
182 active_model_->CommitComposing();
183 active_model_->EndComposing();
184 if (enable_delta_model) {
185 std::string
text = active_model_->GetText();
187 SendStateUpdateWithDelta(*active_model_, &delta);
189 SendStateUpdate(*active_model_);
195 if (active_model_ ==
nullptr) {
198 std::string text_before_change = active_model_->GetText();
199 TextRange composing_before_change = active_model_->composing_range();
200 active_model_->AddText(
text);
201 active_model_->UpdateComposingText(
text,
TextRange(cursor_pos, cursor_pos));
202 std::string text_after_change = active_model_->GetText();
203 if (enable_delta_model) {
205 fml::Utf8ToUtf16(text_before_change), composing_before_change,
text);
206 SendStateUpdateWithDelta(*active_model_, &delta);
208 SendStateUpdate(*active_model_);
212 void TextInputPlugin::HandleMethodCall(
215 const std::string& method = method_call.
method_name();
220 FlutterWindowsView* view = engine_->
view(view_id_);
221 if (view ==
nullptr) {
222 std::stringstream ss;
223 ss <<
"Text input is not available because view with view_id=" << view_id_
224 <<
" cannot be found";
228 if (active_model_ !=
nullptr && active_model_->composing()) {
229 active_model_->CommitComposing();
230 active_model_->EndComposing();
231 SendStateUpdate(*active_model_);
233 view->OnResetImeComposing();
234 active_model_ =
nullptr;
240 const rapidjson::Document& args = *method_call.
arguments();
242 const rapidjson::Value& client_id_json = args[0];
243 const rapidjson::Value& client_config = args[1];
244 if (client_id_json.IsNull()) {
248 if (client_config.IsNull()) {
250 "Could not set client, missing arguments.");
253 client_id_ = client_id_json.GetInt();
255 if (enable_delta_model_json != client_config.MemberEnd() &&
256 enable_delta_model_json->value.IsBool()) {
257 enable_delta_model = enable_delta_model_json->value.GetBool();
259 auto view_id_json = client_config.FindMember(
kViewId);
260 if (view_id_json != client_config.MemberEnd() &&
261 view_id_json->value.IsInt()) {
262 view_id_ = view_id_json->value.GetInt();
265 "Could not set client, view ID is null.");
270 if (input_action_json != client_config.MemberEnd() &&
271 input_action_json->value.IsString()) {
272 input_action_ = input_action_json->value.GetString();
275 auto input_type_info_json = client_config.FindMember(
kTextInputType);
276 if (input_type_info_json != client_config.MemberEnd() &&
277 input_type_info_json->value.IsObject()) {
278 auto input_type_json =
280 if (input_type_json != input_type_info_json->value.MemberEnd() &&
281 input_type_json->value.IsString()) {
282 input_type_ = input_type_json->value.GetString();
285 active_model_ = std::make_unique<TextInputModel>();
291 const rapidjson::Document& args = *method_call.
arguments();
293 if (active_model_ ==
nullptr) {
296 "Set editing state has been invoked, but no client is set.");
300 if (
text == args.MemberEnd() ||
text->value.IsNull()) {
302 "Set editing state has been invoked, but without text.");
307 if (base == args.MemberEnd() || base->value.IsNull() ||
308 extent == args.MemberEnd() || extent->value.IsNull()) {
310 "Selection base/extent values invalid.");
314 int selection_base = base->value.GetInt();
315 int selection_extent = extent->value.GetInt();
316 if (selection_base == -1 && selection_extent == -1) {
317 selection_base = selection_extent = 0;
321 if (base == args.MemberEnd() || base->value.IsNull() ||
322 extent == args.MemberEnd() || extent->value.IsNull()) {
324 "Composing base/extent values invalid.");
327 int composing_base = base->value.GetInt();
328 int composing_extent = extent->value.GetInt();
329 if (composing_base == -1 && composing_extent == -1) {
330 composing_base = composing_extent = 0;
333 active_model_->SetText(
text->value.GetString(),
334 TextRange(selection_base, selection_extent),
335 TextRange(composing_base, composing_extent));
337 FlutterWindowsView* view = engine_->
view(view_id_);
338 if (view ==
nullptr) {
339 std::stringstream ss;
340 ss <<
"Text input is not available because view with view_id=" << view_id_
341 <<
" cannot be found";
349 const rapidjson::Document& args = *method_call.
arguments();
350 auto x = args.FindMember(
kXKey);
351 auto y = args.FindMember(
kYKey);
354 if (x == args.MemberEnd() || x->value.IsNull() ||
355 y == args.MemberEnd() || y->value.IsNull() ||
356 width == args.MemberEnd() || width->value.IsNull() ||
357 height == args.MemberEnd() || height->value.IsNull()) {
359 "Composing rect values invalid.");
362 composing_rect_ = {{x->value.GetDouble(), y->value.GetDouble()},
363 {width->value.GetDouble(), height->value.GetDouble()}};
365 Rect transformed_rect = GetCursorRect();
366 view->OnCursorRectUpdated(transformed_rect);
368 FlutterWindowsView* view = engine_->
view(view_id_);
369 if (view ==
nullptr) {
370 std::stringstream ss;
371 ss <<
"Text input is not available because view with view_id=" << view_id_
372 <<
" cannot be found";
380 const rapidjson::Document& args = *method_call.
arguments();
382 if (transform == args.MemberEnd() || transform->value.IsNull() ||
383 !transform->value.IsArray() || transform->value.Size() != 16) {
385 "EditableText transform invalid.");
389 for (
auto& entry : transform->value.GetArray()) {
390 if (entry.IsNull()) {
392 "EditableText transform contains null value.");
395 editabletext_transform_[i / 4][i % 4] = entry.GetDouble();
398 Rect transformed_rect = GetCursorRect();
399 view->OnCursorRectUpdated(transformed_rect);
401 result->NotImplemented();
409 Rect TextInputPlugin::GetCursorRect()
const {
410 Point transformed_point = {
411 composing_rect_.left() * editabletext_transform_[0][0] +
412 composing_rect_.top() * editabletext_transform_[1][0] +
413 editabletext_transform_[3][0],
414 composing_rect_.left() * editabletext_transform_[0][1] +
415 composing_rect_.top() * editabletext_transform_[1][1] +
416 editabletext_transform_[3][1]};
417 return {transformed_point, composing_rect_.size()};
420 void TextInputPlugin::SendStateUpdate(
const TextInputModel& model) {
421 auto args = std::make_unique<rapidjson::Document>(rapidjson::kArrayType);
422 auto& allocator = args->GetAllocator();
423 args->PushBack(client_id_, allocator);
425 TextRange selection = model.selection();
426 rapidjson::Value editing_state(rapidjson::kObjectType);
433 int composing_base = model.composing() ? model.composing_range().base() : -1;
434 int composing_extent =
435 model.composing() ? model.composing_range().extent() : -1;
438 editing_state.AddMember(
439 kTextKey, rapidjson::Value(model.GetText(), allocator).Move(), allocator);
440 args->PushBack(editing_state, allocator);
445 void TextInputPlugin::SendStateUpdateWithDelta(
const TextInputModel& model,
446 const TextEditingDelta* delta) {
447 auto args = std::make_unique<rapidjson::Document>(rapidjson::kArrayType);
448 auto& allocator = args->GetAllocator();
449 args->PushBack(client_id_, allocator);
451 rapidjson::Value object(rapidjson::kObjectType);
452 rapidjson::Value deltas(rapidjson::kArrayType);
453 rapidjson::Value deltaJson(rapidjson::kObjectType);
456 deltaJson.AddMember(
kDeltaTextKey, delta->delta_text(), allocator);
457 deltaJson.AddMember(
kDeltaStartKey, delta->delta_start(), allocator);
458 deltaJson.AddMember(
kDeltaEndKey, delta->delta_end(), allocator);
460 TextRange selection = model.selection();
466 int composing_base = model.composing() ? model.composing_range().base() : -1;
467 int composing_extent =
468 model.composing() ? model.composing_range().extent() : -1;
472 deltas.PushBack(deltaJson, allocator);
473 object.AddMember(
kDeltasKey, deltas, allocator);
474 args->PushBack(
object, allocator);
479 void TextInputPlugin::EnterPressed(TextInputModel* model) {
482 std::u16string text_before_change = fml::Utf8ToUtf16(model->GetText());
483 TextRange selection_before_change = model->selection();
484 model->AddText(u
"\n");
485 if (enable_delta_model) {
486 TextEditingDelta delta(text_before_change, selection_before_change,
488 SendStateUpdateWithDelta(*model, &delta);
490 SendStateUpdate(*model);
493 auto args = std::make_unique<rapidjson::Document>(rapidjson::kArrayType);
494 auto& allocator = args->GetAllocator();
495 args->PushBack(client_id_, allocator);
496 args->PushBack(rapidjson::Value(input_action_, allocator).Move(), allocator);
508 if (active_model_ !=
nullptr && active_model_->composing()) {
509 active_model_->CommitComposing();
510 active_model_->EndComposing();
513 active_model_ =
nullptr;
FlutterWindowsView * view(FlutterViewId view_id) const
const std::string & method_name() const
const T * arguments() const
TextInputPlugin(flutter::BinaryMessenger *messenger, FlutterWindowsEngine *engine)
virtual void ComposeCommitHook()
void OnViewRemoved(FlutterViewId view_id)
virtual void ComposeChangeHook(const std::u16string &text, int cursor_pos)
virtual void TextHook(const std::u16string &text)
virtual void ComposeEndHook()
virtual void KeyboardHook(int key, int scancode, int action, char32_t character, bool extended, bool was_down)
virtual ~TextInputPlugin()
virtual void ComposeBeginHook()
constexpr FlutterViewId kImplicitViewId
A change in the state of an input field.
static constexpr char kClearClientMethod[]
static constexpr char kSetEditableSizeAndTransform[]
static constexpr char kSetMarkedTextRect[]
static constexpr char kEnableDeltaModel[]
static constexpr char kSelectionIsDirectionalKey[]
static constexpr char kDeltaEndKey[]
static constexpr char kHideMethod[]
static constexpr char kMultilineInputType[]
static constexpr char kDeltaOldTextKey[]
static constexpr char kWidthKey[]
static constexpr char kSetEditingStateMethod[]
static constexpr char kSelectionExtentKey[]
static constexpr char kHeightKey[]
static constexpr char kDeltaTextKey[]
static constexpr char kDeltasKey[]
static constexpr char kTextInputType[]
static constexpr char kChannelName[]
static constexpr char kXKey[]
static constexpr char kInputActionNewline[]
static constexpr char kInternalConsistencyError[]
static constexpr char kBadArgumentError[]
static constexpr char kSetClientMethod[]
static constexpr char kUpdateEditingStateMethod[]
static constexpr char kPerformActionMethod[]
static constexpr char kYKey[]
static constexpr char kShowMethod[]
static constexpr char kComposingExtentKey[]
static constexpr char kTextInputAction[]
static constexpr char kTransformKey[]
static constexpr char kComposingBaseKey[]
static constexpr char kTextInputTypeName[]
static constexpr char kUpdateEditingStateWithDeltasMethod[]
static constexpr char kAffinityDownstream[]
static constexpr char kSelectionAffinityKey[]
static constexpr char kTextKey[]
static constexpr char kViewId[]
static constexpr char kSelectionBaseKey[]
static constexpr char kDeltaStartKey[]