aboutsummaryrefslogtreecommitdiff
path: root/include/cru/platform/gui/InputMethod.h
blob: b4a1e9d9ee3a58be40d5655392b1ea3311d75b33 (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
#pragma once
#include "Base.h"

#include "cru/base/Event.h"

#include <memory>
#include <vector>

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

using CompositionClauses = std::vector<CompositionClause>;

struct CompositionText {
  String text;
  CompositionClauses clauses;
  TextRange selection;
};

/**
 * \remarks I think it's time to standatdize this. The most important thing is
 * the events.
 *
 * The events hould be triggered in this way.
 * 1. Any time the IME begins to work, CompositionStartEvent is fired. Only
 * once. Not triggerred again until CompositionEndEvent is fired.
 * 2. Any time composition state changed, maybe user typed more characters, or
 * user commit part of composition, CompositionEvent is fired.
 * 3. TextEvent is fired when user commit part or whole of the composition. And
 * you can use the args to get what characters are committed. So it is where you
 * get the real text user want to give you.
 * 4. Whenever a commit happens, TextEvent first, followed by CompositionEvent.
 * Each for once. So use the TextEvent to get real input and use
 * CompositionEvent to update UI.
 * 5. When composition stops, a final CompositionEndEvent is fired. Also only
 * once.
 */
struct IInputMethodContext : virtual IPlatformResource {
  // 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 left-top. Relative to window left-top. Use this
  // method to prepare typing.
  virtual void SetCandidateWindowPosition(const Point& point) = 0;

  virtual IEvent<std::nullptr_t>* CompositionStartEvent() = 0;
  virtual IEvent<std::nullptr_t>* CompositionEndEvent() = 0;
  virtual IEvent<std::nullptr_t>* CompositionEvent() = 0;
  virtual IEvent<StringView>* TextEvent() = 0;
};
}  // namespace cru::platform::gui