• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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         value.text = newText;
32         // Default set selection to the end of text is more consistent with the intuition of user.
33         value.selection.Update(value.GetWideText().length());
34         SetValue(std::move(value), needFireChangeEvent);
35     }
36 
SetHint(const std::string & hint)37     void SetHint(const std::string& hint)
38     {
39         auto value = GetValue();
40         value.hint = hint;
41         SetValue(std::move(value));
42     }
43 
SetSelection(const TextSelection & selection)44     void SetSelection(const TextSelection& selection)
45     {
46         auto value = GetValue();
47         value.selection = selection;
48         SetValue(std::move(value));
49     }
50 
Clear()51     void Clear()
52     {
53         SetValue(TextEditingValue());
54     }
55 
GetText()56     const std::string& GetText() const
57     {
58         return GetValue().text;
59     }
60 
GetSelection()61     const TextSelection& GetSelection() const
62     {
63         return GetValue().selection;
64     }
65 };
66 
67 } // namespace OHOS::Ace
68 
69 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMMON_IME_TEXT_EDIT_CONTROLLER_H
70