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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
|
#include "cru/osx/gui/Window.hpp"
#include "WindowPrivate.h"
#include "CursorPrivate.h"
#include "InputMethodPrivate.h"
#include "cru/common/Logger.hpp"
#include "cru/common/Range.hpp"
#include "cru/osx/Convert.hpp"
#include "cru/osx/graphics/quartz/Convert.hpp"
#include "cru/osx/graphics/quartz/Painter.hpp"
#include "cru/osx/gui/Cursor.hpp"
#include "cru/osx/gui/InputMethod.hpp"
#include "cru/osx/gui/Keyboard.hpp"
#include "cru/osx/gui/Resource.hpp"
#include "cru/osx/gui/UiApplication.hpp"
#include "cru/platform/Check.hpp"
#include "cru/platform/gui/Base.hpp"
#include "cru/platform/gui/Cursor.hpp"
#include "cru/platform/gui/InputMethod.hpp"
#include "cru/platform/gui/Keyboard.hpp"
#include "cru/platform/gui/TimerHelper.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 <gsl/gsl_assert>
#include <limits>
#include <memory>
using cru::platform::osx::Convert;
using cru::platform::graphics::osx::quartz::Convert;
namespace cru::platform::gui::osx {
namespace {
inline NSWindowStyleMask CalcWindowStyleMask(bool frame) {
return frame ? NSWindowStyleMaskTitled | NSWindowStyleMaskClosable |
NSWindowStyleMaskMiniaturizable | NSWindowStyleMaskResizable
: NSWindowStyleMaskBorderless;
}
}
namespace details {
void OsxWindowPrivate::OnWindowWillClose() {
destroy_event_.Raise(nullptr);
window_ = nil;
CGLayerRelease(draw_layer_);
}
void OsxWindowPrivate::OnWindowDidExpose() { osx_window_->RequestRepaint(); }
void OsxWindowPrivate::OnWindowDidUpdate() {}
void OsxWindowPrivate::OnWindowDidResize() {
NSRect rect = [NSWindow contentRectForFrameRect:[window_ frame]
styleMask:CalcWindowStyleMask(frame_)];
content_rect_ = cru::platform::graphics::osx::quartz::Convert(rect);
auto view = [window_ contentView];
[view removeTrackingArea:[view trackingAreas][0]];
auto tracking_area = [[NSTrackingArea alloc]
initWithRect:CGRectMake(0, 0, content_rect_.width, content_rect_.height)
options:(NSTrackingMouseEnteredAndExited | NSTrackingMouseMoved | NSTrackingActiveAlways)
owner:view
userInfo:nil];
[view addTrackingArea:tracking_area];
CGLayerRelease(draw_layer_);
draw_layer_ = CGLayerCreateWithContext(nullptr, rect.size, nullptr);
Ensures(draw_layer_);
resize_event_.Raise(osx_window_->GetClientSize());
osx_window_->RequestRepaint();
}
void OsxWindowPrivate::OnMouseEnterLeave(MouseEnterLeaveType type) {
mouse_enter_leave_event_.Raise(type);
if (type == MouseEnterLeaveType::Enter) {
mouse_in_ = true;
UpdateCursor();
} else {
mouse_in_ = false;
}
}
void OsxWindowPrivate::OnMouseMove(Point p) { mouse_move_event_.Raise(p); }
void OsxWindowPrivate::OnMouseDown(MouseButton button, Point p, KeyModifier key_modifier) {
mouse_down_event_.Raise({button, p, key_modifier});
}
void OsxWindowPrivate::OnMouseUp(MouseButton button, Point p, KeyModifier key_modifier) {
mouse_up_event_.Raise({button, p, key_modifier});
}
void OsxWindowPrivate::OnMouseWheel(float delta, Point p, KeyModifier key_modifier) {
mouse_wheel_event_.Raise({delta, p, key_modifier});
}
void OsxWindowPrivate::OnKeyDown(KeyCode key, KeyModifier key_modifier) {
key_down_event_.Raise({key, key_modifier});
}
void OsxWindowPrivate::OnKeyUp(KeyCode key, KeyModifier key_modifier) {
key_up_event_.Raise({key, key_modifier});
}
void OsxWindowPrivate::UpdateCursor() {
auto cursor = cursor_ == nullptr
? std::dynamic_pointer_cast<OsxCursor>(
osx_window_->GetUiApplication()->GetCursorManager()->GetSystemCursor(
SystemCursorType::Arrow))
: cursor_;
[cursor->p_->ns_cursor_ set];
}
}
OsxWindow::OsxWindow(OsxUiApplication* ui_application, INativeWindow* parent, bool frame)
: OsxGuiResource(ui_application), p_(new details::OsxWindowPrivate(this)) {
p_->window_delegate_ = [[CruWindowDelegate alloc] init:p_.get()];
p_->parent_ = parent;
p_->frame_ = frame;
p_->content_rect_ = {100, 100, 400, 200};
p_->input_method_context_ = std::make_unique<OsxInputMethodContext>(this);
}
OsxWindow::~OsxWindow() {
Close();
dynamic_cast<OsxUiApplication*>(GetUiApplication())->UnregisterWindow(this);
}
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:nil];
} else {
[p_->window_ orderOut:nil];
}
} else {
if (is_visible) {
CreateWindow();
[p_->window_ orderFront:nil];
}
}
}
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);
}
}
void OsxWindow::RequestRepaint() {
if (!p_->draw_timer_) {
p_->draw_timer_ = GetUiApplication()->SetImmediate([this] {
p_->paint_event_.Raise(nullptr);
p_->draw_timer_.Release();
});
}
}
std::unique_ptr<graphics::IPainter> OsxWindow::BeginPaint() {
CGContextRef cg_context = CGLayerGetContext(p_->draw_layer_);
return std::make_unique<cru::platform::graphics::osx::quartz::QuartzCGContextPainter>(
GetUiApplication()->GetGraphicsFactory(), cg_context, false, GetClientSize(),
[this](graphics::osx::quartz::QuartzCGContextPainter*) {
log::Debug(u"Finish painting and invalidate view.");
[[p_->window_ contentView] setNeedsDisplay:YES];
});
}
void OsxWindow::CreateWindow() {
NSRect content_rect = CGRectMake(p_->content_rect_.left, p_->content_rect_.top,
p_->content_rect_.width, p_->content_rect_.height);
NSWindowStyleMask style_mask = CalcWindowStyleMask(p_->frame_);
p_->window_ = [[CruWindow alloc] init:p_.get() contentRect:content_rect style:style_mask];
Ensures(p_->window_);
[p_->window_ setDelegate:p_->window_delegate_];
NSView* content_view = [[CruView alloc] init:p_.get()
input_context_p:p_->input_method_context_->p_.get()
frame:Rect(Point{}, p_->content_rect_.GetSize())];
[p_->window_ setContentView:content_view];
p_->draw_layer_ = CGLayerCreateWithContext(
nullptr, cru::platform::graphics::osx::quartz::Convert(p_->content_rect_.GetSize()), nullptr);
Ensures(p_->draw_layer_);
RequestRepaint();
}
Point OsxWindow::GetMousePosition() {
auto p = [p_->window_ mouseLocationOutsideOfEventStream];
return Point(p.x, p.y);
}
bool OsxWindow::CaptureMouse() { return true; }
bool OsxWindow::ReleaseMouse() { return true; }
void OsxWindow::SetCursor(std::shared_ptr<ICursor> cursor) {
p_->cursor_ = CheckPlatform<OsxCursor>(cursor, GetPlatformId());
if (p_->mouse_in_) {
p_->UpdateCursor();
}
}
IEvent<std::nullptr_t>* OsxWindow::DestroyEvent() { return &p_->destroy_event_; }
IEvent<std::nullptr_t>* OsxWindow::PaintEvent() { return &p_->paint_event_; }
IEvent<Size>* OsxWindow::ResizeEvent() { return &p_->resize_event_; }
IEvent<FocusChangeType>* OsxWindow::FocusEvent() { return &p_->focus_event_; }
IEvent<MouseEnterLeaveType>* OsxWindow::MouseEnterLeaveEvent() {
return &p_->mouse_enter_leave_event_;
}
IEvent<Point>* OsxWindow::MouseMoveEvent() { return &p_->mouse_move_event_; }
IEvent<NativeMouseButtonEventArgs>* OsxWindow::MouseDownEvent() { return &p_->mouse_down_event_; }
IEvent<NativeMouseButtonEventArgs>* OsxWindow::MouseUpEvent() { return &p_->mouse_up_event_; }
IEvent<NativeMouseWheelEventArgs>* OsxWindow::MouseWheelEvent() { return &p_->mouse_wheel_event_; }
IEvent<NativeKeyEventArgs>* OsxWindow::KeyDownEvent() { return &p_->key_down_event_; }
IEvent<NativeKeyEventArgs>* OsxWindow::KeyUpEvent() { return &p_->key_up_event_; }
IInputMethodContext* OsxWindow::GetInputMethodContext() { return p_->input_method_context_.get(); }
}
@implementation CruWindow {
cru::platform::gui::osx::details::OsxWindowPrivate* _p;
}
- (instancetype)init:(cru::platform::gui::osx::details::OsxWindowPrivate*)p
contentRect:(NSRect)contentRect
style:(NSWindowStyleMask)style {
[super initWithContentRect:contentRect
styleMask:style
backing:NSBackingStoreBuffered
defer:false];
_p = p;
[self setAcceptsMouseMovedEvents:YES];
return self;
}
- (BOOL)canBecomeKeyWindow {
return YES;
}
- (void)keyDown:(NSEvent*)event {
cru::log::TagDebug(u"CruWindow", u"Recieved key down.");
cru::platform::gui::KeyModifier key_modifier;
if (event.modifierFlags & NSEventModifierFlagControl)
key_modifier |= cru::platform::gui::KeyModifiers::ctrl;
if (event.modifierFlags & NSEventModifierFlagOption)
key_modifier |= cru::platform::gui::KeyModifiers::alt;
if (event.modifierFlags & NSEventModifierFlagShift)
key_modifier |= cru::platform::gui::KeyModifiers::shift;
auto c = cru::platform::gui::osx::KeyCodeFromOsxToCru(event.keyCode);
_p->OnKeyDown(c, key_modifier);
[super keyDown:event];
}
- (void)keyUp:(NSEvent*)event {
cru::log::TagDebug(u"CruWindow", u"Recieved key up.");
cru::platform::gui::KeyModifier key_modifier;
if (event.modifierFlags & NSEventModifierFlagControl)
key_modifier |= cru::platform::gui::KeyModifiers::ctrl;
if (event.modifierFlags & NSEventModifierFlagOption)
key_modifier |= cru::platform::gui::KeyModifiers::alt;
if (event.modifierFlags & NSEventModifierFlagShift)
key_modifier |= cru::platform::gui::KeyModifiers::shift;
auto c = cru::platform::gui::osx::KeyCodeFromOsxToCru(event.keyCode);
_p->OnKeyUp(c, key_modifier);
[super keyUp:event];
}
@end
@implementation CruView {
cru::platform::gui::osx::details::OsxWindowPrivate* _p;
cru::platform::gui::osx::details::OsxInputMethodContextPrivate* _input_context_p;
NSMutableAttributedString* _input_context_text;
}
- (instancetype)init:(cru::platform::gui::osx::details::OsxWindowPrivate*)p
input_context_p:
(cru::platform::gui::osx::details::OsxInputMethodContextPrivate*)input_context_p
frame:(cru::platform::Rect)frame {
[super initWithFrame:cru::platform::graphics::osx::quartz::Convert(frame)];
_p = p;
_input_context_p = input_context_p;
auto tracking_area = [[NSTrackingArea alloc]
initWithRect:Convert(frame)
options:(NSTrackingMouseEnteredAndExited | NSTrackingMouseMoved | NSTrackingActiveAlways)
owner:self
userInfo:nil];
[self addTrackingArea:tracking_area];
return self;
}
- (void)drawRect:(NSRect)dirtyRect {
cru::log::TagDebug(u"CruView", u"Begin to draw layer in view.");
auto cg_context = [[NSGraphicsContext currentContext] CGContext];
auto layer = _p->GetDrawLayer();
Ensures(layer);
CGContextDrawLayerAtPoint(cg_context, CGPointMake(0, 0), layer);
}
- (BOOL)acceptsFirstResponder {
return YES;
}
- (BOOL)canBecomeKeyView {
return YES;
}
- (void)mouseMoved:(NSEvent*)event {
// cru::log::TagDebug(u"CruView", u"Recieved mouse move.");
_p->OnMouseMove(cru::platform::Point(event.locationInWindow.x, event.locationInWindow.y));
}
- (void)mouseEntered:(NSEvent*)event {
// cru::log::TagDebug(u"CruView", u"Recieved mouse enter.");
_p->OnMouseEnterLeave(cru::platform::gui::MouseEnterLeaveType::Enter);
}
- (void)mouseExited:(NSEvent*)event {
// cru::log::TagDebug(u"CruView", u"Recieved mouse exit.");
_p->OnMouseEnterLeave(cru::platform::gui::MouseEnterLeaveType::Leave);
}
- (void)mouseDown:(NSEvent*)event {
// cru::log::TagDebug(u"CruView", u"Recieved mouse down.");
[[self window] makeKeyWindow];
cru::platform::gui::KeyModifier key_modifier;
if (event.modifierFlags & NSEventModifierFlagControl)
key_modifier |= cru::platform::gui::KeyModifiers::ctrl;
if (event.modifierFlags & NSEventModifierFlagOption)
key_modifier |= cru::platform::gui::KeyModifiers::alt;
if (event.modifierFlags & NSEventModifierFlagShift)
key_modifier |= cru::platform::gui::KeyModifiers::shift;
cru::platform::Point p(event.locationInWindow.x, event.locationInWindow.y);
_p->OnMouseDown(cru::platform::gui::mouse_buttons::left, p, key_modifier);
}
- (void)mouseUp:(NSEvent*)event {
// cru::log::TagDebug(u"CruView", u"Recieved mouse up.");
cru::platform::gui::KeyModifier key_modifier;
if (event.modifierFlags & NSEventModifierFlagControl)
key_modifier |= cru::platform::gui::KeyModifiers::ctrl;
if (event.modifierFlags & NSEventModifierFlagOption)
key_modifier |= cru::platform::gui::KeyModifiers::alt;
if (event.modifierFlags & NSEventModifierFlagShift)
key_modifier |= cru::platform::gui::KeyModifiers::shift;
cru::platform::Point p(event.locationInWindow.x, event.locationInWindow.y);
_p->OnMouseUp(cru::platform::gui::mouse_buttons::left, p, key_modifier);
}
- (void)rightMouseDown:(NSEvent*)event {
// cru::log::TagDebug(u"CruView", u"Recieved right mouse down.");
cru::platform::gui::KeyModifier key_modifier;
if (event.modifierFlags & NSEventModifierFlagControl)
key_modifier |= cru::platform::gui::KeyModifiers::ctrl;
if (event.modifierFlags & NSEventModifierFlagOption)
key_modifier |= cru::platform::gui::KeyModifiers::alt;
if (event.modifierFlags & NSEventModifierFlagShift)
key_modifier |= cru::platform::gui::KeyModifiers::shift;
cru::platform::Point p(event.locationInWindow.x, event.locationInWindow.y);
_p->OnMouseDown(cru::platform::gui::mouse_buttons::right, p, key_modifier);
}
- (void)rightMouseUp:(NSEvent*)event {
// cru::log::TagDebug(u"CruView", u"Recieved right mouse up.");
cru::platform::gui::KeyModifier key_modifier;
if (event.modifierFlags & NSEventModifierFlagControl)
key_modifier |= cru::platform::gui::KeyModifiers::ctrl;
if (event.modifierFlags & NSEventModifierFlagOption)
key_modifier |= cru::platform::gui::KeyModifiers::alt;
if (event.modifierFlags & NSEventModifierFlagShift)
key_modifier |= cru::platform::gui::KeyModifiers::shift;
cru::platform::Point p(event.locationInWindow.x, event.locationInWindow.y);
_p->OnMouseUp(cru::platform::gui::mouse_buttons::right, p, key_modifier);
}
- (void)scrollWheel:(NSEvent*)event {
// cru::log::TagDebug(u"CruView", u"Recieved mouse wheel.");
cru::platform::gui::KeyModifier key_modifier;
if (event.modifierFlags & NSEventModifierFlagControl)
key_modifier |= cru::platform::gui::KeyModifiers::ctrl;
if (event.modifierFlags & NSEventModifierFlagOption)
key_modifier |= cru::platform::gui::KeyModifiers::alt;
if (event.modifierFlags & NSEventModifierFlagShift)
key_modifier |= cru::platform::gui::KeyModifiers::shift;
cru::platform::Point p(event.locationInWindow.x, event.locationInWindow.y);
_p->OnMouseWheel(static_cast<float>(event.scrollingDeltaY), p, key_modifier);
}
- (BOOL)hasMarkedText {
return _input_context_text != nil;
}
- (NSRange)markedRange {
return _input_context_text == nil ? NSRange{NSNotFound, 0}
: NSRange{0, [_input_context_text length]};
}
- (NSRange)selectedRange {
return NSMakeRange(_input_context_p->GetSelectionRange().position,
_input_context_p->GetSelectionRange().count);
}
- (void)setMarkedText:(id)string
selectedRange:(NSRange)selectedRange
replacementRange:(NSRange)replacementRange {
if (_input_context_text == nil) {
_input_context_text = [[NSMutableAttributedString alloc] init];
_input_context_p->RaiseCompositionStartEvent();
}
[_input_context_text deleteCharactersInRange:replacementRange];
[_input_context_text
insertAttributedString:[[NSAttributedString alloc] initWithString:(NSString*)string]
atIndex:replacementRange.location];
cru::platform::gui::CompositionText composition_text;
composition_text.text = Convert((CFStringRef)[_input_context_text string]);
composition_text.selection.position = replacementRange.location + selectedRange.location;
composition_text.selection.count = selectedRange.length;
_input_context_p->SetCompositionText(composition_text);
_input_context_p->RaiseCompositionEvent();
}
- (void)unmarkText {
_input_context_text = nil;
_input_context_p->RaiseCompositionEndEvent();
}
- (NSArray<NSAttributedStringKey>*)validAttributesForMarkedText {
return @[
(NSString*)kCTUnderlineColorAttributeName, (NSString*)kCTUnderlineStyleAttributeName,
(NSString*)kCTForegroundColorAttributeName, (NSString*)kCTBackgroundColorAttributeName
];
}
- (NSAttributedString*)attributedSubstringForProposedRange:(NSRange)range
actualRange:(NSRangePointer)actualRange {
return [_input_context_text attributedSubstringFromRange:range];
}
- (void)insertText:(id)string replacementRange:(NSRange)replacementRange {
_input_context_text = nil;
cru::String s = Convert((CFStringRef)string);
_input_context_p->RaiseCompositionEndEvent();
_input_context_p->RaiseTextEvent(s);
}
- (NSUInteger)characterIndexForPoint:(NSPoint)point {
return NSNotFound;
}
- (NSRect)firstRectForCharacterRange:(NSRange)range actualRange:(NSRangePointer)actualRange {
NSRect result;
result.origin.x = _input_context_p->GetCandidateWindowPosition().x;
result.origin.y = _input_context_p->GetCandidateWindowPosition().y;
result.size.height = 16;
result.size.width = 0;
return result;
}
- (void)doCommandBySelector:(SEL)selector {
_input_context_p->PerformSel(selector);
}
@end
@implementation CruWindowDelegate {
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
|