From c354127d4dfd2b960a6fa7ca173d65cb005801fa Mon Sep 17 00:00:00 2001 From: Chris Bracken Date: Thu, 2 Feb 2017 16:29:22 -0800 Subject: [PATCH] Support status bar taps in FlutterAppDelegate (#3387) Provide a default status bar tap handler for FlutterAppDelegate. By default, taps are passed to the key window's rootViewController if it's a FlutterViewController. Apps with custom app delegates can customize this behaviour to send the tap to the FlutterViewController(s) they prefer by overriding touchesBegan:withEvent. --- .../framework/Headers/FlutterAppDelegate.h | 14 ++++++++++--- .../framework/Source/FlutterAppDelegate.mm | 21 +++++++++++++++++++ 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/shell/platform/darwin/ios/framework/Headers/FlutterAppDelegate.h b/shell/platform/darwin/ios/framework/Headers/FlutterAppDelegate.h index ef7a4e2315a..967c55745b7 100644 --- a/shell/platform/darwin/ios/framework/Headers/FlutterAppDelegate.h +++ b/shell/platform/darwin/ios/framework/Headers/FlutterAppDelegate.h @@ -9,10 +9,18 @@ #include "FlutterMacros.h" -// Empty implementation of UIApplicationDelegate, for simple apps -// that don't need to customize the application delegate. +/** + * UIApplicationDelegate subclass for simple apps that want default behavior. + * + * This class provides the following behaviors: + * * Status bar touches are forwarded to the key window's root view + * FlutterViewController, in order to trigger scroll to top. + * + * App delegates for Flutter applications are *not* required to inherit from + * this class. Developers of custom app delegate classes should copy and paste + * code as necessary from FlutterAppDelegate.mm. + */ FLUTTER_EXPORT -__attribute__((deprecated("subclass UIResponder"))) @interface FlutterAppDelegate : UIResponder @property(strong, nonatomic) UIWindow* window; diff --git a/shell/platform/darwin/ios/framework/Source/FlutterAppDelegate.mm b/shell/platform/darwin/ios/framework/Source/FlutterAppDelegate.mm index b69ee926376..6af8ce94216 100644 --- a/shell/platform/darwin/ios/framework/Source/FlutterAppDelegate.mm +++ b/shell/platform/darwin/ios/framework/Source/FlutterAppDelegate.mm @@ -3,7 +3,28 @@ // found in the LICENSE file. #include "flutter/shell/platform/darwin/ios/framework/Headers/FlutterAppDelegate.h" +#include "flutter/shell/platform/darwin/ios/framework/Headers/FlutterViewController.h" @implementation FlutterAppDelegate +// Returns the key window's rootViewController, if it's a FlutterViewController. +// Otherwise, returns nil. +- (FlutterViewController*)rootFlutterViewController { + UIViewController *viewController = + [UIApplication sharedApplication].keyWindow.rootViewController; + if ([viewController isKindOfClass:[FlutterViewController class]]) { + return (FlutterViewController*)viewController; + } + return nil; +} + +- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { + [super touchesBegan:touches withEvent:event]; + + // Pass status bar taps to key window Flutter rootViewController. + if (self.rootFlutterViewController != nil) { + [self.rootFlutterViewController handleStatusBarTouches:event]; + } +} + @end