浏览代码

Use YY_TARGET_IS_EXTENSION to support App Extension target

ibireme 9 年之前
父节点
当前提交
e405e748cb
共有 3 个文件被更改,包括 24 次插入2 次删除
  1. 8 1
      YYText/Component/YYTextEffectWindow.m
  2. 12 1
      YYText/Component/YYTextKeyboardManager.m
  3. 4 0
      YYText/YYTextView.m

+ 8 - 1
YYText/Component/YYTextEffectWindow.m

@@ -21,6 +21,7 @@
     static YYTextEffectWindow *one;
     static dispatch_once_t onceToken;
     dispatch_once(&onceToken, ^{
+#ifndef YY_TARGET_IS_EXTENSION
         one = [self new];
         one.frame = (CGRect){.size = YYTextScreenSize()};
         one.userInteractionEnabled = NO;
@@ -31,17 +32,20 @@
         one.opaque = NO;
         one.backgroundColor = [UIColor clearColor];
         one.layer.backgroundColor = [UIColor clearColor].CGColor;
+#endif
     });
     return one;
 }
 
 // Bring self to front
 - (void)_updateWindowLevel {
+#ifndef YY_TARGET_IS_EXTENSION
     UIWindow *top = [UIApplication sharedApplication].windows.lastObject;
     UIWindow *key = [UIApplication sharedApplication].keyWindow;
     if (key && key.windowLevel > top.windowLevel) top = key;
     if (top == self) return;
     self.windowLevel = top.windowLevel + 1;
+#endif
 }
 
 - (YYTextDirection)_keyboardDirection {
@@ -188,7 +192,7 @@
  @return Magnifier rotation radius.
  */
 - (CGFloat)_updateMagnifier:(YYTextMagnifier *)mag {
-    
+#ifndef YY_TARGET_IS_EXTENSION
     UIView *hostView = mag.hostView;
     UIWindow *hostWindow = [hostView isKindOfClass:[UIWindow class]] ? (id)hostView : hostView.window;
     if (!hostView || !hostWindow) return 0;
@@ -259,6 +263,9 @@
     mag.snapshot = image;
     mag.captureFadeAnimation = NO;
     return rotation;
+#else
+    return 0;
+#endif
 }
 
 - (void)showMagnifier:(YYTextMagnifier *)mag {

+ 12 - 1
YYText/Component/YYTextKeyboardManager.m

@@ -146,9 +146,11 @@ static int _YYTextKeyboardViewFrameObserverKey;
 }
 
 + (void)load {
+#ifndef YY_TARGET_IS_EXTENSION
     dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
         [self defaultManager];
     });
+#endif
 }
 
 - (void)addObserver:(id<YYTextKeyboardObserver>)observer {
@@ -162,6 +164,7 @@ static int _YYTextKeyboardViewFrameObserverKey;
 }
 
 - (UIWindow *)keyboardWindow {
+#ifndef YY_TARGET_IS_EXTENSION
     UIWindow *window = nil;
     for (window in [UIApplication sharedApplication].windows) {
         if ([self _getKeyboardViewFromWindow:window]) return window;
@@ -194,11 +197,12 @@ static int _YYTextKeyboardViewFrameObserverKey;
     if (kbWindows.count == 1) {
         return kbWindows.firstObject;
     }
-    
+#endif
     return nil;
 }
 
 - (UIView *)keyboardView {
+#ifndef YY_TARGET_IS_EXTENSION
     UIWindow *window = nil;
     UIView *view = nil;
     for (window in [UIApplication sharedApplication].windows) {
@@ -208,6 +212,7 @@ static int _YYTextKeyboardViewFrameObserverKey;
     window = [UIApplication sharedApplication].keyWindow;
     view = [self _getKeyboardViewFromWindow:window];
     if (view) return view;
+#endif
     return nil;
 }
 
@@ -362,6 +367,7 @@ static int _YYTextKeyboardViewFrameObserverKey;
 }
 
 - (void)_notifyAllObservers {
+#ifndef YY_TARGET_IS_EXTENSION
     UIView *keyboard = self.keyboardView;
     UIWindow *window = keyboard.window;
     if (!window) {
@@ -443,9 +449,11 @@ static int _YYTextKeyboardViewFrameObserverKey;
     _fromFrame = trans.toFrame;
     _fromVisible = trans.toVisible;
     _fromOrientation = [UIApplication sharedApplication].statusBarOrientation;
+#endif
 }
 
 - (CGRect)convertRect:(CGRect)rect toView:(UIView *)view {
+#ifndef YY_TARGET_IS_EXTENSION
     if (CGRectIsNull(rect)) return rect;
     if (CGRectIsInfinite(rect)) return rect;
     
@@ -472,6 +480,9 @@ static int _YYTextKeyboardViewFrameObserverKey;
     rect = [toWindow convertRect:rect fromWindow:mainWindow];
     rect = [view convertRect:rect fromView:toWindow];
     return rect;
+#else
+    return CGRectZero;
+#endif
 }
 
 @end

+ 4 - 0
YYText/YYTextView.m

@@ -1561,8 +1561,10 @@ typedef NS_ENUM(NSUInteger, YYTextMoveDirection) {
 /// Returns the `root` view controller (returns nil if not found).
 - (UIViewController *)_getRootViewController {
     UIViewController *ctrl = nil;
+#ifndef YY_TARGET_IS_EXTENSION
     if (!ctrl) ctrl = [UIApplication sharedApplication].keyWindow.rootViewController;
     if (!ctrl) ctrl = [[UIApplication sharedApplication].windows.firstObject rootViewController];
+#endif
     if (!ctrl) ctrl = self.yy_viewController;
     if (!ctrl) return nil;
     
@@ -1660,6 +1662,7 @@ typedef NS_ENUM(NSUInteger, YYTextMoveDirection) {
 
 /// Show undo alert if it can undo or redo.
 - (void)_showUndoAlert {
+#ifndef YY_TARGET_IS_EXTENSION
     _state.firstResponderBeforeUndoAlert = self.isFirstResponder;
     __weak typeof(self) _self = self;
     NSArray *strings = [self _localizedUndoStrings];
@@ -1727,6 +1730,7 @@ typedef NS_ENUM(NSUInteger, YYTextMoveDirection) {
 #pragma clang diagnostic pop
         }
     }
+#endif
 }
 
 /// Get the localized undo alert strings based on app's main bundle.