2929
3030using  namespace  facebook ::react; 
3131
32+ #if  !TARGET_OS_OSX //  [macOS]
3233//  ParagraphTextView is an auxiliary view we set as contentView so the drawing
3334//  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]
3935@interface  RCTParagraphTextView  : RCTUIView  //  [macOS]
4036#else  //  [macOS
37+ //  On macOS, we also defer drawing to an NSTextView,
38+ //  in order to get more native behaviors like text selection.
4139@interface  RCTParagraphTextView  : NSTextView  //  [macOS]
4240#endif //  macOS]
4341
4442@property  (nonatomic ) ParagraphShadowNode::ConcreteState::Shared state;
4543@property  (nonatomic ) ParagraphAttributes paragraphAttributes;
4644@property  (nonatomic ) LayoutMetrics layoutMetrics;
4745
46+ #if  TARGET_OS_OSX //  [macOS]
47+ // / UIKit compatibility shim that simply calls `[self setNeedsDisplay:YES]`
48+ - (void )setNeedsDisplay ;
49+ #endif 
50+ 
4851@end 
4952
5053#if  !TARGET_OS_OSX //  [macOS]
@@ -161,13 +164,9 @@ - (void)updateProps:(const Props::Shared &)props oldProps:(const Props::Shared &
161164- (void )updateState : (const  State::Shared &)state  oldState : (const  State::Shared &)oldState 
162165{
163166  _state = std::static_pointer_cast<const  ParagraphShadowNode::ConcreteState>(state);
164- #if  !TARGET_OS_OSX //  [macOS]
165167  _textView.state  = _state;
166168  [_textView setNeedsDisplay ];
167169  [self  setNeedsLayout ];
168- #else  //  [macOS
169-   [self  _updateTextView ];
170- #endif  //  macOS]
171170}
172171
173172- (void )updateLayoutMetrics : (const  LayoutMetrics &)layoutMetrics 
@@ -177,52 +176,9 @@ - (void)updateLayoutMetrics:(const LayoutMetrics &)layoutMetrics
177176  //  re-applying individual sub-values which weren't changed.
178177  [super  updateLayoutMetrics: layoutMetrics oldLayoutMetrics: _layoutMetrics];
179178  _textView.layoutMetrics  = _layoutMetrics;
180- #if  !TARGET_OS_OSX //  [macOS]
181179  [_textView setNeedsDisplay ];
182180  [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 ];
224181}
225- #endif  //  macOS]
226182
227183- (void )prepareForRecycle 
228184{
@@ -425,11 +381,13 @@ - (void)copy:(id)sender
425381  return  RCTParagraphComponentView.class ;
426382}
427383
428- #if  !TARGET_OS_OSX //  [macOS]
429384@implementation  RCTParagraphTextView  {
385+ #if  !TARGET_OS_OSX //  [macOS]
430386  CAShapeLayer  *_highlightLayer;
387+ #endif  //  macOS]
431388}
432389
390+ 
433391- (void )drawRect : (CGRect)rect 
434392{
435393  if  (!_state) {
@@ -447,6 +405,7 @@ - (void)drawRect:(CGRect)rect
447405
448406  CGRect frame = RCTCGRectFromRect (_layoutMetrics.getContentFrame ());
449407
408+ #if  !TARGET_OS_OSX //  [macOS]
450409  [nativeTextLayoutManager drawAttributedString: _state->getData ().attributedString
451410                            paragraphAttributes: _paragraphAttributes
452411                                          frame: frame
@@ -464,11 +423,33 @@ - (void)drawRect:(CGRect)rect
464423                                  self->_highlightLayer  = nil ;
465424                                }
466425                              }];
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 
467446}
468447
469- @end 
470- #else  //  [macOS
471- @implementation  RCTParagraphTextView 
448+ #if  TARGET_OS_OSX //  [macOS
449+ - (void )setNeedsDisplay 
450+ {
451+   [self  setNeedsDisplay: YES ];
452+ }
472453
473454- (BOOL )canBecomeKeyView 
474455{
@@ -484,6 +465,6 @@ - (BOOL)resignFirstResponder
484465
485466  return  [super  resignFirstResponder ];
486467}
468+ #endif  //  macOS]
487469
488470@end 
489- #endif  //  macOS]
0 commit comments