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_CPP_TEXT_INPUT_MODEL_H_ 6 #define FLUTTER_SHELL_PLATFORM_CPP_TEXT_INPUT_MODEL_H_ 7 8 #include <memory> 9 #include <string> 10 11 #include "rapidjson/document.h" 12 13 namespace flutter { 14 // Handles underlying text input state, using a simple ASCII model. 15 // 16 // Ignores special states like "insert mode" for now. 17 class TextInputModel { 18 public: 19 TextInputModel(int client_id, const rapidjson::Value& config); 20 virtual ~TextInputModel(); 21 22 // Attempts to set the text state. 23 // 24 // Returns false if the state is not valid (base or extent are out of 25 // bounds, or base is less than extent). 26 bool SetEditingState(size_t selection_base, 27 size_t selection_extent, 28 const std::string& text); 29 30 // Adds a character. 31 // 32 // Either appends after the cursor (when selection base and extent are the 33 // same), or deletes the selected characters, replacing the text with the 34 // character specified. 35 void AddCharacter(char c); 36 37 // Deletes either the selection, or one character ahead of the cursor. 38 // 39 // Deleting one character ahead of the cursor occurs when the selection base 40 // and extent are the same. 41 // 42 // Returns true if any deletion actually occurred. 43 bool Delete(); 44 45 // Deletes either the selection, or one character behind the cursor. 46 // 47 // Deleting one character behind the cursor occurs when the selection base 48 // and extent are the same. 49 // 50 // Returns true if any deletion actually occurred. 51 bool Backspace(); 52 53 // Attempts to move the cursor backward. 54 // 55 // Returns true if the cursor could be moved. Changes base and extent to be 56 // equal to either the extent (if extent is at the end of the string), or 57 // for extent to be equal to 58 bool MoveCursorBack(); 59 60 // Attempts to move the cursor forward. 61 // 62 // Returns true if the cursor could be moved. 63 bool MoveCursorForward(); 64 65 // Attempts to move the cursor to the beginning. 66 // 67 // Returns true if the cursor could be moved. 68 void MoveCursorToBeginning(); 69 70 // Attempts to move the cursor to the back. 71 // 72 // Returns true if the cursor could be moved. 73 void MoveCursorToEnd(); 74 75 // Returns the state in the form of a platform message. 76 std::unique_ptr<rapidjson::Document> GetState() const; 77 78 // Id of the text input client. client_id()79 int client_id() const { return client_id_; } 80 81 // Keyboard type of the client. See available options: 82 // https://docs.flutter.io/flutter/services/TextInputType-class.html input_type()83 std::string input_type() const { return input_type_; } 84 85 // An action requested by the user on the input client. See available options: 86 // https://docs.flutter.io/flutter/services/TextInputAction-class.html input_action()87 std::string input_action() const { return input_action_; } 88 89 private: 90 void DeleteSelected(); 91 92 std::string text_; 93 int client_id_; 94 std::string input_type_; 95 std::string input_action_; 96 std::string::iterator selection_base_; 97 std::string::iterator selection_extent_; 98 }; 99 100 } // namespace flutter 101 102 #endif // FLUTTER_SHELL_PLATFORM_CPP_TEXT_INPUT_MODEL_H_ 103