aboutsummaryrefslogtreecommitdiff
path: root/src/ui/render/window_render_object.cpp
blob: e2e968979e2cf05f5d0c29788cbc0bbc2545f64f (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
#include "cru/ui/render/window_render_object.hpp"

#include "cru/common/logger.hpp"
#include "cru/platform/graph/util/painter_util.hpp"
#include "cru/platform/native/native_window.hpp"
#include "cru/platform/native/ui_application.hpp"
#include "cru/ui/window.hpp"

#include <cassert>

namespace cru::ui::render {
class WindowRenderHost : public IRenderHost,
                         public SelfResolvable<WindowRenderHost> {
 public:
  WindowRenderHost(WindowRenderObject* render_object)
      : render_object_(render_object) {
    assert(render_object != nullptr);
  }

  void InvalidateLayout() override;

  void InvalidatePaint() override {
    render_object_->GetWindow()->GetNativeWindow()->Repaint();
  }

  IEvent<AfterLayoutEventArgs>* AfterLayoutEvent() override {
    return &after_layout_event_;
  }

 private:
  WindowRenderObject* render_object_;

  bool need_layout_ = false;

  Event<AfterLayoutEventArgs> after_layout_event_;
};

void WindowRenderHost::InvalidateLayout() {
  if (!need_layout_) {
    log::Debug(L"A relayout is required.");
    platform::native::UiApplication::GetInstance()->InvokeLater(
        [resolver = this->CreateResolver()] {
          if (const auto host = resolver.Resolve()) {
            host->render_object_->Relayout();
            host->need_layout_ = false;
            host->after_layout_event_.Raise(AfterLayoutEventArgs{});
            log::Debug(L"A relayout finished.");
            host->InvalidatePaint();
          }
        });
    need_layout_ = true;
  }
}

WindowRenderObject::WindowRenderObject(Window* window)
    : window_(window), render_host_(new WindowRenderHost(this)) {
  SetChildMode(ChildMode::Single);
  SetRenderHost(render_host_.get());
}

void WindowRenderObject::Relayout() {
  const auto client_size = window_->GetNativeWindow()->GetClientSize();
  Measure(client_size);
  Layout(Rect{Point{}, client_size});
}

void WindowRenderObject::Draw(platform::graph::Painter* painter) {
  painter->Clear(colors::white);
  if (const auto child = GetChild()) {
    auto offset = child->GetOffset();
    platform::graph::util::WithTransform(
        painter, platform::Matrix::Translation(offset.x, offset.y),
        [child](platform::graph::Painter* p) { child->Draw(p); });
  }
}

RenderObject* WindowRenderObject::HitTest(const Point& point) {
  if (const auto child = GetChild()) {
    auto offset = child->GetOffset();
    Point p{point.x - offset.x, point.y - offset.y};
    const auto result = child->HitTest(p);
    if (result != nullptr) {
      return result;
    }
  }
  return Rect{Point{}, GetSize()}.IsPointInside(point) ? this : nullptr;
}

namespace {
void SetRenderHostRecursive(RenderObject* render_object, IRenderHost* host) {
  render_object->SetRenderHost(host);
  for (const auto child : render_object->GetChildren()) {
    SetRenderHostRecursive(render_object, host);
  }
}
}  // namespace

void WindowRenderObject::OnAddChild(RenderObject* new_child, int position) {
  SetRenderHostRecursive(new_child, render_host_.get());
}

void WindowRenderObject::OnRemoveChild(RenderObject* new_child, int position) {
  SetRenderHostRecursive(new_child, nullptr);
}

Size WindowRenderObject::OnMeasureContent(const Size& available_size) {
  if (const auto child = GetChild()) child->Measure(available_size);
  return available_size;
}

void WindowRenderObject::OnLayoutContent(const Rect& content_rect) {
  if (const auto child = GetChild()) child->Layout(content_rect);
}
}  // namespace cru::ui::render