1 /* 2 * Copyright (c) 2021 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_COMMON_IME_TEXT_EDIT_CONTROLLER_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMMON_IME_TEXT_EDIT_CONTROLLER_H 18 19 #include <string> 20 21 #include "base/utils/value_change_notifier.h" 22 #include "core/common/ime/text_editing_value.h" 23 24 namespace OHOS::Ace { 25 26 class TextEditController : public ValueChangeNotifier<TextEditingValue> { 27 public: 28 void SetText(const std::string& newText, bool needFireChangeEvent = true) 29 { 30 auto value = GetValue(); 31 if (value.text != newText) { 32 value.text = newText; 33 // Default set selection to the end of text is more consistent with the intuition of user. 34 value.selection.Update(static_cast<int32_t>(value.GetWideText().length())); 35 SetValue(std::move(value), needFireChangeEvent); 36 } 37 } 38 SetHint(const std::string & hint)39 void SetHint(const std::string& hint) 40 { 41 auto value = GetValue(); 42 value.hint = hint; 43 SetValue(std::move(value)); 44 } 45 SetSelection(const TextSelection & selection)46 void SetSelection(const TextSelection& selection) 47 { 48 auto value = GetValue(); 49 value.selection = selection; 50 SetValue(std::move(value)); 51 } 52 Clear()53 void Clear() 54 { 55 SetValue(TextEditingValue()); 56 } 57 GetText()58 const std::string& GetText() const 59 { 60 return GetValue().text; 61 } 62 GetSelection()63 const TextSelection& GetSelection() const 64 { 65 return GetValue().selection; 66 } 67 }; 68 69 } // namespace OHOS::Ace 70 71 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMMON_IME_TEXT_EDIT_CONTROLLER_H 72