aboutsummaryrefslogtreecommitdiff
path: root/include/cru/platform/native/InputMethod.hpp
blob: 1ede15b2dc0b70bed1f212b6346bf75f3e95e69d (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
#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::string text;
  CompositionClauses clauses;
  TextRange selection;
};

inline std::ostream& operator<<(std::ostream& stream,
                                const CompositionText& composition_text) {
  stream << "text: " << composition_text.text << "\n"
         << "clauses:\n";
  for (int i = 0; i < static_cast<int>(composition_text.clauses.size()); i++) {
    const auto& clause = composition_text.clauses[i];
    stream << "\t" << i << ". start:" << clause.start << " end:" << clause.end;
    if (clause.target) {
      stream << " target";
    }
    stream << "\n";
  }
  stream << "selection: position:" << composition_text.selection.position
         << " 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::string_view>* TextEvent() = 0;
};

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