aboutsummaryrefslogtreecommitdiff
path: root/src/ui/UiManager.cpp
blob: 7981aa864bb9265d8ba19428986323083dc08b8f (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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include "cru/ui/UiManager.hpp"
#include <optional>

#include "Helper.hpp"
#include "cru/platform/GraphBase.hpp"
#include "cru/platform/graphics/Brush.hpp"
#include "cru/platform/graphics/Factory.hpp"
#include "cru/platform/graphics/Font.hpp"
#include "cru/platform/gui/Cursor.hpp"
#include "cru/platform/gui/UiApplication.hpp"
#include "cru/ui/Base.hpp"
#include "cru/ui/style/ApplyBorderStyleInfo.hpp"
#include "cru/ui/style/Condition.hpp"
#include "cru/ui/style/Styler.hpp"

namespace cru::ui {
using namespace cru::platform::graphics;
using namespace cru::ui::style;
using namespace cru::ui::helper;

namespace {
std::unique_ptr<ISolidColorBrush> CreateSolidColorBrush(IGraphFactory* factory,
                                                        const Color& color) {
  auto brush = factory->CreateSolidColorBrush();
  brush->SetColor(color);
  return brush;
}
}  // namespace

UiManager* UiManager::GetInstance() {
  static UiManager* instance = new UiManager();
  GetUiApplication()->AddOnQuitHandler([] {
    delete instance;
    instance = nullptr;
  });
  return instance;
}

UiManager::UiManager() {
  const auto factory = GetGraphFactory();

  theme_resource_.default_font_family = u"等线";

  theme_resource_.default_font =
      factory->CreateFont(theme_resource_.default_font_family, 24.0f);

  const auto black_brush =
      std::shared_ptr<platform::graphics::ISolidColorBrush>(
          CreateSolidColorBrush(factory, colors::black));
  theme_resource_.text_brush = black_brush;
  theme_resource_.text_selection_brush =
      CreateSolidColorBrush(factory, colors::skyblue);
  theme_resource_.caret_brush = black_brush;

  theme_resource_.button_style.AddStyleRule(
      {NoCondition::Create(),
       BorderStyler::Create(ApplyBorderStyleInfo{std::nullopt, Thickness(3),
                                                 CornerRadius(5), std::nullopt,
                                                 std::nullopt}),
       u"DefaultButton"});
  theme_resource_.button_style.AddStyleRule(
      {ClickStateCondition::Create(ClickState::None),
       CompoundStyler::Create(
           BorderStyler::Create(ApplyBorderStyleInfo{
               CreateSolidColorBrush(factory, Color::FromHex(0x00bfff))}),
           CursorStyler::Create(platform::gui::SystemCursorType::Arrow)),
       u"DefaultButtonNormal"});
  theme_resource_.button_style.AddStyleRule(
      {ClickStateCondition::Create(ClickState::Hover),
       CompoundStyler::Create(
           BorderStyler::Create(ApplyBorderStyleInfo{
               CreateSolidColorBrush(factory, Color::FromHex(0x47d1ff))}),
           CursorStyler::Create(platform::gui::SystemCursorType::Hand)),
       u"DefaultButtonHover"});
  theme_resource_.button_style.AddStyleRule(
      {ClickStateCondition::Create(ClickState::Press),
       CompoundStyler::Create(
           BorderStyler::Create(ApplyBorderStyleInfo{
               CreateSolidColorBrush(factory, Color::FromHex(0x91e4ff))}),
           CursorStyler::Create(platform::gui::SystemCursorType::Hand)),
       u"DefaultButtonPress"});
  theme_resource_.button_style.AddStyleRule(
      {ClickStateCondition::Create(ClickState::PressInactive),
       CompoundStyler::Create(
           BorderStyler::Create(ApplyBorderStyleInfo{
               CreateSolidColorBrush(factory, Color::FromHex(0x91e4ff))}),
           CursorStyler::Create(platform::gui::SystemCursorType::Arrow)),
       u"DefaultButtonPressInactive"});

  theme_resource_.text_box_style.AddStyleRule(
      {NoCondition::Create(),
       BorderStyler::Create(
           ApplyBorderStyleInfo{std::nullopt, Thickness{1}, CornerRadius{5}}),
       u"DefaultTextBox"});
  theme_resource_.text_box_style.AddStyleRule(
      {HoverCondition::Create(false),
       BorderStyler::Create(ApplyBorderStyleInfo{
           CreateSolidColorBrush(factory, Color::FromHex(0xced4da))}),
       u"DefaultTextBoxNormal"});
  theme_resource_.text_box_style.AddStyleRule(
      {HoverCondition::Create(true),
       BorderStyler::Create(ApplyBorderStyleInfo{
           CreateSolidColorBrush(factory, Color::FromHex(0xced4da))}),
       u"DefaultTextBoxHover"});
  theme_resource_.text_box_style.AddStyleRule(
      {FocusCondition::Create(true),
       BorderStyler::Create(ApplyBorderStyleInfo{
           CreateSolidColorBrush(factory, Color::FromHex(0x495057))}),
       u"DefaultTextBoxFocus"});

  theme_resource_.menu_item_style.AddStyleRule(
      {NoCondition::Create(),
       BorderStyler::Create(
           ApplyBorderStyleInfo{std::nullopt, Thickness{0}, CornerRadius{0}}),
       u"DefaultMenuItem"});
}

UiManager::~UiManager() = default;
}  // namespace cru::ui