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

#include "cru/platform/painter_util.hpp"

#include <cassert>

namespace cru::ui::render {
void WindowRenderObject::MeasureAndLayout() {
  const auto client_size = window_->GetClientSize();
  Measure(client_size);
  Layout(Rect{Point{}, client_size});
}

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

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(point);
    if (result != nullptr) {
      return result;
    }
  }
  return Rect{Point{}, GetSize()}.IsPointInside(point) ? this : nullptr;
}

void WindowRenderObject::OnAddChild(RenderObject* new_child, int position) {
  assert(GetChildren().size() == 1);
}

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