Ver código fonte

【VisionPro适配】适配VisionOS

xcbosa mbp16 1 ano atrás
pai
commit
f749474e94

+ 10 - 0
YYText/Component/YYTextEffectWindow.m

@@ -72,11 +72,15 @@
     UIApplication *app = YYTextSharedApplication();
     if (!app) return;
     
+#if TARGET_OS_VISION
+    
+#else
     UIWindow *top = app.windows.lastObject;
     UIWindow *key = app.keyWindow;
     if (key && key.windowLevel > top.windowLevel) top = key;
     if (top == self) return;
     self.windowLevel = top.windowLevel + 1;
+#endif
 }
 
 - (YYTextDirection)_keyboardDirection {
@@ -269,17 +273,23 @@
     CGContextTranslateCTM(context, tp.x - captureCenter.x, tp.y - captureCenter.y);
     
     NSMutableArray *windows = app.windows.mutableCopy;
+#if !TARGET_OS_VISION
     UIWindow *keyWindow = app.keyWindow;
     if (![windows containsObject:keyWindow]) [windows addObject:keyWindow];
+#endif
     [windows sortUsingComparator:^NSComparisonResult(UIWindow *w1, UIWindow *w2) {
         if (w1.windowLevel < w2.windowLevel) return NSOrderedAscending;
         else if (w1.windowLevel > w2.windowLevel) return NSOrderedDescending;
         return NSOrderedSame;
     }];
+#if !TARGET_OS_VISION
     UIScreen *mainScreen = [UIScreen mainScreen];
+#endif
     for (UIWindow *window in windows) {
         if (window.hidden || window.alpha <= 0.01) continue;
+#if !TARGET_OS_VISION
         if (window.screen != mainScreen) continue;
+#endif
         if ([window isKindOfClass:self.class]) break; //don't capture window above self
         CGContextSaveGState(context);
         CGContextConcatCTM(context, YYTextCGAffineTransformGetFromViews(window, self));

+ 4 - 0
YYText/Component/YYTextInput.h

@@ -77,7 +77,11 @@ typedef NS_ENUM(NSInteger, YYTextAffinity) {
 @interface YYTextSelectionRect : UITextSelectionRect <NSCopying>
 
 @property (nonatomic, readwrite) CGRect rect;
+#if TARGET_OS_VISION
+@property (nonatomic, readwrite) NSInteger writingDirection;
+#else
 @property (nonatomic, readwrite) UITextWritingDirection writingDirection;
+#endif
 @property (nonatomic, readwrite) BOOL containsStart;
 @property (nonatomic, readwrite) BOOL containsEnd;
 @property (nonatomic, readwrite) BOOL isVertical;

+ 16 - 0
YYText/Component/YYTextKeyboardManager.m

@@ -179,7 +179,9 @@ static int _YYTextKeyboardViewFrameObserverKey;
     for (window in app.windows) {
         if ([self _getKeyboardViewFromWindow:window]) return window;
     }
+#if !TARGET_OS_VISION
     window = app.keyWindow;
+#endif
     if ([self _getKeyboardViewFromWindow:window]) return window;
     
     NSMutableArray *kbWindows = nil;
@@ -220,7 +222,9 @@ static int _YYTextKeyboardViewFrameObserverKey;
         view = [self _getKeyboardViewFromWindow:window];
         if (view) return view;
     }
+#if !TARGET_OS_VISION
     window = app.keyWindow;
+#endif
     view = [self _getKeyboardViewFromWindow:window];
     if (view) return view;
     return nil;
@@ -405,9 +409,11 @@ static int _YYTextKeyboardViewFrameObserverKey;
     
     UIView *keyboard = self.keyboardView;
     UIWindow *window = keyboard.window;
+#if !TARGET_OS_VISION
     if (!window) {
         window = app.keyWindow;
     }
+#endif
     if (!window) {
         window = app.windows.firstObject;
     }
@@ -433,7 +439,11 @@ static int _YYTextKeyboardViewFrameObserverKey;
         
         // Fix iPad(iOS7) keyboard frame error after rorate device when the keyboard is not docked to bottom.
         if (((int)[self _systemVersion]) == 7) {
+#if TARGET_OS_VISION
+            UIInterfaceOrientation ori = UIInterfaceOrientationLandscapeLeft;
+#else
             UIInterfaceOrientation ori = app.statusBarOrientation;
+#endif
             if (_fromOrientation != UIInterfaceOrientationUnknown && _fromOrientation != ori) {
                 switch (ori) {
                     case UIInterfaceOrientationPortrait: {
@@ -483,7 +493,9 @@ static int _YYTextKeyboardViewFrameObserverKey;
     _hasObservedChange = NO;
     _fromFrame = trans.toFrame;
     _fromVisible = trans.toVisible;
+#if !TARGET_OS_VISION
     _fromOrientation = app.statusBarOrientation;
+#endif
 }
 
 - (CGRect)convertRect:(CGRect)rect toView:(UIView *)view {
@@ -493,7 +505,11 @@ static int _YYTextKeyboardViewFrameObserverKey;
     if (CGRectIsNull(rect)) return rect;
     if (CGRectIsInfinite(rect)) return rect;
     
+#if TARGET_OS_VISION
+    UIWindow *mainWindow = nil;
+#else
     UIWindow *mainWindow = app.keyWindow;
+#endif
     if (!mainWindow) mainWindow = app.windows.firstObject;
     if (!mainWindow) { // no window ?!
         if (view) {

+ 4 - 0
YYText/Component/YYTextLayout.m

@@ -2045,7 +2045,9 @@ fail:
             } else {
                 topRect.rect = CGRectMake(_container.path ? startLine.left : _container.insets.left, startLine.top, topOffset - startLine.left, startLine.height);
             }
+#if !TARGET_OS_VISION
             topRect.writingDirection = UITextWritingDirectionRightToLeft;
+#endif
         } else {
             if (isVertical) {
                 topRect.rect = CGRectMake(startLine.left, topOffset, startLine.width, (_container.path ? startLine.bottom : _container.size.height - _container.insets.bottom) - topOffset);
@@ -2066,7 +2068,9 @@ fail:
             } else {
                 bottomRect.rect = CGRectMake(bottomOffset, endLine.top, (_container.path ? endLine.right : _container.size.width - _container.insets.right) - bottomOffset, endLine.height);
             }
+#if !TARGET_OS_VISION
             bottomRect.writingDirection = UITextWritingDirectionRightToLeft;
+#endif
         } else {
             if (isVertical) {
                 CGFloat top = _container.path ? endLine.top : _container.insets.top;

+ 4 - 0
YYText/Utility/YYTextAsyncLayer.m

@@ -92,7 +92,11 @@ static dispatch_queue_t YYTextAsyncLayerGetReleaseQueue() {
     static CGFloat scale; //global
     static dispatch_once_t onceToken;
     dispatch_once(&onceToken, ^{
+#if TARGET_OS_VISION
+        scale = 1.f;
+#else
         scale = [UIScreen mainScreen].scale;
+#endif
     });
     self.contentsScale = scale;
     _sentinel = [_YYTextSentinel new];

+ 8 - 0
YYText/Utility/YYTextUtilities.m

@@ -267,15 +267,22 @@ CGRect YYTextCGRectFitWithContentMode(CGRect rect, CGSize size, UIViewContentMod
 }
 
 CGFloat YYTextScreenScale() {
+#if TARGET_OS_VISION
+    return 1.f;
+#else
     static CGFloat scale;
     static dispatch_once_t onceToken;
     dispatch_once(&onceToken, ^{
         scale = [UIScreen mainScreen].scale;
     });
     return scale;
+#endif
 }
 
 CGSize YYTextScreenSize() {
+#if TARGET_OS_VISION
+    return UIApplication.sharedApplication.windows.firstObject.frame.size;
+#else
     static CGSize size;
     static dispatch_once_t onceToken;
     dispatch_once(&onceToken, ^{
@@ -287,6 +294,7 @@ CGSize YYTextScreenSize() {
         }
     });
     return size;
+#endif
 }
 
 

+ 4 - 0
YYText/YYLabel.m

@@ -377,7 +377,11 @@ static dispatch_queue_t YYLabelGetReleaseQueue() {
 
 - (void)_initLabel {
     ((YYTextAsyncLayer *)self.layer).displaysAsynchronously = NO;
+#if TARGET_OS_VISION
+    self.layer.contentsScale = 1.f;
+#else
     self.layer.contentsScale = [UIScreen mainScreen].scale;
+#endif
     self.contentMode = UIViewContentModeRedraw;
     
     _attachmentViews = [NSMutableArray new];

+ 46 - 3
YYText/YYTextView.m

@@ -104,7 +104,11 @@ typedef NS_ENUM(NSUInteger, YYTextMoveDirection) {
 @end
 
 
-@interface YYTextView () <UIScrollViewDelegate, UIAlertViewDelegate, YYTextDebugTarget, YYTextKeyboardObserver> {
+@interface YYTextView () <UIScrollViewDelegate,
+#if !TARGET_OS_VISION
+UIAlertViewDelegate,
+#endif
+YYTextDebugTarget, YYTextKeyboardObserver> {
     
     YYTextRange *_selectedTextRange; /// nonnull
     YYTextRange *_markedTextRange;
@@ -636,6 +640,14 @@ typedef NS_ENUM(NSUInteger, YYTextMoveDirection) {
     
     if (self.isFirstResponder || _containerView.isFirstResponder) {
         dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.01 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
+#if TARGET_OS_VISION
+            UIMenuController *menu = [UIMenuController sharedMenuController];
+            [menu update];
+            if (!_state.showingMenu || !menu.menuVisible) {
+                _state.showingMenu = YES;
+                [menu showMenuFromView:_selectionView rect:CGRectStandardize(rect)];
+            }
+#else
             UIMenuController *menu = [UIMenuController sharedMenuController];
             [menu setTargetRect:CGRectStandardize(rect) inView:_selectionView];
             [menu update];
@@ -643,6 +655,7 @@ typedef NS_ENUM(NSUInteger, YYTextMoveDirection) {
                 _state.showingMenu = YES;
                 [menu setMenuVisible:YES animated:YES];
             }
+#endif
         });
     }
 }
@@ -652,7 +665,11 @@ typedef NS_ENUM(NSUInteger, YYTextMoveDirection) {
     if (_state.showingMenu) {
         _state.showingMenu = NO;
         UIMenuController *menu = [UIMenuController sharedMenuController];
+#if TARGET_OS_VISION
+        [menu hideMenu];
+#else
         [menu setMenuVisible:NO animated:YES];
+#endif
     }
     if (_containerView.isFirstResponder) {
         _state.ignoreFirstResponder = YES;
@@ -733,7 +750,11 @@ typedef NS_ENUM(NSUInteger, YYTextMoveDirection) {
             if (CGRectGetMinY(inter) > CGRectGetMinY(bounds)) { // keyboard below self.top
                 
                 UIEdgeInsets originalContentInset = self.contentInset;
+#if TARGET_OS_VISION
+                UIEdgeInsets originalScrollIndicatorInsets = self.verticalScrollIndicatorInsets;
+#else
                 UIEdgeInsets originalScrollIndicatorInsets = self.scrollIndicatorInsets;
+#endif
                 if (_insetModifiedByKeyboard) {
                     originalContentInset = _originalContentInset;
                     originalScrollIndicatorInsets = _originalScrollIndicatorInsets;
@@ -744,7 +765,11 @@ typedef NS_ENUM(NSUInteger, YYTextMoveDirection) {
                     if (!_insetModifiedByKeyboard) {
                         _insetModifiedByKeyboard = YES;
                         _originalContentInset = self.contentInset;
+#if TARGET_OS_VISION
+                        _originalScrollIndicatorInsets = self.verticalScrollIndicatorInsets;
+#else
                         _originalScrollIndicatorInsets = self.scrollIndicatorInsets;
+#endif
                     }
                     UIEdgeInsets newInset = originalContentInset;
                     UIEdgeInsets newIndicatorInsets = originalScrollIndicatorInsets;
@@ -1575,6 +1600,9 @@ typedef NS_ENUM(NSUInteger, YYTextMoveDirection) {
 
 /// Returns the `root` view controller (returns nil if not found).
 - (UIViewController *)_getRootViewController {
+#if TARGET_OS_VISION
+    return self.yy_viewController;
+#else
     UIViewController *ctrl = nil;
     UIApplication *app = YYTextSharedApplication();
     if (!ctrl) ctrl = app.keyWindow.rootViewController;
@@ -1587,6 +1615,7 @@ typedef NS_ENUM(NSUInteger, YYTextMoveDirection) {
     }
     if (!ctrl.view.window) return nil;
     return ctrl;
+#endif
 }
 
 /// Clear the undo and redo stack, and capture current state to undo stack.
@@ -1677,6 +1706,9 @@ typedef NS_ENUM(NSUInteger, YYTextMoveDirection) {
 /// Show undo alert if it can undo or redo.
 #ifdef __IPHONE_OS_VERSION_MIN_REQUIRED
 - (void)_showUndoRedoAlert NS_EXTENSION_UNAVAILABLE_IOS(""){
+#if TARGET_OS_VISION
+    return;
+#else
     _state.firstResponderBeforeUndoAlert = self.isFirstResponder;
     __weak typeof(self) _self = self;
     NSArray *strings = [self _localizedUndoStrings];
@@ -1744,6 +1776,7 @@ typedef NS_ENUM(NSUInteger, YYTextMoveDirection) {
 #pragma clang diagnostic pop
         }
     }
+#endif
 }
 #endif
 
@@ -2169,7 +2202,7 @@ typedef NS_ENUM(NSUInteger, YYTextMoveDirection) {
     
     [_inputDelegate selectionWillChange:self];
     [_inputDelegate textWillChange:self];
-     _innerText = text;
+    _innerText = text;
     [self _parseText];
     _selectedTextRange = [YYTextRange rangeWithRange:NSMakeRange(0, _innerText.length)];
     [_inputDelegate textDidChange:self];
@@ -2741,7 +2774,7 @@ typedef NS_ENUM(NSUInteger, YYTextMoveDirection) {
 - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
     [self _endTouchTracking];
     [self _hideMenu];
-
+    
     if (!_state.swallowTouch) [super touchesCancelled:touches withEvent:event];
 }
 
@@ -3208,6 +3241,7 @@ typedef NS_ENUM(NSUInteger, YYTextMoveDirection) {
 
 #pragma mark - @protocol UIALertViewDelegate
 
+#if !TARGET_OS_VISION
 - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
     NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
     if (title.length == 0) return;
@@ -3219,6 +3253,7 @@ typedef NS_ENUM(NSUInteger, YYTextMoveDirection) {
     }
     [self _restoreFirstResponderAfterUndoAlert];
 }
+#endif
 
 #pragma mark - @protocol UIKeyInput
 
@@ -3455,12 +3490,16 @@ typedef NS_ENUM(NSUInteger, YYTextMoveDirection) {
     _lastTypeRange = _selectedTextRange.asRange;
 }
 
+#if TARGET_OS_VISION
+
+#else
 - (void)setBaseWritingDirection:(UITextWritingDirection)writingDirection forRange:(YYTextRange *)range {
     if (!range) return;
     range = [self _correctedTextRange:range];
     [_innerText yy_setBaseWritingDirection:(NSWritingDirection)writingDirection range:range.asRange];
     [self _commitUpdate];
 }
+#endif
 
 - (NSString *)textInRange:(YYTextRange *)range {
     range = [self _correctedTextRange:range];
@@ -3468,6 +3507,9 @@ typedef NS_ENUM(NSUInteger, YYTextMoveDirection) {
     return [_innerText.string substringWithRange:range.asRange];
 }
 
+#if TARGET_OS_VISION
+
+#else
 - (UITextWritingDirection)baseWritingDirectionForPosition:(YYTextPosition *)position inDirection:(UITextStorageDirection)direction {
     [self _updateIfNeeded];
     position = [self _correctedTextPosition:position];
@@ -3487,6 +3529,7 @@ typedef NS_ENUM(NSUInteger, YYTextMoveDirection) {
     
     return UITextWritingDirectionNatural;
 }
+#endif
 
 - (YYTextPosition *)beginningOfDocument {
     return [YYTextPosition positionWithOffset:0];