You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

67 lines
2.6 KiB

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