Flutter macOS Embedder
FlutterAppDelegate.mm
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
7 
8 #import <AppKit/AppKit.h>
9 
10 #include "flutter/fml/logging.h"
13 #include "flutter/shell/platform/embedder/embedder.h"
14 
15 @interface FlutterAppDelegate ()
16 
17 /**
18  * Returns the display name of the application as set in the Info.plist.
19  */
20 - (NSString*)applicationName;
21 
22 @property(nonatomic) FlutterAppLifecycleRegistrar* lifecycleRegistrar;
23 @end
24 
25 @implementation FlutterAppDelegate
26 
27 - (instancetype)init {
28  if (self = [super init]) {
29  _terminationHandler = nil;
30  _lifecycleRegistrar = [[FlutterAppLifecycleRegistrar alloc] init];
31  }
32  return self;
33 }
34 
35 - (void)applicationWillFinishLaunching:(NSNotification*)notification {
36  // Update UI elements to match the application name.
37  NSString* applicationName = [self applicationName];
38  _mainFlutterWindow.title = applicationName;
39  for (NSMenuItem* menuItem in _applicationMenu.itemArray) {
40  menuItem.title = [menuItem.title stringByReplacingOccurrencesOfString:@"APP_NAME"
41  withString:applicationName];
42  }
43 }
44 
45 #pragma mark - Delegate handling
46 
47 - (void)addApplicationLifecycleDelegate:(NSObject<FlutterAppLifecycleDelegate>*)delegate {
48  [self.lifecycleRegistrar addDelegate:delegate];
49 }
50 
51 - (void)removeApplicationLifecycleDelegate:(NSObject<FlutterAppLifecycleDelegate>*)delegate {
52  [self.lifecycleRegistrar removeDelegate:delegate];
53 }
54 
55 #pragma mark Private Methods
56 
57 - (NSString*)applicationName {
58  NSString* applicationName =
59  [NSBundle.mainBundle objectForInfoDictionaryKey:@"CFBundleDisplayName"];
60  if (!applicationName) {
61  applicationName = [NSBundle.mainBundle objectForInfoDictionaryKey:@"CFBundleName"];
62  }
63  return applicationName;
64 }
65 
66 #pragma mark NSApplicationDelegate
67 
68 - (void)application:(NSApplication*)application openURLs:(NSArray<NSURL*>*)urls {
69  for (NSObject<FlutterAppLifecycleDelegate>* delegate in self.lifecycleRegistrar.delegates) {
70  if ([delegate respondsToSelector:@selector(handleOpenURLs:)] &&
71  [delegate handleOpenURLs:urls]) {
72  return;
73  }
74  }
75 }
76 
77 - (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication* _Nonnull)sender {
78  // If the framework has already told us to terminate, terminate immediately.
79  if ([self terminationHandler] == nil || [[self terminationHandler] shouldTerminate]) {
80  return NSTerminateNow;
81  }
82 
83  // Send a termination request to the framework.
84  FlutterEngineTerminationHandler* terminationHandler = [self terminationHandler];
85  [terminationHandler requestApplicationTermination:sender
86  exitType:kFlutterAppExitTypeCancelable
87  result:nil];
88 
89  // Cancel termination to allow the framework to handle the request asynchronously. When the
90  // termination request returns from the app, if termination is desired, this method will be
91  // reinvoked with self.terminationHandler.shouldTerminate set to YES.
92  return NSTerminateCancel;
93 }
94 
95 @end
FlutterAppDelegate_Internal.h
FlutterAppLifecycleRegistrar
Definition: FlutterAppLifecycleDelegate.h:126
FlutterAppLifecycleDelegate-p
Definition: FlutterAppLifecycleDelegate.h:21
-[FlutterAppLifecycleDelegate-p handleOpenURLs:]
BOOL handleOpenURLs:(NSArray< NSURL * > *urls)
FlutterAppDelegate
Definition: FlutterAppDelegate.h:54
FlutterAppDelegate.h
FlutterAppLifecycleDelegate_Internal.h
-[FlutterEngineTerminationHandler requestApplicationTermination:exitType:result:]
void requestApplicationTermination:exitType:result:(NSApplication *sender,[exitType] FlutterAppExitType type,[result] nullable FlutterResult result)
FlutterEngineTerminationHandler
Definition: FlutterEngine.mm:188
FlutterAppLifecycleDelegate.h