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 #include "core/components/grid_layout/render_grid_layout_item.h"
17 
18 #include "core/components/grid_layout/grid_layout_item_component.h"
19 #include "core/components/grid_layout/render_grid_layout.h"
20 
21 namespace OHOS::Ace {
22 
Create()23 RefPtr<RenderNode> RenderGridLayoutItem::Create()
24 {
25     return AceType::MakeRefPtr<RenderGridLayoutItem>();
26 }
27 
Update(const RefPtr<Component> & component)28 void RenderGridLayoutItem::Update(const RefPtr<Component>& component)
29 {
30     const RefPtr<GridLayoutItemComponent> gridItem = AceType::DynamicCast<GridLayoutItemComponent>(component);
31     if (!gridItem) {
32         return;
33     }
34     SetColumnIndex(gridItem->GetColumnIndex());
35     SetRowIndex(gridItem->GetRowIndex());
36     SetColumnSpan(gridItem->GetColumnSpan());
37     SetRowSpan(gridItem->GetRowSpan());
38     SetForceRebuild(gridItem->ForceRebuild());
39     InitAnimationController(GetContext());
40     onSelectId_ = gridItem->GetOnSelectId();
41     selectable_ = gridItem->GetSelectable();
42     itemWidth_ = gridItem->GetGridItemWidth();
43     itemHeight_ = gridItem->GetGridItemHeight();
44     MarkNeedLayout();
45 }
46 
PerformLayout()47 void RenderGridLayoutItem::PerformLayout()
48 {
49     if (GetChildren().empty()) {
50         LOGE("RenderGridLayoutItem: no child found in RenderGridLayoutItem!");
51     } else {
52         auto child = GetChildren().front();
53         child->Layout(GetLayoutParam());
54         child->SetPosition(Offset::Zero());
55         SetLayoutSize(child->GetLayoutSize());
56     }
57 }
58 
HandleOnFocus()59 void RenderGridLayoutItem::HandleOnFocus()
60 {
61     auto parent = GetParent().Upgrade();
62     while (parent) {
63         auto gridLayout = AceType::DynamicCast<RenderGridLayout>(parent);
64         if (gridLayout) {
65             gridLayout->UpdateFocusInfo(index_);
66             break;
67         }
68         parent = parent->GetParent().Upgrade();
69     }
70 }
71 
SetColumnIndex(int32_t columnIndex)72 void RenderGridLayoutItem::SetColumnIndex(int32_t columnIndex)
73 {
74     if (columnIndex < 0) {
75         return;
76     }
77     columnIndex_ = columnIndex;
78 }
79 
SetRowIndex(int32_t rowIndex)80 void RenderGridLayoutItem::SetRowIndex(int32_t rowIndex)
81 {
82     if (rowIndex < 0) {
83         return;
84     }
85     rowIndex_ = rowIndex;
86 }
87 
SetColumnSpan(int32_t columnSpan)88 void RenderGridLayoutItem::SetColumnSpan(int32_t columnSpan)
89 {
90     if (columnSpan <= 0) {
91         LOGW("Invalid columnSpan %{public}d", columnSpan);
92         return;
93     }
94     columnSpan_ = columnSpan;
95 }
96 
SetRowSpan(int32_t rowSpan)97 void RenderGridLayoutItem::SetRowSpan(int32_t rowSpan)
98 {
99     if (rowSpan <= 0) {
100         LOGW("Invalid rowSpan %{public}d", rowSpan);
101         return;
102     }
103     rowSpan_ = rowSpan;
104 }
105 
OnLongPressEvent()106 void RenderGridLayoutItem::OnLongPressEvent()
107 {
108     if (!OnItemLongPressed_) {
109         LOGE("%{public}s OnItemLongPressed_ is null.", __PRETTY_FUNCTION__);
110         return;
111     }
112 
113     OnItemLongPressed_(index_, AceType::WeakClaim(this));
114 }
115 
SetOnItemLongPressed(const OnItemLongPressed & func)116 void RenderGridLayoutItem::SetOnItemLongPressed(const OnItemLongPressed& func)
117 {
118     if (!func) {
119         LOGE("%{public}s func is null.", __PRETTY_FUNCTION__);
120     }
121     if (!OnItemLongPressed_) {
122         LOGI("%{public}s OnItemLongPressed_ is null before.", __PRETTY_FUNCTION__);
123     }
124     OnItemLongPressed_ = func;
125     if (!OnItemLongPressed_) {
126         LOGE("%{public}s OnItemLongPressed_ is null after.", __PRETTY_FUNCTION__);
127     }
128 }
129 
InitAnimationController(const WeakPtr<PipelineContext> & context)130 void RenderGridLayoutItem::InitAnimationController(const WeakPtr<PipelineContext>& context)
131 {
132     if (!animationController_) {
133         animationController_ = CREATE_ANIMATOR(context);
134         animationController_->SetDuration(ITEM_ANIMATION_DURATION);
135     }
136 }
137 
GetAnimationController()138 RefPtr<Animator> RenderGridLayoutItem::GetAnimationController()
139 {
140     return animationController_;
141 }
142 
AnimationAddInterpolator(const RefPtr<Animation<Point>> & animation)143 bool RenderGridLayoutItem::AnimationAddInterpolator(const RefPtr<Animation<Point>>& animation)
144 {
145     if (animationController_) {
146         if (animationController_->IsRunning()) {
147             animationController_->ClearInterpolators();
148             animationController_->ClearAllListeners();
149             animationController_->Stop();
150         }
151         animationController_->AddInterpolator(animation);
152         return true;
153     }
154     return false;
155 }
156 
AnimationPlay()157 void RenderGridLayoutItem::AnimationPlay()
158 {
159     if (animationController_) {
160         animationController_->Play();
161     }
162 }
163 } // namespace OHOS::Ace
164