aboutsummaryrefslogtreecommitdiff
path: root/src/ui/controls/RootControl.cpp
blob: 015703c3a80774d50c48bf1f47f70cb204e1697e (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
#include "cru/ui/controls/RootControl.hpp"

#include "cru/common/Base.hpp"
#include "cru/platform/gui/Base.hpp"
#include "cru/ui/host/WindowHost.hpp"
#include "cru/ui/render/Base.hpp"
#include "cru/ui/render/StackLayoutRenderObject.hpp"
#include "gsl/pointers"

#include <memory>

namespace cru::ui::controls {
RootControl::RootControl(Control* attached_control)
    : attached_control_(attached_control) {
  render_object_ = std::make_unique<render::StackLayoutRenderObject>();
  render_object_->SetAttachedControl(this);
  SetContainerRenderObject(render_object_.get());
  window_host_ = std::make_unique<host::WindowHost>(this);
}

RootControl::~RootControl() {}

render::RenderObject* RootControl::GetRenderObject() const {
  return render_object_.get();
}

void RootControl::EnsureWindowCreated() { this->GetNativeWindow(true); }

Rect RootControl::GetRect() { return window_host_->GetWindowRect(); }

void RootControl::SetRect(const Rect& rect) {
  window_host_->SetWindowRect(rect);
}

void RootControl::Show(bool create) {
  platform::gui::INativeWindow* native_window = GetNativeWindow(create);
  if (!native_window) return;
  native_window->SetVisible(true);
}

platform::gui::INativeWindow* RootControl::GetNativeWindow(bool create) {
  const auto host = GetWindowHost();
  platform::gui::INativeWindow* native_window = host->GetNativeWindow();
  if (!create) return native_window;
  if (!native_window) {
    native_window = this->CreateNativeWindow(
        host, attached_control_
                  ? attached_control_->GetWindowHost()->GetNativeWindow()
                  : nullptr);
  }
  return native_window;
}
}  // namespace cru::ui::controls