aboutsummaryrefslogtreecommitdiff
path: root/src/ui/ui_manager.cpp
blob: 4d14575b560d8806c1247ef57941452b9d880de1 (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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include "ui_manager.hpp"

#include <Windows.h>
#include <d2d1.h>
#include <dwrite.h>

#include "application.hpp"
#include "exception.hpp"
#include "graph/graph_manager.hpp"
#include "graph/graph_util.hpp"
#include "util/com_util.hpp"

namespace cru::ui {
namespace {
void GetSystemCaretInfo(CaretInfo* caret_info) {
  caret_info->caret_blink_duration =
      std::chrono::milliseconds(::GetCaretBlinkTime());
  DWORD caret_width;
  if (!::SystemParametersInfoW(SPI_GETCARETWIDTH, 0, &caret_width, 0))
    throw Win32Error(::GetLastError(), "Failed to get system caret width.");
  caret_info->half_caret_width = caret_width / 2.0f;
}

IDWriteTextFormat* CreateDefaultTextFormat() {
  const auto dwrite_factory =
      graph::GraphManager::GetInstance()->GetDWriteFactory();
  IDWriteTextFormat* text_format;

  ThrowIfFailed(dwrite_factory->CreateTextFormat(
      L"等线", nullptr, DWRITE_FONT_WEIGHT_NORMAL, DWRITE_FONT_STYLE_NORMAL,
      DWRITE_FONT_STRETCH_NORMAL, 24.0, L"zh-cn", &text_format));

  ThrowIfFailed(text_format->SetTextAlignment(DWRITE_TEXT_ALIGNMENT_CENTER));
  ThrowIfFailed(
      text_format->SetParagraphAlignment(DWRITE_PARAGRAPH_ALIGNMENT_CENTER));

  return text_format;
}
}  // namespace

PredefineResources::PredefineResources() {
  try {
    button_normal_border_brush =
        graph::CreateSolidColorBrush(D2D1::ColorF(D2D1::ColorF::Black));

    text_block_selection_brush =
        graph::CreateSolidColorBrush(D2D1::ColorF(D2D1::ColorF::LightSkyBlue));
    text_block_text_brush =
        graph::CreateSolidColorBrush(D2D1::ColorF(D2D1::ColorF::Black));
    text_block_text_format = CreateDefaultTextFormat();
  } catch (...) {
    util::SafeRelease(button_normal_border_brush);
    util::SafeRelease(text_block_selection_brush);
    util::SafeRelease(text_block_text_brush);
    util::SafeRelease(text_block_text_format);
  }
}

PredefineResources::~PredefineResources() {
  util::SafeRelease(button_normal_border_brush);
  util::SafeRelease(text_block_selection_brush);
  util::SafeRelease(text_block_text_brush);
  util::SafeRelease(text_block_text_format);
}

UiManager* UiManager::GetInstance() {
  return Application::GetInstance()->ResolveSingleton<UiManager>(
      [](auto) { return new UiManager{}; });
}

UiManager::UiManager() : predefine_resources_() {
  GetSystemCaretInfo(&caret_info_);
}
}  // namespace cru::ui