• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_COMPONENTS_NG_PROPERTIES_ACCESSIBILITY_PROPERTY_H
17 #define FOUNDATION_ACE_FRAMEWORKS_COMPONENTS_NG_PROPERTIES_ACCESSIBILITY_PROPERTY_H
18 
19 #include <cstdint>
20 #include <string>
21 #include <unordered_set>
22 
23 #include "base/memory/ace_type.h"
24 #include "core/accessibility/accessibility_utils.h"
25 
26 namespace OHOS::Ace::NG {
27 using ActionNoParam = std::function<void()>;
28 using ActionSetTextImpl = std::function<void(const std::string&)>;
29 using ActionScrollForwardImpl = ActionNoParam;
30 using ActionScrollBackwardImpl = ActionNoParam;
31 using ActionSetSelectionImpl = std::function<void(int32_t start, int32_t end)>;
32 using ActionCopyImpl = ActionNoParam;
33 using ActionCutImpl = ActionNoParam;
34 using ActionPasteImpl = ActionNoParam;
35 using ActionSelectImpl = ActionNoParam;
36 using ActionClearSelectionImpl = ActionNoParam;
37 using ActionMoveTextImpl = std::function<void(int32_t moveUnit, bool forward)>;
38 
39 class FrameNode;
40 class ACE_EXPORT AccessibilityProperty : public virtual AceType {
41     DECLARE_ACE_TYPE(AccessibilityProperty, AceType);
42 
43 public:
44     AccessibilityProperty() = default;
45 
46     ~AccessibilityProperty() override = default;
47 
GetText()48     virtual std::string GetText() const
49     {
50         return propText_.value_or("");
51     }
52 
SetText(const std::string & text)53     virtual void SetText(const std::string& text)
54     {
55         propText_ = text;
56     }
57 
IsCheckable()58     virtual bool IsCheckable() const
59     {
60         return false;
61     }
62 
IsChecked()63     virtual bool IsChecked() const
64     {
65         return false;
66     }
67 
IsSelected()68     virtual bool IsSelected() const
69     {
70         return false;
71     }
72 
IsPassword()73     virtual bool IsPassword() const
74     {
75         return false;
76     }
77 
IsEditable()78     virtual bool IsEditable() const
79     {
80         return false;
81     }
82 
IsMultiLine()83     virtual bool IsMultiLine() const
84     {
85         return false;
86     }
87 
IsDeletable()88     virtual bool IsDeletable() const
89     {
90         return false;
91     }
92 
IsHint()93     virtual bool IsHint() const
94     {
95         return false;
96     }
97 
IsScrollable()98     virtual bool IsScrollable() const
99     {
100         return false;
101     }
102 
GetCurrentIndex()103     virtual int32_t GetCurrentIndex() const
104     {
105         return -1;
106     }
107 
GetBeginIndex()108     virtual int32_t GetBeginIndex() const
109     {
110         return -1;
111     }
112 
GetEndIndex()113     virtual int32_t GetEndIndex() const
114     {
115         return -1;
116     }
117 
ToJsonValue(std::unique_ptr<JsonValue> & json)118     virtual void ToJsonValue(std::unique_ptr<JsonValue>& json) const
119     {
120         json->Put("scrollable", IsScrollable());
121     }
122 
FromJson(const std::unique_ptr<JsonValue> & json)123     virtual void FromJson(const std::unique_ptr<JsonValue>& json) {}
124 
HasRange()125     virtual bool HasRange() const
126     {
127         return false;
128     }
129 
GetAccessibilityValue()130     virtual AccessibilityValue GetAccessibilityValue() const
131     {
132         return AccessibilityValue();
133     }
134 
SetHost(const WeakPtr<FrameNode> & host)135     void SetHost(const WeakPtr<FrameNode>& host)
136     {
137         host_ = host;
138     }
139 
GetHintText()140     virtual std::string GetHintText() const
141     {
142         return "";
143     }
144 
GetTextLengthLimit()145     virtual int32_t GetTextLengthLimit() const
146     {
147         return -1;
148     }
149 
GetCollectionInfo()150     virtual AceCollectionInfo GetCollectionInfo() const
151     {
152         return AceCollectionInfo();
153     }
154 
GetCollectionItemInfo()155     virtual AceCollectionItemInfo GetCollectionItemInfo() const
156     {
157         return AceCollectionItemInfo();
158     }
159 
GetErrorText()160     virtual std::string GetErrorText() const
161     {
162         return "";
163     }
164 
GetTextSelectionStart()165     virtual int32_t GetTextSelectionStart() const
166     {
167         return 0;
168     }
169 
GetTextSelectionEnd()170     virtual int32_t GetTextSelectionEnd() const
171     {
172         return 0;
173     }
174 
GetTextInputType()175     virtual AceTextCategory GetTextInputType() const
176     {
177         return AceTextCategory::INPUT_TYPE_DEFAULT;
178     }
179 
GetCollectionItemCounts()180     virtual int32_t GetCollectionItemCounts() const
181     {
182         return 0;
183     }
184 
GetContentInvalid()185     virtual bool GetContentInvalid() const
186     {
187         return true;
188     }
189 
AddSupportAction(AceAction action)190     void AddSupportAction(AceAction action)
191     {
192         supportActions_ |= (1UL << static_cast<uint32_t>(action));
193     }
194 
195     std::unordered_set<AceAction> GetSupportAction() const;
196 
ResetSupportAction()197     void ResetSupportAction()
198     {
199         supportActions_ = 0;
200         SetSpecificSupportAction();
201     };
202 
SetActionSetText(const ActionSetTextImpl & actionSetTextImpl)203     void SetActionSetText(const ActionSetTextImpl& actionSetTextImpl)
204     {
205         actionSetTextImpl_ = actionSetTextImpl;
206     }
207 
ActActionSetText(const std::string & text)208     bool ActActionSetText(const std::string& text)
209     {
210         if (actionSetTextImpl_) {
211             actionSetTextImpl_(text);
212             return true;
213         }
214         return false;
215     }
216 
SetActionSetSelection(const ActionSetSelectionImpl & actionSetSelection)217     void SetActionSetSelection(const ActionSetSelectionImpl& actionSetSelection)
218     {
219         actionSetSelectionImpl_ = actionSetSelection;
220     }
221 
ActActionSetSelection(int32_t start,int32_t end)222     bool ActActionSetSelection(int32_t start, int32_t end)
223     {
224         if (actionSetSelectionImpl_) {
225             actionSetSelectionImpl_(start, end);
226             return true;
227         }
228         return false;
229     }
230 
SetActionMoveText(const ActionMoveTextImpl & actionMoveText)231     void SetActionMoveText(const ActionMoveTextImpl& actionMoveText)
232     {
233         actionMoveTextImpl_ = actionMoveText;
234     }
235 
ActActionMoveText(int32_t moveUnit,bool forward)236     bool ActActionMoveText(int32_t moveUnit, bool forward)
237     {
238         if (actionMoveTextImpl_) {
239             actionMoveTextImpl_(moveUnit, forward);
240             return true;
241         }
242         return false;
243     }
244 
SetActionScrollForward(const ActionScrollForwardImpl & actionScrollForwardImpl)245     void SetActionScrollForward(const ActionScrollForwardImpl& actionScrollForwardImpl)
246     {
247         actionScrollForwardImpl_ = actionScrollForwardImpl;
248     }
249 
ActActionScrollForward()250     bool ActActionScrollForward()
251     {
252         if (actionScrollForwardImpl_) {
253             actionScrollForwardImpl_();
254             return true;
255         }
256         return false;
257     }
258 
SetActionScrollBackward(const ActionScrollBackwardImpl & actionScrollBackwardImpl)259     void SetActionScrollBackward(const ActionScrollBackwardImpl& actionScrollBackwardImpl)
260     {
261         actionScrollBackwardImpl_ = actionScrollBackwardImpl;
262     }
263 
ActActionScrollBackward()264     bool ActActionScrollBackward()
265     {
266         if (actionScrollBackwardImpl_) {
267             actionScrollBackwardImpl_();
268             return true;
269         }
270         return false;
271     }
272 
SetActionCopy(const ActionCopyImpl & actionCopyImpl)273     void SetActionCopy(const ActionCopyImpl& actionCopyImpl)
274     {
275         actionCopyImpl_ = actionCopyImpl;
276     }
277 
ActActionCopy()278     bool ActActionCopy()
279     {
280         if (actionCopyImpl_) {
281             actionCopyImpl_();
282             return true;
283         }
284         return false;
285     }
286 
SetActionCut(const ActionCutImpl & actionCutImpl)287     void SetActionCut(const ActionCutImpl& actionCutImpl)
288     {
289         actionCutImpl_ = actionCutImpl;
290     }
291 
ActActionCut()292     bool ActActionCut()
293     {
294         if (actionCutImpl_) {
295             actionCutImpl_();
296             return true;
297         }
298         return false;
299     }
300 
SetActionPaste(const ActionPasteImpl & actionPasteImpl)301     void SetActionPaste(const ActionPasteImpl& actionPasteImpl)
302     {
303         actionPasteImpl_ = actionPasteImpl;
304     }
305 
ActActionPaste()306     bool ActActionPaste()
307     {
308         if (actionPasteImpl_) {
309             actionPasteImpl_();
310             return true;
311         }
312         return false;
313     }
314 
SetActionSelect(const ActionSelectImpl & actionSelectImpl)315     void SetActionSelect(const ActionSelectImpl& actionSelectImpl)
316     {
317         actionSelectImpl_ = actionSelectImpl;
318     }
319 
ActActionSelect()320     bool ActActionSelect()
321     {
322         if (actionSelectImpl_) {
323             actionSelectImpl_();
324             return true;
325         }
326         return false;
327     }
328 
SetActionClearSelection(const ActionClearSelectionImpl & actionClearSelectionImpl)329     void SetActionClearSelection(const ActionClearSelectionImpl& actionClearSelectionImpl)
330     {
331         actionClearSelectionImpl_ = actionClearSelectionImpl;
332     }
333 
ActActionClearSelection()334     bool ActActionClearSelection()
335     {
336         if (actionClearSelectionImpl_) {
337             actionClearSelectionImpl_();
338             return true;
339         }
340         return false;
341     }
342 
SetAccessibilityGroup(bool accessibilityGroup)343     void SetAccessibilityGroup(bool accessibilityGroup)
344     {
345         accessibilityGroup_ = accessibilityGroup;
346     }
347 
SetAccessibilityText(const std::string & text)348     void SetAccessibilityText(const std::string& text)
349     {
350         accessibilityText_ = text;
351     }
352 
SetAccessibilityDescription(const std::string & accessibilityDescription)353     void SetAccessibilityDescription(const std::string& accessibilityDescription)
354     {
355         accessibilityDescription_ = accessibilityDescription;
356     }
357 
SetAccessibilityLevel(const std::string & accessibilityLevel)358     void SetAccessibilityLevel(const std::string& accessibilityLevel)
359     {
360         if (accessibilityLevel == "auto" || accessibilityLevel == "yes" || accessibilityLevel == "no" ||
361             accessibilityLevel == "no-hide-descendants") {
362             accessibilityLevel_ = accessibilityLevel;
363         } else {
364             accessibilityLevel_ = "auto";
365         }
366     }
367 
IsAccessibilityGroup()368     bool IsAccessibilityGroup() const
369     {
370         return accessibilityGroup_;
371     }
372 
373     std::string GetAccessibilityText(bool isParentGroup = false);
374 
GetAccessibilityDescription()375     std::string GetAccessibilityDescription()
376     {
377         return accessibilityDescription_.value_or("");
378     }
379 
GetAccessibilityLevel()380     virtual std::string GetAccessibilityLevel()
381     {
382         if (!accessibilityLevel_.has_value()) {
383             return "yes";
384         }
385         accessibilityLevel_ = accessibilityLevel_ == "auto" ? "yes" : accessibilityLevel_;
386         return accessibilityLevel_.value();
387     }
388 
389 protected:
SetSpecificSupportAction()390     virtual void SetSpecificSupportAction() {}
391     std::optional<std::string> propText_;
392     WeakPtr<FrameNode> host_;
393     uint64_t supportActions_ = 0;
394     ActionSetTextImpl actionSetTextImpl_;
395     ActionSetSelectionImpl actionSetSelectionImpl_;
396     ActionMoveTextImpl actionMoveTextImpl_;
397     ActionScrollForwardImpl actionScrollForwardImpl_;
398     ActionScrollBackwardImpl actionScrollBackwardImpl_;
399     ActionCopyImpl actionCopyImpl_;
400     ActionCutImpl actionCutImpl_;
401     ActionPasteImpl actionPasteImpl_;
402     ActionSelectImpl actionSelectImpl_;
403     ActionClearSelectionImpl actionClearSelectionImpl_;
404     bool accessibilityGroup_ = false;
405     std::optional<std::string> accessibilityText_;
406     std::optional<std::string> accessibilityDescription_;
407     std::optional<std::string> accessibilityLevel_;
408     ACE_DISALLOW_COPY_AND_MOVE(AccessibilityProperty);
409 };
410 } // namespace OHOS::Ace::NG
411 
412 #endif
413