aboutsummaryrefslogtreecommitdiff
path: root/demos/InputMethod/main.cpp
blob: 6c0fba3c9fe4eb581a51e4ea75e9dc7873e3aa2f (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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include "cru/platform/Color.h"
#include "cru/platform/GraphicsBase.h"
#include "cru/platform/Matrix.h"
#include "cru/platform/bootstrap/Bootstrap.h"
#include "cru/platform/graphics/Factory.h"
#include "cru/platform/graphics/Font.h"
#include "cru/platform/graphics/Painter.h"
#include "cru/platform/gui/InputMethod.h"
#include "cru/platform/gui/UiApplication.h"
#include "cru/platform/gui/Window.h"

using namespace cru;
using namespace cru::platform;
using namespace cru::platform::graphics;
using namespace cru::platform::gui;

int main() {
  IUiApplication* application = bootstrap::CreateUiApplication();
  application->SetQuitOnAllWindowClosed(true);

  auto graphics_factory = application->GetGraphicsFactory();

  auto window = application->CreateWindow();

  auto input_method_context = window->GetInputMethodContext();

  auto brush = graphics_factory->CreateSolidColorBrush();
  brush->SetColor(colors::black);

  auto odd_clause_brush = graphics_factory->CreateSolidColorBrush();
  odd_clause_brush->SetColor(colors::yellow);
  auto even_clause_brush = graphics_factory->CreateSolidColorBrush();
  even_clause_brush->SetColor(colors::green);
  auto target_clause_brush = graphics_factory->CreateSolidColorBrush();
  target_clause_brush->SetColor(colors::blue);

  std::shared_ptr<IFont> font = graphics_factory->CreateFont(String{}, 30);

  auto prompt_text_layout =
      graphics_factory->CreateTextLayout(font,
                                         u"Ctrl+1: Enable IME\n"
                                         u"Ctrl+2: Disable IME\n"
                                         u"Ctrl+3: Complete composition.\n"
                                         u"Ctrl+4: Cancel composition.");
  String committed_text;
  auto text_layout = graphics_factory->CreateTextLayout(font, u"");
  CompositionText composition_text;

  auto update_text_layout_width = [&prompt_text_layout,
                                   &text_layout](const Size& size) {
    prompt_text_layout->SetMaxWidth(size.width);
    text_layout->SetMaxWidth(size.width);
  };

  update_text_layout_width(window->GetClientSize());
  window->ResizeEvent()->AddHandler(update_text_layout_width);

  window->PaintEvent()->AddSpyOnlyHandler([&]() {
    auto painter = window->BeginPaint();
    painter->Clear(colors::white);

    painter->DrawText(Point{}, prompt_text_layout.get(), brush.get());
    painter->PushState();
    painter->ConcatTransform(
        Matrix::Translation(0, prompt_text_layout->GetTextBounds().height));

    for (int i = 0; i < static_cast<int>(composition_text.clauses.size());
         i++) {
      const auto& clause = composition_text.clauses[i];
      auto rects = text_layout->TextRangeRect(TextRange::FromTwoSides(
          clause.start, clause.end, committed_text.size()));
      const auto& b = clause.target
                          ? target_clause_brush
                          : (i % 2 ? odd_clause_brush : even_clause_brush);
      for (auto& rect : rects) {
        painter->FillRectangle(rect, b.get());
      }
    }

    painter->DrawText(Point{}, text_layout.get(), brush.get());

    const auto cursor_pos = composition_text.selection.position +
                            static_cast<int>(committed_text.size());
    auto cursor_rect = text_layout->TextSinglePoint(cursor_pos, false);
    painter->FillRectangle(
        Rect{cursor_rect.left, cursor_rect.top, 3, cursor_rect.height},
        brush.get());

    painter->PopState();
    painter->EndDraw();
  });

  window->KeyDownEvent()->AddHandler(
      [&input_method_context](const NativeKeyEventArgs& args) {
        if (args.modifier & KeyModifiers::ctrl) {
          switch (args.key) {
            case KeyCode::N1:
              input_method_context->EnableIME();
              break;
            case KeyCode::N2:
              input_method_context->DisableIME();
              break;
            case KeyCode::N3:
              input_method_context->CompleteComposition();
              break;
            case KeyCode::N4:
              input_method_context->CancelComposition();
              break;
            default:
              break;
          }
        }
      });

  auto update_state = [&] {
    text_layout->SetText(committed_text + composition_text.text);
    auto y = prompt_text_layout->GetTextBounds().height;
    const auto cursor_pos = composition_text.selection.position +
                            static_cast<int>(committed_text.size());
    auto cursor_rect = text_layout->TextSinglePoint(cursor_pos, false);
    input_method_context->SetCandidateWindowPosition(
        {cursor_rect.left, cursor_rect.GetBottom()});
    window->RequestRepaint();
  };

  input_method_context->TextEvent()->AddHandler([&](const StringView& c) {
    committed_text += c;
    update_state();
  });

  input_method_context->CompositionEvent()->AddHandler([&](auto) {
    composition_text = input_method_context->GetCompositionText();
    update_state();
  });

  input_method_context->CompositionEndEvent()->AddHandler([&](auto) {
    composition_text = {};
    update_state();
  });

  window->SetVisibility(WindowVisibilityType::Show);

  return application->Run();
}