aboutsummaryrefslogtreecommitdiff
path: root/src/osx/gui/Window.mm
blob: ff5572f233ae698f04b4cb407cf17705babfd527 (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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
#include "cru/osx/gui/Window.hpp"

#include "cru/common/Range.hpp"
#include "cru/osx/Convert.hpp"
#include "cru/osx/graphics/quartz/Painter.hpp"
#include "cru/osx/gui/Keyboard.hpp"
#include "cru/osx/gui/UiApplication.hpp"
#include "cru/platform/gui/Base.hpp"
#include "cru/platform/gui/InputMethod.hpp"
#include "cru/platform/gui/Keyboard.hpp"
#include "cru/platform/gui/Window.hpp"

#include <AppKit/NSGraphicsContext.h>
#include <AppKit/NSTextInputContext.h>
#include <AppKit/NSWindow.h>
#include <Foundation/NSAttributedString.h>
#include <Foundation/NSString.h>

#include <limits>
#include <memory>

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

@interface WindowDelegate : NSObject <NSWindowDelegate>
- (id)init:(cru::platform::gui::osx::details::OsxWindowPrivate*)p;
@end

@interface InputClient : NSObject <NSTextInputClient>
- (id)init:(cru::platform::gui::osx::details::OsxInputMethodContextPrivate*)p;
@end

namespace cru::platform::gui::osx {
namespace details {

class OsxWindowPrivate {
  friend OsxWindow;

 public:
  explicit OsxWindowPrivate(OsxWindow* osx_window) : osx_window_(osx_window) {
    window_delegate_ = [[WindowDelegate alloc] init:this];
  }

  CRU_DELETE_COPY(OsxWindowPrivate)
  CRU_DELETE_MOVE(OsxWindowPrivate)

  ~OsxWindowPrivate() = default;

 public:
  void OnWindowWillClose();
  void OnWindowDidExpose();
  void OnWindowDidUpdate();
  void OnWindowDidResize();

 private:
  OsxWindow* osx_window_;

  INativeWindow* parent_;

  bool frame_;
  Rect content_rect_;

  NSWindow* window_;
  WindowDelegate* window_delegate_;
};

void OsxWindowPrivate::OnWindowWillClose() { osx_window_->destroy_event_.Raise(nullptr); }
void OsxWindowPrivate::OnWindowDidExpose() { [window_ update]; }
void OsxWindowPrivate::OnWindowDidUpdate() { osx_window_->paint_event_.Raise(nullptr); }
void OsxWindowPrivate::OnWindowDidResize() {
  osx_window_->resize_event_.Raise(osx_window_->GetClientSize());
}
}

namespace {
inline NSWindowStyleMask CalcWindowStyleMask(bool frame) {
  return frame ? NSWindowStyleMaskTitled | NSWindowStyleMaskClosable |
                     NSWindowStyleMaskMiniaturizable | NSWindowStyleMaskResizable
               : NSWindowStyleMaskBorderless;
}

}

OsxWindow::OsxWindow(OsxUiApplication* ui_application, INativeWindow* parent, bool frame)
    : OsxGuiResource(ui_application), p_(new details::OsxWindowPrivate(this)) {
  p_->parent_ = parent;

  p_->frame_ = frame;
  p_->content_rect_ = {100, 100, 400, 200};
}

OsxWindow::~OsxWindow() {
  if (p_->window_) {
    [p_->window_ close];
  }
}

void OsxWindow::Close() {
  if (p_->window_) {
    [p_->window_ close];
  }
}

INativeWindow* OsxWindow::GetParent() { return p_->parent_; }

bool OsxWindow::IsVisible() {
  if (!p_->window_) return false;
  return [p_->window_ isVisible];
}

void OsxWindow::SetVisible(bool is_visible) {
  if (p_->window_) {
    if (is_visible) {
      [p_->window_ orderFront:p_->window_];
    } else {
      [p_->window_ orderOut:p_->window_];
    }
  } else {
    if (is_visible) {
      CreateWindow();
    }
  }
}

Size OsxWindow::GetClientSize() { return p_->content_rect_.GetSize(); }

void OsxWindow::SetClientSize(const Size& size) {
  if (p_->window_) {
    [p_->window_ setContentSize:NSSize{size.width, size.height}];
  } else {
    p_->content_rect_.SetSize(size);
  }
}

Rect OsxWindow::GetWindowRect() {
  if (p_->window_) {
    auto r = [p_->window_ frame];
    return Rect(r.origin.x, r.origin.y, r.size.width, r.size.height);
  } else {
    NSRect rr{p_->content_rect_.left, p_->content_rect_.top, p_->content_rect_.width,
              p_->content_rect_.height};
    auto r = [NSWindow frameRectForContentRect:rr styleMask:CalcWindowStyleMask(p_->frame_)];
    return Rect(r.origin.x, r.origin.y, r.size.width, r.size.height);
  }
}

void OsxWindow::SetWindowRect(const Rect& rect) {
  auto rr = NSRect{rect.left, rect.top, rect.width, rect.height};
  if (p_->window_) {
    [p_->window_ setFrame:rr display:false];
  } else {
    auto r = [NSWindow contentRectForFrameRect:rr styleMask:CalcWindowStyleMask(p_->frame_)];
    p_->content_rect_ = Rect(r.origin.x, r.origin.y, r.size.width, r.size.height);
  }
}

std::unique_ptr<graphics::IPainter> OsxWindow::BeginPaint() {
  NSGraphicsContext* ns_graphics_context =
      [NSGraphicsContext graphicsContextWithWindow:p_->window_];

  CGContextRef cg_context = [ns_graphics_context CGContext];

  return std::make_unique<cru::platform::graphics::osx::quartz::QuartzCGContextPainter>(
      GetUiApplication()->GetGraphicsFactory(), cg_context, true, GetClientSize());
}

void OsxWindow::CreateWindow() {
  NSRect content_rect{p_->content_rect_.left, p_->content_rect_.top, p_->content_rect_.width,
                      p_->content_rect_.height};

  NSWindowStyleMask style_mask = CalcWindowStyleMask(p_->frame_);

  p_->window_ = [[NSWindow alloc] initWithContentRect:content_rect
                                            styleMask:style_mask
                                              backing:NSBackingStoreBuffered
                                                defer:false];

  [p_->window_ setDelegate:p_->window_delegate_];

  [p_->window_
      trackEventsMatchingMask:NSEventMaskAny
                      timeout:std::numeric_limits<double>::max()
                         mode:NSRunLoopCommonModes
                      handler:^(NSEvent* _Nullable event, BOOL* _Nonnull stop) {
                        KeyModifier key_modifer;
                        if (event.modifierFlags & NSEventModifierFlagControl)
                          key_modifer |= KeyModifiers::ctrl;
                        if (event.modifierFlags & NSEventModifierFlagOption)
                          key_modifer |= KeyModifiers::alt;
                        if (event.modifierFlags & NSEventModifierFlagShift)
                          key_modifer |= KeyModifiers::shift;

                        switch (event.type) {
                          case NSEventTypeMouseEntered:
                            this->mouse_enter_leave_event_.Raise(MouseEnterLeaveType::Enter);
                            break;
                          case NSEventTypeMouseExited:
                            this->mouse_enter_leave_event_.Raise(MouseEnterLeaveType::Leave);
                            break;
                          case NSEventTypeMouseMoved:
                            this->mouse_move_event_.Raise(
                                Point(event.locationInWindow.x, event.locationInWindow.y));
                            break;
                          case NSEventTypeLeftMouseDown:
                            this->mouse_down_event_.Raise(NativeMouseButtonEventArgs{
                                mouse_buttons::left,
                                Point(event.locationInWindow.x, event.locationInWindow.y),
                                key_modifer});
                            break;
                          case NSEventTypeLeftMouseUp:
                            this->mouse_up_event_.Raise(NativeMouseButtonEventArgs{
                                mouse_buttons::left,
                                Point(event.locationInWindow.x, event.locationInWindow.y),
                                key_modifer});
                            break;
                          case NSEventTypeRightMouseDown:
                            this->mouse_down_event_.Raise(NativeMouseButtonEventArgs{
                                mouse_buttons::right,
                                Point(event.locationInWindow.x, event.locationInWindow.y),
                                key_modifer});
                            break;
                          case NSEventTypeRightMouseUp:
                            this->mouse_up_event_.Raise(NativeMouseButtonEventArgs{
                                mouse_buttons::right,
                                Point(event.locationInWindow.x, event.locationInWindow.y),
                                key_modifer});
                            break;
                          case NSEventTypeScrollWheel:
                            this->mouse_wheel_event_.Raise(NativeMouseWheelEventArgs{
                                static_cast<float>(event.scrollingDeltaY),
                                Point(event.locationInWindow.x, event.locationInWindow.y),
                                key_modifer});
                            break;
                          case NSEventTypeKeyDown:
                            this->key_down_event_.Raise(NativeKeyEventArgs{
                                KeyCodeFromOsxToCru(event.keyCode), key_modifer});
                            break;
                          case NSEventTypeKeyUp:
                            this->key_up_event_.Raise(NativeKeyEventArgs{
                                KeyCodeFromOsxToCru(event.keyCode), key_modifer});
                            break;
                          default:
                            break;
                        }
                        *stop = false;
                      }];
}

namespace details {
class OsxInputMethodContextPrivate {
  friend OsxInputMethodContext;

 public:
  explicit OsxInputMethodContextPrivate(OsxInputMethodContext* input_method_context);

  CRU_DELETE_COPY(OsxInputMethodContextPrivate)
  CRU_DELETE_MOVE(OsxInputMethodContextPrivate)

  ~OsxInputMethodContextPrivate() = default;

  void SetCompositionText(CompositionText composition_text) {
    composition_text_ = std::move(composition_text);
  }

  void RaiseCompositionStartEvent();
  void RaiseCompositionEndEvent();
  void RaiseCompositionEvent();
  void RaiseTextEvent(StringView text);

  Point GetCandidateWindowPosition() const { return candidate_window_point_; }
  void SetCandidateWindowPosition(const Point& p) { candidate_window_point_ = p; }

  Range GetSelectionRange() const { return selection_range_; }
  void SetSelectionRange(Range selection_range) { selection_range_ = selection_range; }

 private:
  CompositionText composition_text_;

  Range selection_range_;

  OsxInputMethodContext* input_method_context_;

  // On Osx, this is the text lefttop point on screen.
  Point candidate_window_point_;

  Event<std::nullptr_t> composition_start_event_;
  Event<std::nullptr_t> composition_event_;
  Event<std::nullptr_t> composition_end_event_;
  Event<StringView> text_event_;
};

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 OsxInputMethodContext::EnableIME() { [[NSTextInputContext currentInputContext] deactivate]; }

void OsxInputMethodContext::DisableIME() { [[NSTextInputContext currentInputContext] activate]; }

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 WindowDelegate
cru::platform::gui::osx::details::OsxWindowPrivate* p_;

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

- (void)windowWillClose:(NSNotification*)notification {
  p_->OnWindowWillClose();
}

- (void)windowDidExpose:(NSNotification*)notification {
  p_->OnWindowDidExpose();
}

- (void)windowDidUpdate:(NSNotification*)notification {
  p_->OnWindowDidUpdate();
}

- (void)windowDidResize:(NSNotification*)notification {
  p_->OnWindowDidResize();
}
@end

@implementation InputClient
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 {
  // TODO: Call with window.
}
@end