From 62ee1c8b95d3563c21197a5f5a03e278adf999c1 Mon Sep 17 00:00:00 2001 From: xster Date: Mon, 31 Jul 2017 15:12:32 -0700 Subject: [PATCH] Extend iOS's drawing time to viewWillAppear <-> viewDidDisappear (flutter/engine#3934) * fix (with squashed debug stuff) * clean up --- engine/src/flutter/lib/ui/window.dart | 14 +-- .../framework/Source/FlutterViewController.mm | 85 ++++++++++++------- 2 files changed, 61 insertions(+), 38 deletions(-) diff --git a/engine/src/flutter/lib/ui/window.dart b/engine/src/flutter/lib/ui/window.dart index e7b1ccee0d..04710f9970 100644 --- a/engine/src/flutter/lib/ui/window.dart +++ b/engine/src/flutter/lib/ui/window.dart @@ -43,10 +43,12 @@ enum AppLifecycleState { /// The application is in an inactive state and is not receiving user input. /// - /// On iOS, this state corresponds to an app running in the foreground - /// inactive state. Apps transition to this state when in a phone call, - /// responding to a TouchID request, or when entering the app switcher. Apps - /// in this state should assume that they may be [paused] at any time. + /// On iOS, this state corresponds to an app or the Flutter host view running + /// in the foreground inactive state. Apps transition to this state when in + /// a phone call, responding to a TouchID request, when entering the app + /// switcher or the control center, or when the UIViewController hosting the + /// Flutter app is transitioning. Apps in this state should assume that they + /// may be [paused] at any time. /// /// On Android, this state is currently unused. inactive, @@ -57,8 +59,8 @@ enum AppLifecycleState { /// When the application is in this state, the engine will not call the /// [Window.onBeginFrame] and [Window.onDrawFrame] callbacks. /// - /// Apps in this state should assume that they may enter the [suspending] - /// state at any time. + /// Android apps in this state should assume that they may enter the + /// [suspending] state at any time. paused, /// The application will be suspended momentarily. diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterViewController.mm b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterViewController.mm index 2102d68456..8fcb8a816a 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterViewController.mm +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterViewController.mm @@ -274,34 +274,80 @@ class PlatformMessageResponseDarwin : public blink::PlatformMessageResponse { [view release]; } +#pragma mark - Surface creation and teardown updates + +- (void)surfaceUpdated:(BOOL)appeared { + FTL_CHECK(_platformView != nullptr); + + if (appeared) { + _platformView->NotifyCreated(); + } else { + _platformView->NotifyDestroyed(); + } +} + #pragma mark - UIViewController lifecycle notifications - (void)viewWillAppear:(BOOL)animated { + TRACE_EVENT0("flutter", "viewWillAppear"); [self connectToEngineAndLoad]; + // Only recreate surface on subsequent appearances when viewport metrics are known. + // First time surface creation is done on viewDidLayoutSubviews. + if (_viewportMetrics.physical_width) + [self surfaceUpdated:YES]; + [_lifecycleChannel.get() sendMessage:@"AppLifecycleState.inactive"]; + [super viewWillAppear:animated]; } +- (void)viewDidAppear:(BOOL)animated { + TRACE_EVENT0("flutter", "viewDidAppear"); + [self onLocaleUpdated:nil]; + [self onVoiceOverChanged:nil]; + [_lifecycleChannel.get() sendMessage:@"AppLifecycleState.resumed"]; + + [super viewDidAppear:animated]; +} + +- (void)viewWillDisappear:(BOOL)animated { + TRACE_EVENT0("flutter", "viewWillDisappear"); + [_lifecycleChannel.get() sendMessage:@"AppLifecycleState.inactive"]; + + [super viewWillDisappear:animated]; +} + - (void)viewDidDisappear:(BOOL)animated { + TRACE_EVENT0("flutter", "viewDidDisappear"); + [self surfaceUpdated:NO]; [_lifecycleChannel.get() sendMessage:@"AppLifecycleState.paused"]; [super viewDidDisappear:animated]; } +- (void)dealloc { + [[NSNotificationCenter defaultCenter] removeObserver:self]; + [super dealloc]; +} + #pragma mark - Application lifecycle notifications - (void)applicationBecameActive:(NSNotification*)notification { + TRACE_EVENT0("flutter", "applicationBecameActive"); [_lifecycleChannel.get() sendMessage:@"AppLifecycleState.resumed"]; } - (void)applicationWillResignActive:(NSNotification*)notification { + TRACE_EVENT0("flutter", "applicationWillResignActive"); [_lifecycleChannel.get() sendMessage:@"AppLifecycleState.inactive"]; } - (void)applicationDidEnterBackground:(NSNotification*)notification { + TRACE_EVENT0("flutter", "applicationDidEnterBackground"); [_lifecycleChannel.get() sendMessage:@"AppLifecycleState.paused"]; } - (void)applicationWillEnterForeground:(NSNotification*)notification { + TRACE_EVENT0("flutter", "applicationWillEnterForeground"); [_lifecycleChannel.get() sendMessage:@"AppLifecycleState.inactive"]; } @@ -426,11 +472,18 @@ static inline PointerChangeMapperPhase PointerChangePhaseFromUITouchPhase(UITouc CGSize viewSize = self.view.bounds.size; CGFloat scale = [UIScreen mainScreen].scale; + // First time since creation that the dimensions of its view is known. + bool firstViewBoundsUpdate = !_viewportMetrics.physical_width; _viewportMetrics.device_pixel_ratio = scale; _viewportMetrics.physical_width = viewSize.width * scale; _viewportMetrics.physical_height = viewSize.height * scale; _viewportMetrics.physical_padding_top = [self statusBarPadding] * scale; [self updateViewportMetrics]; + + // This must run after updateViewportMetrics so that the surface creation tasks are queued after + // the viewport metrics update tasks. + if (firstViewBoundsUpdate) + [self surfaceUpdated:YES]; } #pragma mark - Keyboard events @@ -526,38 +579,6 @@ static inline PointerChangeMapperPhase PointerChangePhaseFromUITouchPhase(UITouc [_localizationChannel.get() invokeMethod:@"setLocale" arguments:@[ languageCode, countryCode ]]; } -#pragma mark - Surface creation and teardown updates - -- (void)surfaceUpdated:(BOOL)appeared { - FTL_CHECK(_platformView != nullptr); - - if (appeared) { - _platformView->NotifyCreated(); - } else { - _platformView->NotifyDestroyed(); - } -} - -- (void)viewDidAppear:(BOOL)animated { - [self surfaceUpdated:YES]; - [self onLocaleUpdated:nil]; - [self onVoiceOverChanged:nil]; - [_lifecycleChannel.get() sendMessage:@"AppLifecycleState.resumed"]; - - [super viewDidAppear:animated]; -} - -- (void)viewWillDisappear:(BOOL)animated { - [self surfaceUpdated:NO]; - - [super viewWillDisappear:animated]; -} - -- (void)dealloc { - [[NSNotificationCenter defaultCenter] removeObserver:self]; - [super dealloc]; -} - #pragma mark - Status Bar touch event handling // Standard iOS status bar height in pixels.