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

#include "cru/common/Base.hpp"
#include "cru/platform/gui/Base.hpp"
#include "cru/platform/gui/Window.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,
                         host::CreateWindowParams params)
    : 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, params);

  if (gain_focus_on_create_and_destroy_when_lose_focus_) {
    auto native_window = window_host_->GetNativeWindow();
    native_window->CreateEvent()->AddHandler(
        [](platform::gui::INativeWindow* window) {
          window->CreateEvent()->AddHandler(
              [window](std::nullptr_t) { window->RequestFocus(); });
        });

    native_window->FocusEvent()->AddHandler(
        [native_window](platform::gui::FocusChangeType type) {
          if (type == platform::gui::FocusChangeType::Lost) {
            native_window->Close();
          }
        });
  }
}

RootControl::~RootControl() {}

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

void RootControl::SetGainFocusOnCreateAndDestroyWhenLoseFocus(bool value) {
  gain_focus_on_create_and_destroy_when_lose_focus_ = value;
}
}  // namespace cru::ui::controls