forked from microsoft/react-native-macos
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRCTUITextView.mm
More file actions
701 lines (591 loc) · 21.5 KB
/
RCTUITextView.mm
File metadata and controls
701 lines (591 loc) · 21.5 KB
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
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#import <React/RCTUITextView.h>
#import <React/RCTUtils.h>
#import <React/UIView+React.h>
#import <React/RCTBackedTextInputDelegateAdapter.h>
#import <React/RCTTextAttributes.h>
@implementation RCTUITextView {
#if !TARGET_OS_OSX // [macOS]
UILabel *_placeholderView;
UITextView *_detachedTextView;
#endif // [macOS]
RCTBackedTextViewDelegateAdapter *_textInputDelegateAdapter;
NSDictionary<NSAttributedStringKey, id> *_defaultTextAttributes;
#if !TARGET_OS_OSX // [macOS]
NSArray<UIBarButtonItemGroup *> *_initialValueLeadingBarButtonGroups;
NSArray<UIBarButtonItemGroup *> *_initialValueTrailingBarButtonGroups;
#endif // [macOS]
NSArray<NSString *> *_acceptDragAndDropTypes;
#if TARGET_OS_OSX // [macOS
NSArray<NSPasteboardType> *_readablePasteboardTypes;
#endif // macOS]
}
#if !TARGET_OS_OSX // [macOS]
static UIFont *defaultPlaceholderFont(void)
{
return [UIFont preferredFontForTextStyle:UIFontTextStyleBody];
}
#else // [macOS
static NSFont *defaultPlaceholderFont(void)
{
return [NSFont systemFontOfSize:[NSFont systemFontSize]];
}
#endif // macOS]
static RCTUIColor *defaultPlaceholderColor(void) // [macOS]
{
// Default placeholder color from UITextField.
return [RCTUIColor placeholderTextColor]; // [macOS]
}
- (instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(textDidChange)
name:UITextViewTextDidChangeNotification
object:self];
#if !TARGET_OS_OSX // [macOS]
_placeholderView = [[UILabel alloc] initWithFrame:self.bounds];
_placeholderView.isAccessibilityElement = NO;
_placeholderView.numberOfLines = 0;
[self addSubview:_placeholderView];
#else // [macOS
// Fix blurry text on non-retina displays.
self.canDrawSubviewsIntoLayer = YES;
self.allowsUndo = YES;
#endif // macOS]
_textInputDelegateAdapter = [[RCTBackedTextViewDelegateAdapter alloc] initWithTextView:self];
self.backgroundColor = [RCTUIColor clearColor]; // [macOS]
self.textColor = [RCTUIColor labelColor]; // [macOS]
// This line actually removes 5pt (default value) left and right padding in UITextView.
#if !TARGET_OS_OSX // [macOS]
self.textContainer.lineFragmentPadding = 0;
#else // [macOS
// macOS has a bug where setting this to 0 will cause the scroll view to scroll to top when
// inserting a newline at the bottom of a NSTextView when it has more rows than can be displayed
// on screen.
self.textContainer.lineFragmentPadding = 1;
#endif //macOS]
#if !TARGET_OS_OSX // [macOS]
self.scrollsToTop = NO;
#endif // [macOS]
self.scrollEnabled = YES;
#if !TARGET_OS_OSX // [macOS]
_initialValueLeadingBarButtonGroups = nil;
_initialValueTrailingBarButtonGroups = nil;
#endif // [macOS]
}
return self;
}
- (void)setDelegate:(id<UITextViewDelegate>)delegate
{
// Delegate is set inside `[RCTBackedTextViewDelegateAdapter initWithTextView]` and
// it cannot be changed from outside.
if (super.delegate) {
return;
}
[super setDelegate:delegate];
}
#pragma mark - Accessibility
- (void)setIsAccessibilityElement:(BOOL)isAccessibilityElement
{
// UITextView is accessible by default (some nested views are) and disabling that is not supported.
// On iOS accessible elements cannot be nested, therefore enabling accessibility for some container view
// (even in a case where this view is a part of public API of TextInput on iOS) shadows some features implemented
// inside the component.
}
- (NSString *)accessibilityLabel
{
NSMutableString *accessibilityLabel = [NSMutableString new];
NSString *superAccessibilityLabel = [super accessibilityLabel];
if (superAccessibilityLabel.length > 0) {
[accessibilityLabel appendString:superAccessibilityLabel];
}
if (self.placeholder.length > 0 && self.attributedText.string.length == 0) {
if (accessibilityLabel.length > 0) {
[accessibilityLabel appendString:@" "];
}
[accessibilityLabel appendString:self.placeholder];
}
return accessibilityLabel;
}
#pragma mark - Properties
- (void)setAcceptDragAndDropTypes:(NSArray<NSString *> *)acceptDragAndDropTypes
{
_acceptDragAndDropTypes = acceptDragAndDropTypes;
}
- (nullable NSArray<NSString *> *)acceptDragAndDropTypes
{
return _acceptDragAndDropTypes;
}
- (void)setPlaceholder:(NSString *)placeholder
{
_placeholder = placeholder;
[self _updatePlaceholder];
}
- (void)setPlaceholderColor:(RCTUIColor *)placeholderColor // [macOS]
{
_placeholderColor = placeholderColor;
[self _updatePlaceholder];
}
#if TARGET_OS_OSX // [macOS
- (void)toggleAutomaticSpellingCorrection:(id)sender
{
self.automaticSpellingCorrectionEnabled = !self.isAutomaticSpellingCorrectionEnabled;
[_textInputDelegate automaticSpellingCorrectionDidChange:self.isAutomaticSpellingCorrectionEnabled];
}
- (void)toggleContinuousSpellChecking:(id)sender
{
self.continuousSpellCheckingEnabled = !self.isContinuousSpellCheckingEnabled;
[_textInputDelegate continuousSpellCheckingDidChange:self.isContinuousSpellCheckingEnabled];
}
- (void)toggleGrammarChecking:(id)sender
{
self.grammarCheckingEnabled = !self.isGrammarCheckingEnabled;
[_textInputDelegate grammarCheckingDidChange:self.isGrammarCheckingEnabled];
}
- (void)setSelectionColor:(RCTUIColor *)selectionColor
{
NSMutableDictionary *selectTextAttributes = self.selectedTextAttributes.mutableCopy;
selectTextAttributes[NSBackgroundColorAttributeName] = selectionColor ?: [NSColor selectedControlColor];
self.selectedTextAttributes = selectTextAttributes.copy;
}
- (RCTUIColor*)selectionColor
{
return (RCTUIColor*)self.selectedTextAttributes[NSBackgroundColorAttributeName];
}
- (void)setCursorColor:(NSColor *)cursorColor
{
_cursorColor = cursorColor;
self.insertionPointColor = cursorColor;
}
- (void)setEnabledTextCheckingTypes:(NSTextCheckingTypes)checkingType
{
[super setEnabledTextCheckingTypes:checkingType];
self.automaticDataDetectionEnabled = checkingType != 0;
}
- (NSTextAlignment)textAlignment
{
return self.alignment;
}
- (NSString*)text
{
return self.string;
}
- (void)setText:(NSString *)text
{
self.string = text;
}
- (void)setReadablePasteBoardTypes:(NSArray<NSPasteboardType> *)readablePasteboardTypes
{
_readablePasteboardTypes = readablePasteboardTypes;
}
- (void)setTypingAttributes:(__unused NSDictionary *)typingAttributes
{
// Prevent NSTextView from changing its own typing attributes out from under us.
[super setTypingAttributes:_defaultTextAttributes];
}
- (NSAttributedString*)attributedText
{
return self.textStorage;
}
- (BOOL)becomeFirstResponder
{
BOOL success = [super becomeFirstResponder];
if (success) {
id<RCTBackedTextInputDelegate> textInputDelegate = [self textInputDelegate];
if ([textInputDelegate respondsToSelector:@selector(textInputDidBeginEditing)]) {
[textInputDelegate textInputDidBeginEditing];
}
}
return success;
}
- (BOOL)resignFirstResponder
{
if (self.selectable) {
self.selectedRange = NSMakeRange(NSNotFound, 0);
}
BOOL success = [super resignFirstResponder];
if (success) {
// Break undo coalescing when losing focus.
[self breakUndoCoalescing];
}
return success;
}
#endif // macOS]
- (void)setDefaultTextAttributes:(NSDictionary<NSAttributedStringKey, id> *)defaultTextAttributes
{
if ([_defaultTextAttributes isEqualToDictionary:defaultTextAttributes]) {
return;
}
_defaultTextAttributes = defaultTextAttributes;
self.typingAttributes = defaultTextAttributes;
[self _updatePlaceholder];
}
- (NSDictionary<NSAttributedStringKey, id> *)defaultTextAttributes
{
return _defaultTextAttributes;
}
- (void)textDidChange
{
_textWasPasted = NO;
[self _invalidatePlaceholderVisibility];
}
- (void)setDisableKeyboardShortcuts:(BOOL)disableKeyboardShortcuts
{
#if TARGET_OS_IOS
// Initialize the initial values only once
if (_initialValueLeadingBarButtonGroups == nil) {
// Capture initial values of leading and trailing button groups
_initialValueLeadingBarButtonGroups = self.inputAssistantItem.leadingBarButtonGroups;
_initialValueTrailingBarButtonGroups = self.inputAssistantItem.trailingBarButtonGroups;
}
if (disableKeyboardShortcuts) {
self.inputAssistantItem.leadingBarButtonGroups = @[];
self.inputAssistantItem.trailingBarButtonGroups = @[];
} else {
// Restore the initial values
self.inputAssistantItem.leadingBarButtonGroups = _initialValueLeadingBarButtonGroups;
self.inputAssistantItem.trailingBarButtonGroups = _initialValueTrailingBarButtonGroups;
}
_disableKeyboardShortcuts = disableKeyboardShortcuts;
#endif
}
#pragma mark - Overrides
- (void)setFont:(UIFont *)font
{
[super setFont:font];
[self _updatePlaceholder];
}
- (void)setTextAlignment:(NSTextAlignment)textAlignment
{
#if !TARGET_OS_OSX // [macOS]
[super setTextAlignment:textAlignment];
_placeholderView.textAlignment = textAlignment;
#else // [macOS
self.alignment = textAlignment;
[self setNeedsDisplay:YES];
#endif // macOS]
}
- (void)setAttributedText:(NSAttributedString *)attributedText
{
#if !TARGET_OS_OSX // [macOS]
[super setAttributedText:attributedText];
#else // [macOS
// Break undo coalescing when the text is changed by JS (e.g. autocomplete).
[self breakUndoCoalescing];
// Avoid Exception thrown while executing UI block: *** -[NSBigMutableString replaceCharactersInRange:withString:]: nil argument
[self.textStorage setAttributedString:attributedText ?: [NSAttributedString new]];
#endif // macOS]
[self textDidChange];
}
#pragma mark - Overrides
#if !TARGET_OS_OSX // [macOS]
- (void)setSelectedTextRange:(UITextRange *)selectedTextRange notifyDelegate:(BOOL)notifyDelegate
#else // [macOS
- (void)setSelectedTextRange:(NSRange)selectedTextRange notifyDelegate:(BOOL)notifyDelegate
#endif // macOS]
{
if (!notifyDelegate) {
// We have to notify an adapter that following selection change was initiated programmatically,
// so the adapter must not generate a notification for it.
[_textInputDelegateAdapter skipNextTextInputDidChangeSelectionEventWithTextRange:selectedTextRange];
}
#if !TARGET_OS_OSX // [macOS]
[super setSelectedTextRange:selectedTextRange];
#else // [macOS
[super setSelectedRange:selectedTextRange];
#endif // macOS]
}
// After restoring the previous cursor position, we manually trigger the scroll to the new cursor position (PR 38679).
- (void)scrollRangeToVisible:(NSRange)range
{
[super scrollRangeToVisible:range];
}
#if TARGET_OS_OSX // [macOS
- (NSRange)selectedTextRange
{
return [super selectedRange];
}
- (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)draggingInfo
{
NSDragOperation dragOperation = [self.textInputDelegate textInputDraggingEntered:draggingInfo];
NSDragOperation superOperation = [super draggingEntered:draggingInfo];
// The delegate's operation should take precedence.
return dragOperation != NSDragOperationNone ? dragOperation : superOperation;
}
- (void)draggingExited:(id<NSDraggingInfo>)draggingInfo
{
[self.textInputDelegate textInputDraggingExited:draggingInfo];
[super draggingExited:draggingInfo];
}
- (BOOL)performDragOperation:(id<NSDraggingInfo>)draggingInfo
{
if ([self.textInputDelegate textInputShouldHandleDragOperation:draggingInfo]) {
return [super performDragOperation:draggingInfo];
}
return YES;
}
- (NSArray *)readablePasteboardTypes
{
return _readablePasteboardTypes ? _readablePasteboardTypes : [super readablePasteboardTypes];
}
// Remove the default touchbar that comes with NSTextView, since the actions that come with the
// default touchbar are currently not supported by RCTUITextView
- (NSTouchBar *)makeTouchBar
{
return nil;
}
#endif // macOS]
- (void)paste:(id)sender
{
#if TARGET_OS_OSX // [macOS
if ([self.textInputDelegate textInputShouldHandlePaste:self])
{
#endif // macOS]
_textWasPasted = YES;
[super paste:sender];
#if TARGET_OS_OSX // [macOS
}
#endif // macOS]
}
// Turn off scroll animation to fix flaky scrolling.
// This is only necessary for iOS < 14.
#if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && __IPHONE_OS_VERSION_MAX_ALLOWED < 140000
- (void)setContentOffset:(CGPoint)contentOffset animated:(__unused BOOL)animated
{
[super setContentOffset:contentOffset animated:NO];
}
#endif
#if TARGET_OS_OSX // [macOS
#pragma mark - Placeholder
- (NSAttributedString*)placeholderTextAttributedString
{
if (self.placeholder == nil) {
return nil;
}
return [[NSAttributedString alloc] initWithString:self.placeholder attributes:[self _placeholderTextAttributes]];
}
- (void)drawRect:(NSRect)dirtyRect
{
[super drawRect:dirtyRect];
if (self.text.length == 0 && self.placeholder) {
NSAttributedString *attributedPlaceholderString = self.placeholderTextAttributedString;
if (attributedPlaceholderString) {
NSTextStorage *textStorage = [[NSTextStorage alloc] initWithAttributedString:attributedPlaceholderString];
NSTextContainer *textContainer = [[NSTextContainer alloc] initWithContainerSize:self.textContainer.containerSize];
NSLayoutManager *layoutManager = [NSLayoutManager new];
textContainer.lineFragmentPadding = self.textContainer.lineFragmentPadding;
[layoutManager addTextContainer:textContainer];
[textStorage addLayoutManager:layoutManager];
NSRange glyphRange = [layoutManager glyphRangeForTextContainer:textContainer];
[layoutManager drawGlyphsForGlyphRange:glyphRange atPoint:self.textContainerOrigin];
}
}
}
#pragma mark - Text Insets
- (void)setTextContainerInsets:(UIEdgeInsets)textContainerInsets
{
// NSTextView has a NSSize textContainerInset property
// UITextview has a UIEdgeInsets textContainerInset property
// RCTUITextView mac only has a UIEdgeInsets textContainerInsets property
// UI/NSTextField do NOT have textContainerInset properties
_textContainerInsets = textContainerInsets;
super.textContainerInset = NSMakeSize(MIN(textContainerInsets.left, textContainerInsets.right), MIN(textContainerInsets.top, textContainerInsets.bottom));
if (self.shouldDrawInsertionPoint) {
[self updateInsertionPointStateAndRestartTimer:NO];
}
}
#endif // macOS]
- (void)selectAll:(id)sender
{
[super selectAll:sender];
#if !TARGET_OS_OSX // [macOS]
// `selectAll:` does not work for UITextView when it's being called inside UITextView's delegate methods.
dispatch_async(dispatch_get_main_queue(), ^{
UITextRange *selectionRange = [self textRangeFromPosition:self.beginningOfDocument toPosition:self.endOfDocument];
[self setSelectedTextRange:selectionRange notifyDelegate:NO];
});
#endif // [macOS]
}
#pragma mark - Layout
- (CGFloat)preferredMaxLayoutWidth
{
// Returning size DOES contain `textContainerInset` (aka `padding`).
return _preferredMaxLayoutWidth ?: self.placeholderSize.width;
}
- (CGSize)placeholderSize
{
#if !TARGET_OS_OSX // [macOS]
UIEdgeInsets textContainerInset = self.textContainerInset;
#else // [macOS
UIEdgeInsets textContainerInset = self.textContainerInsets;
#endif // macOS]
NSString *placeholder = self.placeholder ?: @"";
CGSize maxPlaceholderSize =
CGSizeMake(UIEdgeInsetsInsetRect(self.bounds, textContainerInset).size.width, CGFLOAT_MAX);
CGSize placeholderSize = [placeholder boundingRectWithSize:maxPlaceholderSize
options:NSStringDrawingUsesLineFragmentOrigin
attributes:[self _placeholderTextAttributes]
context:nil]
.size;
placeholderSize = CGSizeMake(RCTCeilPixelValue(placeholderSize.width), RCTCeilPixelValue(placeholderSize.height));
placeholderSize.width += textContainerInset.left + textContainerInset.right;
placeholderSize.height += textContainerInset.top + textContainerInset.bottom;
// Returning size DOES contain `textContainerInset` (aka `padding`; as `sizeThatFits:` does).
return placeholderSize;
}
- (CGSize)contentSize
{
#if !TARGET_OS_OSX // [macOS]
CGSize contentSize = super.contentSize;
CGSize placeholderSize = _placeholderView.isHidden ? CGSizeZero : self.placeholderSize;
#else // [macOS
CGSize contentSize = super.intrinsicContentSize;
CGSize placeholderSize = self.placeholderSize;
#endif // macOS]
// When a text input is empty, it actually displays a placeholder.
// So, we have to consider `placeholderSize` as a minimum `contentSize`.
// Returning size DOES contain `textContainerInset` (aka `padding`).
return CGSizeMake(MAX(contentSize.width, placeholderSize.width), MAX(contentSize.height, placeholderSize.height));
}
#if !TARGET_OS_OSX // [macOS]
- (void)layoutSubviews
{
[super layoutSubviews];
CGRect textFrame = UIEdgeInsetsInsetRect(self.bounds, self.textContainerInset);
CGFloat placeholderHeight = [_placeholderView sizeThatFits:textFrame.size].height;
textFrame.size.height = MIN(placeholderHeight, textFrame.size.height);
_placeholderView.frame = textFrame;
}
#endif // [macOS]
- (CGSize)intrinsicContentSize
{
// Returning size DOES contain `textContainerInset` (aka `padding`).
return [self sizeThatFits:CGSizeMake(self.preferredMaxLayoutWidth, CGFLOAT_MAX)];
}
- (CGSize)sizeThatFits:(CGSize)size
{
// Returned fitting size depends on text size and placeholder size.
#if !TARGET_OS_OSX // [macOS]
CGSize textSize = [super sizeThatFits:size];
#else // [macOS
[self.layoutManager glyphRangeForTextContainer:self.textContainer];
NSRect rect = [self.layoutManager usedRectForTextContainer:self.textContainer];
CGSize textSize = CGSizeMake(MIN(rect.size.width, size.width), rect.size.height);
#endif // macOS]
CGSize placeholderSize = self.placeholderSize;
// Returning size DOES contain `textContainerInset` (aka `padding`).
return CGSizeMake(MAX(textSize.width, placeholderSize.width), MAX(textSize.height, placeholderSize.height));
}
#pragma mark - Context Menu
#if !TARGET_OS_OSX // [macOS]
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
if (_contextMenuHidden) {
return NO;
}
return [super canPerformAction:action withSender:sender];
}
- (void)buildMenuWithBuilder:(id<UIMenuBuilder>)builder
{
#if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 170000
if (@available(iOS 17.0, *)) {
if (_contextMenuHidden) {
[builder removeMenuForIdentifier:UIMenuAutoFill];
}
}
#endif
[super buildMenuWithBuilder:builder];
}
#pragma mark - Dictation
- (void)dictationRecordingDidEnd
{
_dictationRecognizing = YES;
}
- (void)removeDictationResultPlaceholder:(id)placeholder willInsertResult:(BOOL)willInsertResult
{
[super removeDictationResultPlaceholder:placeholder willInsertResult:willInsertResult];
_dictationRecognizing = NO;
}
#endif // [macOS]
#pragma mark - Placeholder
- (void)_invalidatePlaceholderVisibility
{
#if !TARGET_OS_OSX // [macOS]
BOOL isVisible = _placeholder.length != 0 && self.attributedText.length == 0;
_placeholderView.hidden = !isVisible;
#else // [macOS
[self setNeedsDisplay:YES];
#endif // macOS]
}
#if !TARGET_OS_OSX // [macOS]
- (void)deleteBackward {
id<RCTBackedTextInputDelegate> textInputDelegate = [self textInputDelegate];
if ([textInputDelegate textInputShouldHandleDeleteBackward:self]) {
[super deleteBackward];
}
}
#else // [macOS
- (BOOL)performKeyEquivalent:(NSEvent *)event {
if (self.window.firstResponder == self && !self.hasMarkedText && ![self.textInputDelegate textInputShouldHandleKeyEvent:event]) {
return YES;
}
return [super performKeyEquivalent:event];
}
- (void)keyDown:(NSEvent *)event {
// If has marked text, handle by native and return
// Do this check before textInputShouldHandleKeyEvent as that one attempts to send the event to JS
if (self.hasMarkedText) {
[super keyDown:event];
return;
}
// textInputShouldHandleKeyEvent represents if native should handle the event instead of JS.
// textInputShouldHandleKeyEvent also sends keyDown event to JS internally, so we only call this once
if ([self.textInputDelegate textInputShouldHandleKeyEvent:event]) {
[super keyDown:event];
[self.textInputDelegate submitOnKeyDownIfNeeded:event];
}
}
- (void)keyUp:(NSEvent *)event {
if ([self.textInputDelegate textInputShouldHandleKeyEvent:event]) {
[super keyUp:event];
}
}
#endif // macOS]
- (void)_updatePlaceholder
{
#if !TARGET_OS_OSX // [macOS]
_placeholderView.attributedText = [[NSAttributedString alloc] initWithString:_placeholder ?: @""
attributes:[self _placeholderTextAttributes]];
#else // [macOS
[self setNeedsDisplay:YES];
#endif // macOS]
[self _invalidatePlaceholderVisibility];
}
- (NSDictionary<NSAttributedStringKey, id> *)_placeholderTextAttributes
{
NSMutableDictionary<NSAttributedStringKey, id> *textAttributes =
[_defaultTextAttributes mutableCopy] ?: [NSMutableDictionary new];
[textAttributes setValue:self.placeholderColor ?: defaultPlaceholderColor() forKey:NSForegroundColorAttributeName];
if (![textAttributes objectForKey:NSFontAttributeName]) {
[textAttributes setValue:defaultPlaceholderFont() forKey:NSFontAttributeName];
}
return textAttributes;
}
#pragma mark - Caret Manipulation
#if !TARGET_OS_OSX // [macOS]
- (CGRect)caretRectForPosition:(UITextPosition *)position
{
if (_caretHidden) {
return CGRectZero;
}
return [super caretRectForPosition:position];
}
#endif // [macOS]
#pragma mark - Utility Methods
@end