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 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_MENU_MENU_ITEM_GROUP_MENU_ITEM_GROUP_LAYOUT_ALGORITHM_H
16 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_MENU_MENU_ITEM_GROUP_MENU_ITEM_GROUP_LAYOUT_ALGORITHM_H
17 
18 #include <map>
19 
20 #include "base/geometry/dimension.h"
21 #include "base/geometry/ng/offset_t.h"
22 #include "core/components_ng/base/ui_node.h"
23 #include "core/components_ng/layout/box_layout_algorithm.h"
24 
25 namespace OHOS::Ace::NG {
26 class ACE_EXPORT MenuItemGroupLayoutAlgorithm : public BoxLayoutAlgorithm {
27     DECLARE_ACE_TYPE(MenuItemGroupLayoutAlgorithm, BoxLayoutAlgorithm);
28 
29 public:
30     using ItemPositionMap = std::map<int32_t, std::pair<float, float>>;
31 
MenuItemGroupLayoutAlgorithm(int32_t headerIndex,int32_t footerIndex,int32_t itemStartIndex)32     MenuItemGroupLayoutAlgorithm(int32_t headerIndex, int32_t footerIndex, int32_t itemStartIndex)
33         : headerIndex_(headerIndex), footerIndex_(footerIndex), itemStartIndex_(itemStartIndex)
34     {}
35     ~MenuItemGroupLayoutAlgorithm() override = default;
36 
37     void Measure(LayoutWrapper* layoutWrapper) override;
38 
39     void Layout(LayoutWrapper* layoutWrapper) override;
40 
41 private:
42     bool NeedHeaderPadding(const RefPtr<FrameNode>& host);
43     bool NeedFooterPadding(const RefPtr<FrameNode>& host);
44 
45     void LayoutHeader(LayoutWrapper* layoutWrapper);
46     void LayoutFooter(LayoutWrapper* layoutWrapper);
47     void LayoutIndex(const RefPtr<LayoutWrapper>& wrapper, const OffsetF& offset);
48     void LayoutMenuItem(LayoutWrapper* layoutWrapper);
49 
50     float GetChildrenMaxWidth(
51         const std::list<RefPtr<LayoutWrapper>>& children, const LayoutConstraintF& layoutConstraint);
52     std::list<WeakPtr<UINode>> GetItemsAndGroups(const RefPtr<FrameNode>& host) const;
53     RefPtr<FrameNode> GetBrotherNode(const RefPtr<FrameNode>& host);
54     bool IsLastNode(const RefPtr<FrameNode>& host) const;
55 
56     void UpdateHeaderAndFooterMargin(LayoutWrapper* layoutWrapper) const;
57 
58     int32_t headerIndex_ = -1;
59     int32_t footerIndex_ = -1;
60     int32_t itemStartIndex_ = 0;
61 
62     bool needHeaderPadding_ = false;
63     bool needFooterPadding_ = false;
64 
65     float groupDividerPadding_ = 0.0f;
66 
67     ItemPositionMap itemPosition_;
68 
69     ACE_DISALLOW_COPY_AND_MOVE(MenuItemGroupLayoutAlgorithm);
70 };
71 } // namespace OHOS::Ace::NG
72 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_MENU_MENU_ITEM_GROUP_MENU_ITEM_GROUP_LAYOUT_ALGORITHM_H
73