• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "focus_event_handler.h"
20 #include "core/components_ng/base/geometry_node.h"
21 #include "core/components_ng/event/focus_box.h"
22 #include "core/components_ng/event/touch_event.h"
23 #include "core/event/key_event.h"
24 #include "core/gestures/gesture_event.h"
25 
26 namespace OHOS::Ace::NG {
27 
28 class FocusView;
29 class FocusManager;
30 
31 using TabIndexNodeList = std::list<std::pair<int32_t, WeakPtr<FocusHub>>>;
32 using OnGetNextFocusNodeFunc = std::function<RefPtr<FocusHub>(FocusReason, FocusIntension)>;
33 constexpr int32_t DEFAULT_TAB_FOCUSED_INDEX = -2;
34 constexpr int32_t NONE_TAB_FOCUSED_INDEX = -1;
35 constexpr int32_t MASK_FOCUS_STEP_VERTICAL = 0x01;
36 constexpr int32_t MASK_FOCUS_STEP_FORWARD = 0x10;
37 constexpr int32_t MASK_FOCUS_STEP_TAB = 0x5;
38 
39 enum class FocusNodeType : int32_t {
40     DEFAULT = 0,
41     GROUP_DEFAULT = 1,
42 };
43 enum class ScopeType : int32_t {
44     OTHERS = 0,
45     FLEX = 1,
46     PROJECT_AREA = 2,
47 };
48 enum class FocusStep : int32_t {
49     NONE = 0x0,
50     LEFT = 0x1,
51     UP = 0x2,
52     RIGHT = 0x11,
53     DOWN = 0x12,
54     LEFT_END = 0x3,
55     UP_END = 0x4,
56     RIGHT_END = 0X13,
57     DOWN_END = 0x14,
58     SHIFT_TAB = 0x5,
59     TAB = 0x15,
60 };
61 enum class RequestFocusResult : int32_t {
62     DEFAULT = 0,
63     NON_FOCUSABLE = 1,
64     NON_FOCUSABLE_ANCESTOR = 2,
65     NON_EXIST = 3,
66     NON_FOCUSABLE_BY_TAB = 4,
67 };
68 enum class SwitchingStartReason : int32_t {
69     DEFAULT = 0,
70     WINDOW_FOCUS = 1,
71     REQUEST_FOCUS = 2,
72     LOST_FOCUS_TO_VIEW_ROOT = 3,
73     REMOVE_SELF = 4,
74     REMOVE_CHILD = 5,
75     LOST_FOCUS_TO_TABSTOP = 6,
76 };
77 enum class SwitchingEndReason : int32_t {
78     DEFAULT = 0,
79     FOCUS_GUARD_DESTROY = 1,
80     DEPENDENCE_SELF = 2,
81     NO_FOCUSABLE_CHILD = 3,
82     NODE_FOCUS = 4,
83     TAB_STOP = 5,
84 };
85 enum class SwitchingUpdateReason : int32_t {
86     DEFAULT = 0,
87     SWITCH_FOCUS = 1,
88     ON_FOCUS_NODE = 2,
89 };
90 
91 using GetNextFocusNodeFunc = std::function<bool(FocusStep, const WeakPtr<FocusHub>&, WeakPtr<FocusHub>&)>;
92 
93 enum class FocusStyleType : int32_t {
94     NONE = -1,
95     INNER_BORDER = 0,
96     OUTER_BORDER = 1,
97     CUSTOM_BORDER = 2,
98     CUSTOM_REGION = 3,
99     FORCE_BORDER = 4,
100     FORCE_NONE = 5,
101 };
102 
103 enum class FocusDependence : int32_t {
104     CHILD = 0,
105     SELF = 1,
106     AUTO = 2,
107 };
108 
109 enum class FocusPriority : int32_t {
110     AUTO = 0,
111     PRIOR = 2000,
112     PREVIOUS = 3000,
113 };
114 
115 class ACE_EXPORT FocusPaintParam : public virtual AceType {
116     DECLARE_ACE_TYPE(FocusPaintParam, AceType);
117 
118 public:
119     FocusPaintParam() = default;
120     ~FocusPaintParam() override = default;
121 
HasPaintRect()122     bool HasPaintRect() const
123     {
124         return paintRect.has_value();
125     }
GetPaintRect()126     const RoundRect& GetPaintRect() const
127     {
128         return paintRect.value();
129     }
130 
HasPaintColor()131     bool HasPaintColor() const
132     {
133         return paintColor.has_value();
134     }
GetPaintColor()135     const Color& GetPaintColor() const
136     {
137         return paintColor.value();
138     }
139 
HasPaintWidth()140     bool HasPaintWidth() const
141     {
142         return paintWidth.has_value();
143     }
GetPaintWidth()144     const Dimension& GetPaintWidth() const
145     {
146         return paintWidth.value();
147     }
148 
HasFocusPadding()149     bool HasFocusPadding() const
150     {
151         return focusPadding.has_value();
152     }
GetFocusPadding()153     const Dimension& GetFocusPadding() const
154     {
155         return focusPadding.value();
156     }
SetPaintRect(const RoundRect & rect)157     void SetPaintRect(const RoundRect& rect)
158     {
159         paintRect = rect;
160     }
SetPaintColor(const Color & color)161     void SetPaintColor(const Color& color)
162     {
163         paintColor = color;
164     }
SetPaintWidth(const Dimension & width)165     void SetPaintWidth(const Dimension& width)
166     {
167         paintWidth = width;
168     }
SetFocusPadding(const Dimension & padding)169     void SetFocusPadding(const Dimension& padding)
170     {
171         focusPadding = padding;
172     }
173 
174 private:
175     std::optional<RoundRect> paintRect;
176     std::optional<Color> paintColor;
177     std::optional<Dimension> paintWidth;
178     std::optional<Dimension> focusPadding;
179 };
180 
181 class ACE_EXPORT FocusPattern : public virtual AceType {
182     DECLARE_ACE_TYPE(FocusPattern, AceType);
183 
184 public:
185     FocusPattern() = default;
FocusPattern(FocusType focusType,bool focusable)186     FocusPattern(FocusType focusType, bool focusable) : focusType_(focusType), focusable_(focusable) {}
FocusPattern(FocusType focusType,bool focusable,FocusStyleType styleType)187     FocusPattern(FocusType focusType, bool focusable, FocusStyleType styleType)
188         : focusType_(focusType), focusable_(focusable), styleType_(styleType)
189     {}
FocusPattern(FocusType focusType,bool focusable,FocusStyleType styleType,const FocusPaintParam & paintParams)190     FocusPattern(FocusType focusType, bool focusable, FocusStyleType styleType, const FocusPaintParam& paintParams)
191         : focusType_(focusType), focusable_(focusable), styleType_(styleType)
192     {
193         if (!paintParams_) {
194             paintParams_ = std::make_unique<FocusPaintParam>();
195         }
196         if (paintParams.HasPaintRect()) {
197             paintParams_->SetPaintRect(paintParams.GetPaintRect());
198         }
199         if (paintParams.HasPaintColor()) {
200             paintParams_->SetPaintColor(paintParams.GetPaintColor());
201         }
202         if (paintParams.HasPaintWidth()) {
203             paintParams_->SetPaintWidth(paintParams.GetPaintWidth());
204         }
205         if (paintParams.HasFocusPadding()) {
206             paintParams_->SetFocusPadding(paintParams.GetFocusPadding());
207         }
208     }
FocusPattern(const FocusPattern & focusPattern)209     FocusPattern(const FocusPattern& focusPattern)
210     {
211         focusType_ = focusPattern.GetFocusType();
212         focusable_ = focusPattern.GetFocusable();
213         styleType_ = focusPattern.GetStyleType();
214         if (focusPattern.GetFocusPaintParams()) {
215             SetFocusPaintParams(*focusPattern.GetFocusPaintParams());
216         }
217         isFocusActiveWhenFocused_ = focusPattern.GetIsFocusActiveWhenFocused();
218     }
219     ~FocusPattern() override = default;
220 
GetFocusType()221     FocusType GetFocusType() const
222     {
223         return focusType_;
224     }
SetFocusType(FocusType type)225     void SetFocusType(FocusType type)
226     {
227         focusType_ = type;
228     }
229 
GetFocusable()230     bool GetFocusable() const
231     {
232         return focusable_;
233     }
SetFocusable(bool focusable)234     void SetFocusable(bool focusable)
235     {
236         focusable_ = focusable;
237     }
238 
GetStyleType()239     FocusStyleType GetStyleType() const
240     {
241         return styleType_;
242     }
SetStyleType(FocusStyleType styleType)243     void SetStyleType(FocusStyleType styleType)
244     {
245         styleType_ = styleType;
246     }
247 
GetFocusPaintParams()248     const std::unique_ptr<FocusPaintParam>& GetFocusPaintParams() const
249     {
250         return paintParams_;
251     }
SetFocusPaintParams(const FocusPaintParam & paintParams)252     void SetFocusPaintParams(const FocusPaintParam& paintParams)
253     {
254         if (!paintParams_) {
255             paintParams_ = std::make_unique<FocusPaintParam>();
256         }
257         if (paintParams.HasPaintRect()) {
258             paintParams_->SetPaintRect(paintParams.GetPaintRect());
259         }
260         if (paintParams.HasPaintColor()) {
261             paintParams_->SetPaintColor(paintParams.GetPaintColor());
262         }
263         if (paintParams.HasPaintWidth()) {
264             paintParams_->SetPaintWidth(paintParams.GetPaintWidth());
265         }
266         if (paintParams.HasFocusPadding()) {
267             paintParams_->SetFocusPadding(paintParams.GetFocusPadding());
268         }
269     }
270 
GetIsFocusActiveWhenFocused()271     bool GetIsFocusActiveWhenFocused() const
272     {
273         return isFocusActiveWhenFocused_;
274     }
SetIsFocusActiveWhenFocused(bool value)275     void SetIsFocusActiveWhenFocused(bool value)
276     {
277         isFocusActiveWhenFocused_ = value;
278     }
279 
280 private:
281     FocusType focusType_ = FocusType::DISABLE;
282     bool focusable_ = false;
283     FocusStyleType styleType_ = FocusStyleType::NONE;
284     std::unique_ptr<FocusPaintParam> paintParams_ = nullptr;
285     bool isFocusActiveWhenFocused_ = false;
286 };
287 
288 enum class ScopeFocusDirection {
289     VERTICAL = 0,
290     HORIZONTAL,
291     UNIVERSAL,
292 };
293 
294 struct ScopeFocusAlgorithm final {
295     ScopeFocusAlgorithm() = default;
ScopeFocusAlgorithmfinal296     ScopeFocusAlgorithm(bool isVertical, bool isLeftToRight, ScopeType scopeType)
297         : isVertical(isVertical), isLeftToRight(isLeftToRight), scopeType(scopeType)
298     {}
ScopeFocusAlgorithmfinal299     ScopeFocusAlgorithm(bool isVertical, bool isLeftToRight, ScopeType scopeType, GetNextFocusNodeFunc&& function)
300         : isVertical(isVertical), isLeftToRight(isLeftToRight), scopeType(scopeType),
301           getNextFocusNode(std::move(function))
302     {}
ScopeFocusAlgorithmfinal303     ScopeFocusAlgorithm(ScopeFocusDirection direction, bool isVertical, bool isLeftToRight, ScopeType scopeType)
304         : direction(direction), isVertical(isVertical), isLeftToRight(isLeftToRight), scopeType(scopeType)
305     {}
ScopeFocusAlgorithmfinal306     ScopeFocusAlgorithm(ScopeFocusDirection direction, bool isVertical, bool isLeftToRight, ScopeType scopeType,
307         GetNextFocusNodeFunc&& function)
308         : direction(direction), isVertical(isVertical), isLeftToRight(isLeftToRight), scopeType(scopeType),
309           getNextFocusNode(std::move(function))
310     {}
311     ~ScopeFocusAlgorithm() = default;
312 
313     // isVertical will be deleted
314     ScopeFocusDirection direction { ScopeFocusDirection::VERTICAL };
315     bool isVertical { true };
316     bool isLeftToRight { true };
317     ScopeType scopeType { ScopeType::OTHERS };
318     GetNextFocusNodeFunc getNextFocusNode;
319 };
320 
321 class ACE_FORCE_EXPORT FocusHub : public virtual FocusEventHandler, public virtual FocusState {
322     DECLARE_ACE_TYPE(FocusHub, FocusEventHandler, FocusState);
323 public:
324     explicit FocusHub(const WeakPtr<EventHub>& eventHub, FocusType type = FocusType::DISABLE, bool focusable = false)
FocusState(eventHub,type)325         : FocusState(eventHub, type), FocusEventHandler(), focusable_(focusable)
326     {}
327     explicit FocusHub(const WeakPtr<FrameNode>& frameNode, FocusType type = FocusType::DISABLE, bool focusable = false)
FocusState(frameNode,type)328         : FocusState(frameNode, type), FocusEventHandler(), focusable_(focusable)
329     {}
FocusHub(const WeakPtr<EventHub> & eventHub,const FocusPattern & focusPattern)330     explicit FocusHub(const WeakPtr<EventHub>& eventHub, const FocusPattern& focusPattern)
331         : FocusState(eventHub), FocusEventHandler()
332     {
333         focusable_ = focusPattern.GetFocusable();
334         focusType_ = focusPattern.GetFocusType();
335         focusStyleType_ = focusPattern.GetStyleType();
336         if (focusPattern.GetFocusPaintParams()) {
337             SetFocusPaintParamsPtr(focusPattern.GetFocusPaintParams());
338         }
339         isFocusActiveWhenFocused_ = focusPattern.GetIsFocusActiveWhenFocused();
340     }
FocusHub(const WeakPtr<FrameNode> & frameNode,const FocusPattern & focusPattern)341     explicit FocusHub(const WeakPtr<FrameNode>& frameNode, const FocusPattern& focusPattern)
342         : FocusState(frameNode), FocusEventHandler()
343     {
344         focusable_ = focusPattern.GetFocusable();
345         focusType_ = focusPattern.GetFocusType();
346         focusStyleType_ = focusPattern.GetStyleType();
347         if (focusPattern.GetFocusPaintParams()) {
348             SetFocusPaintParamsPtr(focusPattern.GetFocusPaintParams());
349         }
350         isFocusActiveWhenFocused_ = focusPattern.GetIsFocusActiveWhenFocused();
351     }
352     ~FocusHub() override = default;
353 
354     static constexpr int32_t SCROLL_TO_HEAD = -1;
355     static constexpr int32_t SCROLL_TO_TAIL = -2;
356 
SetFocusStyleType(FocusStyleType type)357     void SetFocusStyleType(FocusStyleType type)
358     {
359         focusStyleType_ = type;
360     }
GetFocusStyleType()361     FocusStyleType GetFocusStyleType() const
362     {
363         return focusStyleType_;
364     }
365 
GetBlurReason()366     BlurReason GetBlurReason() const
367     {
368         return blurReason_;
369     }
370 
SetFocusPaintParamsPtr(const std::unique_ptr<FocusPaintParam> & paramsPtr)371     void SetFocusPaintParamsPtr(const std::unique_ptr<FocusPaintParam>& paramsPtr)
372     {
373         CHECK_NULL_VOID(paramsPtr);
374         if (!focusPaintParamsPtr_) {
375             focusPaintParamsPtr_ = std::make_unique<FocusPaintParam>();
376         }
377         if (paramsPtr->HasPaintRect()) {
378             focusPaintParamsPtr_->SetPaintRect(paramsPtr->GetPaintRect());
379         }
380         if (paramsPtr->HasPaintColor()) {
381             focusPaintParamsPtr_->SetPaintColor(paramsPtr->GetPaintColor());
382         }
383         if (paramsPtr->HasPaintWidth()) {
384             focusPaintParamsPtr_->SetPaintWidth(paramsPtr->GetPaintWidth());
385         }
386         if (paramsPtr->HasFocusPadding()) {
387             focusPaintParamsPtr_->SetFocusPadding(paramsPtr->GetFocusPadding());
388         }
389     }
390 
HasPaintRect()391     bool HasPaintRect() const
392     {
393         return focusPaintParamsPtr_ ? focusPaintParamsPtr_->HasPaintRect() : false;
394     }
GetPaintRect()395     RoundRect GetPaintRect() const
396     {
397         CHECK_NULL_RETURN(focusPaintParamsPtr_, RoundRect());
398         return focusPaintParamsPtr_->GetPaintRect();
399     }
400 
HasPaintColor()401     bool HasPaintColor() const
402     {
403         return focusPaintParamsPtr_ ? focusPaintParamsPtr_->HasPaintColor() : false;
404     }
GetPaintColor()405     const Color& GetPaintColor() const
406     {
407         CHECK_NULL_RETURN(focusPaintParamsPtr_, Color::TRANSPARENT);
408         return focusPaintParamsPtr_->GetPaintColor();
409     }
410 
HasPaintWidth()411     bool HasPaintWidth() const
412     {
413         return focusPaintParamsPtr_ ? focusPaintParamsPtr_->HasPaintWidth() : false;
414     }
GetPaintWidth()415     Dimension GetPaintWidth() const
416     {
417         CHECK_NULL_RETURN(focusPaintParamsPtr_, Dimension());
418         return focusPaintParamsPtr_->GetPaintWidth();
419     }
420 
HasFocusPadding()421     bool HasFocusPadding() const
422     {
423         return focusPaintParamsPtr_ ? focusPaintParamsPtr_->HasFocusPadding() : false;
424     }
425 
HasBackwardFocusMovement()426     bool HasBackwardFocusMovement() const
427     {
428         return hasBackwardMovement_;
429     }
430 
HasForwardFocusMovement()431     bool HasForwardFocusMovement() const
432     {
433         return hasForwardMovement_;
434     }
435 
ClearFocusMovementFlags()436     void ClearFocusMovementFlags()
437     {
438         hasBackwardMovement_ = false;
439         hasForwardMovement_ = false;
440     }
441 
442     bool HasBackwardFocusMovementInChildren();
443     bool HasForwardFocusMovementInChildren();
444     void ClearFocusMovementFlagsInChildren();
445 
GetFocusPadding()446     Dimension GetFocusPadding() const
447     {
448         CHECK_NULL_RETURN(focusPaintParamsPtr_, Dimension());
449         return focusPaintParamsPtr_->GetFocusPadding();
450     }
SetPaintRect(const RoundRect & rect)451     void SetPaintRect(const RoundRect& rect)
452     {
453         if (!focusPaintParamsPtr_) {
454             focusPaintParamsPtr_ = std::make_unique<FocusPaintParam>();
455         }
456         CHECK_NULL_VOID(focusPaintParamsPtr_);
457         focusPaintParamsPtr_->SetPaintRect(rect);
458     }
SetPaintColor(const Color & color)459     void SetPaintColor(const Color& color)
460     {
461         if (!focusPaintParamsPtr_) {
462             focusPaintParamsPtr_ = std::make_unique<FocusPaintParam>();
463         }
464         CHECK_NULL_VOID(focusPaintParamsPtr_);
465         focusPaintParamsPtr_->SetPaintColor(color);
466     }
SetPaintWidth(const Dimension & width)467     void SetPaintWidth(const Dimension& width)
468     {
469         if (!focusPaintParamsPtr_) {
470             focusPaintParamsPtr_ = std::make_unique<FocusPaintParam>();
471         }
472         CHECK_NULL_VOID(focusPaintParamsPtr_);
473         focusPaintParamsPtr_->SetPaintWidth(width);
474     }
SetFocusPadding(const Dimension & padding)475     void SetFocusPadding(const Dimension& padding)
476     {
477         if (!focusPaintParamsPtr_) {
478             focusPaintParamsPtr_ = std::make_unique<FocusPaintParam>();
479         }
480         CHECK_NULL_VOID(focusPaintParamsPtr_);
481         focusPaintParamsPtr_->SetFocusPadding(padding);
482     }
483 
484     RefPtr<FocusManager> GetFocusManager() const;
485     RefPtr<FocusHub> GetParentFocusHub() const;
486     RefPtr<FocusHub> GetParentFocusHubWithBoundary() const;
487     RefPtr<FocusHub> GetRootFocusHub();
488     RefPtr<FocusHub> GetFocusLeaf();
489 
490     bool HandleEvent(const NonPointerEvent& event);
491     bool HandleFocusTravel(const FocusEvent& event) override;
492     bool HandleFocusNavigation(const FocusEvent& event);
493     bool RequestFocusImmediately(FocusReason reason = FocusReason::DEFAULT);
494     void RequestFocus() const;
495     void SwitchFocus(const RefPtr<FocusHub>& focusNode, FocusReason focusReason = FocusReason::DEFAULT);
496     void HandleLastFocusNodeInFocusWindow();
497 
498     static void LostFocusToViewRoot();
499     void LostFocusToTabStop(const RefPtr<FocusHub>& focusNode);
500 
501     bool IsViewRootScope();
502     void LostFocus(BlurReason reason = BlurReason::FOCUS_SWITCH);
503     void LostSelfFocus();
504     void RemoveSelf(BlurReason reason = BlurReason::FRAME_DESTROY);
505     void RemoveChild(const RefPtr<FocusHub>& focusNode, BlurReason reason = BlurReason::FRAME_DESTROY);
506     bool GoToNextFocusLinear(FocusStep step, const RectF& rect = RectF());
507     bool TryRequestFocus(const RefPtr<FocusHub>& focusNode, const RectF& rect, FocusStep step = FocusStep::NONE);
InheritFocus()508     void InheritFocus()
509     {
510         OnFocusScope(true);
511     }
512 
513     void CollectTabIndexNodes(TabIndexNodeList& tabIndexNodes);
514     bool GoToFocusByTabNodeIdx(TabIndexNodeList& tabIndexNodes, int32_t tabNodeIdx);
515     bool HandleFocusByTabIndex(const KeyEvent& event);
516     RefPtr<FocusHub> GetChildFocusNodeByType(FocusNodeType nodeType = FocusNodeType::DEFAULT);
517     RefPtr<FocusHub> GetChildFocusNodeById(const std::string& id);
518     bool TriggerFocusScroll();
519     int32_t GetFocusingTabNodeIdx(TabIndexNodeList& tabIndexNodes) const;
520     bool RequestFocusImmediatelyById(const std::string& id, bool isSyncRequest = false);
521     RefPtr<FocusHub> GetFocusNodeFromSubWindow(const std::string& id);
522     RefPtr<FocusView> GetFirstChildFocusView();
523 
524     bool IsFocusableByTab();
525     bool IsFocusableNodeByTab();
526     bool IsFocusableScopeByTab();
527 
528     bool IsFocusableWholePath();
529     bool IsSelfFocusableWholePath();
530 
531     bool IsFocusable();
532     bool IsFocusableNode();
533     bool IsFocusableScope();
534 
535     bool IsSyncRequestFocusable();
536     bool IsSyncRequestFocusableNode();
537     bool IsSyncRequestFocusableScope();
538 
IsParentFocusable()539     bool IsParentFocusable() const
540     {
541         return parentFocusable_;
542     }
543     void SetParentFocusable(bool parentFocusable);
544 
545     void SetFocusable(bool focusable, bool isExplicit = true);
546 
GetFocusable()547     bool GetFocusable() const
548     {
549         return focusable_;
550     }
551 
552     void SetShow(bool show);
553     void SetEnabled(bool enabled);
554 
555     bool IsShow() const;
556 
557     bool IsEnabled() const;
558 
559     bool IsCurrentFocusWholePath();
560 
561     bool HasFocusedChild();
562 
SetOnFocusInternal(std::function<void (FocusReason reason)> && onFocusInternal)563     void SetOnFocusInternal(std::function<void(FocusReason reason)>&& onFocusInternal)
564     {
565         onFocusInternal_ = std::move(onFocusInternal);
566     }
SetOnBlurInternal(OnBlurFunc && onBlurInternal)567     void SetOnBlurInternal(OnBlurFunc&& onBlurInternal)
568     {
569         onBlurInternal_ = std::move(onBlurInternal);
570     }
SetOnBlurReasonInternal(OnBlurReasonFunc && onBlurReasonInternal)571     void SetOnBlurReasonInternal(OnBlurReasonFunc&& onBlurReasonInternal)
572     {
573         onBlurReasonInternal_ = std::move(onBlurReasonInternal);
574     }
SetOnPreFocusCallback(OnPreFocusFunc && onPreFocusCallback)575     void SetOnPreFocusCallback(OnPreFocusFunc&& onPreFocusCallback)
576     {
577         onPreFocusCallback_ = std::move(onPreFocusCallback);
578     }
579 
SetOnGetNextFocusNodeFunc(OnGetNextFocusNodeFunc && onGetNextFocusNodeFunc)580     void SetOnGetNextFocusNodeFunc(OnGetNextFocusNodeFunc&& onGetNextFocusNodeFunc)
581     {
582         onGetNextFocusNodeFunc_ = std::move(onGetNextFocusNodeFunc);
583     }
584 
IsAllowedLoop()585     bool IsAllowedLoop()
586     {
587         return allowedLoop_;
588     }
SetAllowedLoop(bool allowedLoop)589     void SetAllowedLoop(bool allowedLoop)
590     {
591         allowedLoop_ = allowedLoop;
592     }
593 
SetOnClearFocusStateInternal(OnClearFocusStateFunc && onClearFocusCallback)594     void SetOnClearFocusStateInternal(OnClearFocusStateFunc&& onClearFocusCallback)
595     {
596         onClearFocusStateCallback_ = std::move(onClearFocusCallback);
597     }
598 
SetOnPaintFocusStateInternal(OnPaintFocusStateFunc && onPaintFocusCallback)599     void SetOnPaintFocusStateInternal(OnPaintFocusStateFunc&& onPaintFocusCallback)
600     {
601         onPaintFocusStateCallback_ = std::move(onPaintFocusCallback);
602     }
603 
604     std::list<RefPtr<FocusHub>>::iterator FlushChildrenFocusHub(std::list<RefPtr<FocusHub>>& focusNodes);
605     /* Manipulation on node-tree is forbidden in operation. */
606     bool AnyChildFocusHub(const std::function<bool(const RefPtr<FocusHub>&)>& operation, bool isReverse = false);
607     bool AllChildFocusHub(const std::function<void(const RefPtr<FocusHub>&)>& operation, bool isReverse = false);
608 
IsChild()609     bool IsChild() const
610     {
611         return focusType_ == FocusType::NODE;
612     }
613 
SetRect(const RectF & rect)614     void SetRect(const RectF& rect)
615     {
616         rectFromOrigin_ = rect;
617     }
GetRect()618     const RectF& GetRect() const
619     {
620         return rectFromOrigin_;
621     }
622 
623     void DumpFocusTree(int32_t depth, bool hasJson = false);
624     void DumpFocusNodeTree(int32_t depth);
625     void DumpFocusScopeTree(int32_t depth);
626     void DumpFocusUie();
627     void DumpFocusUieInJson(std::unique_ptr<JsonValue>& json);
628 
SetFocusType(FocusType type)629     void SetFocusType(FocusType type)
630     {
631         if (focusType_ != type && type == FocusType::DISABLE) {
632             RemoveSelf(BlurReason::FOCUS_SWITCH);
633         }
634         focusType_ = type;
635 
636         if (IsImplicitFocusableScope() && focusDepend_ == FocusDependence::CHILD) {
637             focusDepend_ = FocusDependence::AUTO;
638         }
639     }
GetFocusType()640     FocusType GetFocusType() const
641     {
642         return focusType_;
643     }
644 
GetTabIndex()645     int32_t GetTabIndex() const
646     {
647         return focusCallbackEvents_ ? focusCallbackEvents_->tabIndex_ : 0;
648     }
SetTabIndex(int32_t tabIndex)649     void SetTabIndex(int32_t tabIndex)
650     {
651         if (!focusCallbackEvents_) {
652             focusCallbackEvents_ = MakeRefPtr<FocusCallbackEvents>();
653         }
654         focusCallbackEvents_->tabIndex_ = tabIndex;
655     }
656 
IsDefaultFocus()657     bool IsDefaultFocus() const
658     {
659         return focusCallbackEvents_ ? focusCallbackEvents_->isDefaultFocus_ : false;
660     }
661 
SetIsDefaultFocus(bool isDefaultFocus)662     void SetIsDefaultFocus(bool isDefaultFocus)
663     {
664         if (!focusCallbackEvents_) {
665             focusCallbackEvents_ = MakeRefPtr<FocusCallbackEvents>();
666         }
667         focusCallbackEvents_->isDefaultFocus_ = isDefaultFocus;
668     }
669 
IsDefaultGroupFocus()670     bool IsDefaultGroupFocus() const
671     {
672         return focusCallbackEvents_ ? focusCallbackEvents_->isDefaultGroupFocus_ : false;
673     }
674 
SetIsDefaultGroupFocus(bool isDefaultGroupFocus)675     void SetIsDefaultGroupFocus(bool isDefaultGroupFocus)
676     {
677         if (!focusCallbackEvents_) {
678             focusCallbackEvents_ = MakeRefPtr<FocusCallbackEvents>();
679         }
680         focusCallbackEvents_->isDefaultGroupFocus_ = isDefaultGroupFocus;
681     }
682 
GetDefaultFocusNode()683     WeakPtr<FocusHub> GetDefaultFocusNode() const
684     {
685         return focusCallbackEvents_ ? focusCallbackEvents_->defaultFocusNode_ : nullptr;
686     }
687 
SetDefaultFocusNode(const WeakPtr<FocusHub> & node)688     void SetDefaultFocusNode(const WeakPtr<FocusHub>& node)
689     {
690         if (!focusCallbackEvents_) {
691             focusCallbackEvents_ = MakeRefPtr<FocusCallbackEvents>();
692         }
693         focusCallbackEvents_->defaultFocusNode_ = node;
694     }
695 
IsFocusOnTouch()696     std::optional<bool> IsFocusOnTouch() const
697     {
698         return focusCallbackEvents_ ? focusCallbackEvents_->isFocusOnTouch_ : std::nullopt;
699     }
700 
701     void SetIsFocusOnTouch(bool isFocusOnTouch);
702 
SetIsDefaultHasFocused(bool isDefaultHasFocused)703     void SetIsDefaultHasFocused(bool isDefaultHasFocused)
704     {
705         if (!focusCallbackEvents_) {
706             focusCallbackEvents_ = MakeRefPtr<FocusCallbackEvents>();
707         }
708         focusCallbackEvents_->isDefaultHasFocused_ = isDefaultHasFocused;
709     }
710 
IsDefaultHasFocused()711     bool IsDefaultHasFocused() const
712     {
713         return focusCallbackEvents_ ? focusCallbackEvents_->isDefaultHasFocused_ : false;
714     }
715 
SetIsDefaultGroupHasFocused(bool isDefaultGroupHasFocused)716     void SetIsDefaultGroupHasFocused(bool isDefaultGroupHasFocused)
717     {
718         if (!focusCallbackEvents_) {
719             focusCallbackEvents_ = MakeRefPtr<FocusCallbackEvents>();
720         }
721         focusCallbackEvents_->isDefaultGroupHasFocused_ = isDefaultGroupHasFocused;
722     }
723 
IsDefaultGroupHasFocused()724     bool IsDefaultGroupHasFocused() const
725     {
726         return focusCallbackEvents_ ? focusCallbackEvents_->isDefaultGroupHasFocused_ : false;
727     }
728 
IsImplicitFocusableScope()729     bool IsImplicitFocusableScope() const
730     {
731         return (focusType_ == FocusType::SCOPE) && focusable_ && implicitFocusable_;
732     }
733 
734     std::optional<std::string> GetInspectorKey() const;
735 
736     bool PaintFocusState(bool isNeedStateStyles = true);
737     bool PaintFocusStateToRenderContext();
738     void GetPaintColorFromBox(Color& paintColor);
739     void GetPaintWidthFromBox(Dimension& paintWidth);
740     void GetPaintPaddingVp(Dimension& focusPaddingVp);
741     bool PaintAllFocusState();
742     bool PaintInnerFocusState(const RoundRect& paintRect, bool forceUpdate = false);
743     void ClearFocusState(bool isNeedStateStyles = true, bool isNeedClearCallBack = true);
744     void ClearAllFocusState();
745 
SetInnerFocusPaintRectCallback(const std::function<void (RoundRect &)> & callback)746     void SetInnerFocusPaintRectCallback(const std::function<void(RoundRect&)>& callback)
747     {
748         getInnerFocusRectFunc_ = callback;
749     }
750 
SetIsFocusUnit(bool isFocusUnit)751     void SetIsFocusUnit(bool isFocusUnit)
752     {
753         isFocusUnit_ = isFocusUnit;
754     }
755 
GetFocusDependence()756     FocusDependence GetFocusDependence() const
757     {
758         return focusDepend_;
759     }
SetFocusDependence(FocusDependence focusDepend)760     void SetFocusDependence(FocusDependence focusDepend)
761     {
762         focusDepend_ = focusDepend;
763     }
764 
GetFocusableCount()765     size_t GetFocusableCount()
766     {
767         size_t count = 0;
768         AllChildFocusHub([&count](const RefPtr<FocusHub>& child) {
769             if (child->IsFocusable()) {
770                 count++;
771             }
772         });
773         return count;
774     }
775 
SetIsFocusActiveWhenFocused(bool value)776     void SetIsFocusActiveWhenFocused(bool value)
777     {
778         isFocusActiveWhenFocused_ = value;
779     }
GetIsFocusActiveWhenFocused()780     bool GetIsFocusActiveWhenFocused() const
781     {
782         return isFocusActiveWhenFocused_;
783     }
784 
IsFocusStepVertical(FocusStep step)785     static inline bool IsFocusStepVertical(FocusStep step)
786     {
787         return (static_cast<uint32_t>(step) & MASK_FOCUS_STEP_VERTICAL) == 0;
788     }
789 
790     static inline bool IsFocusStepForward(FocusStep step, bool isRtl = false)
791     {
792         bool isForward = (static_cast<uint32_t>(step) & MASK_FOCUS_STEP_FORWARD) != 0;
793         if (isRtl && (step == FocusStep::RIGHT || step == FocusStep::LEFT)) {
794             isForward = !isForward;
795         }
796         return isForward;
797     }
798 
IsFocusStepTab(FocusStep step)799     static inline bool IsFocusStepTab(FocusStep step)
800     {
801         return (static_cast<uint32_t>(step) & MASK_FOCUS_STEP_TAB) == MASK_FOCUS_STEP_TAB;
802     }
803 
IsHomeOrEndStep(FocusStep step)804     static inline bool IsHomeOrEndStep(FocusStep step)
805     {
806         return step == FocusStep::UP_END || step == FocusStep::LEFT_END || step == FocusStep::DOWN_END ||
807                step == FocusStep::RIGHT_END;
808     }
809 
810     static inline FocusStep GetRealFocusStepByTab(FocusStep moveStep, bool isRtl = false)
811     {
812         if (isRtl) {
813             return moveStep == FocusStep::TAB ? FocusStep::LEFT : FocusStep::RIGHT;
814         } else {
815             return moveStep == FocusStep::TAB ? FocusStep::RIGHT : FocusStep::LEFT;
816         }
817     }
818 
819     static double GetProjectAreaOnRect(const RectF& rect, const RectF& projectRect, FocusStep step);
820 
821     void SetFocusScopeId(const std::string& focusScopeId, bool isGroup, bool arrowKeyStepOut = true);
822     void SetFocusScopePriority(const std::string& focusScopeId, const uint32_t focusPriority);
823     void RemoveFocusScopeIdAndPriority();
824     bool AcceptFocusOfPriorityChild();
825     bool SetLastWeakFocusNodeToPreviousNode();
826     void SetLastWeakFocusToPreviousInFocusView();
GetIsFocusGroup()827     bool GetIsFocusGroup() const
828     {
829         return isGroup_;
830     }
831 
GetIsFocusScope()832     bool GetIsFocusScope() const
833     {
834         return isFocusScope_;
835     }
836 
GetFocusScopeId()837     std::string GetFocusScopeId() const
838     {
839         return focusScopeId_;
840     }
841 
GetFocusBox()842     FocusBox& GetFocusBox()
843     {
844         return box_;
845     }
846 
GetFocusPriority()847     FocusPriority GetFocusPriority() const
848     {
849         return focusPriority_;
850     }
851 
852     static void ToJsonValue(
853         const RefPtr<FocusHub>& hub, std::unique_ptr<JsonValue>& json, const InspectorFilter& filter);
854 
855     bool FocusToHeadOrTailChild(bool isHead);
856 
857     WeakPtr<FocusHub> GetUnfocusableParentFocusNode();
858 
859     bool IsNeedPaintFocusStateSelf();
860 
861     void LostChildFocusToSelf();
862 
863     static bool IsFocusStepKey(KeyCode keyCode);
864 
865     bool GetNextFocusByStep(const KeyEvent& keyEvent);
866 
SetDirectionalKeyFocus(bool directionalKeyFocus)867     void SetDirectionalKeyFocus(bool directionalKeyFocus)
868     {
869         enableDirectionalKeyFocus_ = directionalKeyFocus;
870     }
871 
GetDirectionalKeyFocus()872     bool GetDirectionalKeyFocus() const
873     {
874         return enableDirectionalKeyFocus_;
875     }
876 
SetIsNodeNeedKey(bool isNodeNeedKey)877     void SetIsNodeNeedKey(bool isNodeNeedKey)
878     {
879         isNodeNeedKey_ = isNodeNeedKey;
880     }
881 
GetOnGetNextFocusNodeFunc()882     OnGetNextFocusNodeFunc GetOnGetNextFocusNodeFunc()
883     {
884         return onGetNextFocusNodeFunc_;
885     }
886 
SetNextFocus(FocusIntension key,const std::variant<WeakPtr<AceType>,std::string> & nextFocus)887     void SetNextFocus(FocusIntension key, const std::variant<WeakPtr<AceType>, std::string>& nextFocus)
888     {
889         FocusState::SetNextFocus(static_cast<int32_t>(key), nextFocus);
890     }
891 
892     RefPtr<FocusHub> GetHeadOrTailChild(bool isHead, bool isHomeOrEnd = false);
893     RefPtr<FocusHub> FindHeadOrTailDescendantFocus(bool isHead, bool isHomeOrEnd = false);
894 
895     // multi thread function start
896     void RemoveSelfMultiThread(BlurReason reason);
897     void RemoveSelfExecuteFunction(BlurReason reason);
898     // multi thread function end
899 
900 protected:
901     bool RequestNextFocusOfKeyTab(const FocusEvent& event);
902     bool RequestNextFocusOfKeyEnter();
903     bool RequestNextFocusOfKeyEsc();
904 
905     bool AcceptFocusOfSpecifyChild(FocusStep step);
906     bool AcceptFocusOfLastFocus();
907     bool AcceptFocusByRectOfLastFocus(const RectF& rect);
908     bool AcceptFocusByRectOfLastFocusNode(const RectF& rect);
909     bool AcceptFocusByRectOfLastFocusFlex(const RectF& rect);
910 
911     bool CalculateRect(const RefPtr<FocusHub>& childNode, RectF& rect) const;
912     bool RequestNextFocus(FocusStep moveStep);
913     bool RequestNextFocusByDefaultAlgorithm(FocusStep moveStep, const RectF& rect);
914     bool RequestNextFocusByCustomAlgorithm(FocusStep moveStep, const RectF& rect);
915 
916     void OnFocus();
917     void OnFocusNode(bool currentHasFocused = false);
918     void OnFocusScope(bool currentHasFocused = false);
919     void OnBlur();
920     void OnBlurNode();
921     void OnBlurScope();
922 
HandleFocus()923     void HandleFocus()
924     {
925         // Need update: void RenderNode::MoveWhenOutOfViewPort(bool hasEffect)
926         OnFocus();
927     }
928 
929     void HandleAccessibilityEvent();
930 
931 private:
932     friend class FocusView;
933 
934     friend class FocusManager;
935 
936     bool CalculatePosition();
937 
938     bool IsLeafFocusScope();
939 
940     void ClearLastFocusNode();
941 
942     void SetScopeFocusAlgorithm();
943 
944     void SetLastFocusNodeIndex(const RefPtr<FocusHub>& focusNode);
945 
946     void ScrollToLastFocusIndex() const;
947 
948     void CheckFocusStateStyle(bool onFocus);
949     bool HasFocusStateStyle();
950 
951     bool IsNeedPaintFocusState();
952 
953     bool ScrollByOffset();
954     bool ScrollByOffsetToParent(const RefPtr<FrameNode>& parentFrameNode) const;
955 
956     RefPtr<FocusHub> GetNearestNodeByProjectArea(const std::list<RefPtr<FocusHub>>& allNodes, FocusStep step);
957 
958     bool UpdateFocusView();
959 
960     bool IsFocusAbleChildOf(const RefPtr<FocusHub>& parentFocusHub);
961     bool IsChildOf(const RefPtr<FocusHub>& parentFocusHub);
962     void CloseChildFocusView();
963     WeakPtr<FocusHub> GetChildPriorfocusNode(const std::string& focusScopeId);
964     bool RequestFocusByPriorityInScope();
965     bool IsInFocusGroup();
966     bool IsNestingFocusGroup();
967     void SetLastWeakFocusNodeWholeScope(const std::string& focusScopeId);
968 
969     void RaiseZIndex(); // Recover z-index in ClearFocusState
970 
971     bool RequestFocusImmediatelyInner(FocusReason reason = FocusReason::DEFAULT);
972     bool RequestUserNextFocus(const FocusEvent& event);
973     bool RequestNextFocusByKey(const FocusEvent& event);
974 
975     bool IsComponentDirectionRtl();
976 
977     void DumpFocusNodeTreeInJson(int32_t depth);
978     void DumpFocusScopeTreeInJson(int32_t depth);
979 
980     bool SkipFocusMoveBeforeRemove();
981 
982     bool IsArrowKeyStepOut(FocusStep moveStep);
983 
984     bool IsLastWeakNodeFocused() const;
985 
986     std::function<void(FocusReason reason)> onFocusInternal_;
987     OnBlurFunc onBlurInternal_;
988     OnBlurReasonFunc onBlurReasonInternal_;
989     OnPreFocusFunc onPreFocusCallback_;
990     OnClearFocusStateFunc onClearFocusStateCallback_;
991     OnPaintFocusStateFunc onPaintFocusStateCallback_;
992     OnGetNextFocusNodeFunc onGetNextFocusNodeFunc_;
993 
994     RefPtr<TouchEventImpl> focusOnTouchListener_;
995 
996     int32_t lastFocusNodeIndex_ { -1 };
997     int32_t lastTabIndexNodeId_ { DEFAULT_TAB_FOCUSED_INDEX };
998 
999     bool focusable_ { true };
1000     bool isFocusableExplicit_ { false };
1001     bool implicitFocusable_ { false };
1002     bool parentFocusable_ { true };
1003     bool isFocusUnit_ { false };
1004     bool hasForwardMovement_ { false };
1005     bool hasBackwardMovement_ { false };
1006     bool isFocusActiveWhenFocused_ { false };
1007     bool isRaisedZIndex_ { false };
1008     bool allowedLoop_ { true };
1009 
1010     FocusStyleType focusStyleType_ = FocusStyleType::NONE;
1011     std::unique_ptr<FocusPaintParam> focusPaintParamsPtr_;
1012     std::function<void(RoundRect&)> getInnerFocusRectFunc_;
1013     FocusBox box_;
1014 
1015     RectF rectFromOrigin_;
1016     ScopeFocusAlgorithm focusAlgorithm_;
1017     BlurReason blurReason_ = BlurReason::FOCUS_SWITCH;
1018     FocusReason focusReason_ = FocusReason::DEFAULT;
1019     FocusDependence focusDepend_ = FocusDependence::CHILD;
1020 
1021     std::string focusScopeId_;
1022     bool isFocusScope_ { false };
1023     bool isGroup_ { false };
1024     FocusPriority focusPriority_ = FocusPriority::AUTO;
1025     bool arrowKeyStepOut_ { true };
1026     bool isSwitchByEnter_ { false };
1027     bool enableDirectionalKeyFocus_ { false };
1028     bool isCustomFocusTravel_ = false;
1029     WeakPtr<FocusHub> nextFocusTravelNode_;
1030 };
1031 } // namespace OHOS::Ace::NG
1032 
1033 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_EVENT_FOCUS_HUB_H
1034