• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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_ng/pattern/grid/grid_utils.h"
17 
18 namespace OHOS::Ace::NG {
19 namespace {
20 
21 const std::string UNIT_AUTO = "auto";
22 
23 } // namespace
24 
ParseArgs(const std::string & args)25 std::string GridUtils::ParseArgs(const std::string& args)
26 {
27     if (args.empty() || args.find(UNIT_AUTO) == std::string::npos) {
28         return args;
29     }
30     std::string rowsArgs;
31     std::vector<std::string> strs;
32     StringUtils::StringSplitter(args, ' ', strs);
33     std::string current;
34     size_t rowArgSize = strs.size();
35     for (size_t i = 0; i < rowArgSize; ++i) {
36         current = strs[i];
37         // "auto" means 1fr in grid
38         if (strs[i] == std::string(UNIT_AUTO)) {
39             current = "1fr";
40         }
41         rowsArgs += " " + current;
42     }
43     return rowsArgs;
44 }
45 
46 namespace {
GetRowGap(const RefPtr<GridLayoutProperty> & props,float frameHeight)47 inline float GetRowGap(const RefPtr<GridLayoutProperty>& props, float frameHeight)
48 {
49     auto scale = props->GetLayoutConstraint()->scaleProperty;
50     return ConvertToPx(props->GetRowsGap().value_or(0.0_vp), scale, frameHeight).value_or(0);
51 }
52 
GetColumnGap(const RefPtr<GridLayoutProperty> & props,float frameWidth)53 inline float GetColumnGap(const RefPtr<GridLayoutProperty>& props, float frameWidth)
54 {
55     auto scale = props->GetLayoutConstraint()->scaleProperty;
56     return ConvertToPx(props->GetColumnsGap().value_or(0.0_vp), scale, frameWidth).value_or(0);
57 }
58 } // namespace
59 
GetMainGap(const RefPtr<GridLayoutProperty> & props,const SizeF & frameSize,Axis axis)60 float GridUtils::GetMainGap(const RefPtr<GridLayoutProperty>& props, const SizeF& frameSize, Axis axis)
61 {
62     return axis == Axis::HORIZONTAL ? GetColumnGap(props, frameSize.Width()) : GetRowGap(props, frameSize.Height());
63 }
64 
GetCrossGap(const RefPtr<GridLayoutProperty> & props,const SizeF & frameSize,Axis axis)65 float GridUtils::GetCrossGap(const RefPtr<GridLayoutProperty>& props, const SizeF& frameSize, Axis axis)
66 {
67     return axis == Axis::HORIZONTAL ? GetRowGap(props, frameSize.Height()) : GetColumnGap(props, frameSize.Width());
68 }
69 
CheckNeedCacheLayout(const RefPtr<LayoutWrapper> & layoutWrapper)70 bool GridUtils::CheckNeedCacheLayout(const RefPtr<LayoutWrapper>& layoutWrapper)
71 {
72     return !layoutWrapper || (layoutWrapper->CheckNeedForceMeasureAndLayout() && !layoutWrapper->CheckHasPreMeasured());
73 }
74 
75 } // namespace OHOS::Ace::NG