1 /*
2  * Copyright (c) 2022-2023 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_PATTERNS_TEXT_FIELD_TEXT_EDITING_VALUE_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_TEXT_FIELD_TEXT_EDITING_VALUE_H
18 
19 #include <algorithm>
20 #include <functional>
21 #include <string>
22 
23 #include "base/utils/string_utils.h"
24 
25 namespace OHOS::Ace::NG {
26 
27 struct TextEditingValueNG {
28     bool operator==(const TextEditingValueNG& other) const
29     {
30         return text == other.text;
31     }
32 
33     bool operator!=(const TextEditingValueNG& other) const
34     {
35         return !operator==(other);
36     }
37 
EmptyTextEditingValueNG38     bool Empty() const
39     {
40         return text.empty();
41     }
42 
GetWideTextTextEditingValueNG43     std::wstring GetWideText() const
44     {
45         return StringUtils::ToWstring(text);
46     }
47 
CursorMoveLeftTextEditingValueNG48     void CursorMoveLeft()
49     {
50         caretPosition = std::max(0, caretPosition - 1);
51     }
52 
CursorMoveRightTextEditingValueNG53     void CursorMoveRight()
54     {
55         caretPosition = std::min(static_cast<int32_t>(GetWideText().length()), caretPosition + 1);
56     }
57 
CursorMoveToPositionTextEditingValueNG58     void CursorMoveToPosition(int32_t position)
59     {
60         caretPosition = std::clamp(position, 0, static_cast<int32_t>(GetWideText().length()));
61     }
62 
GetValueBeforeCursorTextEditingValueNG63     std::string GetValueBeforeCursor() const
64     {
65         auto wideText = GetWideText();
66         if (caretPosition > static_cast<int32_t>(wideText.length()) || caretPosition <= 0) {
67             return "";
68         }
69         return StringUtils::ToString(wideText.substr(0, caretPosition));
70     }
71 
GetValueAfterCursorTextEditingValueNG72     std::string GetValueAfterCursor() const
73     {
74         auto wideText = GetWideText();
75         if (caretPosition > static_cast<int32_t>(wideText.length()) || caretPosition < 0) {
76             return "";
77         }
78         return StringUtils::ToString(wideText.substr(caretPosition));
79     }
80 
GetValueBeforePositionTextEditingValueNG81     std::string GetValueBeforePosition(int32_t position) const
82     {
83         auto wideText = GetWideText();
84         position = std::clamp(position, 0, static_cast<int32_t>(wideText.length()));
85         LOGI("GetValueBeforePosition %{public}d", position);
86         return StringUtils::ToString(wideText.substr(0, position));
87     }
88 
GetValueAfterPositionTextEditingValueNG89     std::string GetValueAfterPosition(int32_t position) const
90     {
91         auto wideText = GetWideText();
92         position = std::clamp(position, 0, static_cast<int32_t>(wideText.length()));
93         LOGI("GetValueAfterPosition %{public}d", position);
94         return StringUtils::ToString(wideText.substr(position));
95     }
96 
GetSelectedTextTextEditingValueNG97     std::string GetSelectedText(int32_t start, int32_t end) const
98     {
99         auto wideText = GetWideText();
100         auto min = std::clamp(std::max(std::min(start, end), 0), 0, static_cast<int32_t>(wideText.length()));
101         auto max = std::clamp(std::min(std::max(start, end), static_cast<int32_t>(wideText.length())), 0,
102             static_cast<int32_t>(wideText.length()));
103         return StringUtils::ToString(wideText.substr(min, max - min));
104     }
105 
CaretAtLastTextEditingValueNG106     bool CaretAtLast() const
107     {
108         return static_cast<int32_t>(GetWideText().length()) == caretPosition;
109     }
110 
LastCharTextEditingValueNG111     char16_t LastChar() const
112     {
113         if (text.empty()) {
114             return 0;
115         }
116         if (static_cast<size_t>(caretPosition) > text.length()) {
117             return 0;
118         }
119         return text[std::max(0, caretPosition - 1)];
120     }
121 
ToStringTextEditingValueNG122     std::string ToString() const
123     {
124         return GetValueBeforeCursor() + "|" + GetValueAfterCursor();
125     }
126 
ResetTextEditingValueNG127     void Reset()
128     {
129         text.clear();
130         caretPosition = 0;
131     }
132 
133     std::string text;
134     int32_t caretPosition = 0;
135 };
136 
137 } // namespace OHOS::Ace::NG
138 
139 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_TEXT_FIELD_TEXT_EDITING_VALUE_H
140