• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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_PATTERN_TEXT_FIELD_TEXT_SELECTOR_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_TEXT_FIELD_TEXT_SELECTOR_H
18 
19 #include <cstdint>
20 #include <string>
21 
22 #include "base/geometry/ng/offset_t.h"
23 #include "base/geometry/ng/rect_t.h"
24 
25 namespace OHOS::Ace::NG {
26 
27 using OnAccessibilityCallback = std::function<void()>;
28 
29 enum class CaretUpdateType {
30     PRESSED,
31     LONG_PRESSED,
32     ICON_PRESSED,
33     DEL,
34     EVENT,
35     HANDLE_MOVE,
36     HANDLE_MOVE_DONE,
37     INPUT,
38     NONE,
39     RIGHT_CLICK,
40     VISIBLE_PASSWORD_ICON
41 };
42 /**
43  * Stands for selection indexes
44  * We use base/destination to indicate the start/end position because of uncertain direction.
45  */
46 struct TextSelector {
47     TextSelector() = default;
TextSelectorTextSelector48     TextSelector(int32_t base, int32_t destination) : baseOffset(base), destinationOffset(destination) {}
49 
SetOnAccessibilityTextSelector50     void SetOnAccessibility(OnAccessibilityCallback&& onAccessibilityCallback)
51     {
52         if (onAccessibilityCallback) {
53             onAccessibilityCallback_ = std::move(onAccessibilityCallback);
54         }
55     }
56 
FireAccessibilityCallbackTextSelector57     void FireAccessibilityCallback()
58     {
59         if (onAccessibilityCallback_) {
60             onAccessibilityCallback_();
61         }
62     }
63 
UpdateTextSelector64     void Update(int32_t base, int32_t destination)
65     {
66         if ((baseOffset != base) || (destinationOffset != destination)) {
67             if ((baseOffset != destinationOffset) || (base != destination)) {
68                 FireAccessibilityCallback();
69             }
70         }
71         baseOffset = base;
72         destinationOffset = destination;
73     }
74 
75     // Usually called when none is selected.
UpdateTextSelector76     void Update(int32_t both)
77     {
78         if ((baseOffset != both) || (destinationOffset != both)) {
79             if (baseOffset != destinationOffset) {
80                 FireAccessibilityCallback();
81             }
82         }
83         baseOffset = both;
84         destinationOffset = both;
85     }
86 
87     bool operator==(const TextSelector& other) const
88     {
89         return baseOffset == other.baseOffset && destinationOffset == other.destinationOffset;
90     }
91 
92     bool operator!=(const TextSelector& other) const
93     {
94         return !operator==(other);
95     }
96 
GetTextStartTextSelector97     inline int32_t GetTextStart() const
98     {
99         return std::min(baseOffset, destinationOffset);
100     }
101 
GetTextEndTextSelector102     inline int32_t GetTextEnd() const
103     {
104         return std::max(baseOffset, destinationOffset);
105     }
106 
GetStartTextSelector107     inline int32_t GetStart() const
108     {
109         return baseOffset;
110     }
111 
GetEndTextSelector112     inline int32_t GetEnd() const
113     {
114         return destinationOffset;
115     }
116 
IsValidTextSelector117     inline bool IsValid() const
118     {
119         return baseOffset > -1 && destinationOffset > -1;
120     }
121 
MoveSelectionLeftTextSelector122     bool MoveSelectionLeft()
123     {
124         destinationOffset = std::max(0, destinationOffset - 1);
125         return destinationOffset == baseOffset;
126     }
127 
MoveSelectionRightTextSelector128     bool MoveSelectionRight()
129     {
130         destinationOffset = std::min(charCount, destinationOffset + 1);
131         return destinationOffset == baseOffset;
132     }
133 
GetSelectHeightTextSelector134     double GetSelectHeight() const
135     {
136         return std::max(firstHandle.Height(), secondHandle.Height());
137     }
138 
StartEqualToDestTextSelector139     bool StartEqualToDest() const
140     {
141         return baseOffset == destinationOffset;
142     }
143 
ToStringTextSelector144     std::string ToString()
145     {
146         std::string result;
147         result.append("base offset: ");
148         result.append(std::to_string(baseOffset));
149         result.append(", destination offset: ");
150         result.append(std::to_string(destinationOffset));
151         return result;
152     }
153 
154     // May larger than, smaller than or equal to destinationOffset.
155     int32_t baseOffset = -1;
156     OffsetF selectionBaseOffset;
157 
158     // When paints caret, this is where the caret position is.
159     int32_t destinationOffset = -1;
160     OffsetF selectionDestinationOffset;
161 
162     int32_t charCount = 0;
163     RectF firstHandle;
164     RectF secondHandle;
165     OffsetF firstHandleOffset_;
166     OffsetF secondHandleOffset_;
167     OnAccessibilityCallback onAccessibilityCallback_;
168 };
169 
170 } // namespace OHOS::Ace::NG
171 
172 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_TEXT_FIELD_TEXT_SELECTOR_H
173