1 /*
2  * Copyright (c) 2024 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_MANAGER_FOCUS_FOCUS_VIEW_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_MANAGER_FOCUS_FOCUS_VIEW_H
18 
19 #include <list>
20 
21 #include "base/memory/ace_type.h"
22 #include "base/memory/referenced.h"
23 #include "base/utils/utils.h"
24 #include "core/components_ng/event/focus_hub.h"
25 
26 namespace OHOS::Ace::NG {
27 
28 const char ENTRY_ANY_FOCUSVIEW[] = "entry_any_view";
29 
30 class FocusView : public virtual AceType {
31     DECLARE_ACE_TYPE(FocusView, AceType);
32 
33 public:
34     FocusView() = default;
35     ~FocusView() override = default;
36 
37     virtual std::list<int32_t> GetRouteOfFirstScope() = 0;
IsEntryFocusView()38     virtual bool IsEntryFocusView()
39     {
40         return true;
41     }
IsFocusViewLegal()42     virtual bool IsFocusViewLegal()
43     {
44         return true;
45     }
46 
47     RefPtr<FrameNode> GetFrameNode();
48     std::string GetFrameName();
49     int32_t GetFrameId();
50 
51     void FocusViewShow(bool isTriggerByStep = false);
52     void FocusViewHide();
53     void FocusViewClose(bool isDetachFromTree = false);
54 
55     virtual void LostViewFocus();
56 
57     RefPtr<FocusHub> GetFocusHub();
58 
59     static RefPtr<FocusView> GetCurrentFocusView();
60     RefPtr<FocusView> GetEntryFocusView();
61     RefPtr<FocusHub> GetViewRootScope();
62     bool IsRootScopeCurrentFocus();
63     bool IsChildFocusViewOf(const RefPtr<FocusView>& parent);
64     bool HasParentFocusHub();
65 
66     bool RequestDefaultFocus();
67     bool TriggerFocusMove();
68 
69     void SetIsViewRootScopeFocused(bool isViewRootScopeFocused);
GetIsViewRootScopeFocused()70     bool GetIsViewRootScopeFocused() const
71     {
72         return isViewRootScopeFocused_;
73     }
74 
SetIsViewHasFocused(bool isViewHasFocused)75     void SetIsViewHasFocused(bool isViewHasFocused)
76     {
77         isViewHasFocused_ = isViewHasFocused;
78     }
GetIsViewHasFocused()79     bool GetIsViewHasFocused() const
80     {
81         return isViewHasFocused_;
82     }
83 
SetViewRootScope(const RefPtr<FocusHub> & viewRootScope)84     void SetViewRootScope(const RefPtr<FocusHub>& viewRootScope)
85     {
86         if (!viewRootScope) {
87             rootScopeSpecified_ = nullptr;
88             return;
89         }
90         rootScopeSpecified_ = AceType::WeakClaim(AceType::RawPtr(viewRootScope));
91     }
92 
SetIsViewHasShow(bool isViewHasShow)93     void SetIsViewHasShow(bool isViewHasShow)
94     {
95         isViewHasShow_ = isViewHasShow;
96     }
97 
GetIsViewHasShow()98     bool GetIsViewHasShow() const
99     {
100         return isViewHasShow_;
101     }
102 
103     std::pair<bool, bool> HandleDefaultFocusNode(
104         const RefPtr<FocusHub>& defaultFocusNode, bool isViewRootScopeHasChildFocused);
105 
106     void FocusViewDidShow(const RefPtr<FocusHub>& focusHub);
107 
108 private:
109     bool neverShown_ = true;
110     bool isViewRootScopeFocused_ = true;
111     bool isViewHasFocused_ = false;
112     bool isViewHasShow_ = false;
113 
114     WeakPtr<FocusHub> rootScopeSpecified_;
115     bool GetFocusViewFocusable();
116 
117     ACE_DISALLOW_COPY_AND_MOVE(FocusView);
118 };
119 } // namespace OHOS::Ace::NG
120 
121 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_MANAGER_FOCUS_FOCUS_VIEW_H
122