1 /* 2 * Copyright (c) 2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_TEXT_FIELD_TEXT_FIELD_MODEL_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_TEXT_FIELD_TEXT_FIELD_MODEL_H 18 19 #include <functional> 20 #include <optional> 21 #include <utility> 22 #include <vector> 23 24 #include "base/geometry/dimension.h" 25 #include "base/memory/referenced.h" 26 #include "base/utils/macros.h" 27 #include "core/common/ime/text_input_action.h" 28 #include "core/common/ime/text_input_type.h" 29 #include "core/components/box/drag_drop_event.h" 30 #include "core/components/common/properties/color.h" 31 #include "core/components/common/properties/text_style.h" 32 #include "core/components_ng/property/measure_property.h" 33 34 namespace OHOS::Ace { 35 36 struct Font { 37 std::optional<FontWeight> fontWeight; 38 std::optional<Dimension> fontSize; 39 std::optional<FontStyle> fontStyle; 40 std::vector<std::string> fontFamilies; 41 }; 42 43 enum class InputStyle { 44 DEFAULT, 45 INLINE, 46 }; 47 48 class ACE_EXPORT TextFieldControllerBase : public AceType { 49 DECLARE_ACE_TYPE(TextFieldControllerBase, AceType); 50 51 public: Focus(bool focus)52 virtual void Focus(bool focus) {} 53 ShowError(const std::string & errorText)54 virtual void ShowError(const std::string& errorText) {} Delete()55 virtual void Delete() {} Insert(const std::string & args)56 virtual void Insert(const std::string& args) {} 57 CaretPosition(int32_t caretPosition)58 virtual void CaretPosition(int32_t caretPosition) {} 59 SetCaretPosition(std::function<void (const int32_t)> && setCaretPosition)60 void SetCaretPosition(std::function<void(const int32_t)>&& setCaretPosition) 61 { 62 setCaretPosition_ = std::move(setCaretPosition); 63 } 64 EscapeString(const std::string & value,std::string & result)65 static bool EscapeString(const std::string& value, std::string& result) 66 { 67 const std::unordered_map<std::string, std::string> escapeMap = { { "a", "\a" }, { "b", "\b" }, { "f", "\f" }, 68 { "n", "\n" }, { "r", "\r" }, { "t", "\t" }, { "v", "\v" }, { "'", "\'" }, { "\"", "\"" }, { "\\", "\\" }, 69 { "?", "\?" }, { "0", "\0" } }; 70 size_t i = 0; 71 for (; i < value.size();) { 72 std::string ch = value.substr(i, 1); 73 if (ch == "\\") { 74 if (i + 1 >= value.size()) { 75 return false; 76 } 77 i++; 78 // cannot escape this combo 79 auto nextChar = value.substr(i, 1); 80 auto mapTuple = escapeMap.find(nextChar); 81 if (mapTuple == escapeMap.end()) { 82 LOGE("Find escape \\%{public}s failed", nextChar.c_str()); 83 return false; 84 } 85 ch = mapTuple->second; 86 } 87 result += ch; 88 i++; 89 } 90 return true; 91 } 92 93 protected: 94 std::function<void(const int32_t)> setCaretPosition_; 95 }; 96 97 class ACE_EXPORT TextFieldModel { 98 public: 99 static TextFieldModel* GetInstance(); 100 virtual ~TextFieldModel() = default; 101 102 virtual RefPtr<TextFieldControllerBase> CreateTextInput( 103 const std::optional<std::string>& placeholder, const std::optional<std::string>& value) = 0; 104 105 virtual RefPtr<TextFieldControllerBase> CreateTextArea( 106 const std::optional<std::string>& placeholder, const std::optional<std::string>& value) = 0; 107 SetWidthAuto(bool isAuto)108 virtual void SetWidthAuto(bool isAuto) {} 109 virtual void SetType(TextInputType value) = 0; 110 virtual void SetPlaceholderColor(const Color& value) = 0; 111 virtual void SetPlaceholderFont(const Font& value) = 0; 112 virtual void SetEnterKeyType(TextInputAction value) = 0; 113 virtual void SetTextAlign(TextAlign value) = 0; 114 virtual void SetCaretColor(const Color& value) = 0; 115 virtual void SetMaxLength(uint32_t value) = 0; 116 virtual void SetMaxLines(uint32_t value) = 0; 117 virtual void SetFontSize(const Dimension& value) = 0; 118 virtual void SetFontWeight(FontWeight value) = 0; 119 virtual void SetTextColor(const Color& value) = 0; 120 virtual void SetFontStyle(FontStyle value) = 0; 121 virtual void SetFontFamily(const std::vector<std::string>& value) = 0; 122 virtual void SetInputFilter(const std::string& value, const std::function<void(const std::string&)>& onError) = 0; 123 virtual void SetInputStyle(InputStyle value) = 0; 124 virtual void SetShowPasswordIcon(bool value) = 0; 125 virtual void SetOnEditChanged(std::function<void(bool)>&& func) = 0; 126 virtual void SetOnSubmit(std::function<void(int32_t)>&& func) = 0; 127 virtual void SetOnChange(std::function<void(const std::string&)>&& func) = 0; 128 virtual void SetOnCopy(std::function<void(const std::string&)>&& func) = 0; 129 virtual void SetOnCut(std::function<void(const std::string&)>&& func) = 0; 130 virtual void SetOnPaste(std::function<void(const std::string&)>&& func) = 0; 131 virtual void SetCopyOption(CopyOptions copyOption) = 0; 132 virtual void ResetMaxLength() = 0; 133 134 private: 135 static std::unique_ptr<TextFieldModel> instance_; 136 }; 137 138 } // namespace OHOS::Ace 139 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_TEXT_FIELD_TEXT_FIELD_MODEL_H 140