aboutsummaryrefslogtreecommitdiff
path: root/include/cru/platform/native/InputMethod.hpp
blob: c975825f869370c17c80de15ad4804587e4f2e21 (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
#pragma once
#include "../Resource.hpp"
#include "Base.hpp"

#include "cru/common/Event.hpp"

#include <iostream>
#include <memory>
#include <vector>

namespace cru::platform::native {
struct CompositionClause {
  int start;
  int end;
  bool target;
};

using CompositionClauses = std::vector<CompositionClause>;

struct CompositionText {
  std::u16string text;
  CompositionClauses clauses;
  TextRange selection;
};

// inline std::basic_ostream<char16_t>& operator<<(
//     std::basic_ostream<char16_t>& stream,
//     const CompositionText& composition_text) {
//   stream << u"text: " << composition_text.text << u"\n" << u"clauses:\n";
//   for (int i = 0; i < static_cast<int>(composition_text.clauses.size()); i++) {
//     const auto& clause = composition_text.clauses[i];
//     stream << u"\t" << i << u". start:" << clause.start << u" end:"
//            << clause.end;
//     if (clause.target) {
//       stream << u" target";
//     }
//     stream << u"\n";
//   }
//   stream << u"selection: position:" << composition_text.selection.position
//          << u" count:" << composition_text.selection.count;
//   return stream;
// }

struct IInputMethodContext : virtual INativeResource {
  // Return true if you should draw composition text manually. Return false if
  // system will take care of that for you.
  virtual bool ShouldManuallyDrawCompositionText() = 0;

  virtual void EnableIME() = 0;

  virtual void DisableIME() = 0;

  virtual void CompleteComposition() = 0;

  virtual void CancelComposition() = 0;

  virtual CompositionText GetCompositionText() = 0;

  // Set the candidate window lefttop. Use this method to prepare typing.
  virtual void SetCandidateWindowPosition(const Point& point) = 0;

  // Triggered when user starts composition.
  virtual IEvent<std::nullptr_t>* CompositionStartEvent() = 0;

  // Triggered when user stops composition.
  virtual IEvent<std::nullptr_t>* CompositionEndEvent() = 0;

  // Triggered every time composition text changes.
  virtual IEvent<std::nullptr_t>* CompositionEvent() = 0;

  virtual IEvent<std::u16string_view>* TextEvent() = 0;
};

struct IInputMethodManager : virtual INativeResource {
  virtual std::unique_ptr<IInputMethodContext> GetContext(
      INativeWindow* window) = 0;
};
}  // namespace cru::platform::native