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_LIST_LIST_ITEM_ELEMENT_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_LIST_LIST_ITEM_ELEMENT_H
18 
19 #include "core/components/list/list_item_component.h"
20 #include "core/pipeline/base/sole_child_element.h"
21 
22 namespace OHOS::Ace {
23 
24 class ListItemElement : public SoleChildElement, public FocusGroup {
25     DECLARE_ACE_TYPE(ListItemElement, SoleChildElement, FocusGroup);
26 
27 public:
28     bool RequestNextFocus(bool vertical, bool reverse, const Rect& rect) override;
29     virtual void HandleOnFocus();
30     virtual void HandleOnBlur();
31     virtual void HandleOnClick();
32 
33     bool CanUpdate(const RefPtr<Component>& newComponent) override;
34 
GetItemType()35     std::string GetItemType() const
36     {
37         return type_;
38     }
39 
GetFlags()40     uint32_t GetFlags() const
41     {
42         return flags_;
43     }
44 
TestFlag(uint32_t flag)45     bool TestFlag(uint32_t flag) const
46     {
47         return (flags_ & flag);
48     }
49 
RemoveFlag(uint32_t flag)50     void RemoveFlag(uint32_t flag)
51     {
52         flags_ &= ~flag;
53     }
54 
GetIndex()55     int32_t GetIndex() const
56     {
57         return index_;
58     }
59 
SetIndex(int32_t index)60     void SetIndex(int32_t index)
61     {
62         index_ = index;
63         if (listItemComponent_) {
64             listItemComponent_->SetIndex(index);
65         }
66     }
67 
AttachRenderNode(const RefPtr<RenderNode> & proxyNode)68     void AttachRenderNode(const RefPtr<RenderNode>& proxyNode)
69     {
70         proxyNode_ = proxyNode;
71     }
72 
GetProxyRenderNode()73     RefPtr<RenderNode> GetProxyRenderNode() const
74     {
75         return proxyNode_;
76     }
77 
78     bool OnKeyEvent(const KeyEvent& keyEvent) override;
79 
GetListItem(const RefPtr<Element> & element)80     static RefPtr<ListItemElement> GetListItem(const RefPtr<Element>& element)
81     {
82         RefPtr<Element> listItem = element;
83         while (listItem) {
84             if (AceType::InstanceOf<ListItemElement>(listItem)) {
85                 return AceType::DynamicCast<ListItemElement>(listItem);
86             }
87             listItem = listItem->GetFirstChild();
88         }
89         return nullptr;
90     }
91 
GetKey()92     int32_t GetKey() const
93     {
94         return key_;
95     }
96 
SetKey(int32_t key)97     void SetKey(int32_t key)
98     {
99         key_ = key;
100     }
101 
102 private:
103     RefPtr<RenderNode> CreateRenderNode() override;
104 
105     void Update() override;
106     void PerformBuild() override;
107 
108     uint32_t flags_ = 0;
109     std::string type_;
110     int32_t index_ = -1;
111     Radius topLeftRadius_;
112     Radius topRightRadius_;
113     Radius bottomLeftRadius_;
114     Radius bottomRightRadius_;
115     int32_t key_ = -1;
116 
117     RefPtr<RenderNode> proxyNode_;
118     RefPtr<ListItemComponent> listItemComponent_;
119 };
120 
121 } // namespace OHOS::Ace
122 
123 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_LIST_LIST_ITEM_ELEMENT_H
124