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

#include "../Helper.hpp"
#include "cru/platform/graph/util/Painter.hpp"
#include "cru/ui/UiHost.hpp"

namespace cru::ui::render {
WindowRenderObject::WindowRenderObject(UiHost* host) {
  SetChildMode(ChildMode::Single);
  ui_host_ = host;
  after_layout_event_guard_.Reset(host->AfterLayoutEvent()->AddHandler(
      [this](auto) { NotifyAfterLayoutRecursive(this); }));
}

void WindowRenderObject::Draw(platform::graph::IPainter* 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::IPainter* 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;
}

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

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