• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 #include "core/components_ng/pattern/grid/grid_layout_info.h"
16 
17 #include "base/utils/utils.h"
18 
19 namespace OHOS::Ace::NG {
GetItemIndexByPosition(int32_t position)20 int32_t GridLayoutInfo::GetItemIndexByPosition(int32_t position)
21 {
22     auto iter = positionItemIndexMap_.find(position);
23     if (iter != positionItemIndexMap_.end()) {
24         return iter->second;
25     }
26     return position;
27 }
28 
GetPositionByItemIndex(int32_t itemIndex)29 int32_t GridLayoutInfo::GetPositionByItemIndex(int32_t itemIndex)
30 {
31     auto position = itemIndex;
32     auto find = std::find_if(positionItemIndexMap_.begin(), positionItemIndexMap_.end(),
33         [itemIndex](const std::pair<int32_t, int32_t>& item) { return item.second == itemIndex; });
34     if (find != positionItemIndexMap_.end()) {
35         position = find->first;
36     }
37 
38     return position;
39 }
40 
GetOriginalIndex() const41 int32_t GridLayoutInfo::GetOriginalIndex() const
42 {
43     return currentMovingItemPosition_;
44 }
45 
ClearDragState()46 void GridLayoutInfo::ClearDragState()
47 {
48     positionItemIndexMap_.clear();
49     currentMovingItemPosition_ = -1;
50 }
51 
MoveItemsBack(int32_t from,int32_t to,int32_t itemIndex)52 void GridLayoutInfo::MoveItemsBack(int32_t from, int32_t to, int32_t itemIndex)
53 {
54     auto lastItemIndex = itemIndex;
55     for (int32_t i = from; i <= to; ++i) {
56         int32_t mainIndex = (i - startIndex_) / crossCount_ + startMainLineIndex_;
57         int32_t crossIndex = (i - startIndex_) % crossCount_;
58         if (i == from) {
59             gridMatrix_[mainIndex][crossIndex] = itemIndex;
60         } else {
61             auto index = GetItemIndexByPosition(i - 1);
62             gridMatrix_[mainIndex][crossIndex] = index;
63             positionItemIndexMap_[i - 1] = lastItemIndex;
64             lastItemIndex = index;
65         }
66     }
67     positionItemIndexMap_[from] = itemIndex;
68     positionItemIndexMap_[to] = lastItemIndex;
69     currentMovingItemPosition_ = from;
70 }
71 
MoveItemsForward(int32_t from,int32_t to,int32_t itemIndex)72 void GridLayoutInfo::MoveItemsForward(int32_t from, int32_t to, int32_t itemIndex)
73 {
74     for (int32_t i = from; i <= to; ++i) {
75         int32_t mainIndex = (i - startIndex_) / crossCount_ + startMainLineIndex_;
76         int32_t crossIndex = (i - startIndex_) % crossCount_;
77         if (i == to) {
78             gridMatrix_[mainIndex][crossIndex] = itemIndex;
79         } else {
80             auto index = GetItemIndexByPosition(i + 1);
81             gridMatrix_[mainIndex][crossIndex] = index;
82             positionItemIndexMap_[i] = index;
83         }
84     }
85     positionItemIndexMap_[to] = itemIndex;
86     currentMovingItemPosition_ = to;
87 }
88 
SwapItems(int32_t itemIndex,int32_t insertIndex)89 void GridLayoutInfo::SwapItems(int32_t itemIndex, int32_t insertIndex)
90 {
91     currentMovingItemPosition_ = currentMovingItemPosition_ == -1 ? itemIndex : currentMovingItemPosition_;
92     auto insertPositon = insertIndex;
93     // drag from another grid
94     if (itemIndex == -1) {
95         if (currentMovingItemPosition_ == -1) {
96             MoveItemsBack(insertPositon, childrenCount_, itemIndex);
97             return;
98         }
99     } else {
100         insertPositon = GetPositionByItemIndex(insertIndex);
101     }
102 
103     if (currentMovingItemPosition_ > insertPositon) {
104         MoveItemsBack(insertPositon, currentMovingItemPosition_, itemIndex);
105         return;
106     }
107 
108     if (insertPositon > currentMovingItemPosition_) {
109         MoveItemsForward(currentMovingItemPosition_, insertPositon, itemIndex);
110     }
111 }
112 
UpdateEndLine(float mainSize,float mainGap)113 void GridLayoutInfo::UpdateEndLine(float mainSize, float mainGap)
114 {
115     if (mainSize >= lastMainSize_) {
116         return;
117     }
118     for (auto i = startMainLineIndex_; i < endMainLineIndex_; ++i) {
119         mainSize -= (lineHeightMap_[i] + mainGap);
120         if (LessOrEqual(mainSize + mainGap, 0)) {
121             endMainLineIndex_ = i;
122             break;
123         }
124     }
125 }
126 } // namespace OHOS::Ace::NG