FLEXWindow.m 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. //
  2. // FLEXWindow.m
  3. // Flipboard
  4. //
  5. // Created by Ryan Olson on 4/13/14.
  6. // Copyright (c) 2020 Flipboard. 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. self.windowLevel = UIWindowLevelStatusBar + 100.0;
  20. }
  21. return self;
  22. }
  23. - (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {
  24. BOOL pointInside = NO;
  25. if ([self.eventDelegate shouldHandleTouchAtPoint:point]) {
  26. pointInside = [super pointInside:point withEvent:event];
  27. }
  28. return pointInside;
  29. }
  30. - (BOOL)shouldAffectStatusBarAppearance {
  31. return [self isKeyWindow];
  32. }
  33. - (BOOL)canBecomeKeyWindow {
  34. return [self.eventDelegate canBecomeKeyWindow];
  35. }
  36. - (void)makeKeyWindow {
  37. _previousKeyWindow = FLEXUtility.appKeyWindow;
  38. [super makeKeyWindow];
  39. }
  40. - (void)resignKeyWindow {
  41. [super resignKeyWindow];
  42. _previousKeyWindow = nil;
  43. }
  44. + (void)initialize {
  45. // This adds a method (superclass override) at runtime which gives us the status bar behavior we want.
  46. // The FLEX window is intended to be an overlay that generally doesn't affect the app underneath.
  47. // Most of the time, we want the app's main window(s) to be in control of status bar behavior.
  48. // Done at runtime with an obfuscated selector because it is private API. But you shouldn't ship this to the App Store anyways...
  49. NSString *canAffectSelectorString = [@[@"_can", @"Affect", @"Status", @"Bar", @"Appearance"] componentsJoinedByString:@""];
  50. SEL canAffectSelector = NSSelectorFromString(canAffectSelectorString);
  51. Method shouldAffectMethod = class_getInstanceMethod(self, @selector(shouldAffectStatusBarAppearance));
  52. IMP canAffectImplementation = method_getImplementation(shouldAffectMethod);
  53. class_addMethod(self, canAffectSelector, canAffectImplementation, method_getTypeEncoding(shouldAffectMethod));
  54. // One more...
  55. NSString *canBecomeKeySelectorString = [NSString stringWithFormat:@"_%@", NSStringFromSelector(@selector(canBecomeKeyWindow))];
  56. SEL canBecomeKeySelector = NSSelectorFromString(canBecomeKeySelectorString);
  57. Method canBecomeKeyMethod = class_getInstanceMethod(self, @selector(canBecomeKeyWindow));
  58. IMP canBecomeKeyImplementation = method_getImplementation(canBecomeKeyMethod);
  59. class_addMethod(self, canBecomeKeySelector, canBecomeKeyImplementation, method_getTypeEncoding(canBecomeKeyMethod));
  60. }
  61. @end