aboutsummaryrefslogtreecommitdiff
path: root/src/ui/render/flex_layout_render_object.hpp
blob: 7172f0c0e63ba6ed5d02dbf9469b0657d822875c (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
#pragma once
#include "pre.hpp"

#include <optional>

#include "render_object.hpp"

namespace cru::ui::render {
enum class FlexDirection {
  Horizontal,
  HorizontalReverse,
  Vertical,
  VertivalReverse
};

enum class Alignment { Start, End, Center };

struct FlexChildLayoutData {
  std::optional<float> flex_basis;
  float flex_grow = 0;
  float flex_shrink = 0;
  Alignment alignment = Alignment::Center;
};

class FlexLayoutRenderObject : public RenderObject {
 public:
  FlexLayoutRenderObject() = default;
  FlexLayoutRenderObject(const FlexLayoutRenderObject& other) = delete;
  FlexLayoutRenderObject& operator=(const FlexLayoutRenderObject& other) =
      delete;
  FlexLayoutRenderObject(FlexLayoutRenderObject&& other) = delete;
  FlexLayoutRenderObject& operator=(FlexLayoutRenderObject&& other) = delete;
  ~FlexLayoutRenderObject() override = default;

  FlexDirection GetFlexDirection() const { return direction_; }
  void SetFlexDirection(FlexDirection direction) { direction_ = direction; }

  FlexChildLayoutData* GetChildLayoutData(int position);

  void Draw(ID2D1RenderTarget* render_target) override;

  RenderObject* HitTest(const Point& point) override;

 protected:
  void OnAddChild(RenderObject* new_child, int position) override;
  void OnRemoveChild(RenderObject* removed_child, int position) override;

  Size OnMeasureContent(const Size& available_size) override;
  void OnLayoutContent(const Rect& content_rect) override;

 private:
  FlexDirection direction_ = FlexDirection::Horizontal;
  std::vector<FlexChildLayoutData> child_layout_data_{};
};
}  // namespace cru::ui::render