• 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_EXPORT FocusHub : public virtual FocusEventHandler, public virtual FocusState {
DECLARE_ACE_TYPE(FocusHub,FocusEventHandler,FocusState)322     DECLARE_ACE_TYPE(FocusHub, FocusEventHandler, FocusState)
323 public:
324     explicit FocusHub(const WeakPtr<EventHub>& eventHub, FocusType type = FocusType::DISABLE, bool focusable = false)
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<FocusView> GetFirstChildFocusView();
522 
523     bool IsFocusableByTab();
524     bool IsFocusableNodeByTab();
525     bool IsFocusableScopeByTab();
526 
527     bool IsFocusableWholePath();
528     bool IsSelfFocusableWholePath();
529 
530     bool IsFocusable();
531     bool IsFocusableNode();
532     bool IsFocusableScope();
533 
534     bool IsSyncRequestFocusable();
535     bool IsSyncRequestFocusableNode();
536     bool IsSyncRequestFocusableScope();
537 
IsParentFocusable()538     bool IsParentFocusable() const
539     {
540         return parentFocusable_;
541     }
542     void SetParentFocusable(bool parentFocusable);
543 
544     void SetFocusable(bool focusable, bool isExplicit = true);
545 
GetFocusable()546     bool GetFocusable() const
547     {
548         return focusable_;
549     }
550 
551     void SetShow(bool show);
552     void SetEnabled(bool enabled);
553 
554     bool IsShow() const;
555 
556     bool IsEnabled() const;
557 
558     bool IsCurrentFocusWholePath();
559 
560     bool HasFocusedChild();
561 
SetOnFocusInternal(OnFocusFunc && onFocusInternal)562     void SetOnFocusInternal(OnFocusFunc&& onFocusInternal)
563     {
564         onFocusInternal_ = std::move(onFocusInternal);
565     }
SetOnBlurInternal(OnBlurFunc && onBlurInternal)566     void SetOnBlurInternal(OnBlurFunc&& onBlurInternal)
567     {
568         onBlurInternal_ = std::move(onBlurInternal);
569     }
SetOnBlurReasonInternal(OnBlurReasonFunc && onBlurReasonInternal)570     void SetOnBlurReasonInternal(OnBlurReasonFunc&& onBlurReasonInternal)
571     {
572         onBlurReasonInternal_ = std::move(onBlurReasonInternal);
573     }
SetOnPreFocusCallback(OnPreFocusFunc && onPreFocusCallback)574     void SetOnPreFocusCallback(OnPreFocusFunc&& onPreFocusCallback)
575     {
576         onPreFocusCallback_ = std::move(onPreFocusCallback);
577     }
578 
SetOnGetNextFocusNodeFunc(OnGetNextFocusNodeFunc && onGetNextFocusNodeFunc)579     void SetOnGetNextFocusNodeFunc(OnGetNextFocusNodeFunc&& onGetNextFocusNodeFunc)
580     {
581         onGetNextFocusNodeFunc_ = std::move(onGetNextFocusNodeFunc);
582     }
583 
IsAllowedLoop()584     bool IsAllowedLoop()
585     {
586         return allowedLoop_;
587     }
SetAllowedLoop(bool allowedLoop)588     void SetAllowedLoop(bool allowedLoop)
589     {
590         allowedLoop_ = allowedLoop;
591     }
592 
SetOnClearFocusStateInternal(OnClearFocusStateFunc && onClearFocusCallback)593     void SetOnClearFocusStateInternal(OnClearFocusStateFunc&& onClearFocusCallback)
594     {
595         onClearFocusStateCallback_ = std::move(onClearFocusCallback);
596     }
597 
SetOnPaintFocusStateInternal(OnPaintFocusStateFunc && onPaintFocusCallback)598     void SetOnPaintFocusStateInternal(OnPaintFocusStateFunc&& onPaintFocusCallback)
599     {
600         onPaintFocusStateCallback_ = std::move(onPaintFocusCallback);
601     }
602 
603     std::list<RefPtr<FocusHub>>::iterator FlushChildrenFocusHub(std::list<RefPtr<FocusHub>>& focusNodes);
604     /* Manipulation on node-tree is forbidden in operation. */
605     bool AnyChildFocusHub(const std::function<bool(const RefPtr<FocusHub>&)>& operation, bool isReverse = false);
606     bool AllChildFocusHub(const std::function<void(const RefPtr<FocusHub>&)>& operation, bool isReverse = false);
607 
IsChild()608     bool IsChild() const
609     {
610         return focusType_ == FocusType::NODE;
611     }
612 
SetRect(const RectF & rect)613     void SetRect(const RectF& rect)
614     {
615         rectFromOrigin_ = rect;
616     }
GetRect()617     const RectF& GetRect() const
618     {
619         return rectFromOrigin_;
620     }
621 
622     void DumpFocusTree(int32_t depth, bool hasJson = false);
623     void DumpFocusNodeTree(int32_t depth);
624     void DumpFocusScopeTree(int32_t depth);
625     void DumpFocusUie();
626     void DumpFocusUieInJson(std::unique_ptr<JsonValue>& json);
627 
SetFocusType(FocusType type)628     void SetFocusType(FocusType type)
629     {
630         if (focusType_ != type && type == FocusType::DISABLE) {
631             RemoveSelf(BlurReason::FOCUS_SWITCH);
632         }
633         focusType_ = type;
634 
635         if (IsImplicitFocusableScope() && focusDepend_ == FocusDependence::CHILD) {
636             focusDepend_ = FocusDependence::AUTO;
637         }
638     }
GetFocusType()639     FocusType GetFocusType() const
640     {
641         return focusType_;
642     }
643 
GetTabIndex()644     int32_t GetTabIndex() const
645     {
646         return focusCallbackEvents_ ? focusCallbackEvents_->tabIndex_ : 0;
647     }
SetTabIndex(int32_t tabIndex)648     void SetTabIndex(int32_t tabIndex)
649     {
650         if (!focusCallbackEvents_) {
651             focusCallbackEvents_ = MakeRefPtr<FocusCallbackEvents>();
652         }
653         focusCallbackEvents_->tabIndex_ = tabIndex;
654     }
655 
IsDefaultFocus()656     bool IsDefaultFocus() const
657     {
658         return focusCallbackEvents_ ? focusCallbackEvents_->isDefaultFocus_ : false;
659     }
660 
SetIsDefaultFocus(bool isDefaultFocus)661     void SetIsDefaultFocus(bool isDefaultFocus)
662     {
663         if (!focusCallbackEvents_) {
664             focusCallbackEvents_ = MakeRefPtr<FocusCallbackEvents>();
665         }
666         focusCallbackEvents_->isDefaultFocus_ = isDefaultFocus;
667     }
668 
IsDefaultGroupFocus()669     bool IsDefaultGroupFocus() const
670     {
671         return focusCallbackEvents_ ? focusCallbackEvents_->isDefaultGroupFocus_ : false;
672     }
673 
SetIsDefaultGroupFocus(bool isDefaultGroupFocus)674     void SetIsDefaultGroupFocus(bool isDefaultGroupFocus)
675     {
676         if (!focusCallbackEvents_) {
677             focusCallbackEvents_ = MakeRefPtr<FocusCallbackEvents>();
678         }
679         focusCallbackEvents_->isDefaultGroupFocus_ = isDefaultGroupFocus;
680     }
681 
GetDefaultFocusNode()682     WeakPtr<FocusHub> GetDefaultFocusNode() const
683     {
684         return focusCallbackEvents_ ? focusCallbackEvents_->defaultFocusNode_ : nullptr;
685     }
686 
SetDefaultFocusNode(const WeakPtr<FocusHub> & node)687     void SetDefaultFocusNode(const WeakPtr<FocusHub>& node)
688     {
689         if (!focusCallbackEvents_) {
690             focusCallbackEvents_ = MakeRefPtr<FocusCallbackEvents>();
691         }
692         focusCallbackEvents_->defaultFocusNode_ = node;
693     }
694 
IsFocusOnTouch()695     std::optional<bool> IsFocusOnTouch() const
696     {
697         return focusCallbackEvents_ ? focusCallbackEvents_->isFocusOnTouch_ : std::nullopt;
698     }
699 
700     void SetIsFocusOnTouch(bool isFocusOnTouch);
701 
SetIsDefaultHasFocused(bool isDefaultHasFocused)702     void SetIsDefaultHasFocused(bool isDefaultHasFocused)
703     {
704         if (!focusCallbackEvents_) {
705             focusCallbackEvents_ = MakeRefPtr<FocusCallbackEvents>();
706         }
707         focusCallbackEvents_->isDefaultHasFocused_ = isDefaultHasFocused;
708     }
709 
IsDefaultHasFocused()710     bool IsDefaultHasFocused() const
711     {
712         return focusCallbackEvents_ ? focusCallbackEvents_->isDefaultHasFocused_ : false;
713     }
714 
SetIsDefaultGroupHasFocused(bool isDefaultGroupHasFocused)715     void SetIsDefaultGroupHasFocused(bool isDefaultGroupHasFocused)
716     {
717         if (!focusCallbackEvents_) {
718             focusCallbackEvents_ = MakeRefPtr<FocusCallbackEvents>();
719         }
720         focusCallbackEvents_->isDefaultGroupHasFocused_ = isDefaultGroupHasFocused;
721     }
722 
IsDefaultGroupHasFocused()723     bool IsDefaultGroupHasFocused() const
724     {
725         return focusCallbackEvents_ ? focusCallbackEvents_->isDefaultGroupHasFocused_ : false;
726     }
727 
IsImplicitFocusableScope()728     bool IsImplicitFocusableScope() const
729     {
730         return (focusType_ == FocusType::SCOPE) && focusable_ && implicitFocusable_;
731     }
732 
733     std::optional<std::string> GetInspectorKey() const;
734 
735     bool PaintFocusState(bool isNeedStateStyles = true);
736     bool PaintFocusStateToRenderContext();
737     void GetPaintColorFromBox(Color& paintColor);
738     void GetPaintWidthFromBox(Dimension& paintWidth);
739     void GetPaintPaddingVp(Dimension& focusPaddingVp);
740     bool PaintAllFocusState();
741     bool PaintInnerFocusState(const RoundRect& paintRect, bool forceUpdate = false);
742     void ClearFocusState(bool isNeedStateStyles = true, bool isNeedClearCallBack = true);
743     void ClearAllFocusState();
744 
SetInnerFocusPaintRectCallback(const std::function<void (RoundRect &)> & callback)745     void SetInnerFocusPaintRectCallback(const std::function<void(RoundRect&)>& callback)
746     {
747         getInnerFocusRectFunc_ = callback;
748     }
749 
SetIsFocusUnit(bool isFocusUnit)750     void SetIsFocusUnit(bool isFocusUnit)
751     {
752         isFocusUnit_ = isFocusUnit;
753     }
754 
GetFocusDependence()755     FocusDependence GetFocusDependence() const
756     {
757         return focusDepend_;
758     }
SetFocusDependence(FocusDependence focusDepend)759     void SetFocusDependence(FocusDependence focusDepend)
760     {
761         focusDepend_ = focusDepend;
762     }
763 
GetFocusableCount()764     size_t GetFocusableCount()
765     {
766         size_t count = 0;
767         AllChildFocusHub([&count](const RefPtr<FocusHub>& child) {
768             if (child->IsFocusable()) {
769                 count++;
770             }
771         });
772         return count;
773     }
774 
SetIsFocusActiveWhenFocused(bool value)775     void SetIsFocusActiveWhenFocused(bool value)
776     {
777         isFocusActiveWhenFocused_ = value;
778     }
GetIsFocusActiveWhenFocused()779     bool GetIsFocusActiveWhenFocused() const
780     {
781         return isFocusActiveWhenFocused_;
782     }
783 
IsFocusStepVertical(FocusStep step)784     static inline bool IsFocusStepVertical(FocusStep step)
785     {
786         return (static_cast<uint32_t>(step) & MASK_FOCUS_STEP_VERTICAL) == 0;
787     }
788 
789     static inline bool IsFocusStepForward(FocusStep step, bool isRtl = false)
790     {
791         bool isForward = (static_cast<uint32_t>(step) & MASK_FOCUS_STEP_FORWARD) != 0;
792         if (isRtl && (step == FocusStep::RIGHT || step == FocusStep::LEFT)) {
793             isForward = !isForward;
794         }
795         return isForward;
796     }
797 
IsFocusStepTab(FocusStep step)798     static inline bool IsFocusStepTab(FocusStep step)
799     {
800         return (static_cast<uint32_t>(step) & MASK_FOCUS_STEP_TAB) == MASK_FOCUS_STEP_TAB;
801     }
802 
803     static inline FocusStep GetRealFocusStepByTab(FocusStep moveStep, bool isRtl = false)
804     {
805         if (isRtl) {
806             return moveStep == FocusStep::TAB ? FocusStep::LEFT : FocusStep::RIGHT;
807         } else {
808             return moveStep == FocusStep::TAB ? FocusStep::RIGHT : FocusStep::LEFT;
809         }
810     }
811 
812     static double GetProjectAreaOnRect(const RectF& rect, const RectF& projectRect, FocusStep step);
813 
814     void SetFocusScopeId(const std::string& focusScopeId, bool isGroup, bool arrowKeyStepOut = true);
815     void SetFocusScopePriority(const std::string& focusScopeId, const uint32_t focusPriority);
816     void RemoveFocusScopeIdAndPriority();
817     bool AcceptFocusOfPriorityChild();
818     bool SetLastWeakFocusNodeToPreviousNode();
819     void SetLastWeakFocusToPreviousInFocusView();
GetIsFocusGroup()820     bool GetIsFocusGroup() const
821     {
822         return isGroup_;
823     }
824 
GetIsFocusScope()825     bool GetIsFocusScope() const
826     {
827         return isFocusScope_;
828     }
829 
GetFocusScopeId()830     std::string GetFocusScopeId() const
831     {
832         return focusScopeId_;
833     }
834 
GetFocusBox()835     FocusBox& GetFocusBox()
836     {
837         return box_;
838     }
839 
GetFocusPriority()840     FocusPriority GetFocusPriority() const
841     {
842         return focusPriority_;
843     }
844 
845     static void ToJsonValue(
846         const RefPtr<FocusHub>& hub, std::unique_ptr<JsonValue>& json, const InspectorFilter& filter);
847 
848     bool FocusToHeadOrTailChild(bool isHead);
849 
850     WeakPtr<FocusHub> GetUnfocusableParentFocusNode();
851 
852     bool IsNeedPaintFocusStateSelf();
853 
SetDirectionalKeyFocus(bool directionalKeyFocus)854     void SetDirectionalKeyFocus(bool directionalKeyFocus)
855     {
856         enableDirectionalKeyFocus_ = directionalKeyFocus;
857     }
858 
GetDirectionalKeyFocus()859     bool GetDirectionalKeyFocus() const
860     {
861         return enableDirectionalKeyFocus_;
862     }
863 
864     void LostChildFocusToSelf();
865 
866     static bool IsFocusStepKey(KeyCode keyCode);
867 
868     bool GetNextFocusByStep(const KeyEvent& keyEvent);
869 
SetIsNodeNeedKey(bool isNodeNeedKey)870     void SetIsNodeNeedKey(bool isNodeNeedKey)
871     {
872         isNodeNeedKey_ = isNodeNeedKey;
873     }
874 
GetOnGetNextFocusNodeFunc()875     OnGetNextFocusNodeFunc GetOnGetNextFocusNodeFunc()
876     {
877         return onGetNextFocusNodeFunc_;
878     }
879 
SetNextFocus(FocusIntension key,const std::variant<WeakPtr<AceType>,std::string> & nextFocus)880     void SetNextFocus(FocusIntension key, const std::variant<WeakPtr<AceType>, std::string>& nextFocus)
881     {
882         FocusState::SetNextFocus(static_cast<int32_t>(key), nextFocus);
883     }
884 protected:
885     bool RequestNextFocusOfKeyTab(const FocusEvent& event);
886     bool RequestNextFocusOfKeyEnter();
887     bool RequestNextFocusOfKeyEsc();
888 
889     bool AcceptFocusOfSpecifyChild(FocusStep step);
890     bool AcceptFocusOfLastFocus();
891     bool AcceptFocusByRectOfLastFocus(const RectF& rect);
892     bool AcceptFocusByRectOfLastFocusNode(const RectF& rect);
893     bool AcceptFocusByRectOfLastFocusFlex(const RectF& rect);
894 
895     bool CalculateRect(const RefPtr<FocusHub>& childNode, RectF& rect) const;
896     bool RequestNextFocus(FocusStep moveStep);
897     bool RequestNextFocusByDefaultAlgorithm(FocusStep moveStep, const RectF& rect);
898     bool RequestNextFocusByCustomAlgorithm(FocusStep moveStep, const RectF& rect);
899 
900     void OnFocus();
901     void OnFocusNode(bool currentHasFocused = false);
902     void OnFocusScope(bool currentHasFocused = false);
903     void OnBlur();
904     void OnBlurNode();
905     void OnBlurScope();
906 
HandleFocus()907     void HandleFocus()
908     {
909         // Need update: void RenderNode::MoveWhenOutOfViewPort(bool hasEffect)
910         OnFocus();
911     }
912 
913 private:
914     friend class FocusView;
915 
916     friend class FocusManager;
917 
918     bool CalculatePosition();
919 
920     bool IsLeafFocusScope();
921 
922     void ClearLastFocusNode();
923 
924     void SetScopeFocusAlgorithm();
925 
926     void SetLastFocusNodeIndex(const RefPtr<FocusHub>& focusNode);
927 
928     void ScrollToLastFocusIndex() const;
929 
930     void CheckFocusStateStyle(bool onFocus);
931     bool HasFocusStateStyle();
932 
933     bool IsNeedPaintFocusState();
934 
935     bool ScrollByOffset();
936     bool ScrollByOffsetToParent(const RefPtr<FrameNode>& parentFrameNode) const;
937 
938     RefPtr<FocusHub> GetNearestNodeByProjectArea(const std::list<RefPtr<FocusHub>>& allNodes, FocusStep step);
939 
940     bool UpdateFocusView();
941 
942     bool IsFocusAbleChildOf(const RefPtr<FocusHub>& parentFocusHub);
943     bool IsChildOf(const RefPtr<FocusHub>& parentFocusHub);
944     void CloseChildFocusView();
945     WeakPtr<FocusHub> GetChildPriorfocusNode(const std::string& focusScopeId);
946     bool RequestFocusByPriorityInScope();
947     bool IsInFocusGroup();
948     bool IsNestingFocusGroup();
949     void SetLastWeakFocusNodeWholeScope(const std::string& focusScopeId);
950 
951     void RaiseZIndex(); // Recover z-index in ClearFocusState
952 
953     bool RequestFocusImmediatelyInner(FocusReason reason = FocusReason::DEFAULT);
954     bool RequestUserNextFocus(const FocusEvent& event);
955     bool RequestNextFocusByKey(const FocusEvent& event);
956 
957     bool IsComponentDirectionRtl();
958 
959     void DumpFocusNodeTreeInJson(int32_t depth);
960     void DumpFocusScopeTreeInJson(int32_t depth);
961 
962     bool SkipFocusMoveBeforeRemove();
963 
964     bool IsArrowKeyStepOut(FocusStep moveStep);
965 
966     bool IsLastWeakNodeFocused() const;
967 
968     OnFocusFunc onFocusInternal_;
969     OnBlurFunc onBlurInternal_;
970     OnBlurReasonFunc onBlurReasonInternal_;
971     OnPreFocusFunc onPreFocusCallback_;
972     OnClearFocusStateFunc onClearFocusStateCallback_;
973     OnPaintFocusStateFunc onPaintFocusStateCallback_;
974     OnGetNextFocusNodeFunc onGetNextFocusNodeFunc_;
975 
976     RefPtr<TouchEventImpl> focusOnTouchListener_;
977 
978     int32_t lastFocusNodeIndex_ { -1 };
979     int32_t lastTabIndexNodeId_ { DEFAULT_TAB_FOCUSED_INDEX };
980 
981     bool focusable_ { true };
982     bool isFocusableExplicit_ { false };
983     bool implicitFocusable_ { false };
984     bool parentFocusable_ { true };
985     bool isFocusUnit_ { false };
986     bool hasForwardMovement_ { false };
987     bool hasBackwardMovement_ { false };
988     bool isFocusActiveWhenFocused_ { false };
989     bool isRaisedZIndex_ { false };
990     bool allowedLoop_ { true };
991 
992     FocusStyleType focusStyleType_ = FocusStyleType::NONE;
993     std::unique_ptr<FocusPaintParam> focusPaintParamsPtr_;
994     std::function<void(RoundRect&)> getInnerFocusRectFunc_;
995     FocusBox box_;
996 
997     RectF rectFromOrigin_;
998     ScopeFocusAlgorithm focusAlgorithm_;
999     BlurReason blurReason_ = BlurReason::FOCUS_SWITCH;
1000     FocusReason focusReason_ = FocusReason::DEFAULT;
1001     FocusDependence focusDepend_ = FocusDependence::CHILD;
1002 
1003     std::string focusScopeId_;
1004     bool isFocusScope_ { false };
1005     bool isGroup_ { false };
1006     FocusPriority focusPriority_ = FocusPriority::AUTO;
1007     bool arrowKeyStepOut_ { true };
1008     bool enableDirectionalKeyFocus_ { false };
1009     bool isSwitchByEnter_ { false };
1010     bool isCustomFocusTravel_ = false;
1011     WeakPtr<FocusHub> nextFocusTravelNode_;
1012 };
1013 } // namespace OHOS::Ace::NG
1014 
1015 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_EVENT_FOCUS_HUB_H
1016