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_COMPONENTS_NG_EVENT_FOCUS_HUB_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_EVENT_FOCUS_HUB_H 18 19 #include "base/memory/ace_type.h" 20 #include "core/components_ng/base/geometry_node.h" 21 #include "core/components_ng/event/touch_event.h" 22 #include "core/event/key_event.h" 23 #include "core/gestures/gesture_info.h" 24 25 namespace OHOS::Ace::NG { 26 27 class FrameNode; 28 class FocusHub; 29 class EventHub; 30 31 using TabIndexNodeList = std::list<std::pair<int32_t, WeakPtr<FocusHub>>>; 32 constexpr int32_t DEFAULT_TAB_FOCUSED_INDEX = -2; 33 constexpr int32_t NONE_TAB_FOCUSED_INDEX = -1; 34 constexpr int32_t MASK_FOCUS_STEP_FORWARD = 0x10; 35 36 enum class FocusType : int32_t { 37 DISABLE = 0, 38 NODE = 1, 39 SCOPE = 2, 40 }; 41 enum class FocusNodeType : int32_t { 42 DEFAULT = 0, 43 GROUP_DEFAULT = 1, 44 }; 45 enum class ScopeType : int32_t { 46 OTHERS = 0, 47 FLEX = 1, 48 }; 49 enum class FocusStep : int32_t { 50 NONE = 0x0, 51 LEFT = 0x1, 52 UP = 0x2, 53 RIGHT = 0x11, 54 DOWN = 0x12, 55 LEFT_END = 0x3, 56 UP_END = 0x4, 57 RIGHT_END = 0X13, 58 DOWN_END = 0x14, 59 }; 60 61 using GetNextFocusNodeFunc = std::function<void(FocusStep, const WeakPtr<FocusHub>&, WeakPtr<FocusHub>&)>; 62 63 enum class FocusStyleType : int32_t { 64 NONE = -1, 65 INNER_BORDER = 0, 66 OUTER_BORDER = 1, 67 CUSTOM_BORDER = 2, 68 CUSTOM_REGION = 3, 69 }; 70 71 class ACE_EXPORT FocusPaintParam : public virtual AceType { 72 DECLARE_ACE_TYPE(FocusPaintParam, AceType) 73 74 public: 75 FocusPaintParam() = default; 76 ~FocusPaintParam() override = default; 77 HasPaintRect()78 bool HasPaintRect() const 79 { 80 return paintRect.has_value(); 81 } GetPaintRect()82 const RoundRect& GetPaintRect() const 83 { 84 return paintRect.value(); 85 } 86 HasPaintColor()87 bool HasPaintColor() const 88 { 89 return paintColor.has_value(); 90 } GetPaintColor()91 const Color& GetPaintColor() const 92 { 93 return paintColor.value(); 94 } 95 HasPaintWidth()96 bool HasPaintWidth() const 97 { 98 return paintWidth.has_value(); 99 } GetPaintWidth()100 const Dimension& GetPaintWidth() const 101 { 102 return paintWidth.value(); 103 } 104 HasFocusPadding()105 bool HasFocusPadding() const 106 { 107 return focusPadding.has_value(); 108 } GetFocusPadding()109 const Dimension& GetFocusPadding() const 110 { 111 return focusPadding.value(); 112 } SetPaintRect(const RoundRect & rect)113 void SetPaintRect(const RoundRect& rect) 114 { 115 paintRect = rect; 116 } SetPaintColor(const Color & color)117 void SetPaintColor(const Color& color) 118 { 119 paintColor = color; 120 } SetPaintWidth(const Dimension & width)121 void SetPaintWidth(const Dimension& width) 122 { 123 paintWidth = width; 124 } SetFocusPadding(const Dimension & padding)125 void SetFocusPadding(const Dimension& padding) 126 { 127 focusPadding = padding; 128 } 129 130 private: 131 std::optional<RoundRect> paintRect; 132 std::optional<Color> paintColor; 133 std::optional<Dimension> paintWidth; 134 std::optional<Dimension> focusPadding; 135 }; 136 137 class ACE_EXPORT FocusPattern : public virtual AceType { 138 DECLARE_ACE_TYPE(FocusPattern, AceType) 139 140 public: 141 explicit FocusPattern() = default; FocusPattern(FocusType focusType,bool focusable)142 FocusPattern(FocusType focusType, bool focusable) : focusType_(focusType), focusable_(focusable) {} FocusPattern(FocusType focusType,bool focusable,FocusStyleType styleType)143 FocusPattern(FocusType focusType, bool focusable, FocusStyleType styleType) 144 : focusType_(focusType), focusable_(focusable), styleType_(styleType) 145 {} FocusPattern(FocusType focusType,bool focusable,FocusStyleType styleType,const FocusPaintParam & paintParams)146 FocusPattern(FocusType focusType, bool focusable, FocusStyleType styleType, const FocusPaintParam& paintParams) 147 : focusType_(focusType), focusable_(focusable), styleType_(styleType) 148 { 149 if (!paintParams_) { 150 paintParams_ = std::make_unique<FocusPaintParam>(); 151 } 152 if (paintParams.HasPaintRect()) { 153 paintParams_->SetPaintRect(paintParams.GetPaintRect()); 154 } 155 if (paintParams.HasPaintColor()) { 156 paintParams_->SetPaintColor(paintParams.GetPaintColor()); 157 } 158 if (paintParams.HasPaintWidth()) { 159 paintParams_->SetPaintWidth(paintParams.GetPaintWidth()); 160 } 161 if (paintParams.HasFocusPadding()) { 162 paintParams_->SetFocusPadding(paintParams.GetFocusPadding()); 163 } 164 } 165 ~FocusPattern() override = default; 166 GetFocusType()167 FocusType GetFocusType() const 168 { 169 return focusType_; 170 } SetSetFocusType(FocusType type)171 void SetSetFocusType(FocusType type) 172 { 173 focusType_ = type; 174 } 175 GetFocusable()176 bool GetFocusable() const 177 { 178 return focusable_; 179 } SetSetFocusable(bool focusable)180 void SetSetFocusable(bool focusable) 181 { 182 focusable_ = focusable; 183 } 184 GetStyleType()185 FocusStyleType GetStyleType() const 186 { 187 return styleType_; 188 } SetStyleType(FocusStyleType styleType)189 void SetStyleType(FocusStyleType styleType) 190 { 191 styleType_ = styleType; 192 } 193 GetFocusPaintParams()194 const std::unique_ptr<FocusPaintParam>& GetFocusPaintParams() const 195 { 196 return paintParams_; 197 } SetFocusPaintParams(const FocusPaintParam & paintParams)198 void SetFocusPaintParams(const FocusPaintParam& paintParams) 199 { 200 if (!paintParams_) { 201 paintParams_ = std::make_unique<FocusPaintParam>(); 202 } 203 if (paintParams.HasPaintRect()) { 204 paintParams_->SetPaintRect(paintParams.GetPaintRect()); 205 } 206 if (paintParams.HasPaintColor()) { 207 paintParams_->SetPaintColor(paintParams.GetPaintColor()); 208 } 209 if (paintParams.HasPaintWidth()) { 210 paintParams_->SetPaintWidth(paintParams.GetPaintWidth()); 211 } 212 if (paintParams.HasFocusPadding()) { 213 paintParams_->SetFocusPadding(paintParams.GetFocusPadding()); 214 } 215 } 216 217 private: 218 FocusType focusType_ = FocusType::DISABLE; 219 bool focusable_ = false; 220 FocusStyleType styleType_ = FocusStyleType::NONE; 221 std::unique_ptr<FocusPaintParam> paintParams_ = nullptr; 222 }; 223 224 struct ScopeFocusAlgorithm final { 225 ScopeFocusAlgorithm() = default; ScopeFocusAlgorithmfinal226 ScopeFocusAlgorithm(bool isVertical, bool isLeftToRight, ScopeType scopeType) 227 : isVertical(isVertical), isLeftToRight(isLeftToRight), scopeType(scopeType) 228 {} ScopeFocusAlgorithmfinal229 ScopeFocusAlgorithm(bool isVertical, bool isLeftToRight, ScopeType scopeType, GetNextFocusNodeFunc&& function) 230 : isVertical(isVertical), isLeftToRight(isLeftToRight), scopeType(scopeType), 231 getNextFocusNode(std::move(function)) 232 {} 233 ~ScopeFocusAlgorithm() = default; 234 235 bool isVertical { true }; 236 bool isLeftToRight { true }; 237 ScopeType scopeType { ScopeType::OTHERS }; 238 GetNextFocusNodeFunc getNextFocusNode; 239 }; 240 241 class ACE_EXPORT FocusCallbackEvents : public virtual AceType { 242 DECLARE_ACE_TYPE(FocusCallbackEvents, AceType) 243 public: 244 explicit FocusCallbackEvents() = default; 245 ~FocusCallbackEvents() override = default; 246 SetOnFocusCallback(OnFocusFunc && onFocusCallback)247 void SetOnFocusCallback(OnFocusFunc&& onFocusCallback) 248 { 249 onFocusCallback_ = std::move(onFocusCallback); 250 } GetOnFocusCallback()251 const OnFocusFunc& GetOnFocusCallback() 252 { 253 return onFocusCallback_; 254 } 255 SetOnBlurCallback(OnBlurFunc && onBlurCallback)256 void SetOnBlurCallback(OnBlurFunc&& onBlurCallback) 257 { 258 onBlurCallback_ = std::move(onBlurCallback); 259 } GetOnBlurCallback()260 const OnBlurFunc& GetOnBlurCallback() 261 { 262 return onBlurCallback_; 263 } 264 SetOnKeyEventCallback(OnKeyCallbackFunc && onKeyEventCallback)265 void SetOnKeyEventCallback(OnKeyCallbackFunc&& onKeyEventCallback) 266 { 267 onKeyEventCallback_ = std::move(onKeyEventCallback); 268 } GetOnKeyEventCallback()269 const OnKeyCallbackFunc& GetOnKeyEventCallback() 270 { 271 return onKeyEventCallback_; 272 } 273 SetOnClickCallback(GestureEventFunc && onClickEventCallback)274 void SetOnClickCallback(GestureEventFunc&& onClickEventCallback) 275 { 276 onClickEventCallback_ = std::move(onClickEventCallback); 277 } GetOnClickCallback()278 const GestureEventFunc& GetOnClickCallback() 279 { 280 return onClickEventCallback_; 281 } 282 GetTabIndex()283 int32_t GetTabIndex() const 284 { 285 return tabIndex_; 286 } SetTabIndex(int32_t tabIndex)287 void SetTabIndex(int32_t tabIndex) 288 { 289 tabIndex_ = tabIndex; 290 } 291 IsDefaultFocus()292 bool IsDefaultFocus() const 293 { 294 return isDefaultFocus_; 295 } SetIsDefaultFocus(bool isDefaultFocus)296 void SetIsDefaultFocus(bool isDefaultFocus) 297 { 298 isDefaultFocus_ = isDefaultFocus; 299 } 300 IsDefaultGroupFocus()301 bool IsDefaultGroupFocus() const 302 { 303 return isDefaultGroupFocus_; 304 } SetIsDefaultGroupFocus(bool isDefaultGroupFocus)305 void SetIsDefaultGroupFocus(bool isDefaultGroupFocus) 306 { 307 isDefaultGroupFocus_ = isDefaultGroupFocus; 308 } 309 IsFocusOnTouch()310 std::optional<bool> IsFocusOnTouch() const 311 { 312 return isFocusOnTouch_; 313 } SetIsFocusOnTouch(bool isFocusOnTouch)314 void SetIsFocusOnTouch(bool isFocusOnTouch) 315 { 316 isFocusOnTouch_ = isFocusOnTouch; 317 } 318 SetIsDefaultHasFocused(bool isDefaultHasFocused)319 void SetIsDefaultHasFocused(bool isDefaultHasFocused) 320 { 321 isDefaultHasFocused_ = isDefaultHasFocused; 322 } IsDefaultHasFocused()323 bool IsDefaultHasFocused() const 324 { 325 return isDefaultHasFocused_; 326 } 327 SetIsDefaultGroupHasFocused(bool isDefaultGroupHasFocused)328 void SetIsDefaultGroupHasFocused(bool isDefaultGroupHasFocused) 329 { 330 isDefaultGroupHasFocused_ = isDefaultGroupHasFocused; 331 } IsDefaultGroupHasFocused()332 bool IsDefaultGroupHasFocused() const 333 { 334 return isDefaultGroupHasFocused_; 335 } 336 337 private: 338 OnFocusFunc onFocusCallback_; 339 OnBlurFunc onBlurCallback_; 340 OnKeyCallbackFunc onKeyEventCallback_; 341 GestureEventFunc onClickEventCallback_; 342 343 std::optional<bool> isFocusOnTouch_; 344 bool isDefaultFocus_ = { false }; 345 bool isDefaultHasFocused_ = { false }; 346 bool isDefaultGroupFocus_ = { false }; 347 bool isDefaultGroupHasFocused_ { false }; 348 349 int32_t tabIndex_ = 0; 350 }; 351 352 class ACE_EXPORT FocusHub : public virtual AceType { DECLARE_ACE_TYPE(FocusHub,AceType)353 DECLARE_ACE_TYPE(FocusHub, AceType) 354 public: 355 explicit FocusHub(const WeakPtr<EventHub>& eventHub, FocusType type = FocusType::DISABLE, bool focusable = false) 356 : eventHub_(eventHub), focusable_(focusable), focusType_(type) 357 {} 358 ~FocusHub() override = default; 359 SetFocusStyleType(FocusStyleType type)360 void SetFocusStyleType(FocusStyleType type) 361 { 362 focusStyleType_ = type; 363 } SetFocusPaintParamsPtr(const std::unique_ptr<FocusPaintParam> & paramsPtr)364 void SetFocusPaintParamsPtr(const std::unique_ptr<FocusPaintParam>& paramsPtr) 365 { 366 CHECK_NULL_VOID(paramsPtr); 367 if (!focusPaintParamsPtr_) { 368 focusPaintParamsPtr_ = std::make_unique<FocusPaintParam>(); 369 } 370 if (paramsPtr->HasPaintRect()) { 371 focusPaintParamsPtr_->SetPaintRect(paramsPtr->GetPaintRect()); 372 } 373 if (paramsPtr->HasPaintColor()) { 374 focusPaintParamsPtr_->SetPaintColor(paramsPtr->GetPaintColor()); 375 } 376 if (paramsPtr->HasPaintWidth()) { 377 focusPaintParamsPtr_->SetPaintWidth(paramsPtr->GetPaintWidth()); 378 } 379 if (paramsPtr->HasFocusPadding()) { 380 focusPaintParamsPtr_->SetFocusPadding(paramsPtr->GetFocusPadding()); 381 } 382 } 383 HasPaintRect()384 bool HasPaintRect() const 385 { 386 return focusPaintParamsPtr_ ? focusPaintParamsPtr_->HasPaintRect() : false; 387 } GetPaintRect()388 RoundRect GetPaintRect() const 389 { 390 CHECK_NULL_RETURN(focusPaintParamsPtr_, RoundRect()); 391 return focusPaintParamsPtr_->GetPaintRect(); 392 } 393 HasPaintColor()394 bool HasPaintColor() const 395 { 396 return focusPaintParamsPtr_ ? focusPaintParamsPtr_->HasPaintColor() : false; 397 } GetPaintColor()398 const Color& GetPaintColor() const 399 { 400 CHECK_NULL_RETURN(focusPaintParamsPtr_, Color::TRANSPARENT); 401 return focusPaintParamsPtr_->GetPaintColor(); 402 } 403 HasPaintWidth()404 bool HasPaintWidth() const 405 { 406 return focusPaintParamsPtr_ ? focusPaintParamsPtr_->HasPaintWidth() : false; 407 } GetPaintWidth()408 Dimension GetPaintWidth() const 409 { 410 CHECK_NULL_RETURN(focusPaintParamsPtr_, Dimension()); 411 return focusPaintParamsPtr_->GetPaintWidth(); 412 } 413 HasFocusPadding()414 bool HasFocusPadding() const 415 { 416 return focusPaintParamsPtr_ ? focusPaintParamsPtr_->HasFocusPadding() : false; 417 } GetFocusPadding()418 Dimension GetFocusPadding() const 419 { 420 CHECK_NULL_RETURN(focusPaintParamsPtr_, Dimension()); 421 return focusPaintParamsPtr_->GetFocusPadding(); 422 } SetPaintRect(const RoundRect & rect)423 void SetPaintRect(const RoundRect& rect) 424 { 425 if (!focusPaintParamsPtr_) { 426 focusPaintParamsPtr_ = std::unique_ptr<FocusPaintParam>(); 427 } 428 focusPaintParamsPtr_->SetPaintRect(rect); 429 } SetPaintColor(const Color & color)430 void SetPaintColor(const Color& color) 431 { 432 if (!focusPaintParamsPtr_) { 433 focusPaintParamsPtr_ = std::unique_ptr<FocusPaintParam>(); 434 } 435 focusPaintParamsPtr_->SetPaintColor(color); 436 } SetPaintWidth(const Dimension & width)437 void SetPaintWidth(const Dimension& width) 438 { 439 if (!focusPaintParamsPtr_) { 440 focusPaintParamsPtr_ = std::unique_ptr<FocusPaintParam>(); 441 } 442 focusPaintParamsPtr_->SetPaintWidth(width); 443 } SetFocusPadding(const Dimension & padding)444 void SetFocusPadding(const Dimension& padding) 445 { 446 if (!focusPaintParamsPtr_) { 447 focusPaintParamsPtr_ = std::unique_ptr<FocusPaintParam>(); 448 } 449 focusPaintParamsPtr_->SetFocusPadding(padding); 450 } 451 452 RefPtr<FrameNode> GetFrameNode() const; 453 RefPtr<GeometryNode> GetGeometryNode() const; 454 RefPtr<FocusHub> GetParentFocusHub() const; 455 std::string GetFrameName() const; 456 int32_t GetFrameId() const; 457 458 bool HandleKeyEvent(const KeyEvent& keyEvent); 459 bool RequestFocusImmediately(); 460 void RequestFocus() const; 461 void UpdateAccessibilityFocusInfo(); 462 void SwitchFocus(const RefPtr<FocusHub>& focusNode); 463 464 void LostFocus(BlurReason reason = BlurReason::FOCUS_SWITCH); 465 void LostSelfFocus(); 466 void RemoveSelf(); 467 void RemoveChild(const RefPtr<FocusHub>& focusNode); 468 bool GoToNextFocusLinear(bool reverse, const RectF& rect = RectF()); 469 bool TryRequestFocus(const RefPtr<FocusHub>& focusNode, const RectF& rect); 470 471 bool AcceptFocusOfLastFocus(); 472 bool AcceptFocusByRectOfLastFocus(const RectF& rect); 473 bool AcceptFocusByRectOfLastFocusNode(const RectF& rect); 474 bool AcceptFocusByRectOfLastFocusScope(const RectF& rect); 475 bool AcceptFocusByRectOfLastFocusFlex(const RectF& rect); 476 477 void CollectTabIndexNodes(TabIndexNodeList& tabIndexNodes); 478 bool GoToFocusByTabNodeIdx(TabIndexNodeList& tabIndexNodes, int32_t tabNodeIdx); 479 bool HandleFocusByTabIndex(const KeyEvent& event, const RefPtr<FocusHub>& mainFocusHub); 480 RefPtr<FocusHub> GetChildFocusNodeByType(FocusNodeType nodeType = FocusNodeType::DEFAULT); 481 RefPtr<FocusHub> GetChildFocusNodeById(const std::string& id); 482 int32_t GetFocusingTabNodeIdx(TabIndexNodeList& tabIndexNodes); 483 bool RequestFocusImmediatelyById(const std::string& id); 484 485 bool IsFocusableByTab(); 486 bool IsFocusableNodeByTab(); 487 bool IsFocusableScopeByTab(); 488 489 bool IsFocusableWholePath(); 490 491 bool IsFocusable(); 492 bool IsFocusableNode(); 493 bool IsFocusableScope(); 494 IsParentFocusable()495 bool IsParentFocusable() const 496 { 497 return parentFocusable_; 498 } SetParentFocusable(bool parentFocusable)499 void SetParentFocusable(bool parentFocusable) 500 { 501 parentFocusable_ = parentFocusable; 502 } 503 504 void RefreshParentFocusable(bool focusable); 505 506 void RefreshFocus(); 507 508 void SetFocusable(bool focusable); 509 void SetShow(bool show); 510 void SetShowNode(bool show); 511 void SetShowScope(bool show); 512 void SetEnabled(bool enabled); 513 void SetEnabledNode(bool enabled); 514 void SetEnabledScope(bool enabled); CanShow()515 bool CanShow() const 516 { 517 return show_; 518 } 519 520 bool IsEnabled() const; 521 IsCurrentFocus()522 bool IsCurrentFocus() const 523 { 524 return currentFocus_; 525 } 526 SetOnFocusCallback(OnFocusFunc && onFocusCallback)527 void SetOnFocusCallback(OnFocusFunc&& onFocusCallback) 528 { 529 if (!focusCallbackEvents_) { 530 focusCallbackEvents_ = MakeRefPtr<FocusCallbackEvents>(); 531 } 532 focusCallbackEvents_->SetOnFocusCallback(std::move(onFocusCallback)); 533 } GetOnFocusCallback()534 OnFocusFunc GetOnFocusCallback() 535 { 536 return focusCallbackEvents_ ? focusCallbackEvents_->GetOnFocusCallback() : nullptr; 537 } 538 SetOnBlurCallback(OnBlurFunc && onBlurCallback)539 void SetOnBlurCallback(OnBlurFunc&& onBlurCallback) 540 { 541 if (!focusCallbackEvents_) { 542 focusCallbackEvents_ = MakeRefPtr<FocusCallbackEvents>(); 543 } 544 focusCallbackEvents_->SetOnBlurCallback(std::move(onBlurCallback)); 545 } GetOnBlurCallback()546 OnBlurFunc GetOnBlurCallback() 547 { 548 return focusCallbackEvents_ ? focusCallbackEvents_->GetOnBlurCallback() : nullptr; 549 } 550 SetOnKeyCallback(OnKeyCallbackFunc && onKeyCallback)551 void SetOnKeyCallback(OnKeyCallbackFunc&& onKeyCallback) 552 { 553 if (!focusCallbackEvents_) { 554 focusCallbackEvents_ = MakeRefPtr<FocusCallbackEvents>(); 555 } 556 focusCallbackEvents_->SetOnKeyEventCallback(std::move(onKeyCallback)); 557 } GetOnKeyCallback()558 OnKeyCallbackFunc GetOnKeyCallback() 559 { 560 return focusCallbackEvents_ ? focusCallbackEvents_->GetOnKeyEventCallback() : nullptr; 561 } 562 SetOnClickCallback(GestureEventFunc && onClickCallback)563 void SetOnClickCallback(GestureEventFunc&& onClickCallback) 564 { 565 if (!focusCallbackEvents_) { 566 focusCallbackEvents_ = MakeRefPtr<FocusCallbackEvents>(); 567 } 568 focusCallbackEvents_->SetOnClickCallback(std::move(onClickCallback)); 569 } GetOnClickCallback()570 GestureEventFunc GetOnClickCallback() 571 { 572 return focusCallbackEvents_ ? focusCallbackEvents_->GetOnClickCallback() : nullptr; 573 } 574 SetOnFocusInternal(OnFocusFunc && onFocusInternal)575 void SetOnFocusInternal(OnFocusFunc&& onFocusInternal) 576 { 577 onFocusInternal_ = std::move(onFocusInternal); 578 } SetOnBlurInternal(OnBlurFunc && onBlurInternal)579 void SetOnBlurInternal(OnBlurFunc&& onBlurInternal) 580 { 581 onBlurInternal_ = std::move(onBlurInternal); 582 } SetOnBlurReasonInternal(OnBlurReasonFunc && onBlurReasonInternal)583 void SetOnBlurReasonInternal(OnBlurReasonFunc&& onBlurReasonInternal) 584 { 585 onBlurReasonInternal_ = std::move(onBlurReasonInternal); 586 } SetOnPreFocusCallback(OnPreFocusFunc && onPreFocusCallback)587 void SetOnPreFocusCallback(OnPreFocusFunc&& onPreFocusCallback) 588 { 589 onPreFocusCallback_ = std::move(onPreFocusCallback); 590 } SetOnKeyEventInternal(OnKeyEventFunc && onKeyEventInternal)591 void SetOnKeyEventInternal(OnKeyEventFunc&& onKeyEventInternal) 592 { 593 onKeyEventInternal_ = std::move(onKeyEventInternal); 594 } 595 596 std::list<RefPtr<FocusHub>>::iterator FlushChildrenFocusHub(std::list<RefPtr<FocusHub>>& focusNodes); 597 GetChildren()598 std::list<RefPtr<FocusHub>> GetChildren() 599 { 600 std::list<RefPtr<FocusHub>> focusNodes; 601 FlushChildrenFocusHub(focusNodes); 602 return focusNodes; 603 } 604 IsChild()605 bool IsChild() const 606 { 607 return focusType_ == FocusType::NODE; 608 } 609 SetRect(const RectF & rect)610 void SetRect(const RectF& rect) 611 { 612 rectFromOrigin_ = rect; 613 } GetRect()614 const RectF& GetRect() const 615 { 616 return rectFromOrigin_; 617 } 618 619 void DumpFocusTree(int32_t depth); 620 void DumpFocusNodeTree(int32_t depth); 621 void DumpFocusScopeTree(int32_t depth); 622 623 void OnClick(const KeyEvent& event); 624 SetFocusType(FocusType type)625 void SetFocusType(FocusType type) 626 { 627 focusType_ = type; 628 } GetFocusType()629 FocusType GetFocusType() const 630 { 631 return focusType_; 632 } 633 GetTabIndex()634 int32_t GetTabIndex() const 635 { 636 return focusCallbackEvents_ ? focusCallbackEvents_->GetTabIndex() : 0; 637 } SetTabIndex(int32_t tabIndex)638 void SetTabIndex(int32_t tabIndex) 639 { 640 if (!focusCallbackEvents_) { 641 focusCallbackEvents_ = MakeRefPtr<FocusCallbackEvents>(); 642 } 643 focusCallbackEvents_->SetTabIndex(tabIndex); 644 } 645 IsDefaultFocus()646 bool IsDefaultFocus() const 647 { 648 return focusCallbackEvents_ ? focusCallbackEvents_->IsDefaultFocus() : false; 649 } SetIsDefaultFocus(bool isDefaultFocus)650 void SetIsDefaultFocus(bool isDefaultFocus) 651 { 652 if (!focusCallbackEvents_) { 653 focusCallbackEvents_ = MakeRefPtr<FocusCallbackEvents>(); 654 } 655 focusCallbackEvents_->SetIsDefaultFocus(isDefaultFocus); 656 } 657 IsDefaultGroupFocus()658 bool IsDefaultGroupFocus() const 659 { 660 return focusCallbackEvents_ ? focusCallbackEvents_->IsDefaultGroupFocus() : false; 661 } SetIsDefaultGroupFocus(bool isDefaultGroupFocus)662 void SetIsDefaultGroupFocus(bool isDefaultGroupFocus) 663 { 664 if (!focusCallbackEvents_) { 665 focusCallbackEvents_ = MakeRefPtr<FocusCallbackEvents>(); 666 } 667 focusCallbackEvents_->SetIsDefaultGroupFocus(isDefaultGroupFocus); 668 } 669 IsFocusOnTouch()670 std::optional<bool> IsFocusOnTouch() const 671 { 672 return focusCallbackEvents_ ? focusCallbackEvents_->IsFocusOnTouch() : std::nullopt; 673 } 674 void SetIsFocusOnTouch(bool isFocusOnTouch); 675 SetIsDefaultHasFocused(bool isDefaultHasFocused)676 void SetIsDefaultHasFocused(bool isDefaultHasFocused) 677 { 678 if (!focusCallbackEvents_) { 679 focusCallbackEvents_ = MakeRefPtr<FocusCallbackEvents>(); 680 } 681 focusCallbackEvents_->SetIsDefaultHasFocused(isDefaultHasFocused); 682 } IsDefaultHasFocused()683 bool IsDefaultHasFocused() const 684 { 685 return focusCallbackEvents_ ? focusCallbackEvents_->IsDefaultHasFocused() : false; 686 } 687 SetIsDefaultGroupHasFocused(bool isDefaultGroupHasFocused)688 void SetIsDefaultGroupHasFocused(bool isDefaultGroupHasFocused) 689 { 690 if (!focusCallbackEvents_) { 691 focusCallbackEvents_ = MakeRefPtr<FocusCallbackEvents>(); 692 } 693 focusCallbackEvents_->SetIsDefaultGroupHasFocused(isDefaultGroupHasFocused); 694 } IsDefaultGroupHasFocused()695 bool IsDefaultGroupHasFocused() const 696 { 697 return focusCallbackEvents_ ? focusCallbackEvents_->IsDefaultGroupHasFocused() : false; 698 } 699 700 std::optional<std::string> GetInspectorKey() const; 701 702 void PaintFocusState(); 703 void PaintAllFocusState(); 704 void ClearFocusState(); 705 void ClearAllFocusState(); 706 void PaintInnerFocusState(const RoundRect& paintRect); 707 SetInnerFocusPaintRectCallback(const std::function<void (RoundRect &)> & callback)708 void SetInnerFocusPaintRectCallback(const std::function<void(RoundRect&)>& callback) 709 { 710 getInnerFocusRectFunc_ = callback; 711 } 712 SetLastWeakFocusNode(const WeakPtr<FocusHub> & focusHub)713 void SetLastWeakFocusNode(const WeakPtr<FocusHub>& focusHub) 714 { 715 lastWeakFocusNode_ = focusHub; 716 } GetLastWeakFocusNode()717 WeakPtr<FocusHub> GetLastWeakFocusNode() const 718 { 719 return lastWeakFocusNode_; 720 } 721 IsFocusStepVertical(FocusStep step)722 static inline bool IsFocusStepVertical(FocusStep step) 723 { 724 return (static_cast<uint32_t>(step) & 0x1) == 0; 725 } IsFocusStepForward(FocusStep step)726 static inline bool IsFocusStepForward(FocusStep step) 727 { 728 return (static_cast<uint32_t>(step) & MASK_FOCUS_STEP_FORWARD) != 0; 729 } 730 731 protected: 732 bool OnKeyEvent(const KeyEvent& keyEvent); 733 bool OnKeyEventNode(const KeyEvent& keyEvent); 734 bool OnKeyEventScope(const KeyEvent& keyEvent); 735 736 bool CalculateRect(const RefPtr<FocusHub>& childNode, RectF& rect) const; 737 bool RequestNextFocus(FocusStep moveStep, const RectF& rect); 738 739 void OnFocus(); 740 void OnFocusNode(); 741 void OnFocusScope(); 742 void OnBlur(); 743 void OnBlurNode(); 744 void OnBlurScope(); 745 HandleFocus()746 void HandleFocus() 747 { 748 // Need update: void RenderNode::MoveWhenOutOfViewPort(bool hasEffect) 749 OnFocus(); 750 } 751 752 private: 753 bool CalculatePosition(); 754 755 void SetScopeFocusAlgorithm(); 756 757 void CheckFocusStateStyle(bool onFocus); 758 759 OnFocusFunc onFocusInternal_; 760 OnBlurFunc onBlurInternal_; 761 OnBlurReasonFunc onBlurReasonInternal_; 762 OnKeyEventFunc onKeyEventInternal_; 763 OnPreFocusFunc onPreFocusCallback_; 764 765 RefPtr<FocusCallbackEvents> focusCallbackEvents_; 766 767 RefPtr<TouchEventImpl> focusOnTouchListener_; 768 769 WeakPtr<EventHub> eventHub_; 770 771 WeakPtr<FocusHub> lastWeakFocusNode_ { nullptr }; 772 773 bool focusable_ { true }; 774 bool parentFocusable_ { true }; 775 bool currentFocus_ { false }; 776 bool isFirstFocusInPage_ { true }; 777 bool show_ { true }; 778 779 FocusType focusType_ = FocusType::DISABLE; 780 FocusStyleType focusStyleType_ = FocusStyleType::NONE; 781 std::unique_ptr<FocusPaintParam> focusPaintParamsPtr_; 782 std::function<void(RoundRect&)> getInnerFocusRectFunc_; 783 784 RectF rectFromOrigin_; 785 ScopeFocusAlgorithm focusAlgorithm_; 786 BlurReason blurReason_ = BlurReason::FOCUS_SWITCH; 787 }; 788 } // namespace OHOS::Ace::NG 789 790 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_EVENT_FOCUS_HUB_H