11 #include "flutter/fml/string_conversion.h"
24 "TextInput.setEditableSizeAndTransform";
29 "TextInputClient.updateEditingState";
31 "TextInputClient.updateEditingStateWithDeltas";
51 static constexpr
char kXKey[] =
"x";
52 static constexpr
char kYKey[] =
"y";
61 "Internal Consistency Error";
68 if (active_model_ ==
nullptr) {
71 std::u16string text_before_change =
72 fml::Utf8ToUtf16(active_model_->GetText());
73 TextRange selection_before_change = active_model_->selection();
74 active_model_->AddText(
text);
76 if (enable_delta_model) {
79 SendStateUpdateWithDelta(*active_model_, &delta);
81 SendStateUpdate(*active_model_);
91 if (active_model_ ==
nullptr) {
99 EnterPressed(active_model_.get());
114 active_model_(nullptr) {
115 channel_->SetMethodCallHandler(
119 HandleMethodCall(call, std::move(result));
126 if (active_model_ ==
nullptr) {
129 active_model_->BeginComposing();
130 if (enable_delta_model) {
131 std::string
text = active_model_->GetText();
132 TextRange selection = active_model_->selection();
134 SendStateUpdateWithDelta(*active_model_, &delta);
136 SendStateUpdate(*active_model_);
141 if (active_model_ ==
nullptr) {
144 std::string text_before_change = active_model_->GetText();
145 TextRange selection_before_change = active_model_->selection();
146 TextRange composing_before_change = active_model_->composing_range();
147 std::string composing_text_before_change = text_before_change.substr(
148 composing_before_change.
start(), composing_before_change.
length());
149 active_model_->CommitComposing();
176 if (active_model_ ==
nullptr) {
179 std::string text_before_change = active_model_->GetText();
180 TextRange selection_before_change = active_model_->selection();
181 active_model_->CommitComposing();
182 active_model_->EndComposing();
183 if (enable_delta_model) {
184 std::string
text = active_model_->GetText();
186 SendStateUpdateWithDelta(*active_model_, &delta);
188 SendStateUpdate(*active_model_);
194 if (active_model_ ==
nullptr) {
197 std::string text_before_change = active_model_->GetText();
198 TextRange composing_before_change = active_model_->composing_range();
199 active_model_->AddText(
text);
200 cursor_pos += active_model_->composing_range().start();
201 active_model_->UpdateComposingText(
text);
202 active_model_->SetSelection(
TextRange(cursor_pos, cursor_pos));
203 std::string text_after_change = active_model_->GetText();
204 if (enable_delta_model) {
206 fml::Utf8ToUtf16(text_before_change), composing_before_change,
text);
207 SendStateUpdateWithDelta(*active_model_, &delta);
209 SendStateUpdate(*active_model_);
213 void TextInputPlugin::HandleMethodCall(
216 const std::string& method = method_call.
method_name();
221 FlutterWindowsView* view = engine_->
view();
222 if (view ==
nullptr) {
224 "Text input is not available in Windows headless mode");
227 if (active_model_ !=
nullptr && active_model_->composing()) {
228 active_model_->CommitComposing();
229 active_model_->EndComposing();
230 SendStateUpdate(*active_model_);
232 view->OnResetImeComposing();
233 active_model_ =
nullptr;
239 const rapidjson::Document& args = *method_call.
arguments();
241 const rapidjson::Value& client_id_json = args[0];
242 const rapidjson::Value& client_config = args[1];
243 if (client_id_json.IsNull()) {
247 if (client_config.IsNull()) {
249 "Could not set client, missing arguments.");
252 client_id_ = client_id_json.GetInt();
254 if (enable_delta_model_json != client_config.MemberEnd() &&
255 enable_delta_model_json->value.IsBool()) {
256 enable_delta_model = enable_delta_model_json->value.GetBool();
260 if (input_action_json != client_config.MemberEnd() &&
261 input_action_json->value.IsString()) {
262 input_action_ = input_action_json->value.GetString();
265 auto input_type_info_json = client_config.FindMember(
kTextInputType);
266 if (input_type_info_json != client_config.MemberEnd() &&
267 input_type_info_json->value.IsObject()) {
268 auto input_type_json =
270 if (input_type_json != input_type_info_json->value.MemberEnd() &&
271 input_type_json->value.IsString()) {
272 input_type_ = input_type_json->value.GetString();
275 active_model_ = std::make_unique<TextInputModel>();
281 const rapidjson::Document& args = *method_call.
arguments();
283 if (active_model_ ==
nullptr) {
286 "Set editing state has been invoked, but no client is set.");
290 if (
text == args.MemberEnd() ||
text->value.IsNull()) {
292 "Set editing state has been invoked, but without text.");
297 if (base == args.MemberEnd() || base->value.IsNull() ||
298 extent == args.MemberEnd() || extent->value.IsNull()) {
300 "Selection base/extent values invalid.");
304 int selection_base = base->value.GetInt();
305 int selection_extent = extent->value.GetInt();
306 if (selection_base == -1 && selection_extent == -1) {
307 selection_base = selection_extent = 0;
309 active_model_->SetText(
text->value.GetString());
310 active_model_->SetSelection(TextRange(selection_base, selection_extent));
314 if (base == args.MemberEnd() || base->value.IsNull() ||
315 extent == args.MemberEnd() || extent->value.IsNull()) {
317 "Composing base/extent values invalid.");
320 int composing_base = base->value.GetInt();
321 int composing_extent = base->value.GetInt();
322 if (composing_base == -1 && composing_extent == -1) {
323 active_model_->EndComposing();
325 int composing_start = std::min(composing_base, composing_extent);
326 int cursor_offset = selection_base - composing_start;
327 active_model_->SetComposingRange(
328 TextRange(composing_base, composing_extent), cursor_offset);
331 FlutterWindowsView* view = engine_->
view();
332 if (view ==
nullptr) {
334 "Text input is not available in Windows headless mode");
341 const rapidjson::Document& args = *method_call.
arguments();
342 auto x = args.FindMember(
kXKey);
343 auto y = args.FindMember(
kYKey);
346 if (x == args.MemberEnd() || x->value.IsNull() ||
347 y == args.MemberEnd() || y->value.IsNull() ||
348 width == args.MemberEnd() || width->value.IsNull() ||
349 height == args.MemberEnd() || height->value.IsNull()) {
351 "Composing rect values invalid.");
354 composing_rect_ = {{x->value.GetDouble(), y->value.GetDouble()},
355 {width->value.GetDouble(), height->value.GetDouble()}};
357 Rect transformed_rect = GetCursorRect();
358 view->OnCursorRectUpdated(transformed_rect);
360 FlutterWindowsView* view = engine_->
view();
361 if (view ==
nullptr) {
363 "Text input is not available in Windows headless mode");
370 const rapidjson::Document& args = *method_call.
arguments();
372 if (transform == args.MemberEnd() || transform->value.IsNull() ||
373 !transform->value.IsArray() || transform->value.Size() != 16) {
375 "EditableText transform invalid.");
379 for (
auto& entry : transform->value.GetArray()) {
380 if (entry.IsNull()) {
382 "EditableText transform contains null value.");
385 editabletext_transform_[i / 4][i % 4] = entry.GetDouble();
388 Rect transformed_rect = GetCursorRect();
389 view->OnCursorRectUpdated(transformed_rect);
391 result->NotImplemented();
399 Rect TextInputPlugin::GetCursorRect()
const {
400 Point transformed_point = {
401 composing_rect_.
left() * editabletext_transform_[0][0] +
402 composing_rect_.
top() * editabletext_transform_[1][0] +
403 editabletext_transform_[3][0],
404 composing_rect_.
left() * editabletext_transform_[0][1] +
405 composing_rect_.
top() * editabletext_transform_[1][1] +
406 editabletext_transform_[3][1]};
407 return {transformed_point, composing_rect_.
size()};
410 void TextInputPlugin::SendStateUpdate(
const TextInputModel& model) {
411 auto args = std::make_unique<rapidjson::Document>(rapidjson::kArrayType);
412 auto& allocator = args->GetAllocator();
413 args->PushBack(client_id_, allocator);
415 TextRange selection = model.selection();
416 rapidjson::Value editing_state(rapidjson::kObjectType);
423 int composing_base = model.composing() ? model.composing_range().base() : -1;
424 int composing_extent =
425 model.composing() ? model.composing_range().extent() : -1;
428 editing_state.AddMember(
429 kTextKey, rapidjson::Value(model.GetText(), allocator).Move(), allocator);
430 args->PushBack(editing_state, allocator);
435 void TextInputPlugin::SendStateUpdateWithDelta(
const TextInputModel& model,
436 const TextEditingDelta* delta) {
437 auto args = std::make_unique<rapidjson::Document>(rapidjson::kArrayType);
438 auto& allocator = args->GetAllocator();
439 args->PushBack(client_id_, allocator);
441 rapidjson::Value object(rapidjson::kObjectType);
442 rapidjson::Value deltas(rapidjson::kArrayType);
443 rapidjson::Value deltaJson(rapidjson::kObjectType);
446 deltaJson.AddMember(
kDeltaTextKey, delta->delta_text(), allocator);
447 deltaJson.AddMember(
kDeltaStartKey, delta->delta_start(), allocator);
448 deltaJson.AddMember(
kDeltaEndKey, delta->delta_end(), allocator);
450 TextRange selection = model.selection();
456 int composing_base = model.composing() ? model.composing_range().base() : -1;
457 int composing_extent =
458 model.composing() ? model.composing_range().extent() : -1;
462 deltas.PushBack(deltaJson, allocator);
463 object.AddMember(
kDeltasKey, deltas, allocator);
464 args->PushBack(
object, allocator);
469 void TextInputPlugin::EnterPressed(TextInputModel* model) {
472 std::u16string text_before_change = fml::Utf8ToUtf16(model->GetText());
473 TextRange selection_before_change = model->selection();
474 model->AddText(u
"\n");
475 if (enable_delta_model) {
476 TextEditingDelta delta(text_before_change, selection_before_change,
478 SendStateUpdateWithDelta(*model, &delta);
480 SendStateUpdate(*model);
483 auto args = std::make_unique<rapidjson::Document>(rapidjson::kArrayType);
484 auto& allocator = args->GetAllocator();
485 args->PushBack(client_id_, allocator);
486 args->PushBack(rapidjson::Value(input_action_, allocator).Move(), allocator);