1 /* 2 * Copyright (c) 2021-2022 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_ACCESSIBILITY_ACCESSIBILITY_NODE_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_ACCESSIBILITY_ACCESSIBILITY_NODE_H 18 19 #include <functional> 20 #include <list> 21 #include <string> 22 #include <unordered_map> 23 #include <unordered_set> 24 #include <vector> 25 26 #include "base/geometry/axis.h" 27 #include "base/geometry/matrix4.h" 28 #include "base/geometry/rect.h" 29 #include "base/memory/ace_type.h" 30 #include "base/utils/macros.h" 31 #include "core/accessibility/accessibility_utils.h" 32 #include "core/event/ace_event_handler.h" 33 34 namespace OHOS::Ace { 35 36 struct PositionInfo { 37 double width = 0.0; 38 double height = 0.0; 39 double left = 0.0; 40 double top = 0.0; 41 }; 42 43 using ActionClickImpl = std::function<void()>; 44 using ActionLongClickImpl = std::function<void()>; 45 using ActionSetTextImpl = std::function<void(const std::string&)>; 46 using ActionScrollForwardImpl = std::function<bool()>; 47 using ActionScrollBackwardImpl = std::function<bool()>; 48 using ActionFocusImpl = std::function<void()>; 49 using ActionUpdateIdsImpl = std::function<void()>; 50 using FocusChangeCallback = std::function<void(const std::string&)>; 51 using ActionAccessibilityFocusImpl = std::function<void(bool)>; 52 53 using NodeId = int32_t; 54 // If no insertion location is specified, new child will be added to the end of children list by default. 55 constexpr int32_t DEFAULT_INDEX = -1; 56 constexpr uint64_t DEFAULT_ACTIONS = std::numeric_limits<uint64_t>::max(); 57 58 class ACE_EXPORT AccessibilityNode : public AceType { 59 DECLARE_ACE_TYPE(AccessibilityNode, AceType); 60 61 public: 62 using ChartValue = std::unordered_map<std::string, std::vector<std::pair<std::string, double>>>; 63 64 AccessibilityNode(NodeId nodeId, const std::string& nodeName); 65 ~AccessibilityNode() override = default; 66 67 // node event action 68 void SetActionClickImpl(const ActionClickImpl& actionClickImpl); 69 bool ActionClick(); 70 void SetActionLongClickImpl(const ActionLongClickImpl& actionLongClickImpl); 71 bool ActionLongClick(); 72 void SetActionSetTextImpl(const ActionSetTextImpl& actionSetTextImpl); 73 bool ActionSetText(const std::string& text); 74 void SetActionScrollForward(const ActionScrollForwardImpl& actionScrollForwardImpl); 75 bool ActionScrollForward(); 76 void SetActionScrollBackward(const ActionScrollBackwardImpl& actionScrollBackwardImpl); 77 bool ActionScrollBackward(); 78 void SetActionFocusImpl(const ActionFocusImpl& actionFocusImpl); 79 bool ActionFocus(); 80 void SetActionUpdateIdsImpl(const ActionUpdateIdsImpl& actionUpdateIdsImpl); 81 void ActionUpdateIds(); 82 void SetActionAccessibilityFocusImpl(const ActionAccessibilityFocusImpl& actionAccessibilityFocusImpl); 83 bool ActionAccessibilityFocus(bool isFocus); 84 85 // node base 86 void SetAttr(const std::vector<std::pair<std::string, std::string>>& attrs); 87 // used for inspector node in PC preview 88 void SetStyle(const std::vector<std::pair<std::string, std::string>>& styles); 89 void AddEvent(int32_t pageId, const std::vector<std::string>& events); 90 void AddNode(const RefPtr<AccessibilityNode>& node, int32_t slot = DEFAULT_INDEX); 91 void RemoveNode(const RefPtr<AccessibilityNode>& node); 92 void Mount(int32_t slot); 93 void AddOffsetForChildren(const Offset& offset); 94 95 void SetWindowId(uint32_t windowId); 96 97 uint32_t GetWindowId() const; 98 99 void SetIsRootNode(bool isRootNode); 100 101 bool IsRootNode() const; 102 103 void ResetChildList(std::list<RefPtr<AccessibilityNode>>& children); 104 105 const std::list<RefPtr<AccessibilityNode>> GetChildList() const; 106 107 NodeId GetParentId() const; 108 109 RefPtr<AccessibilityNode> GetParentNode() const; 110 111 void SetParentNode(const RefPtr<AccessibilityNode>& parentNode); 112 113 const std::string& GetTag() const; 114 115 void SetTag(const std::string& tag); 116 117 int32_t GetPageId() const; 118 119 void SetPageId(int32_t pageId); 120 121 void SetPositionInfo(const PositionInfo& positionInfo); 122 123 const EventMarker& GetAccessibilityEventMarker() const; 124 125 const EventMarker& GetClickEventMarker() const; 126 127 const EventMarker& GetLongPressEventMarker() const; 128 129 const EventMarker& GetSetTextEventMarker() const; 130 131 const EventMarker& GetFocusEventMarker() const; 132 133 void SetFocusChangeEventMarker(const EventMarker& eventId); 134 135 void OnFocusChange(bool isFocus); 136 137 const EventMarker& GetBlurEventMarker() const; 138 139 // node attr need to barrierfree 140 NodeId GetNodeId() const; 141 142 const std::string& GetText() const; 143 144 void SetText(const std::string& text); 145 146 const std::string& GetHintText() const; 147 148 void SetHintText(const std::string& hintText); 149 150 const std::vector<int32_t>& GetChildIds() const; 151 152 void SetChildIds(const std::vector<int32_t>& ids); 153 154 double GetWidth() const; 155 156 void SetWidth(double width); 157 158 double GetHeight() const; 159 160 void SetHeight(double height); 161 162 double GetLeft() const; 163 164 void SetLeft(double left); 165 166 double GetTop() const; 167 168 void SetTop(double top); 169 170 bool GetCheckedState() const; 171 172 void SetCheckedState(bool state); 173 174 bool GetEnabledState() const; 175 176 void SetEnabledState(bool state); 177 178 bool GetEditable() const; 179 180 void SetEditable(bool editable); 181 182 bool GetFocusedState() const; 183 184 void SetFocusedState(bool state); 185 186 bool GetAccessibilityFocusedState() const; 187 188 void SetAccessibilityFocusedState(bool state); 189 190 bool GetSelectedState() const; 191 192 void SetSelectedState(bool state); 193 194 bool GetCheckableState() const; 195 196 void SetCheckableState(bool state); 197 198 bool GetClickableState() const; 199 200 void SetClickableState(bool state); 201 202 bool GetFocusableState() const; 203 204 void SetFocusableState(bool state); 205 206 bool GetScrollableState() const; 207 208 void SetScrollableState(bool state); 209 210 bool GetLongClickableState() const; 211 212 void SetLongClickableState(bool state); 213 214 bool GetIsMultiLine() const; 215 216 void SetIsMultiLine(bool multiLine); 217 218 bool GetIsPassword() const; 219 220 void SetIsPassword(bool isPassword); 221 222 std::unordered_set<AceAction> GetSupportAction(uint64_t enableActions = DEFAULT_ACTIONS) const; 223 224 void AddSupportAction(AceAction action); 225 226 void SetSupportAction(AceAction action, bool isEnable); 227 228 const std::string& GetAccessibilityLabel() const; 229 230 void SetAccessibilityLabel(const std::string& label); 231 232 const std::string& GetAccessibilityHint() const; 233 234 void SetAccessibilityHint(const std::string& hint); 235 236 const std::string& GetImportantForAccessibility() const; 237 238 void SetImportantForAccessibility(const std::string& importance); 239 240 size_t GetMaxTextLength() const; 241 242 void SetMaxTextLength(size_t length); 243 244 int32_t GetTextSelectionStart() const; 245 void SetTextSelectionStart(int32_t start); 246 247 int32_t GetTextSelectionEnd() const; 248 249 void SetTextSelectionEnd(int32_t end); 250 251 const std::string& GetErrorText() const; 252 253 void SetErrorText(const std::string& errorText); 254 255 const std::string& GetJsComponentId() const; 256 257 void SetJsComponentId(const std::string& jsComponentId); 258 259 bool GetAccessible() const; 260 261 void SetAccessible(bool accessible); 262 263 AccessibilityValue GetAccessibilityValue() const; 264 265 void SetAccessibilityValue(double cur, double min = 0.0, double max = 0.0); 266 267 const std::unique_ptr<ChartValue>& GetChartValue() const; 268 269 void PutChartValue(const std::string& groupName, const std::vector<std::pair<std::string, double>>& values); 270 271 std::string GetInputType() const; 272 273 AceTextCategory GetTextInputType() const; 274 275 void SetTextInputType(AceTextCategory type); 276 277 const AceCollectionInfo& GetCollectionInfo() const; 278 279 void SetCollectionInfo(const AceCollectionInfo& collectionInfo); 280 281 const AceCollectionItemInfo& GetCollectionItemInfo() const; 282 283 void SetCollectionItemInfo(const AceCollectionItemInfo& collectionItemInfo); 284 285 bool GetShown() const; 286 287 bool GetVisible() const; 288 289 void SetVisible(bool visible); 290 291 const Rect& GetRect() const; 292 293 void SetRect(const Rect& rect); 294 GetGlobalRect()295 const Rect& GetGlobalRect() 296 { 297 return globalRect_; 298 } 299 SetGlobalRect(const Rect & rect)300 void SetGlobalRect(const Rect& rect) 301 { 302 globalRect_ = rect; 303 } 304 ClearRect()305 void ClearRect() 306 { 307 rect_ = Rect(0, 0, 0, 0); 308 } 309 IsValidRect()310 bool IsValidRect() const 311 { 312 return isValidRect_; 313 } 314 GetClicked()315 bool GetClicked() const 316 { 317 return isClicked_; 318 } 319 SetClicked(bool clicked)320 void SetClicked(bool clicked) 321 { 322 isClicked_ = clicked; 323 } 324 SetMarginSize(const Size & marginSize)325 void SetMarginSize(const Size& marginSize) 326 { 327 marginSize_ = marginSize; 328 } 329 GetMarginSize()330 Size GetMarginSize() const 331 { 332 return marginSize_; 333 } 334 GetAttrs()335 const std::vector<std::pair<std::string, std::string>>& GetAttrs() const 336 { 337 return attrs_; 338 } 339 GetStyles()340 const std::vector<std::pair<std::string, std::string>>& GetStyles() const 341 { 342 return styles_; 343 } 344 SetClipFlagToChild(bool clipFlag)345 void SetClipFlagToChild(bool clipFlag) 346 { 347 for (auto& child : children_) { 348 child->SetClipFlagToChild(clipFlag); 349 } 350 clipFlag_ = clipFlag; 351 } 352 GetClipFlag()353 bool GetClipFlag() 354 { 355 return clipFlag_; 356 } 357 GetListBeginIndex()358 size_t GetListBeginIndex() const 359 { 360 return listBeginIndex_; 361 } 362 SetListBeginIndex(const size_t & index)363 void SetListBeginIndex(const size_t& index) 364 { 365 listBeginIndex_ = index; 366 } 367 GetListEndIndex()368 size_t GetListEndIndex() const 369 { 370 return listEndIndex_; 371 } 372 SetListEndIndex(const size_t & index)373 void SetListEndIndex(const size_t& index) 374 { 375 listEndIndex_ = index; 376 } 377 GetListItemCounts()378 size_t GetListItemCounts() const 379 { 380 return listItemCounts_; 381 } 382 SetListItemCounts(const size_t & index)383 void SetListItemCounts(const size_t& index) 384 { 385 listItemCounts_ = index; 386 } 387 SetTransformToChild(Matrix4 matrix4)388 void SetTransformToChild(Matrix4 matrix4) 389 { 390 for (auto& child : children_) { 391 child->SetTransformToChild(matrix4); 392 } 393 matrix4_ = matrix4; 394 } 395 GetMatrix4()396 Matrix4 GetMatrix4() 397 { 398 return matrix4_; 399 } 400 GetRectWithTransform(const Rect & rect,Matrix4 & matrix4)401 Rect GetRectWithTransform(const Rect& rect, Matrix4& matrix4) 402 { 403 Point ltPoint = matrix4 * Point(rect.Left(), rect.Top()); 404 Point rtPoint = matrix4 * Point(rect.Right(), rect.Top()); 405 Point lbPoint = matrix4 * Point(rect.Left(), rect.Bottom()); 406 Point rbPoint = matrix4 * Point(rect.Right(), rect.Bottom()); 407 auto left = std::min(std::min(ltPoint.GetX(), rtPoint.GetX()), std::min(lbPoint.GetX(), rbPoint.GetX())); 408 auto right = std::max(std::max(ltPoint.GetX(), rtPoint.GetX()), std::max(lbPoint.GetX(), rbPoint.GetX())); 409 auto top = std::min(std::min(ltPoint.GetY(), rtPoint.GetY()), std::min(lbPoint.GetY(), rbPoint.GetY())); 410 auto bottom = std::max(std::max(ltPoint.GetY(), rtPoint.GetY()), std::max(lbPoint.GetY(), rbPoint.GetY())); 411 return Rect(left, top, right - left, bottom - top); 412 } 413 GetMatrix4Flag()414 bool GetMatrix4Flag() 415 { 416 if (matrix4_ == Matrix4()) { 417 return false; 418 } 419 return true; 420 } 421 422 #if defined(PREVIEW) 423 // used for inspector node in PC preview GetClearRectInfoFlag()424 bool GetClearRectInfoFlag() const 425 { 426 return isClearRectInfo_; 427 } 428 429 // used for inspector node in PC preview SetClearRectInfoFlag(bool isClearRectInfo)430 void SetClearRectInfoFlag(bool isClearRectInfo) 431 { 432 isClearRectInfo_ = isClearRectInfo; 433 } 434 435 // used for inspector node in PC preview SetScaleToChild(double scale)436 void SetScaleToChild(double scale) 437 { 438 for (auto& child : children_) { 439 child->SetScaleToChild(scale); 440 } 441 SetScale(scale); 442 } 443 444 // used for inspector node in PC preview SetScaleCenterToChild(Offset center)445 void SetScaleCenterToChild(Offset center) 446 { 447 for (auto& child : children_) { 448 child->SetScaleCenterToChild(center); 449 } 450 SetScaleCenter(center); 451 } 452 453 // used for inspector node in PC preview GetScale()454 double GetScale() 455 { 456 return scale_; 457 } 458 459 // used for inspector node in PC preview SetScale(double scale)460 void SetScale(double scale) 461 { 462 scale_ = scale; 463 SetIsAnimationNode(true); 464 } 465 466 // used for inspector node in PC preview SetScaleCenter(Offset center)467 void SetScaleCenter(Offset center) 468 { 469 scaleCenter_ = center; 470 } 471 472 // used for inspector node in PC preview GetScaleCenter()473 Offset GetScaleCenter() 474 { 475 return scaleCenter_; 476 } 477 478 // used for inspector node in PC preview SetTranslateOffsetToChild(const Offset & offset)479 void SetTranslateOffsetToChild(const Offset& offset) 480 { 481 for (auto& child : children_) { 482 child->SetTranslateOffsetToChild(offset); 483 } 484 SetTranslateOffset(offset); 485 } 486 SetTranslateOffset(const Offset & offset)487 void SetTranslateOffset(const Offset& offset) 488 { 489 translateOffset_ = offset; 490 SetIsAnimationNode(true); 491 } 492 GetTranslateOffset()493 Offset GetTranslateOffset() const 494 { 495 return translateOffset_; 496 } 497 498 // used for inspector node in PC preview SetRotateToChild(const double & angle,const RotateAxis & Axis)499 void SetRotateToChild(const double& angle, const RotateAxis& Axis) 500 { 501 for (auto& child : children_) { 502 child->SetRotateToChild(angle, Axis); 503 } 504 SetRotateAngle(angle); 505 SetRotateAxis(Axis); 506 } 507 SetRotateAngle(const double & angle)508 void SetRotateAngle(const double& angle) 509 { 510 rotateAngle_ = angle; 511 SetIsAnimationNode(true); 512 } 513 GetRotateAngle()514 double GetRotateAngle() const 515 { 516 return rotateAngle_; 517 } 518 SetRotateAxis(const RotateAxis & Axis)519 void SetRotateAxis(const RotateAxis& Axis) 520 { 521 rotateAxis_ = Axis; 522 } 523 GetRotateAxis(RotateAxis Axis)524 RotateAxis GetRotateAxis(RotateAxis Axis) const 525 { 526 return rotateAxis_; 527 } 528 IsAnimationNode()529 bool IsAnimationNode() const 530 { 531 return isAnimationNode_; 532 } 533 SetIsAnimationNode(bool IsAnimationNode)534 void SetIsAnimationNode(bool IsAnimationNode) 535 { 536 isAnimationNode_ = IsAnimationNode; 537 } 538 GetZIndex()539 int32_t GetZIndex() 540 { 541 return zIndex_; 542 } 543 SetZIndex(int32_t index)544 void SetZIndex(int32_t index) 545 { 546 zIndex_ = index; 547 } 548 549 // only panel has ZIndex,others components is default value 0 SetZIndexToChild(int32_t index)550 void SetZIndexToChild(int32_t index) 551 { 552 for (auto& child : children_) { 553 child->SetZIndexToChild(index); 554 } 555 SetZIndex(index); 556 } 557 UpdateRectWithChildRect()558 void UpdateRectWithChildRect() 559 { 560 if (children_.empty()) { 561 return; 562 } 563 SetRect(children_.front()->GetRect()); 564 } 565 #endif 566 567 protected: 568 // inner use, don't need to barrierfree 569 NodeId nodeId_ = -1; 570 int32_t pageId_ = -1; 571 uint32_t windowId_ = 0; 572 bool isRootNode_ = false; 573 std::string inputType_; 574 WeakPtr<AccessibilityNode> parentNode_; 575 std::list<RefPtr<AccessibilityNode>> children_; 576 ActionClickImpl actionClickImpl_; 577 ActionLongClickImpl actionLongClickImpl_; 578 ActionScrollForwardImpl actionScrollForwardImpl_; 579 ActionScrollBackwardImpl actionScrollBackwardImpl_; 580 ActionFocusImpl actionFocusImpl_; 581 ActionUpdateIdsImpl actionUpdateIdsImpl_; 582 ActionAccessibilityFocusImpl actionAccessibilityFocusIdsImpl_; 583 ActionSetTextImpl actionSetTextImpl_; 584 EventMarker onAccessibilityEventId_; 585 EventMarker onClickId_; 586 EventMarker onLongPressId_; 587 EventMarker onSetTextId_; 588 EventMarker onFocusId_; 589 EventMarker onBlurId_; 590 FocusChangeCallback focusChangeEventId_; 591 592 private: 593 void SetOperableInfo(); 594 595 // node attr need to barrierfree 596 size_t listBeginIndex_ = -1; 597 size_t listEndIndex_ = -1; 598 size_t listItemCounts_ = 0; 599 size_t maxTextLength_ = 0; 600 int32_t textSelectionStart_ = 0; 601 int32_t textSelectionEnd_ = 0; 602 std::string tag_; 603 std::string text_; 604 std::string hintText_; 605 std::string errorText_; 606 std::string jsComponentId_; 607 std::string accessibilityLabel_; 608 std::string accessibilityHint_; 609 std::string importantForAccessibility_; 610 AceTextCategory textInputType_ { AceTextCategory::INPUT_TYPE_DEFAULT }; 611 std::vector<int32_t> childIds_; 612 uint64_t supportActions_ = 0; 613 std::unique_ptr<ChartValue> chartValue_; 614 615 Rect globalRect_; 616 Rect rect_; 617 Size marginSize_; 618 union { 619 struct { 620 bool isValidRect_ : 1; 621 bool isChecked_ : 1; 622 bool isEditable_ : 1; 623 bool isEnabled_ : 1; 624 bool accessible_ : 1; 625 bool isFocused_ : 1; 626 bool isSelected_ : 1; 627 bool isCheckable_ : 1; 628 bool isClickable_ : 1; 629 bool isFocusable_ : 1; 630 bool isScrollable_ : 1; 631 bool isLongClickable_ : 1; 632 bool isMultiLine_ : 1; 633 bool isPassword_ : 1; 634 bool visible_ : 1; 635 bool shown_ : 1; 636 bool isClicked_ : 1; 637 bool isAccessibilityFocused_ : 1; 638 }; 639 uint64_t bits_ = 0; 640 }; 641 AccessibilityValue accessibilityValue_; 642 AceCollectionInfo collectionInfo_; 643 AceCollectionItemInfo collectionItemInfo_; 644 645 std::vector<std::pair<std::string, std::string>> attrs_; 646 std::vector<std::pair<std::string, std::string>> styles_; 647 bool clipFlag_ = false; 648 Matrix4 matrix4_; 649 #if defined(PREVIEW) 650 // used for inspector node in PC preview 651 bool isClearRectInfo_ = false; 652 // focus scale or translateScale for inspector node in PC preview 653 double scale_ = 1.0; 654 Offset scaleCenter_ { 0.0, 0.0 }; 655 Offset translateOffset_ { 0.0, 0.0 }; 656 double rotateAngle_ = 0.0; 657 RotateAxis rotateAxis_ = RotateAxis::AXIS_Z; 658 bool isAnimationNode_ = false; 659 int32_t zIndex_ = 0; 660 #endif 661 }; 662 663 } // namespace OHOS::Ace 664 665 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_ACCESSIBILITY_ACCESSIBILITY_NODE_H 666