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