29
29
30
30
using namespace facebook ::react;
31
31
32
+ #if !TARGET_OS_OSX // [macOS]
32
33
// ParagraphTextView is an auxiliary view we set as contentView so the drawing
33
34
// can happen on top of the layers manipulated by RCTViewComponentView (the parent view)
34
- // [macOS
35
- // On macOS, we defer drawing to an NSTextView rather than a plan NSView, in order
36
- // to get more native behaviors like text selection.
37
- // macOS]
38
- #if !TARGET_OS_OSX // [macOS]
39
35
@interface RCTParagraphTextView : RCTUIView // [macOS]
40
36
#else // [macOS
37
+ // On macOS, we also defer drawing to an NSTextView,
38
+ // in order to get more native behaviors like text selection.
41
39
@interface RCTParagraphTextView : NSTextView // [macOS]
42
40
#endif // macOS]
43
41
44
42
@property (nonatomic ) ParagraphShadowNode::ConcreteState::Shared state;
45
43
@property (nonatomic ) ParagraphAttributes paragraphAttributes;
46
44
@property (nonatomic ) LayoutMetrics layoutMetrics;
47
45
46
+ #if TARGET_OS_OSX // [macOS]
47
+ // / UIKit compatibility shim that simply calls `[self setNeedsDisplay:YES]`
48
+ - (void )setNeedsDisplay ;
49
+ #endif
50
+
48
51
@end
49
52
50
53
#if !TARGET_OS_OSX // [macOS]
@@ -161,13 +164,9 @@ - (void)updateProps:(const Props::Shared &)props oldProps:(const Props::Shared &
161
164
- (void )updateState : (const State::Shared &)state oldState : (const State::Shared &)oldState
162
165
{
163
166
_state = std::static_pointer_cast<const ParagraphShadowNode::ConcreteState>(state);
164
- #if !TARGET_OS_OSX // [macOS]
165
167
_textView.state = _state;
166
168
[_textView setNeedsDisplay ];
167
169
[self setNeedsLayout ];
168
- #else // [macOS
169
- [self _updateTextView ];
170
- #endif // macOS]
171
170
}
172
171
173
172
- (void )updateLayoutMetrics : (const LayoutMetrics &)layoutMetrics
@@ -177,52 +176,9 @@ - (void)updateLayoutMetrics:(const LayoutMetrics &)layoutMetrics
177
176
// re-applying individual sub-values which weren't changed.
178
177
[super updateLayoutMetrics: layoutMetrics oldLayoutMetrics: _layoutMetrics];
179
178
_textView.layoutMetrics = _layoutMetrics;
180
- #if !TARGET_OS_OSX // [macOS]
181
179
[_textView setNeedsDisplay ];
182
180
[self setNeedsLayout ];
183
- #else // [macOS
184
- [self _updateTextView ];
185
- #endif // macOS]
186
- }
187
-
188
- #if TARGET_OS_OSX // [macOS
189
- - (void )_updateTextView
190
- {
191
- if (!_state) {
192
- return ;
193
- }
194
-
195
- auto textLayoutManager = _state->getData ().layoutManager .lock ();
196
-
197
- if (!textLayoutManager) {
198
- return ;
199
- }
200
-
201
- RCTTextLayoutManager *nativeTextLayoutManager =
202
- (RCTTextLayoutManager *)unwrapManagedObject (textLayoutManager->getNativeTextLayoutManager ());
203
-
204
- CGRect frame = RCTCGRectFromRect (_layoutMetrics.getContentFrame ());
205
-
206
- NSTextStorage *textStorage = [nativeTextLayoutManager getTextStorageForAttributedString: _state->getData ().attributedString paragraphAttributes: _paragraphAttributes size: frame.size];
207
-
208
- NSLayoutManager *layoutManager = textStorage.layoutManagers .firstObject ;
209
- NSTextContainer *textContainer = layoutManager.textContainers .firstObject ;
210
-
211
- [_textView replaceTextContainer: textContainer];
212
-
213
- NSArray <NSLayoutManager *> *managers = [[textStorage layoutManagers ] copy ];
214
- for (NSLayoutManager *manager in managers) {
215
- [textStorage removeLayoutManager: manager];
216
- }
217
-
218
- _textView.minSize = frame.size ;
219
- _textView.maxSize = frame.size ;
220
- _textView.frame = frame;
221
- _textView.textStorage .attributedString = textStorage;
222
-
223
- [self setNeedsDisplay ];
224
181
}
225
- #endif // macOS]
226
182
227
183
- (void )prepareForRecycle
228
184
{
@@ -425,11 +381,13 @@ - (void)copy:(id)sender
425
381
return RCTParagraphComponentView.class ;
426
382
}
427
383
428
- #if !TARGET_OS_OSX // [macOS]
429
384
@implementation RCTParagraphTextView {
385
+ #if !TARGET_OS_OSX // [macOS]
430
386
CAShapeLayer *_highlightLayer;
387
+ #endif // macOS]
431
388
}
432
389
390
+
433
391
- (void )drawRect : (CGRect)rect
434
392
{
435
393
if (!_state) {
@@ -447,6 +405,7 @@ - (void)drawRect:(CGRect)rect
447
405
448
406
CGRect frame = RCTCGRectFromRect (_layoutMetrics.getContentFrame ());
449
407
408
+ #if !TARGET_OS_OSX // [macOS]
450
409
[nativeTextLayoutManager drawAttributedString: _state->getData ().attributedString
451
410
paragraphAttributes: _paragraphAttributes
452
411
frame: frame
@@ -464,11 +423,33 @@ - (void)drawRect:(CGRect)rect
464
423
self->_highlightLayer = nil ;
465
424
}
466
425
}];
426
+ #else // [macOS
427
+ NSTextStorage *textStorage = [nativeTextLayoutManager getTextStorageForAttributedString: _state->getData ().attributedString paragraphAttributes: _paragraphAttributes size: frame.size];
428
+
429
+ NSLayoutManager *layoutManager = textStorage.layoutManagers .firstObject ;
430
+ NSTextContainer *textContainer = layoutManager.textContainers .firstObject ;
431
+
432
+ [self replaceTextContainer: textContainer];
433
+
434
+ NSArray <NSLayoutManager *> *managers = [[textStorage layoutManagers ] copy ];
435
+ for (NSLayoutManager *manager in managers) {
436
+ [textStorage removeLayoutManager: manager];
437
+ }
438
+
439
+ self.minSize = frame.size ;
440
+ self.maxSize = frame.size ;
441
+ self.frame = frame;
442
+ [[self textStorage ] setAttributedString: textStorage];
443
+
444
+ [super drawRect: rect];
445
+ #endif
467
446
}
468
447
469
- @end
470
- #else // [macOS
471
- @implementation RCTParagraphTextView
448
+ #if TARGET_OS_OSX // [macOS
449
+ - (void )setNeedsDisplay
450
+ {
451
+ [self setNeedsDisplay: YES ];
452
+ }
472
453
473
454
- (BOOL )canBecomeKeyView
474
455
{
@@ -484,6 +465,6 @@ - (BOOL)resignFirstResponder
484
465
485
466
return [super resignFirstResponder ];
486
467
}
468
+ #endif // macOS]
487
469
488
470
@end
489
- #endif // macOS]
0 commit comments