aboutsummaryrefslogtreecommitdiff
path: root/src/ui/layout_base.h
blob: 662210bd7b95b1a4d703174cbcdf0121c63c5177 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#pragma once

#include <unordered_set>

#include "base.h"
#include "ui_base.h"

namespace cru
{
    namespace ui
    {
        class Control;
        class Window;

        enum class Alignment
        {
            Center,
            Start,
            End
        };

        enum class MeasureMode
        {
            Exactly,
            Content,
            Stretch
        };

        enum class RectRange
        {
            Content, // content excluding padding, border and margin
            Padding, // only including content and padding
            HalfBorder, // including content, padding and half border
            FullBorder, // including content, padding and full border
            Margin // including content, padding, border and margin
        };

        struct Thickness
        {
            constexpr static Thickness Zero()
            {
                return Thickness(0);
            }

            constexpr Thickness() : Thickness(0) { }

            constexpr explicit Thickness(const float width)
                : left(width), top(width), right(width), bottom(width) { }

            constexpr explicit Thickness(const float horizontal, const float vertical)
                : left(horizontal), top(vertical), right(horizontal), bottom(vertical) { }

            constexpr Thickness(const float left, const float top, const float right, const float bottom)
                : left(left), top(top), right(right), bottom(bottom) { }

            float GetHorizontalTotal() const
            {
                return left + right;
            }

            float GetVerticalTotal() const
            {
                return top + bottom;
            }

            float Validate() const
            {
                return left >= 0.0 && top >= 0.0 && right >= 0.0 && bottom >= 0.0;
            }

            float left;
            float top;
            float right;
            float bottom;
        };

        struct LayoutSideParams final
        {
            constexpr static LayoutSideParams Exactly(const float length, const Alignment alignment = Alignment::Center)
            {
                return LayoutSideParams(MeasureMode::Exactly, length, alignment);
            }

            constexpr static LayoutSideParams Content(const Alignment alignment = Alignment::Center)
            {
                return LayoutSideParams(MeasureMode::Content, 0, alignment);
            }

            constexpr static LayoutSideParams Stretch(const Alignment alignment = Alignment::Center)
            {
                return LayoutSideParams(MeasureMode::Stretch, 0, alignment);
            }

            constexpr LayoutSideParams() = default;

            constexpr explicit LayoutSideParams(const MeasureMode mode, const float length, const Alignment alignment)
                : length(length), mode(mode), alignment(alignment)
            {

            }

            constexpr bool Validate() const
            {
                return length >= 0.0;
            }

            float length = 0.0;
            MeasureMode mode = MeasureMode::Content;
            Alignment alignment = Alignment::Center;
        };

        struct BasicLayoutParams final
        {
            BasicLayoutParams() = default;
            BasicLayoutParams(const BasicLayoutParams&) = default;
            BasicLayoutParams(BasicLayoutParams&&) = default;
            BasicLayoutParams& operator = (const BasicLayoutParams&) = default;
            BasicLayoutParams& operator = (BasicLayoutParams&&) = default;
            ~BasicLayoutParams() = default;

            bool Validate() const
            {
                return width.Validate() && height.Validate() && margin.Validate() && padding.Validate();
            }

            LayoutSideParams width;
            LayoutSideParams height;
            Thickness padding;
            Thickness margin;
        };


        class LayoutManager : public Object
        {
        public:
            static LayoutManager* GetInstance();

        public:
            LayoutManager() = default;
            LayoutManager(const LayoutManager& other) = delete;
            LayoutManager(LayoutManager&& other) = delete;
            LayoutManager& operator=(const LayoutManager& other) = delete;
            LayoutManager& operator=(LayoutManager&& other) = delete;
            ~LayoutManager() override = default;


            //*************** region: position cache ***************

            //Mark position cache of the control and its descendants invalid,
            //(which is saved as an auto-managed list internal)
            //and send a message to refresh them.
            void InvalidateControlPositionCache(Control* control);

            //Refresh position cache of the control and its descendants whose cache
            //has been marked as invalid.
            void RefreshInvalidControlPositionCache();

            //Refresh position cache of the control and its descendants immediately.
            static void RefreshControlPositionCache(Control* control);


            //*************** region: layout ***************

            void InvalidateWindowLayout(Window* window);

            void RefreshInvalidWindowLayout();

        private:
            static void RefreshControlPositionCacheInternal(Control* control, const Point& parent_lefttop_absolute);

        private:
            std::unordered_set<Control*> cache_invalid_controls_;
            std::unordered_set<Window*> layout_invalid_windows_;
        };
    }
}