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