aboutsummaryrefslogtreecommitdiff
path: root/src/osx/gui/InputMethod.mm
blob: 2331d8b749acf94555f9dd8655c7c82ee97ab145 (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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include "cru/osx/gui/InputMethod.hpp"

#import <AppKit/AppKit.h>
#include "InputMethodPrivate.h"
#include "WindowPrivate.h"
#include "cru/osx/Convert.hpp"
#include "cru/osx/gui/Window.hpp"

using cru::platform::osx::Convert;

namespace cru::platform::gui::osx {
namespace details {
OsxInputMethodContextPrivate::OsxInputMethodContextPrivate(
    OsxInputMethodContext* input_method_context, OsxWindow* window) {
  input_method_context_ = input_method_context;
  window_ = window;
}

OsxInputMethodContextPrivate::~OsxInputMethodContextPrivate() {}

void OsxInputMethodContextPrivate::RaiseCompositionStartEvent() {
  composition_start_event_.Raise(nullptr);
}
void OsxInputMethodContextPrivate::RaiseCompositionEndEvent() {
  composition_end_event_.Raise(nullptr);
}
void OsxInputMethodContextPrivate::RaiseCompositionEvent() { composition_event_.Raise(nullptr); }

void OsxInputMethodContextPrivate::RaiseTextEvent(StringView text) { text_event_.Raise(text); }

void OsxInputMethodContextPrivate::PerformSel(SEL sel) {
  [window_->p_->GetNSWindow() performSelector:sel];
}

void OsxInputMethodContextPrivate::Activate() {
  if (!window_->p_->GetNSWindow()) return;
  auto input_context = [[window_->p_->GetNSWindow() contentView] inputContext];
  Ensures(input_context);
  [input_context activate];
}

void OsxInputMethodContextPrivate::Deactivate() {
  if (!window_->p_->GetNSWindow()) return;
  auto input_context = [[window_->p_->GetNSWindow() contentView] inputContext];
  Ensures(input_context);
  [input_context deactivate];
}
}

OsxInputMethodContext::OsxInputMethodContext(OsxWindow* window)
    : OsxGuiResource(window->GetUiApplication()) {
  p_ = std::make_unique<details::OsxInputMethodContextPrivate>(this, window);
}

OsxInputMethodContext::~OsxInputMethodContext() {}

void OsxInputMethodContext::EnableIME() { p_->Activate(); }

void OsxInputMethodContext::DisableIME() { p_->Deactivate(); }

bool OsxInputMethodContext::ShouldManuallyDrawCompositionText() { return true; }

void OsxInputMethodContext::CompleteComposition() {
  // TODO: Implement this.
}

void OsxInputMethodContext::CancelComposition() {
  [[NSTextInputContext currentInputContext] discardMarkedText];
}

CompositionText OsxInputMethodContext::GetCompositionText() { return p_->composition_text_; }

void OsxInputMethodContext::SetCandidateWindowPosition(const Point& point) {
  p_->SetCandidateWindowPosition(point);
}

IEvent<std::nullptr_t>* OsxInputMethodContext::CompositionStartEvent() {
  return &p_->composition_start_event_;
}

IEvent<std::nullptr_t>* OsxInputMethodContext::CompositionEndEvent() {
  return &p_->composition_end_event_;
}

IEvent<std::nullptr_t>* OsxInputMethodContext::CompositionEvent() {
  return &p_->composition_event_;
}

IEvent<StringView>* OsxInputMethodContext::TextEvent() { return &p_->text_event_; }
}

@implementation CruInputClient {
  cru::platform::gui::osx::details::OsxInputMethodContextPrivate* _p;
  NSMutableAttributedString* _text;
}

- (id)init:(cru::platform::gui::osx::details::OsxInputMethodContextPrivate*)p {
  _p = p;
  return self;
}

- (BOOL)hasMarkedText {
  return _text != nil;
}

- (NSRange)markedRange {
  return _text == nil ? NSRange{NSNotFound, 0} : NSRange{0, [_text length]};
}

- (NSRange)selectedRange {
  return NSMakeRange(_p->GetSelectionRange().position, _p->GetSelectionRange().count);
}

- (void)setMarkedText:(id)string
        selectedRange:(NSRange)selectedRange
     replacementRange:(NSRange)replacementRange {
  if (_text == nil) {
    _text = [[NSMutableAttributedString alloc] init];
    _p->RaiseCompositionStartEvent();
  }

  [_text deleteCharactersInRange:replacementRange];
  [_text insertAttributedString:[[NSAttributedString alloc] initWithString:(NSString*)string]
                        atIndex:replacementRange.location];

  cru::platform::gui::CompositionText composition_text;
  composition_text.text = Convert((CFStringRef)[_text string]);
  composition_text.selection.position = replacementRange.location + selectedRange.location;
  composition_text.selection.count = selectedRange.length;
  _p->SetCompositionText(composition_text);
  _p->RaiseCompositionEvent();
}

- (void)unmarkText {
  _text = nil;
  _p->RaiseCompositionEndEvent();
}

- (NSArray<NSAttributedStringKey>*)validAttributesForMarkedText {
  return @[
    (NSString*)kCTUnderlineColorAttributeName, (NSString*)kCTUnderlineStyleAttributeName,
    (NSString*)kCTForegroundColorAttributeName, (NSString*)kCTBackgroundColorAttributeName
  ];
}

- (NSAttributedString*)attributedSubstringForProposedRange:(NSRange)range
                                               actualRange:(NSRangePointer)actualRange {
  return [_text attributedSubstringFromRange:range];
}

- (void)insertText:(id)string replacementRange:(NSRange)replacementRange {
  _text = nil;
  cru::String s = Convert((CFStringRef)string);
  _p->RaiseCompositionEndEvent();
  _p->RaiseTextEvent(s);
}

- (NSUInteger)characterIndexForPoint:(NSPoint)point {
  return NSNotFound;
}

- (NSRect)firstRectForCharacterRange:(NSRange)range actualRange:(NSRangePointer)actualRange {
  NSRect result;
  result.origin.x = _p->GetCandidateWindowPosition().x;
  result.origin.y = _p->GetCandidateWindowPosition().y;
  result.size.height = 16;
  result.size.width = 0;
  return result;
}

- (void)doCommandBySelector:(SEL)selector {
  _p->PerformSel(selector);
}
@end