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

#include "cru/base/Base.h"
#include "cru/platform/gui/Base.h"
#include "cru/platform/gui/Window.h"
#include "cru/ui/Base.h"
#include "cru/ui/host/WindowHost.h"

#include <memory>

namespace cru::ui::controls {
RootControl::RootControl(Control* attached_control)
    : attached_control_(attached_control) {
  GetContainerRenderObject()->SetDefaultHorizontalAlignment(Alignment::Stretch);
  GetContainerRenderObject()->SetDefaultVertialAlignment(Alignment::Stretch);
  window_host_ = std::make_unique<host::WindowHost>(this);

  Control::window_host_ = this->window_host_.get();

  window_host_->SetLayoutPreferToFillWindow(true);
}

RootControl::~RootControl() {}

platform::gui::INativeWindow* RootControl::GetNativeWindow() {
  return window_host_->GetNativeWindow();
}

void RootControl::SetGainFocusOnCreateAndDestroyWhenLoseFocus(bool value) {
  gain_focus_on_create_and_destroy_when_lose_focus_event_guard_.Clear();
  if (value) {
    auto native_window = window_host_->GetNativeWindow();

    gain_focus_on_create_and_destroy_when_lose_focus_event_guard_ +=
        native_window->VisibilityChangeEvent()->AddHandler(
            [native_window](platform::gui::WindowVisibilityType type) {
              if (type == platform::gui::WindowVisibilityType::Show) {
                native_window->RequestFocus();
              }
            });

    gain_focus_on_create_and_destroy_when_lose_focus_event_guard_ +=
        native_window->FocusEvent()->AddHandler(
            [native_window](platform::gui::FocusChangeType type) {
              if (type == platform::gui::FocusChangeType::Lose) {
                native_window->Close();
              }
            });
  }
}
}  // namespace cru::ui::controls