FLEXWindow.m 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. //
  2. // FLEXWindow.m
  3. // Flipboard
  4. //
  5. // Created by Ryan Olson on 4/13/14.
  6. // Copyright (c) 2020 FLEX Team. All rights reserved.
  7. //
  8. #import "FLEXWindow.h"
  9. #import "FLEXUtility.h"
  10. #import <objc/runtime.h>
  11. @implementation FLEXWindow
  12. - (id)initWithFrame:(CGRect)frame {
  13. self = [super initWithFrame:frame];
  14. if (self) {
  15. // Some apps have windows at UIWindowLevelStatusBar + n.
  16. // If we make the window level too high, we block out UIAlertViews.
  17. // There's a balance between staying above the app's windows and staying below alerts.
  18. // UIWindowLevelStatusBar + 100 seems to hit that balance.
  19. #if !TARGET_OS_TV
  20. self.windowLevel = UIWindowLevelStatusBar + 100.0;
  21. #endif
  22. }
  23. return self;
  24. }
  25. - (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {
  26. BOOL pointInside = NO;
  27. if ([self.eventDelegate shouldHandleTouchAtPoint:point]) {
  28. pointInside = [super pointInside:point withEvent:event];
  29. }
  30. return pointInside;
  31. }
  32. - (BOOL)shouldAffectStatusBarAppearance {
  33. return [self isKeyWindow];
  34. }
  35. - (BOOL)canBecomeKeyWindow {
  36. return [self.eventDelegate canBecomeKeyWindow];
  37. }
  38. - (void)makeKeyWindow {
  39. _previousKeyWindow = FLEXUtility.appKeyWindow;
  40. [super makeKeyWindow];
  41. }
  42. - (void)resignKeyWindow {
  43. [super resignKeyWindow];
  44. _previousKeyWindow = nil;
  45. }
  46. + (void)initialize {
  47. // This adds a method (superclass override) at runtime which gives us the status bar behavior we want.
  48. // The FLEX window is intended to be an overlay that generally doesn't affect the app underneath.
  49. // Most of the time, we want the app's main window(s) to be in control of status bar behavior.
  50. // Done at runtime with an obfuscated selector because it is private API. But you shouldn't ship this to the App Store anyways...
  51. NSString *canAffectSelectorString = [@[@"_can", @"Affect", @"Status", @"Bar", @"Appearance"] componentsJoinedByString:@""];
  52. SEL canAffectSelector = NSSelectorFromString(canAffectSelectorString);
  53. Method shouldAffectMethod = class_getInstanceMethod(self, @selector(shouldAffectStatusBarAppearance));
  54. IMP canAffectImplementation = method_getImplementation(shouldAffectMethod);
  55. class_addMethod(self, canAffectSelector, canAffectImplementation, method_getTypeEncoding(shouldAffectMethod));
  56. // One more...
  57. NSString *canBecomeKeySelectorString = [NSString stringWithFormat:@"_%@", NSStringFromSelector(@selector(canBecomeKeyWindow))];
  58. SEL canBecomeKeySelector = NSSelectorFromString(canBecomeKeySelectorString);
  59. Method canBecomeKeyMethod = class_getInstanceMethod(self, @selector(canBecomeKeyWindow));
  60. IMP canBecomeKeyImplementation = method_getImplementation(canBecomeKeyMethod);
  61. class_addMethod(self, canBecomeKeySelector, canBecomeKeyImplementation, method_getTypeEncoding(canBecomeKeyMethod));
  62. }
  63. @end