1a3e0fd82Sopenharmony_ci/*
2a3e0fd82Sopenharmony_ci * Copyright (c) 2020-2021 Huawei Device Co., Ltd.
3a3e0fd82Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4a3e0fd82Sopenharmony_ci * you may not use this file except in compliance with the License.
5a3e0fd82Sopenharmony_ci * You may obtain a copy of the License at
6a3e0fd82Sopenharmony_ci *
7a3e0fd82Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8a3e0fd82Sopenharmony_ci *
9a3e0fd82Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10a3e0fd82Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11a3e0fd82Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12a3e0fd82Sopenharmony_ci * See the License for the specific language governing permissions and
13a3e0fd82Sopenharmony_ci * limitations under the License.
14a3e0fd82Sopenharmony_ci */
15a3e0fd82Sopenharmony_ci
16a3e0fd82Sopenharmony_ci/**
17a3e0fd82Sopenharmony_ci * @addtogroup UI_Layout
18a3e0fd82Sopenharmony_ci * @{
19a3e0fd82Sopenharmony_ci *
20a3e0fd82Sopenharmony_ci * @brief Defines UI layouts such as <b>FlexLayout</b> and <b>GridLayout</b>.
21a3e0fd82Sopenharmony_ci *
22a3e0fd82Sopenharmony_ci * @since 1.0
23a3e0fd82Sopenharmony_ci * @version 1.0
24a3e0fd82Sopenharmony_ci */
25a3e0fd82Sopenharmony_ci
26a3e0fd82Sopenharmony_ci/**
27a3e0fd82Sopenharmony_ci * @file flex_layout.h
28a3e0fd82Sopenharmony_ci *
29a3e0fd82Sopenharmony_ci * @brief Declares a flexible layout container. You can perform simple adaptive layout on child views that the
30a3e0fd82Sopenharmony_ci *        container holds, for example, to evenly arrange all child views in the same row or column.
31a3e0fd82Sopenharmony_ci *
32a3e0fd82Sopenharmony_ci * @since 1.0
33a3e0fd82Sopenharmony_ci * @version 1.0
34a3e0fd82Sopenharmony_ci */
35a3e0fd82Sopenharmony_ci
36a3e0fd82Sopenharmony_ci#ifndef GRAPHIC_LITE_FLEX_LAYOUT_H
37a3e0fd82Sopenharmony_ci#define GRAPHIC_LITE_FLEX_LAYOUT_H
38a3e0fd82Sopenharmony_ci
39a3e0fd82Sopenharmony_ci#include "layout.h"
40a3e0fd82Sopenharmony_ci
41a3e0fd82Sopenharmony_cinamespace OHOS {
42a3e0fd82Sopenharmony_ci/**
43a3e0fd82Sopenharmony_ci * @brief Defines a flexible layout container. You can perform simple adaptive layout on child views that the
44a3e0fd82Sopenharmony_ci *        container holds, for example, to evenly arrange all child views in the same row or column.
45a3e0fd82Sopenharmony_ci *
46a3e0fd82Sopenharmony_ci * @since 1.0
47a3e0fd82Sopenharmony_ci * @version 1.0
48a3e0fd82Sopenharmony_ci */
49a3e0fd82Sopenharmony_ciclass FlexLayout : public Layout {
50a3e0fd82Sopenharmony_cipublic:
51a3e0fd82Sopenharmony_ci    static constexpr uint8_t NOWRAP = 0;
52a3e0fd82Sopenharmony_ci    static constexpr uint8_t WRAP = 1;
53a3e0fd82Sopenharmony_ci
54a3e0fd82Sopenharmony_ci    /**
55a3e0fd82Sopenharmony_ci     * @brief A default constructor used to create a <b>FlexLayout</b> instance.
56a3e0fd82Sopenharmony_ci     * @since 1.0
57a3e0fd82Sopenharmony_ci     * @version 1.0
58a3e0fd82Sopenharmony_ci     */
59a3e0fd82Sopenharmony_ci    FlexLayout()
60a3e0fd82Sopenharmony_ci        : majorAlign_(ALIGN_START), secondaryAlign_(ALIGN_CENTER), wrap_(NOWRAP), rowCount_(1), columnCount_(1) {}
61a3e0fd82Sopenharmony_ci
62a3e0fd82Sopenharmony_ci    /**
63a3e0fd82Sopenharmony_ci     * @brief A destructor used to delete the <b>FlexLayout</b> instance.
64a3e0fd82Sopenharmony_ci     * @since 1.0
65a3e0fd82Sopenharmony_ci     * @version 1.0
66a3e0fd82Sopenharmony_ci     */
67a3e0fd82Sopenharmony_ci    virtual ~FlexLayout() {}
68a3e0fd82Sopenharmony_ci
69a3e0fd82Sopenharmony_ci    /**
70a3e0fd82Sopenharmony_ci     * @brief Sets the alignment mode of the primary axis (the axis where the layout direction is located).
71a3e0fd82Sopenharmony_ci     *        The child views in the layout are placed in this mode in the direction of the primary axis.
72a3e0fd82Sopenharmony_ci     * @param align Indicates the alignment mode to set. The value can be <b>ALIGN_START</b>, <b>ALIGN_END</b>,
73a3e0fd82Sopenharmony_ci     *              <b>ALIGN_CENTER</b>, <b>ALIGN_EVENLY</b>, <b>ALIGN_AROUND</b>, or <b>ALIGN_BETWEEN</b>.
74a3e0fd82Sopenharmony_ci     * @since 1.0
75a3e0fd82Sopenharmony_ci     * @version 1.0
76a3e0fd82Sopenharmony_ci     */
77a3e0fd82Sopenharmony_ci    void SetMajorAxisAlign(const AlignType& align)
78a3e0fd82Sopenharmony_ci    {
79a3e0fd82Sopenharmony_ci        majorAlign_ = align;
80a3e0fd82Sopenharmony_ci    }
81a3e0fd82Sopenharmony_ci
82a3e0fd82Sopenharmony_ci    /**
83a3e0fd82Sopenharmony_ci     * @brief Sets the alignment mode of the secondary axis (the axis perpendicular to the set layout direction).
84a3e0fd82Sopenharmony_ci     * @param align Indicates the alignment mode to set. The value can be <b>ALIGN_START</b>, <b>ALIGN_CENTER</b>,
85a3e0fd82Sopenharmony_ci     *              or <b>ALIGN_END</b>.
86a3e0fd82Sopenharmony_ci     * @since 1.0
87a3e0fd82Sopenharmony_ci     * @version 1.0
88a3e0fd82Sopenharmony_ci     */
89a3e0fd82Sopenharmony_ci    void SetSecondaryAxisAlign(const AlignType& align)
90a3e0fd82Sopenharmony_ci    {
91a3e0fd82Sopenharmony_ci        secondaryAlign_ = align;
92a3e0fd82Sopenharmony_ci    }
93a3e0fd82Sopenharmony_ci
94a3e0fd82Sopenharmony_ci    /**
95a3e0fd82Sopenharmony_ci     * @brief Sets whether to support word wrap.
96a3e0fd82Sopenharmony_ci     * @param wrap Indicates the word wrap attribute.
97a3e0fd82Sopenharmony_ci     * @since 1.0
98a3e0fd82Sopenharmony_ci     * @version 1.0
99a3e0fd82Sopenharmony_ci     */
100a3e0fd82Sopenharmony_ci    void SetFlexWrap(uint8_t wrap)
101a3e0fd82Sopenharmony_ci    {
102a3e0fd82Sopenharmony_ci        wrap_ = wrap;
103a3e0fd82Sopenharmony_ci    }
104a3e0fd82Sopenharmony_ci
105a3e0fd82Sopenharmony_ci    /**
106a3e0fd82Sopenharmony_ci     * @brief Lays out all child views according to the preset arrangement mode.
107a3e0fd82Sopenharmony_ci     * @param needInvalidate Specifies whether to refresh the invalidated area after the layout is complete.
108a3e0fd82Sopenharmony_ci     *                       Value <b>true</b> means to refresh the invalidated area after the layout is complete,
109a3e0fd82Sopenharmony_ci     *                       and <b>false</b> means the opposite.
110a3e0fd82Sopenharmony_ci     * @since 1.0
111a3e0fd82Sopenharmony_ci     * @version 1.0
112a3e0fd82Sopenharmony_ci     */
113a3e0fd82Sopenharmony_ci    void LayoutChildren(bool needInvalidate = false) override;
114a3e0fd82Sopenharmony_ci
115a3e0fd82Sopenharmony_ciprivate:
116a3e0fd82Sopenharmony_ci    void LayoutHorizontal();
117a3e0fd82Sopenharmony_ci    void LayoutVertical();
118a3e0fd82Sopenharmony_ci    void CalValidLength(uint16_t& totalValidLength, uint16_t& allChildNum);
119a3e0fd82Sopenharmony_ci    void GetStartPos(const int16_t& length, int16_t& pos, int16_t& interval, int16_t count,
120a3e0fd82Sopenharmony_ci        uint16_t* validLengths, uint16_t* childsNum);
121a3e0fd82Sopenharmony_ci    void GetNoWrapStartPos(const int16_t& length, int16_t& majorPos, int16_t& interval);
122a3e0fd82Sopenharmony_ci    void CalRowCount();
123a3e0fd82Sopenharmony_ci    void GetRowMaxHeight(uint16_t size, uint16_t* maxRosHegiht);
124a3e0fd82Sopenharmony_ci    void GetRowsWidth(uint16_t rowNum, uint16_t* rowsWidth, uint16_t* rowsChildNum);
125a3e0fd82Sopenharmony_ci    void GetRowStartPos(int16_t& pos, int16_t& interval, int16_t count,
126a3e0fd82Sopenharmony_ci        uint16_t* rowsWidth, uint16_t* rowsChildNum);
127a3e0fd82Sopenharmony_ci    void CalColumnCount();
128a3e0fd82Sopenharmony_ci    void GetColumnMaxWidth(uint16_t size, uint16_t* maxColumnsWidth);
129a3e0fd82Sopenharmony_ci    void GetColumnsHeight(uint16_t columnNum, uint16_t* columnsHeight, uint16_t* columnsChildNum);
130a3e0fd82Sopenharmony_ci    void GetColumnStartPos(int16_t& pos, int16_t& interval, int16_t count,
131a3e0fd82Sopenharmony_ci        uint16_t* columnsHeight, uint16_t* columnsChildNum);
132a3e0fd82Sopenharmony_ci    void GetCrossAxisPosY(int16_t& posY, uint16_t& count, uint16_t* rowsMaxHeight, UIView* child);
133a3e0fd82Sopenharmony_ci    void GetCrossAxisPosX(int16_t& posX, uint16_t& count, uint16_t* columnsMaxWidth, UIView* child);
134a3e0fd82Sopenharmony_ci    static constexpr uint16_t MAX_COUNT_DEFAULT = 100;
135a3e0fd82Sopenharmony_ci    AlignType majorAlign_;
136a3e0fd82Sopenharmony_ci    AlignType secondaryAlign_;
137a3e0fd82Sopenharmony_ci    uint8_t wrap_;
138a3e0fd82Sopenharmony_ci    uint16_t rowCount_;
139a3e0fd82Sopenharmony_ci    uint16_t columnCount_;
140a3e0fd82Sopenharmony_ci};
141a3e0fd82Sopenharmony_ci} // namespace OHOS
142a3e0fd82Sopenharmony_ci#endif // GRAPHIC_LITE_FLEX_LAYOUT_H
143