1/* 2* Copyright 2017 Google Inc. 3* 4* Use of this source code is governed by a BSD-style license that can be 5* found in the LICENSE file. 6*/ 7 8#include "include/core/SkTypes.h" 9#include "tools/sk_app/Application.h" 10#include "tools/sk_app/ios/Window_ios.h" 11 12#import <QuartzCore/QuartzCore.h> 13#import <UIKit/UIKit.h> 14 15#if __has_feature(objc_arc) 16#error "File should not be compiled with ARC." 17#endif 18 19namespace { 20 21static int gArgc; 22static char** gArgv; 23 24} 25 26//////////////////////////////////////////////////////////////////// 27 28@interface AppDelegate : UIResponder<UIApplicationDelegate> 29 30@end 31 32@implementation AppDelegate { 33 CADisplayLink* fDisplayLink; // Owned by the run loop. 34 std::unique_ptr<sk_app::Application> fApp; 35} 36 37#pragma mark - UIApplicationDelegate 38 39- (void)applicationWillResignActive:(UIApplication *)sender { 40 sk_app::Window_ios* mainWindow = sk_app::Window_ios::MainWindow(); 41 if (mainWindow) { 42 mainWindow->onActivate(false); 43 } 44} 45 46- (void)applicationDidBecomeActive:(UIApplication *)sender { 47 sk_app::Window_ios* mainWindow = sk_app::Window_ios::MainWindow(); 48 if (mainWindow) { 49 mainWindow->onActivate(true); 50 } 51} 52 53- (void)applicationWillTerminate:(UIApplication *)sender { 54 // Display link retains us, so we break the cycle now. 55 // Note: dealloc is never called. 56 [fDisplayLink invalidate]; 57 fDisplayLink = nil; 58 fApp.reset(); 59} 60 61- (BOOL)application:(UIApplication *)application 62 didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 63 fApp = std::unique_ptr<sk_app::Application>(sk_app::Application::Create(gArgc, gArgv, nullptr)); 64 65 auto mainWindow = sk_app::Window_ios::MainWindow(); 66 mainWindow->onActivate(application.applicationState == UIApplicationStateActive); 67 68 fDisplayLink = [CADisplayLink displayLinkWithTarget:self 69 selector:@selector(displayLinkFired)]; 70 [fDisplayLink addToRunLoop:NSRunLoop.mainRunLoop forMode:NSRunLoopCommonModes]; 71 72 return YES; 73} 74 75- (void)displayLinkFired { 76 // TODO: Hook into CAMetalLayer's drawing event loop or our own run loop observer. 77 // Need to handle animated slides/redraw mode, so we need something that will wake up the 78 // run loop. 79 sk_app::Window_ios::PaintWindow(); 80 81 fApp->onIdle(); 82} 83 84@end 85 86/////////////////////////////////////////////////////////////////// 87 88int main(int argc, char **argv) { 89 gArgc = argc; 90 gArgv = argv; 91 return UIApplicationMain(argc, argv, nil, @"AppDelegate"); 92} 93