1 /*
2  * Copyright (c) 2021 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_FOCUSABLE_FOCUSABLE_COMPONENT_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_FOCUSABLE_FOCUSABLE_COMPONENT_H
18 
19 #include <string>
20 
21 #include "core/components/box/box_component.h"
22 #include "core/components/box/render_box.h"
23 #include "core/components/declaration/common/controllers.h"
24 #include "core/components/focusable/focusable_element.h"
25 #include "core/event/ace_event_handler.h"
26 #include "core/pipeline/base/sole_child_component.h"
27 
28 namespace OHOS::Ace {
29 
30 class ACE_EXPORT FocusableComponent final : public SoleChildComponent {
31     DECLARE_ACE_TYPE(FocusableComponent, SoleChildComponent);
32 
33 public:
FocusableComponent()34     FocusableComponent()
35     {
36         focusableController_ = AceType::MakeRefPtr<FocusableController>();
37     }
FocusableComponent(const RefPtr<Component> & child)38     explicit FocusableComponent(const RefPtr<Component>& child) : SoleChildComponent(child)
39     {
40         focusableController_ = AceType::MakeRefPtr<FocusableController>();
41     }
42     ~FocusableComponent() override = default;
43 
CreateElement()44     RefPtr<Element> CreateElement() override
45     {
46         return MakeRefPtr<FocusableElement>();
47     }
48 
CreateRenderNode()49     RefPtr<RenderNode> CreateRenderNode() override
50     {
51         return RenderBox::Create();
52     }
53 
GetOnClickId()54     const EventMarker& GetOnClickId() const
55     {
56         return onClickId_;
57     }
SetOnClickId(const EventMarker & onClickId)58     void SetOnClickId(const EventMarker& onClickId)
59     {
60         onClickId_ = onClickId;
61     }
62 
GetOnFocusId()63     const EventMarker& GetOnFocusId() const
64     {
65         return onFocusId_;
66     }
SetOnFocusId(const EventMarker & onFocusId)67     void SetOnFocusId(const EventMarker& onFocusId)
68     {
69         onFocusId_ = onFocusId;
70     }
71 
GetOnBlurId()72     const EventMarker& GetOnBlurId() const
73     {
74         return onBlurId_;
75     }
SetOnBlurId(const EventMarker & onBlurId)76     void SetOnBlurId(const EventMarker& onBlurId)
77     {
78         onBlurId_ = onBlurId;
79     }
80 
GetOnFocusMove()81     std::function<void(int)> GetOnFocusMove() const
82     {
83         return onFocusMove_;
84     }
SetOnFocusMove(const std::function<void (int)> & onFocusMove)85     void SetOnFocusMove(const std::function<void(int)>& onFocusMove)
86     {
87         onFocusMove_ = onFocusMove;
88     }
89 
GetOnFocus()90     std::function<void()> GetOnFocus() const
91     {
92         return onFocus_;
93     }
SetOnFocus(const std::function<void ()> & onFocus)94     void SetOnFocus(const std::function<void()>& onFocus)
95     {
96         onFocus_ = onFocus;
97     }
98 
GetOnBlur()99     std::function<void()> GetOnBlur() const
100     {
101         return onBlur_;
102     }
SetOnBlur(const std::function<void ()> & onBlur)103     void SetOnBlur(const std::function<void()>& onBlur)
104     {
105         onBlur_ = onBlur;
106     }
107 
GetOnKeyId()108     const EventMarker& GetOnKeyId() const
109     {
110         return onKeyId_;
111     }
SetOnKeyId(const EventMarker & onKeyId)112     void SetOnKeyId(const EventMarker& onKeyId)
113     {
114         onKeyId_ = onKeyId;
115     }
116 
GetOnDeleteId()117     const EventMarker& GetOnDeleteId() const
118     {
119         return onDeleteId_;
120     }
SetOnDeleteId(const EventMarker & onDeleteId)121     void SetOnDeleteId(const EventMarker& onDeleteId)
122     {
123         onDeleteId_ = onDeleteId;
124     }
125 
GetBoxStyle()126     RefPtr<BoxComponent> GetBoxStyle() const
127     {
128         return boxStyle_;
129     }
SetBoxStyle(const RefPtr<BoxComponent> & boxStyle)130     void SetBoxStyle(const RefPtr<BoxComponent>& boxStyle)
131     {
132         boxStyle_ = boxStyle;
133     }
134 
GetFocusedBoxStyle()135     RefPtr<BoxComponent> GetFocusedBoxStyle() const
136     {
137         return focusedBoxStyle_;
138     }
SetFocusedBoxStyle(const RefPtr<BoxComponent> & boxStyle)139     void SetFocusedBoxStyle(const RefPtr<BoxComponent>& boxStyle)
140     {
141         focusedBoxStyle_ = boxStyle;
142     }
143 
GetAutoFocused()144     bool GetAutoFocused() const
145     {
146         return autoFocused_;
147     }
SetAutoFocused(bool autoFocused)148     void SetAutoFocused(bool autoFocused)
149     {
150         autoFocused_ = autoFocused;
151     }
IsFocusNode()152     bool IsFocusNode() const
153     {
154         return focusNode_;
155     }
SetFocusNode(bool focusNode)156     void SetFocusNode(bool focusNode)
157     {
158         focusNode_ = focusNode;
159     }
160 
IsFocusable()161     bool IsFocusable() const
162     {
163         return focusable_;
164     }
SetFocusable(bool focusable)165     void SetFocusable(bool focusable)
166     {
167         focusable_ = focusable;
168     }
169 
CanShow()170     bool CanShow() const
171     {
172         return show_;
173     }
SetShow(bool show)174     void SetShow(bool show)
175     {
176         show_ = show;
177     }
178 
GetFocusableController()179     RefPtr<FocusableController> GetFocusableController() const
180     {
181         return focusableController_;
182     }
183 
IsDeleteDisabled()184     bool IsDeleteDisabled() {
185         return deleteDisabled_;
186     }
SetDeleteDisabled(bool deleteDisabled)187     void SetDeleteDisabled(bool deleteDisabled) {
188         deleteDisabled_ = deleteDisabled;
189     }
190 
GetTabIndex()191     int32_t GetTabIndex() const
192     {
193         return tabIndex_;
194     }
195 
SetTabIndex(int32_t tabIndex)196     void SetTabIndex(int32_t tabIndex)
197     {
198         tabIndex_ = tabIndex;
199     }
200 
IsEnabled()201     bool IsEnabled() const
202     {
203         return enabled_;
204     }
SetEnabled(bool enabled)205     void SetEnabled(bool enabled)
206     {
207         enabled_ = enabled;
208     }
209 
IsFocusOnTouch()210     bool IsFocusOnTouch() const
211     {
212         return isFocusOnTouch_;
213     }
SetIsFocusOnTouch(bool isFocusOnTouch)214     void SetIsFocusOnTouch(bool isFocusOnTouch)
215     {
216         isFocusOnTouch_ = isFocusOnTouch;
217     }
218 
IsDefaultFocus()219     bool IsDefaultFocus() const
220     {
221         return isDefaultFocus_;
222     }
SetIsDefaultFocus(bool isDefaultFocus)223     void SetIsDefaultFocus(bool isDefaultFocus)
224     {
225         isDefaultFocus_ = isDefaultFocus;
226     }
227 
IsDefaultGroupFocus()228     bool IsDefaultGroupFocus() const
229     {
230         return isDefaultGroupFocus_;
231     }
SetIsDefaultGroupFocus(bool isDefaultGroupFocus)232     void SetIsDefaultGroupFocus(bool isDefaultGroupFocus)
233     {
234         isDefaultGroupFocus_ = isDefaultGroupFocus;
235     }
236 
237 private:
238     EventMarker onClickId_;
239     EventMarker onFocusId_;
240     EventMarker onBlurId_;
241     EventMarker onKeyId_;
242     EventMarker onDeleteId_;
243 
244     RefPtr<BoxComponent> boxStyle_;
245     RefPtr<BoxComponent> focusedBoxStyle_;
246     RefPtr<FocusableController> focusableController_;
247 
248     bool autoFocused_ { false };
249     bool focusNode_ { false };
250     bool focusable_ { false };
251     bool show_ { true };
252     bool deleteDisabled_ { false };
253     bool enabled_ { true };
254     bool isFocusOnTouch_ { false };
255     bool isDefaultFocus_ { false };
256     bool isDefaultGroupFocus_ { false };
257 
258     std::function<void(int)> onFocusMove_;
259     std::function<void()> onFocus_;
260     std::function<void()> onBlur_;
261 
262     int32_t tabIndex_ = 0;
263 };
264 
265 } // namespace OHOS::Ace
266 
267 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_FOCUSABLE_FOCUSABLE_COMPONENT_H
268