-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathENRMUIKit.h
More file actions
398 lines (367 loc) · 14.3 KB
/
ENRMUIKit.h
File metadata and controls
398 lines (367 loc) · 14.3 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
#pragma once
#include <TargetConditionals.h>
// RCTUIKit.h is react-native-macos only. On iOS we import UIKit and define the aliases ourselves.
#if !TARGET_OS_OSX
#import <UIKit/UIKit.h>
#define RCTUIColor UIColor
#define RCTUIImage UIImage
#define RCTUIView UIView
#define RCTUIScrollView UIScrollView
#define RCTUIGraphicsImageRenderer UIGraphicsImageRenderer
#define RCTUIGraphicsImageRendererContext UIGraphicsImageRendererContext
#define RCTUIGraphicsImageRendererFormat UIGraphicsImageRendererFormat
#define ENRMPlatformTextView UITextView
#define ENRMTapRecognizer UITapGestureRecognizer
// Inline helpers that RCTUIKit.h normally provides on iOS.
static inline UIBezierPath *UIBezierPathWithRoundedRect(CGRect rect, CGFloat cornerRadius)
{
return [UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:cornerRadius];
}
static inline void UIBezierPathAppendPath(UIBezierPath *path, UIBezierPath *appendPath)
{
[path appendPath:appendPath];
}
static inline CGFloat UIFontLineHeight(UIFont *font)
{
return font.lineHeight;
}
#else
#import <React/RCTTextUIKit.h>
#import <React/RCTUIKit.h>
#import <React/RCTUITextView.h>
#define ENRMPlatformTextView RCTUITextView
#define ENRMTapRecognizer NSClickGestureRecognizer
#endif
/// On iOS, explicitly sets opaque=NO — without it the renderer produces an opaque backing,
/// breaking transparent backgrounds. macOS handles transparency by default.
static inline RCTUIGraphicsImageRenderer *ImageRendererForSize(CGSize size)
{
#if !TARGET_OS_OSX
RCTUIGraphicsImageRendererFormat *format = [RCTUIGraphicsImageRendererFormat preferredFormat];
format.opaque = NO;
return [[RCTUIGraphicsImageRenderer alloc] initWithSize:size format:format];
#else
return [[RCTUIGraphicsImageRenderer alloc] initWithSize:size];
#endif
}
/// NSBezierPath uses NS-prefixed enum values; UIBezierPath uses kCG-prefixed constants.
static inline void BezierPathSetRoundStyle(UIBezierPath *path)
{
#if !TARGET_OS_OSX
path.lineCapStyle = kCGLineCapRound;
path.lineJoinStyle = kCGLineJoinRound;
#else
path.lineCapStyle = NSLineCapStyleRound;
path.lineJoinStyle = NSLineJoinStyleRound;
#endif
}
/// Cross-platform line segment: NSBezierPath uses lineToPoint: instead of addLineToPoint:.
static inline void BezierPathAddLine(UIBezierPath *path, CGPoint point)
{
#if !TARGET_OS_OSX
[path addLineToPoint:point];
#else
[path lineToPoint:point];
#endif
}
/// Cross-platform quad-curve: NSBezierPath lacks addQuadCurveToPoint:, so we approximate
/// with a cubic Bezier using the standard quadratic-to-cubic conversion.
static inline void BezierPathAddQuadCurve(UIBezierPath *path, CGPoint end, CGPoint control)
{
#if !TARGET_OS_OSX
[path addQuadCurveToPoint:end controlPoint:control];
#else
CGPoint start = [path currentPoint];
[path curveToPoint:end
controlPoint1:CGPointMake(start.x + 2.0 / 3.0 * (control.x - start.x),
start.y + 2.0 / 3.0 * (control.y - start.y))
controlPoint2:CGPointMake(end.x + 2.0 / 3.0 * (control.x - end.x), end.y + 2.0 / 3.0 * (control.y - end.y))];
#endif
}
/// Cross-platform IME composition check: UITextView uses markedTextRange (nullable UITextRange);
/// NSTextView uses hasMarkedText (BOOL).
static inline BOOL ENRMHasMarkedText(ENRMPlatformTextView *textView)
{
#if !TARGET_OS_OSX
return textView.markedTextRange != nil;
#else
return textView.hasMarkedText;
#endif
}
/// Cross-platform plain text read: UITextView uses .text; NSTextView uses .string.
static inline NSString *ENRMGetPlainText(ENRMPlatformTextView *textView)
{
#if !TARGET_OS_OSX
return textView.text;
#else
return textView.string;
#endif
}
/// Cross-platform plain text write: UITextView uses .text setter; NSTextView uses setString:.
static inline void ENRMSetPlainText(ENRMPlatformTextView *textView, NSString *text)
{
#if !TARGET_OS_OSX
textView.text = text;
#else
[textView setString:text];
#endif
}
/// Cross-platform attributed text read: NSTextView exposes content via textStorage;
/// UITextView exposes it via attributedText.
static inline NSAttributedString *ENRMGetAttributedText(ENRMPlatformTextView *textView)
{
#if !TARGET_OS_OSX
return textView.attributedText;
#else
return textView.textStorage;
#endif
}
/// Cross-platform attributed text write: NSTextView uses textStorage setAttributedString:;
/// UITextView uses the attributedText property setter.
static inline void ENRMSetAttributedText(ENRMPlatformTextView *textView, NSAttributedString *text)
{
#if !TARGET_OS_OSX
textView.attributedText = text;
#else
[textView.textStorage setAttributedString:text];
#endif
}
/// Cross-platform text replacement at a given range.
/// iOS uses UITextInput protocol methods; macOS uses NSTextView's insertText:replacementRange:.
static inline void ENRMReplaceTextInRange(ENRMPlatformTextView *textView, NSString *text, NSRange range)
{
#if !TARGET_OS_OSX
UITextPosition *start = [textView positionFromPosition:textView.beginningOfDocument offset:(NSInteger)range.location];
UITextPosition *end = [textView positionFromPosition:textView.beginningOfDocument
offset:(NSInteger)NSMaxRange(range)];
[textView replaceRange:[textView textRangeFromPosition:start toPosition:end] withText:text];
#else
[textView insertText:text replacementRange:range];
#endif
}
/// Cross-platform content size update after measurement.
/// iOS UITextView has a settable contentSize; macOS NSTextView does not.
static inline void ENRMSetContentSize(ENRMPlatformTextView *textView, CGSize size)
{
#if !TARGET_OS_OSX
textView.contentSize = size;
#endif
}
/// Sets default typing attributes on the text view.
/// On macOS, RCTUITextView overrides setTypingAttributes: to use defaultTextAttributes,
/// so we must set that property as well.
static inline void ENRMSetDefaultTypingAttributes(ENRMPlatformTextView *textView, NSDictionary *attrs)
{
#if TARGET_OS_OSX
textView.defaultTextAttributes = attrs;
#endif
textView.typingAttributes = attrs;
}
/// Applies shared configuration to a text view used for markdown input editing.
/// Handles platform differences: scroll indicators, text container insets,
/// drawsBackground (macOS). Sets editable=YES, scrollEnabled=YES.
static inline void ENRMConfigureMarkdownTextInputTextView(ENRMPlatformTextView *textView)
{
textView.font = [UIFont systemFontOfSize:16.0];
textView.backgroundColor = [RCTUIColor clearColor];
textView.editable = YES;
#if !TARGET_OS_OSX
textView.scrollEnabled = YES;
textView.showsVerticalScrollIndicator = NO;
textView.showsHorizontalScrollIndicator = NO;
textView.textContainerInset = UIEdgeInsetsZero;
#else
textView.textContainerInsets = UIEdgeInsetsZero;
textView.drawsBackground = NO;
#endif
textView.textContainer.lineFragmentPadding = 0;
}
/// Cross-platform cursor color: iOS uses tintColor; macOS uses insertionPointColor.
static inline void ENRMSetCursorColor(ENRMPlatformTextView *textView, RCTUIColor *color)
{
#if !TARGET_OS_OSX
textView.tintColor = color;
#else
textView.insertionPointColor = color;
#endif
}
/// Cross-platform selection color: iOS uses tintColor (affects both cursor and selection);
/// macOS selection highlight is managed by the system and not directly settable.
static inline void ENRMSetSelectionColor(ENRMPlatformTextView *textView, RCTUIColor *color)
{
#if !TARGET_OS_OSX
textView.tintColor = color;
#endif
}
/// Cross-platform focus: iOS uses becomeFirstResponder;
/// macOS uses makeFirstResponder: on the window.
static inline void ENRMFocusTextView(ENRMPlatformTextView *textView)
{
#if !TARGET_OS_OSX
[textView becomeFirstResponder];
#else
[textView.window makeFirstResponder:textView];
#endif
}
/// Cross-platform blur: iOS uses resignFirstResponder;
/// macOS clears first responder via the window.
static inline void ENRMBlurTextView(ENRMPlatformTextView *textView)
{
#if !TARGET_OS_OSX
[textView resignFirstResponder];
#else
[textView.window makeFirstResponder:nil];
#endif
}
/// Applies shared configuration to a text view used for markdown rendering.
/// Handles platform differences: scroll indicators, text container insets,
/// drawsBackground (macOS), accessibilityElementsHidden (iOS).
static inline void ENRMConfigureMarkdownTextView(ENRMPlatformTextView *textView)
{
textView.font = [UIFont systemFontOfSize:16.0];
textView.backgroundColor = [RCTUIColor clearColor];
textView.textColor = [RCTUIColor blackColor];
textView.editable = NO;
textView.scrollEnabled = NO;
#if !TARGET_OS_OSX
textView.showsVerticalScrollIndicator = NO;
textView.showsHorizontalScrollIndicator = NO;
textView.textContainerInset = UIEdgeInsetsZero;
#else
textView.textContainerInsets = UIEdgeInsetsZero;
textView.drawsBackground = NO;
#endif
textView.textContainer.lineFragmentPadding = 0;
textView.linkTextAttributes = @{};
textView.selectable = YES;
#if !TARGET_OS_OSX
textView.accessibilityElementsHidden = YES;
#endif
}
/// Result of a text layout measurement pass.
typedef struct {
CGRect usedRect;
CGRect extraLineFragmentRect;
} ENRMTextLayoutResult;
/// Measures text layout in a text view for a given width.
/// On macOS, temporarily disables widthTracksTextView so the container width
/// can be set directly without being overridden by the view's frame.
static inline ENRMTextLayoutResult ENRMMeasureTextLayout(ENRMPlatformTextView *textView, CGFloat maxWidth)
{
#if TARGET_OS_OSX
textView.textContainer.widthTracksTextView = NO;
#endif
textView.textContainer.size = CGSizeMake(maxWidth, CGFLOAT_MAX);
[textView.layoutManager ensureLayoutForTextContainer:textView.textContainer];
ENRMTextLayoutResult result;
result.usedRect = [textView.layoutManager usedRectForTextContainer:textView.textContainer];
result.extraLineFragmentRect = textView.layoutManager.extraLineFragmentRect;
#if TARGET_OS_OSX
textView.textContainer.widthTracksTextView = YES;
#endif
return result;
}
/// Cross-platform display refresh: UIView requires layoutIfNeeded before setNeedsDisplay
/// to flush pending layout before the redraw; NSView takes a BOOL argument.
/// Implemented as a macro to avoid Objective-C++ implicit pointer conversion issues in .mm files.
#if !TARGET_OS_OSX
#define ENRMSetNeedsDisplay(view) \
do { \
[(view) layoutIfNeeded]; \
[(view) setNeedsDisplay]; \
} while (0)
#else
#define ENRMSetNeedsDisplay(view) [(view) setNeedsDisplay:YES]
#endif
/// Invalidates the text container, re-lays out any existing content, and
/// triggers a redraw. On iOS also resets contentOffset to zero.
/// Does NOT touch the text view's frame — callers that need to reposition
/// the view should set the frame themselves before calling this.
static inline void ENRMRefreshTextViewLayout(ENRMPlatformTextView *textView)
{
#if !TARGET_OS_OSX
textView.contentOffset = CGPointZero;
#endif
textView.textContainer.size = CGSizeMake(textView.bounds.size.width, CGFLOAT_MAX);
NSUInteger textLength = ENRMGetAttributedText(textView).length;
if (textLength > 0) {
[textView.layoutManager invalidateLayoutForCharacterRange:NSMakeRange(0, textLength) actualCharacterRange:NULL];
[textView.layoutManager ensureLayoutForTextContainer:textView.textContainer];
}
ENRMSetNeedsDisplay(textView);
}
/// Refreshes a text view's layout and display after it is attached to a window.
/// Sets the frame and text container to the given bounds, invalidates layout for
/// any existing content, then triggers a redraw.
static inline void ENRMRefreshTextViewAfterWindowAttach(ENRMPlatformTextView *textView, CGRect bounds)
{
textView.frame = bounds;
ENRMRefreshTextViewLayout(textView);
}
/// Cross-platform text deselection: UITextView uses selectedTextRange (nullable);
/// NSTextView uses selectedRange (length-based).
static inline void ENRMClearSelection(ENRMPlatformTextView *textView)
{
#if !TARGET_OS_OSX
if (textView.selectedTextRange != nil) {
textView.selectedTextRange = nil;
}
#else
if (textView.selectedRange.length > 0) {
textView.selectedRange = NSMakeRange(0, 0);
}
#endif
}
// ── Placeholder label abstraction ──────────────────────────────────────────────
#if !TARGET_OS_OSX
typedef UILabel ENRMPlaceholderLabel;
#else
typedef NSTextField ENRMPlaceholderLabel;
#endif
/// Creates a placeholder label configured for use inside an ENRMPlatformTextView.
/// On iOS: UILabel with multiline, placeholderTextColor, auto-layout pinned to
/// textContainerInset and lineFragmentPadding.
/// On macOS: borderless, non-editable NSTextField pinned to the text view origin.
static inline ENRMPlaceholderLabel *ENRMCreatePlaceholderLabel(ENRMPlatformTextView *textView, UIFont *font)
{
#if !TARGET_OS_OSX
UILabel *label = [[UILabel alloc] init];
label.numberOfLines = 0;
label.textColor = [UIColor placeholderTextColor];
label.font = font;
label.translatesAutoresizingMaskIntoConstraints = NO;
[textView addSubview:label];
[NSLayoutConstraint activateConstraints:@[
[label.topAnchor constraintEqualToAnchor:textView.topAnchor constant:textView.textContainerInset.top],
[label.leadingAnchor constraintEqualToAnchor:textView.leadingAnchor
constant:textView.textContainer.lineFragmentPadding],
[label.trailingAnchor constraintEqualToAnchor:textView.trailingAnchor
constant:-textView.textContainer.lineFragmentPadding],
]];
return label;
#else
NSTextField *label = [[NSTextField alloc] initWithFrame:CGRectZero];
label.bordered = NO;
label.editable = NO;
label.selectable = NO;
label.drawsBackground = NO;
label.textColor = [NSColor placeholderTextColor];
label.font = font;
label.translatesAutoresizingMaskIntoConstraints = NO;
[textView addSubview:label];
[NSLayoutConstraint activateConstraints:@[
[label.topAnchor constraintEqualToAnchor:textView.topAnchor],
[label.leadingAnchor constraintEqualToAnchor:textView.leadingAnchor],
]];
return label;
#endif
}
/// Cross-platform placeholder text setter.
static inline void ENRMSetPlaceholderText(ENRMPlaceholderLabel *label, NSString *text)
{
#if !TARGET_OS_OSX
label.text = text;
#else
label.stringValue = text;
#endif
}