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 "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 itemWidth_ = gridItem->GetGridItemWidth();
44 itemHeight_ = gridItem->GetGridItemHeight();
45 MarkNeedLayout();
46 }
47
PerformLayout()48 void RenderGridLayoutItem::PerformLayout()
49 {
50 if (GetChildren().empty()) {
51 LOGE("RenderGridLayoutItem: no child found in RenderGridLayoutItem!");
52 } else {
53 LOGD("PerformLayout column:%{public}d row:%{public}d", columnIndex_, rowIndex_);
54 auto child = GetChildren().front();
55 child->Layout(GetLayoutParam());
56 child->SetPosition(Offset::Zero());
57 SetLayoutSize(child->GetLayoutSize());
58 }
59 }
60
HandleOnFocus()61 void RenderGridLayoutItem::HandleOnFocus()
62 {
63 auto parent = GetParent().Upgrade();
64 while (parent) {
65 auto gridLayout = AceType::DynamicCast<RenderGridLayout>(parent);
66 if (gridLayout) {
67 gridLayout->UpdateFocusInfo(index_);
68 break;
69 }
70 parent = parent->GetParent().Upgrade();
71 }
72 }
73
SetColumnIndex(int32_t columnIndex)74 void RenderGridLayoutItem::SetColumnIndex(int32_t columnIndex)
75 {
76 if (columnIndex < 0) {
77 LOGD("Invalid columnIndex %{public}d, use default", columnIndex);
78 return;
79 }
80 columnIndex_ = columnIndex;
81 }
82
SetRowIndex(int32_t rowIndex)83 void RenderGridLayoutItem::SetRowIndex(int32_t rowIndex)
84 {
85 if (rowIndex < 0) {
86 LOGD("Invalid rowIndex %{public}d, use default", rowIndex);
87 return;
88 }
89 rowIndex_ = rowIndex;
90 }
91
SetColumnSpan(int32_t columnSpan)92 void RenderGridLayoutItem::SetColumnSpan(int32_t columnSpan)
93 {
94 if (columnSpan <= 0) {
95 LOGW("Invalid columnSpan %{public}d", columnSpan);
96 return;
97 }
98 columnSpan_ = columnSpan;
99 }
100
SetRowSpan(int32_t rowSpan)101 void RenderGridLayoutItem::SetRowSpan(int32_t rowSpan)
102 {
103 if (rowSpan <= 0) {
104 LOGW("Invalid rowSpan %{public}d", rowSpan);
105 return;
106 }
107 rowSpan_ = rowSpan;
108 }
109
OnLongPressEvent()110 void RenderGridLayoutItem::OnLongPressEvent()
111 {
112 if (!OnItemLongPressed_) {
113 LOGE("%{public}s OnItemLongPressed_ is null.", __PRETTY_FUNCTION__);
114 return;
115 }
116
117 OnItemLongPressed_(index_, AceType::WeakClaim(this));
118 }
119
SetOnItemLongPressed(const OnItemLongPressed & func)120 void RenderGridLayoutItem::SetOnItemLongPressed(const OnItemLongPressed& func)
121 {
122 if (!func) {
123 LOGE("%{public}s func is null.", __PRETTY_FUNCTION__);
124 }
125 if (!OnItemLongPressed_) {
126 LOGI("%{public}s OnItemLongPressed_ is null before.", __PRETTY_FUNCTION__);
127 }
128 OnItemLongPressed_ = func;
129 if (!OnItemLongPressed_) {
130 LOGE("%{public}s OnItemLongPressed_ is null after.", __PRETTY_FUNCTION__);
131 }
132 }
133
InitAnimationController(const WeakPtr<PipelineContext> & context)134 void RenderGridLayoutItem::InitAnimationController(const WeakPtr<PipelineContext>& context)
135 {
136 if (!animationController_) {
137 animationController_ = CREATE_ANIMATOR(context);
138 animationController_->SetDuration(ITEM_ANIMATION_DURATION);
139 }
140 }
141
GetAnimationController()142 RefPtr<Animator> RenderGridLayoutItem::GetAnimationController()
143 {
144 return animationController_;
145 }
146
AnimationAddInterpolator(const RefPtr<Animation<Point>> & animation)147 bool RenderGridLayoutItem::AnimationAddInterpolator(const RefPtr<Animation<Point>>& animation)
148 {
149 LOGD("%{public}s begin.", __PRETTY_FUNCTION__);
150 if (animationController_) {
151 if (animationController_->IsRunning()) {
152 animationController_->ClearInterpolators();
153 animationController_->ClearAllListeners();
154 animationController_->Stop();
155 }
156 animationController_->AddInterpolator(animation);
157 return true;
158 }
159 return false;
160 }
161
AnimationPlay()162 void RenderGridLayoutItem::AnimationPlay()
163 {
164 LOGD("%{public}s begin.", __PRETTY_FUNCTION__);
165 if (animationController_) {
166 animationController_->Play();
167 }
168 }
169 } // namespace OHOS::Ace
170