18#import "flutter/shell/platform/darwin/common/InternalFlutterSwiftCommon/InternalFlutterSwiftCommon.h"
20#import "flutter/shell/platform/darwin/macos/InternalFlutterSwift/InternalFlutterSwift.h"
36#import <CoreVideo/CoreVideo.h>
37#import <IOSurface/IOSurface.h>
54 flutterLocale.
language_code = [[locale objectForKey:NSLocaleLanguageCode] UTF8String];
55 flutterLocale.
country_code = [[locale objectForKey:NSLocaleCountryCode] UTF8String];
56 flutterLocale.
script_code = [[locale objectForKey:NSLocaleScriptCode] UTF8String];
57 flutterLocale.
variant_code = [[locale objectForKey:NSLocaleVariantCode] UTF8String];
63 @"NSApplicationDidChangeAccessibilityEnhancedUserInterfaceNotification";
75- (instancetype)initWithConnection:(NSNumber*)connection
84- (instancetype)initWithConnection:(NSNumber*)connection
87 NSAssert(
self,
@"Super init cannot be nil");
100 FlutterMouseCursorPluginDelegate,
101 FlutterKeyboardManagerDelegate,
102 FlutterTextInputPluginDelegate>
109@property(nonatomic, strong) NSMutableArray<NSNumber*>* isResponseValid;
114@property(nonatomic, strong) NSPointerArray* pluginAppDelegates;
119@property(nonatomic, readonly)
120 NSMutableDictionary<NSString*, FlutterEngineRegistrar*>* pluginRegistrars;
147- (void)shutDownIfNeeded;
152- (void)sendUserLocales;
163- (void)postMainThreadTask:(
FlutterTask)task targetTimeInNanoseconds:(uint64_t)targetTime;
169- (void)loadAOTData:(NSString*)assetsDir;
174- (void)setUpPlatformViewChannel;
179- (void)setUpAccessibilityChannel;
198 _acceptingRequests = NO;
200 _terminator = terminator ? terminator : ^(
id sender) {
203 [[NSApplication sharedApplication] terminate:sender];
205 id<NSApplicationDelegate> appDelegate = [[NSApplication sharedApplication] delegate];
206 if ([appDelegate respondsToSelector:@selector(setTerminationHandler:)]) {
208 flutterAppDelegate.terminationHandler =
self;
215- (void)handleRequestAppExitMethodCall:(NSDictionary<NSString*,
id>*)arguments
217 NSString*
type = arguments[@"type"];
223 FlutterAppExitType exitType =
224 [type isEqualTo:@"cancelable"] ? kFlutterAppExitTypeCancelable : kFlutterAppExitTypeRequired;
226 [
self requestApplicationTermination:[NSApplication sharedApplication]
233- (void)requestApplicationTermination:(
id)sender
234 exitType:(FlutterAppExitType)type
236 _shouldTerminate = YES;
237 if (![
self acceptingRequests]) {
240 type = kFlutterAppExitTypeRequired;
243 case kFlutterAppExitTypeCancelable: {
247 [_engine sendOnChannel:kFlutterPlatformChannel
248 message:[codec encodeMethodCall:methodCall]
249 binaryReply:^(NSData* _Nullable reply) {
250 NSAssert(_terminator, @"terminator shouldn't be nil");
251 id decoded_reply = [codec decodeEnvelope:reply];
252 if ([decoded_reply isKindOfClass:[
FlutterError class]]) {
254 NSLog(@"Method call returned error[%@]: %@ %@", [error code], [error message],
259 if (![decoded_reply isKindOfClass:[NSDictionary class]]) {
260 NSLog(@"Call to System.requestAppExit returned an unexpected object: %@",
265 NSDictionary* replyArgs = (NSDictionary*)decoded_reply;
266 if ([replyArgs[@"response"] isEqual:@"exit"]) {
268 } else if ([replyArgs[@"response"] isEqual:@"cancel"]) {
269 _shouldTerminate = NO;
277 case kFlutterAppExitTypeRequired:
278 NSAssert(
_terminator,
@"terminator shouldn't be nil");
291 return [[NSPasteboard generalPasteboard] clearContents];
294- (NSString*)stringForType:(NSPasteboardType)dataType {
295 return [[NSPasteboard generalPasteboard] stringForType:dataType];
298- (
BOOL)setString:(nonnull NSString*)string forType:(nonnull NSPasteboardType)dataType {
299 return [[NSPasteboard generalPasteboard] setString:string forType:dataType];
310- (instancetype)initWithPlugin:(nonnull NSString*)pluginKey
324 NSString* _pluginKey;
330- (instancetype)initWithPlugin:(NSString*)pluginKey flutterEngine:(
FlutterEngine*)flutterEngine {
333 _pluginKey = [pluginKey copy];
335 _publishedValue = [NSNull null];
340#pragma mark - FlutterPluginRegistrar
342- (
id<FlutterBinaryMessenger>)messenger {
346- (
id<FlutterTextureRegistry>)textures {
351 return [
self viewForIdentifier:kFlutterImplicitViewId];
356 if (controller == nil) {
359 if (!controller.viewLoaded) {
360 [controller loadView];
362 return controller.flutterView;
366 return [_flutterEngine viewControllerForIdentifier:kFlutterImplicitViewId];
369- (void)addMethodCallDelegate:(nonnull
id<FlutterPlugin>)delegate
372 [delegate handleMethodCall:call result:result];
376- (void)addApplicationDelegate:(NSObject<FlutterAppLifecycleDelegate>*)delegate {
377 id<NSApplicationDelegate> appDelegate = [[NSApplication sharedApplication] delegate];
378 if ([appDelegate conformsToProtocol:@protocol(FlutterAppLifecycleProvider)]) {
379 id<FlutterAppLifecycleProvider> lifeCycleProvider =
380 static_cast<id<FlutterAppLifecycleProvider>
>(appDelegate);
381 [lifeCycleProvider addApplicationLifecycleDelegate:delegate];
382 [_flutterEngine.pluginAppDelegates addPointer:(__bridge void*)delegate];
387 withId:(nonnull NSString*)factoryId {
388 [[_flutterEngine platformViewController] registerViewFactory:factory withId:factoryId];
391- (void)publish:(NSObject*)value {
392 _publishedValue =
value;
395- (NSString*)lookupKeyForAsset:(NSString*)asset {
399- (NSString*)lookupKeyForAsset:(NSString*)asset fromPackage:(NSString*)package {
406#pragma mark - Static methods provided to engine configuration
410 [engine engineCallbackOnPlatformMessage:message];
506- (instancetype)initWithName:(NSString*)labelPrefix project:(
FlutterDartProject*)project {
507 return [
self initWithName:labelPrefix project:project allowHeadlessExecution:YES];
514 pthread_t thread = pthread_self();
517 if (!pthread_getschedparam(thread, &policy, ¶m)) {
519 pthread_setschedparam(thread, policy, ¶m);
521 pthread_set_qos_class_self_np(QOS_CLASS_USER_INTERACTIVE, 0);
525- (instancetype)initWithName:(NSString*)labelPrefix
527 allowHeadlessExecution:(
BOOL)allowHeadlessExecution {
529 NSAssert(
self,
@"Super init cannot be nil");
531 [FlutterRunLoop ensureMainLoopInitialized];
538 _pluginAppDelegates = [NSPointerArray weakObjectsPointerArray];
539 _pluginRegistrars = [[NSMutableDictionary alloc] init];
542 _semanticsEnabled = NO;
544 _isResponseValid = [[NSMutableArray alloc] initWithCapacity:1];
545 [_isResponseValid addObject:@YES];
557 NSNotificationCenter* notificationCenter = [NSNotificationCenter defaultCenter];
558 [notificationCenter addObserver:self
559 selector:@selector(sendUserLocales)
560 name:NSCurrentLocaleDidChangeNotification
570 [
self setUpPlatformViewChannel];
575 [
self setUpAccessibilityChannel];
576 [
self setUpNotificationCenterListeners];
577 id<NSApplicationDelegate> appDelegate = [[NSApplication sharedApplication] delegate];
578 if ([appDelegate conformsToProtocol:@protocol(FlutterAppLifecycleProvider)]) {
581 id<FlutterAppLifecycleProvider> lifecycleProvider =
582 static_cast<id<FlutterAppLifecycleProvider>
>(appDelegate);
583 [lifecycleProvider addApplicationLifecycleDelegate:self];
585 _terminationHandler = nil;
594 id<NSApplicationDelegate> appDelegate = [[NSApplication sharedApplication] delegate];
595 if ([appDelegate conformsToProtocol:@protocol(FlutterAppLifecycleProvider)]) {
596 id<FlutterAppLifecycleProvider> lifecycleProvider =
597 static_cast<id<FlutterAppLifecycleProvider>
>(appDelegate);
598 [lifecycleProvider removeApplicationLifecycleDelegate:self];
603 for (id<FlutterAppLifecycleDelegate> delegate in _pluginAppDelegates) {
605 [lifecycleProvider removeApplicationLifecycleDelegate:delegate];
611 for (NSString* pluginName in _pluginRegistrars) {
612 [_pluginRegistrars[pluginName] publish:[NSNull null]];
614 @
synchronized(_isResponseValid) {
615 [_isResponseValid removeAllObjects];
616 [_isResponseValid addObject:@NO];
618 [
self shutDownEngine];
620 _embedderAPI.CollectAOTData(
_aotData);
625 static size_t sTaskRunnerIdentifiers = 0;
630 .runs_task_on_current_thread_callback = [](
void*
user_data) ->
bool {
631 return [[NSThread currentThread] isMainThread];
633 .post_task_callback = [](
FlutterTask task, uint64_t target_time_nanos,
636 [engine postMainThreadTask:task targetTimeInNanoseconds:target_time_nanos];
638 .identifier = ++sTaskRunnerIdentifiers,
639 .destruction_callback =
646 return cocoa_task_runner_description;
651 if (controller == nil) {
655 [controller.flutterView.window makeFirstResponder:controller.flutterView];
659- (
BOOL)runWithEntrypoint:(NSString*)entrypoint {
665 NSLog(
@"Attempted to run an engine with no view controller without headless mode enabled.");
669 [
self addInternalPlugins];
672 std::vector<const char*>
argv = {[
self.executableName UTF8String]};
673 std::vector<std::string> switches =
self.switches;
677 std::find(switches.begin(), switches.end(),
"--enable-impeller=true") != switches.end()) {
678 switches.push_back(
"--enable-impeller=true");
682 std::find(switches.begin(), switches.end(),
"--impeller-use-sdfs=true") != switches.end()) {
683 switches.push_back(
"--impeller-use-sdfs=true");
687 std::find(switches.begin(), switches.end(),
"--enable-flutter-gpu=true") != switches.end()) {
688 switches.push_back(
"--enable-flutter-gpu=true");
691 std::transform(switches.begin(), switches.end(), std::back_inserter(
argv),
692 [](
const std::string& arg) ->
const char* { return arg.c_str(); });
694 std::vector<const char*> dartEntrypointArgs;
695 for (NSString* argument in [
_project dartEntrypointArguments]) {
696 dartEntrypointArgs.push_back([argument UTF8String]);
712 [[engine viewControllerForIdentifier:kFlutterImplicitViewId] updateSemantics:update];
721 std::stringstream stream;
723 stream << tag <<
": ";
726 std::string log = stream.str();
727 [FlutterLogger logDirect:[NSString stringWithUTF8String:log.c_str()]];
730 flutterArguments.
engine_id =
reinterpret_cast<int64_t
>((__bridge
void*)
self);
733 BOOL mergedPlatformUIThread = YES;
734 NSNumber* enableMergedPlatformUIThread =
735 [[NSBundle mainBundle] objectForInfoDictionaryKey:@"FLTEnableMergedPlatformUIThread"];
736 if (enableMergedPlatformUIThread != nil) {
737 mergedPlatformUIThread = enableMergedPlatformUIThread.boolValue;
740 if (mergedPlatformUIThread) {
741 NSLog(
@"Running with merged UI and platform thread. Experimental.");
748 [
self createPlatformThreadTaskDescription];
749 std::optional<FlutterTaskRunnerDescription> uiTaskRunnerDescription;
750 if (mergedPlatformUIThread) {
751 uiTaskRunnerDescription = [
self createPlatformThreadTaskDescription];
756 .platform_task_runner = &platformTaskRunnerDescription,
757 .thread_priority_setter = SetThreadPriority,
758 .ui_task_runner = uiTaskRunnerDescription ? &uiTaskRunnerDescription.value() :
nullptr,
762 [
self loadAOTData:_project.assetsPath];
767 flutterArguments.
compositor = [
self createFlutterCompositor];
771 [engine engineCallbackOnPreEngineRestart];
776 [engine onVSync:baton];
782 [engine onFocusChangeRequest:request];
789 NSLog(
@"Failed to initialize Flutter engine: error %d", result);
793 result = _embedderAPI.RunInitialized(_engine);
795 NSLog(
@"Failed to run an initialized engine: error %d", result);
799 [
self sendUserLocales];
802 NSEnumerator* viewControllerEnumerator = [_viewControllers objectEnumerator];
804 while ((nextViewController = [viewControllerEnumerator nextObject])) {
805 [
self updateWindowMetricsForViewController:nextViewController];
808 [
self updateDisplayConfig];
811 [
self sendInitialSettings];
815- (void)loadAOTData:(NSString*)assetsDir {
816 if (!_embedderAPI.RunsAOTCompiledDartCode()) {
820 BOOL isDirOut =
false;
821 NSFileManager* fileManager = [NSFileManager defaultManager];
825 NSString* elfPath = [NSString pathWithComponents:@[ assetsDir, @"app_elf_snapshot.so" ]];
827 if (![fileManager fileExistsAtPath:elfPath isDirectory:&isDirOut]) {
833 source.
elf_path = [elfPath cStringUsingEncoding:NSUTF8StringEncoding];
835 auto result = _embedderAPI.CreateAOTData(&source, &
_aotData);
837 NSLog(
@"Failed to load AOT data from: %@", elfPath);
844 NSAssert(controller != nil,
@"The controller must not be nil.");
846 NSAssert(controller.
engine == nil,
847 @"The FlutterViewController is unexpectedly attached to "
848 @"engine %@ before initialization.",
852 @"The requested view ID is occupied.");
853 [_viewControllers setObject:controller forKey:@(viewIdentifier)];
854 [controller setUpWithEngine:self viewIdentifier:viewIdentifier];
855 NSAssert(controller.
viewIdentifier == viewIdentifier,
@"Failed to assign view ID.");
859 NSAssert(controller.
attached,
@"The FlutterViewController should switch to the attached mode "
860 @"after it is added to a FlutterEngine.");
862 @"The FlutterViewController was added to %@, but its engine unexpectedly became %@.",
865 if (controller.viewLoaded) {
866 [
self viewControllerViewDidLoad:controller];
869 if (viewIdentifier != kFlutterImplicitViewId) {
881 .view_metrics = &metrics,
884 auto added =
reinterpret_cast<bool*
>(r->
user_data);
888 _embedderAPI.AddView(_engine, &info);
891 NSLog(
@"Failed to add view with ID %llu", viewIdentifier);
901 block:^(CFTimeInterval timestamp, CFTimeInterval targetTimestamp,
904 uint64_t targetTimeNanos =
906 FlutterEngine* engine = weakSelf;
908 engine->_embedderAPI.OnVsync(_engine, baton, timeNanos, targetTimeNanos);
913 [_vsyncWaiters setObject:waiter forKey:@(viewController.viewIdentifier)];
918 if (viewIdentifier != kFlutterImplicitViewId) {
919 bool removed =
false;
928 auto removed =
reinterpret_cast<bool*
>(r->user_data);
929 [FlutterRunLoop.mainRunLoop performBlock:^{
933 _embedderAPI.RemoveView(_engine, &info);
935 [[FlutterRunLoop mainRunLoop] pollFlutterMessagesOnce];
944 if (controller != nil) {
945 [controller detachFromEngine];
947 @"The FlutterViewController unexpectedly stays attached after being removed. "
948 @"In unit tests, this is likely because either the FlutterViewController or "
949 @"the FlutterEngine is mocked. Please subclass these classes instead.");
951 [_viewControllers removeObjectForKey:@(viewIdentifier)];
955 waiter = [_vsyncWaiters objectForKey:@(viewIdentifier)];
956 [_vsyncWaiters removeObjectForKey:@(viewIdentifier)];
961- (void)shutDownIfNeeded {
963 [
self shutDownEngine];
969 NSAssert(controller == nil || controller.
viewIdentifier == viewIdentifier,
970 @"The stored controller has unexpected view ID.");
976 [_viewControllers objectForKey:@(kFlutterImplicitViewId)];
977 if (currentController == controller) {
981 if (currentController == nil && controller != nil) {
983 NSAssert(controller.
engine == nil,
984 @"Failed to set view controller to the engine: "
985 @"The given FlutterViewController is already attached to an engine %@. "
986 @"If you wanted to create an FlutterViewController and set it to an existing engine, "
987 @"you should use FlutterViewController#init(engine:, nibName, bundle:) instead.",
989 [
self registerViewController:controller forIdentifier:kFlutterImplicitViewId];
990 }
else if (currentController != nil && controller == nil) {
991 NSAssert(currentController.
viewIdentifier == kFlutterImplicitViewId,
992 @"The default controller has an unexpected ID %llu", currentController.
viewIdentifier);
994 [
self deregisterViewControllerForIdentifier:kFlutterImplicitViewId];
995 [
self shutDownIfNeeded];
999 @"Failed to set view controller to the engine: "
1000 @"The engine already has an implicit view controller %@. "
1001 @"If you wanted to make the implicit view render in a different window, "
1002 @"you should attach the current view controller to the window instead.",
1008 return [
self viewControllerForIdentifier:kFlutterImplicitViewId];
1021 config, backing_store_out);
1030 ->Present(info->
view_id, info->layers, info->layers_count);
1038- (
id<FlutterBinaryMessenger>)binaryMessenger {
1042#pragma mark - Framework-internal methods
1048 NSAssert(
self.viewController == nil,
1049 @"The engine already has a view controller for the implicit view.");
1050 self.viewController = controller;
1055 [
self registerViewController:controller forIdentifier:viewIdentifier];
1059- (void)enableMultiView {
1061 NSAssert(
self.viewController == nil,
1062 @"Multiview can only be enabled before adding any view controllers.");
1074 _embedderAPI.SendViewFocusEvent(_engine, &event);
1084 _embedderAPI.SendViewFocusEvent(_engine, &event);
1088 [
self deregisterViewControllerForIdentifier:viewController.viewIdentifier];
1089 [
self shutDownIfNeeded];
1093 return _engine !=
nullptr;
1096- (void)updateDisplayConfig:(NSNotification*)notification {
1097 [
self updateDisplayConfig];
1100- (NSArray<NSScreen*>*)screens {
1101 return [NSScreen screens];
1104- (void)updateDisplayConfig {
1109 std::vector<FlutterEngineDisplay>
displays;
1110 for (NSScreen* screen : [
self screens]) {
1111 CGDirectDisplayID displayID =
1112 static_cast<CGDirectDisplayID
>([screen.deviceDescription[@"NSScreenNumber"] integerValue]);
1114 double devicePixelRatio = screen.backingScaleFactor;
1119 display.
width =
static_cast<size_t>(screen.frame.size.width) * devicePixelRatio;
1120 display.
height =
static_cast<size_t>(screen.frame.size.height) * devicePixelRatio;
1123 CVDisplayLinkRef displayLinkRef = nil;
1124 CVReturn
error = CVDisplayLinkCreateWithCGDisplay(displayID, &displayLinkRef);
1127 CVTime nominal = CVDisplayLinkGetNominalOutputVideoRefreshPeriod(displayLinkRef);
1128 if (!(nominal.flags & kCVTimeIsIndefinite)) {
1129 double refreshRate =
static_cast<double>(nominal.timeScale) / nominal.timeValue;
1132 CVDisplayLinkRelease(displayLinkRef);
1143- (void)onSettingsChanged:(NSNotification*)notification {
1145 NSString* brightness =
1146 [[NSUserDefaults standardUserDefaults] stringForKey:@"AppleInterfaceStyle"];
1147 [_settingsChannel sendMessage:@{
1148 @"platformBrightness" : [brightness isEqualToString:@"Dark"] ? @"dark" : @"light",
1150 @"textScaleFactor" : @1.0,
1155- (void)sendInitialSettings {
1157 [[NSDistributedNotificationCenter defaultCenter]
1159 selector:@selector(onSettingsChanged:)
1160 name:@"AppleInterfaceThemeChangedNotification"
1162 [
self onSettingsChanged:nil];
1166 return _embedderAPI;
1169- (nonnull NSString*)executableName {
1170 return [[[NSProcessInfo processInfo] arguments] firstObject] ?:
@"Flutter";
1178 @"The provided view controller is not attached to this engine.");
1180 CGRect scaledBounds = [view convertRectToBacking:view.bounds];
1181 CGSize scaledSize = scaledBounds.size;
1182 double pixelRatio =
view.layer.contentsScale;
1183 auto displayId = [view.window.screen.deviceDescription[@"NSScreenNumber"] integerValue];
1186 .
width =
static_cast<size_t>(scaledSize.width),
1187 .height =
static_cast<size_t>(scaledSize.height),
1188 .pixel_ratio = pixelRatio,
1189 .left =
static_cast<size_t>(scaledBounds.origin.x),
1190 .top =
static_cast<size_t>(scaledBounds.origin.y),
1191 .display_id =
static_cast<uint64_t
>(displayId),
1194 if (
view.sizedToContents) {
1195 CGSize maximumContentSize = [view convertSizeToBacking:view.maximumContentSize];
1196 CGSize minimumContentSize = [view convertSizeToBacking:view.minimumContentSize];
1208 _embedderAPI.SendWindowMetricsEvent(_engine, &windowMetricsEvent);
1212 _embedderAPI.SendPointerEvent(_engine, &event, 1);
1216- (void)setSemanticsEnabled:(
BOOL)enabled {
1217 if (_semanticsEnabled == enabled) {
1220 _semanticsEnabled = enabled;
1223 NSEnumerator* viewControllerEnumerator = [_viewControllers objectEnumerator];
1225 while ((nextViewController = [viewControllerEnumerator nextObject])) {
1226 [nextViewController notifySemanticsEnabledChanged];
1229 _embedderAPI.UpdateSemanticsEnabled(_engine, _semanticsEnabled);
1233 toTarget:(uint16_t)target
1234 withData:(
fml::MallocMapping)data {
1235 _embedderAPI.DispatchSemanticsAction(_engine,
target,
action,
data.GetMapping(),
data.GetSize());
1242#pragma mark - Private methods
1244- (void)sendUserLocales {
1245 if (!
self.running) {
1250 NSMutableArray<NSLocale*>* locales = [NSMutableArray array];
1251 std::vector<FlutterLocale> flutterLocales;
1252 flutterLocales.reserve(locales.count);
1253 for (NSString* localeID in [NSLocale preferredLanguages]) {
1254 NSLocale* locale = [[NSLocale alloc] initWithLocaleIdentifier:localeID];
1255 [locales addObject:locale];
1259 std::vector<const FlutterLocale*> flutterLocaleList;
1260 flutterLocaleList.reserve(flutterLocales.size());
1261 std::transform(flutterLocales.begin(), flutterLocales.end(),
1262 std::back_inserter(flutterLocaleList),
1263 [](
const auto& arg) ->
const auto* { return &arg; });
1264 _embedderAPI.UpdateLocales(_engine, flutterLocaleList.data(), flutterLocaleList.size());
1268 NSData* messageData = nil;
1269 if (
message->message_size > 0) {
1270 messageData = [NSData dataWithBytesNoCopy:(void*)message->message
1271 length:message->message_size
1277 NSMutableArray* isResponseValid =
self.isResponseValid;
1279 _embedderAPI.SendPlatformMessageResponse;
1281 @
synchronized(isResponseValid) {
1282 if (![isResponseValid[0] boolValue]) {
1286 if (responseHandle) {
1287 sendPlatformMessageResponse(weakSelf->_engine, responseHandle,
1288 static_cast<const uint8_t*
>(response.bytes), response.length);
1289 responseHandle = NULL;
1291 NSLog(
@"Error: Message responses can be sent only once. Ignoring duplicate response "
1300 handlerInfo.
handler(messageData, binaryResponseHandler);
1302 binaryResponseHandler(nil);
1306- (void)engineCallbackOnPreEngineRestart {
1307 NSEnumerator* viewControllerEnumerator = [_viewControllers objectEnumerator];
1309 while ((nextViewController = [viewControllerEnumerator nextObject])) {
1312 [_windowController closeAllWindows];
1313 [_platformViewController reset];
1319- (void)onVSync:(uintptr_t)baton {
1324 [_vsyncWaiters objectForKey:[_vsyncWaiters.keyEnumerator nextObject]];
1325 if (waiter != nil) {
1331 self.embedderAPI.OnVsync(_engine, baton, 0, 0);
1334 if ([NSThread isMainThread]) {
1337 [FlutterRunLoop.mainRunLoop performBlock:block];
1344- (void)shutDownEngine {
1345 if (_engine ==
nullptr) {
1351 NSLog(
@"Could not de-initialize the Flutter engine: error %d", result);
1354 result = _embedderAPI.Shutdown(_engine);
1356 NSLog(
@"Failed to shut down Flutter engine: error %d", result);
1362 NSAssert([[NSThread currentThread] isMainThread],
@"Must be called on the main thread.");
1363 return (__bridge
FlutterEngine*)
reinterpret_cast<void*
>(identifier);
1366- (void)setUpPlatformViewChannel {
1373 [_platformViewsChannel setMethodCallHandler:^(FlutterMethodCall* call, FlutterResult result) {
1374 [[weakSelf platformViewController] handleMethodCall:call result:result];
1378- (void)setUpAccessibilityChannel {
1384 [_accessibilityChannel setMessageHandler:^(id message, FlutterReply reply) {
1385 [weakSelf handleAccessibilityEvent:message];
1388- (void)setUpNotificationCenterListeners {
1389 NSNotificationCenter*
center = [NSNotificationCenter defaultCenter];
1391 [center addObserver:self
1392 selector:@selector(onAccessibilityStatusChanged:)
1393 name:kEnhancedUserInterfaceNotification
1395 [center addObserver:self
1396 selector:@selector(applicationWillTerminate:)
1397 name:NSApplicationWillTerminateNotification
1399 [center addObserver:self
1400 selector:@selector(windowDidChangeScreen:)
1401 name:NSWindowDidChangeScreenNotification
1403 [center addObserver:self
1404 selector:@selector(updateDisplayConfig:)
1405 name:NSApplicationDidChangeScreenParametersNotification
1409- (void)addInternalPlugins {
1423 [_platformChannel setMethodCallHandler:^(FlutterMethodCall* call, FlutterResult result) {
1424 [weakSelf handleMethodCall:call result:result];
1431 [_screenshotChannel setMethodCallHandler:^(FlutterMethodCall* call, FlutterResult result) {
1435 message:@"Engine deallocated."
1439 FlutterViewController* viewController =
1440 [strongSelf viewControllerForIdentifier:flutter::kFlutterImplicitViewId];
1441 if (!viewController) {
1443 message:@"No view controller."
1447 NSArray<FlutterSurface*>* frontSurfaces =
1448 viewController.flutterView.surfaceManager.frontSurfaces;
1449 if (frontSurfaces.count == 0) {
1456 FlutterSurface* surface = frontSurfaces.firstObject;
1457 IOSurfaceRef ioSurface = surface.ioSurface;
1459 size_t width = IOSurfaceGetWidth(ioSurface);
1460 size_t height = IOSurfaceGetHeight(ioSurface);
1461 size_t bytesPerRow = IOSurfaceGetBytesPerRow(ioSurface);
1462 size_t bytesPerElement = IOSurfaceGetBytesPerElement(ioSurface);
1463 uint32_t pixelFormat = (uint32_t)IOSurfaceGetPixelFormat(ioSurface);
1465 NSString* formatString;
1466 switch (pixelFormat) {
1467 case kCVPixelFormatType_40ARGBLEWideGamut:
1468 formatString = @"MTLPixelFormatBGRA10_XR";
1470 case kCVPixelFormatType_32BGRA:
1471 formatString = @"MTLPixelFormatBGRA8Unorm";
1474 formatString = [NSString stringWithFormat:@"Unknown(%u)", pixelFormat];
1478 IOSurfaceLock(ioSurface, kIOSurfaceLockReadOnly, nil);
1479 void* baseAddress = IOSurfaceGetBaseAddress(ioSurface);
1482 size_t packedBytesPerRow = width * bytesPerElement;
1483 NSMutableData* packedData = [NSMutableData dataWithLength:packedBytesPerRow * height];
1484 uint8_t* dest = (uint8_t*)packedData.mutableBytes;
1485 for (size_t row = 0; row < height; row++) {
1486 memcpy(dest + row * packedBytesPerRow, (uint8_t*)baseAddress + row * bytesPerRow,
1490 IOSurfaceUnlock(ioSurface, kIOSurfaceLockReadOnly, nil);
1501- (void)didUpdateMouseCursor:(NSCursor*)cursor {
1505 [_lastViewWithPointerEvent didUpdateMouseCursor:cursor];
1508- (void)applicationWillTerminate:(NSNotification*)notification {
1509 [
self shutDownEngine];
1512- (void)windowDidChangeScreen:(NSNotification*)notification {
1515 NSEnumerator* viewControllerEnumerator = [_viewControllers objectEnumerator];
1517 while ((nextViewController = [viewControllerEnumerator nextObject])) {
1518 [
self updateWindowMetricsForViewController:nextViewController];
1519 [nextViewController updateWideGamutForScreen];
1523- (void)onAccessibilityStatusChanged:(NSNotification*)notification {
1524 BOOL enabled = [notification.userInfo[kEnhancedUserInterfaceKey] boolValue];
1525 NSEnumerator* viewControllerEnumerator = [_viewControllers objectEnumerator];
1527 while ((nextViewController = [viewControllerEnumerator nextObject])) {
1531 self.semanticsEnabled = enabled;
1533- (void)handleAccessibilityEvent:(NSDictionary<NSString*,
id>*)annotatedEvent {
1534 NSString*
type = annotatedEvent[@"type"];
1535 if ([
type isEqualToString:
@"announce"]) {
1536 NSString*
message = annotatedEvent[@"data"][@"message"];
1537 NSNumber*
assertiveness = annotatedEvent[@"data"][@"assertiveness"];
1542 NSAccessibilityPriorityLevel priority = [assertiveness isEqualToNumber:@1]
1543 ? NSAccessibilityPriorityHigh
1544 : NSAccessibilityPriorityMedium;
1546 [
self announceAccessibilityMessage:message withPriority:priority];
1550- (void)announceAccessibilityMessage:(NSString*)message
1551 withPriority:(NSAccessibilityPriorityLevel)priority {
1552 NSAccessibilityPostNotificationWithUserInfo(
1554 NSAccessibilityAnnouncementRequestedNotification,
1555 @{NSAccessibilityAnnouncementKey :
message, NSAccessibilityPriorityKey : @(priority)});
1558 if ([call.
method isEqualToString:
@"SystemNavigator.pop"]) {
1559 [[NSApplication sharedApplication] terminate:self];
1561 }
else if ([call.
method isEqualToString:
@"SystemSound.play"]) {
1562 [
self playSystemSound:call.arguments];
1564 }
else if ([call.
method isEqualToString:
@"Clipboard.getData"]) {
1565 result([
self getClipboardData:call.arguments]);
1566 }
else if ([call.
method isEqualToString:
@"Clipboard.setData"]) {
1567 [
self setClipboardData:call.arguments];
1569 }
else if ([call.
method isEqualToString:
@"Clipboard.hasStrings"]) {
1570 result(@{
@"value" : @([
self clipboardHasStrings])});
1571 }
else if ([call.
method isEqualToString:
@"System.exitApplication"]) {
1572 if ([
self terminationHandler] == nil) {
1577 [NSApp terminate:self];
1580 [[
self terminationHandler] handleRequestAppExitMethodCall:call.arguments result:result];
1582 }
else if ([call.
method isEqualToString:
@"System.initializationComplete"]) {
1583 if ([
self terminationHandler] != nil) {
1584 [
self terminationHandler].acceptingRequests = YES;
1592- (void)playSystemSound:(NSString*)soundType {
1593 if ([soundType isEqualToString:
@"SystemSoundType.alert"]) {
1598- (NSDictionary*)getClipboardData:(NSString*)format {
1600 NSString* stringInPasteboard = [
self.pasteboard stringForType:NSPasteboardTypeString];
1601 return stringInPasteboard == nil ? nil : @{
@"text" : stringInPasteboard};
1606- (void)setClipboardData:(NSDictionary*)data {
1608 [
self.pasteboard clearContents];
1609 if (
text && ![
text isEqual:[NSNull null]]) {
1610 [
self.pasteboard setString:text forType:NSPasteboardTypeString];
1614- (
BOOL)clipboardHasStrings {
1615 return [
self.pasteboard stringForType:NSPasteboardTypeString].length > 0;
1618- (
std::vector<std::string>)switches {
1622#pragma mark - FlutterAppLifecycleDelegate
1625 NSString* nextState =
1626 [[NSString alloc] initWithCString:flutter::AppLifecycleStateToString(state)];
1627 [
self sendOnChannel:kFlutterLifecycleChannel
1628 message:[nextState dataUsingEncoding:NSUTF8StringEncoding]];
1635- (void)handleWillBecomeActive:(NSNotification*)notification {
1638 [
self setApplicationState:flutter::AppLifecycleState::kHidden];
1640 [
self setApplicationState:flutter::AppLifecycleState::kResumed];
1648- (void)handleWillResignActive:(NSNotification*)notification {
1651 [
self setApplicationState:flutter::AppLifecycleState::kHidden];
1653 [
self setApplicationState:flutter::AppLifecycleState::kInactive];
1661- (void)handleDidChangeOcclusionState:(NSNotification*)notification {
1662 NSApplicationOcclusionState occlusionState = [[NSApplication sharedApplication] occlusionState];
1663 if (occlusionState & NSApplicationOcclusionStateVisible) {
1666 [
self setApplicationState:flutter::AppLifecycleState::kResumed];
1668 [
self setApplicationState:flutter::AppLifecycleState::kInactive];
1672 [
self setApplicationState:flutter::AppLifecycleState::kHidden];
1676#pragma mark - FlutterBinaryMessenger
1678- (void)sendOnChannel:(nonnull NSString*)channel message:(nullable NSData*)message {
1679 [
self sendOnChannel:channel message:message binaryReply:nil];
1682- (void)sendOnChannel:(NSString*)channel
1683 message:(NSData* _Nullable)message
1690 auto captures = std::make_unique<Captures>();
1692 auto message_reply = [](
const uint8_t*
data,
size_t data_size,
void*
user_data) {
1693 auto captures =
reinterpret_cast<Captures*
>(
user_data);
1694 NSData* reply_data = nil;
1695 if (data !=
nullptr && data_size > 0) {
1696 reply_data = [NSData dataWithBytes:static_cast<const void*>(data) length:data_size];
1698 captures->reply(reply_data);
1703 _engine, message_reply, captures.get(), &response_handle);
1705 NSLog(
@"Failed to create a FlutterPlatformMessageResponseHandle (%d)", create_result);
1715 .message_size =
message.length,
1716 .response_handle = response_handle,
1719 FlutterEngineResult message_result = _embedderAPI.SendPlatformMessage(_engine, &platformMessage);
1721 NSLog(
@"Failed to send message to Flutter engine on channel '%@' (%d).",
channel,
1725 if (response_handle !=
nullptr) {
1727 _embedderAPI.PlatformMessageReleaseResponseHandle(_engine, response_handle);
1729 NSLog(
@"Failed to release the response handle (%d).", release_result);
1735 binaryMessageHandler:
1740 handler:[handler copy]];
1747 NSString* foundChannel = nil;
1750 if ([handlerInfo.
connection isEqual:@(connection)]) {
1756 [_messengerHandlers removeObjectForKey:foundChannel];
1760#pragma mark - FlutterPluginRegistry
1762- (
id<FlutterPluginRegistrar>)registrarForPlugin:(NSString*)pluginName {
1763 id<FlutterPluginRegistrar> registrar =
self.pluginRegistrars[pluginName];
1767 self.pluginRegistrars[pluginName] = registrarImpl;
1768 registrar = registrarImpl;
1773- (nullable NSObject*)valuePublishedByPlugin:(NSString*)pluginName {
1777#pragma mark - FlutterTextureRegistrar
1780 return [_renderer registerTexture:texture];
1783- (
BOOL)registerTextureWithID:(int64_t)textureId {
1784 return _embedderAPI.RegisterExternalTexture(_engine, textureId) ==
kSuccess;
1787- (void)textureFrameAvailable:(int64_t)textureID {
1788 [_renderer textureFrameAvailable:textureID];
1791- (
BOOL)markTextureFrameAvailable:(int64_t)textureID {
1792 return _embedderAPI.MarkExternalTextureFrameAvailable(_engine, textureID) ==
kSuccess;
1795- (void)unregisterTexture:(int64_t)textureID {
1796 [_renderer unregisterTexture:textureID];
1799- (
BOOL)unregisterTextureWithID:(int64_t)textureID {
1800 return _embedderAPI.UnregisterExternalTexture(_engine, textureID) ==
kSuccess;
1803#pragma mark - Task runner integration
1805- (void)postMainThreadTask:(
FlutterTask)task targetTimeInNanoseconds:(uint64_t)targetTime {
1808 const auto engine_time = _embedderAPI.GetCurrentTime();
1809 [FlutterRunLoop.mainRunLoop
1810 performAfterDelay:(targetTime - (double)engine_time) / NSEC_PER_SEC
1813 if (self != nil && self->_engine != nil) {
1814 auto result = _embedderAPI.RunTask(self->_engine, &task);
1815 if (result != kSuccess) {
1816 NSLog(@"Could not post a task to the Flutter engine.");
1823- (
flutter::FlutterCompositor*)macOSCompositor {
1827#pragma mark - FlutterKeyboardManagerDelegate
1834 userData:(
void*)userData {
1835 _embedderAPI.SendKeyEvent(_engine, &event,
callback, userData);
NS_ASSUME_NONNULL_BEGIN typedef void(^ FlutterBinaryReply)(NSData *_Nullable reply)
void(^ FlutterBinaryMessageHandler)(NSData *_Nullable message, FlutterBinaryReply reply)
int64_t FlutterBinaryMessengerConnection
void(^ FlutterResult)(id _Nullable result)
FLUTTER_DARWIN_EXPORT NSObject const * FlutterMethodNotImplemented
FlutterBinaryMessengerConnection _connection
NSInteger clearContents()
FlutterEngineResult FlutterEngineGetProcAddresses(FlutterEngineProcTable *table)
Gets the table of engine function pointers.
#define FLUTTER_API_SYMBOL(symbol)
@ kUnfocused
Specifies that a view does not have platform focus.
@ kFocused
Specifies that a view has platform focus.
@ kFlutterEngineAOTDataSourceTypeElfPath
void(* FlutterPlatformMessageCallback)(const FlutterPlatformMessage *, void *)
@ kFlutterEngineDisplaysUpdateTypeStartup
FlutterThreadPriority
Valid values for priority of Thread.
@ kDisplay
Suitable for threads which generate data for the display.
@ kRaster
Suitable for thread which raster data.
void(* FlutterKeyEventCallback)(bool, void *)
FlutterEngineResult(* FlutterEngineSendPlatformMessageResponseFnPtr)(FLUTTER_API_SYMBOL(FlutterEngine) engine, const FlutterPlatformMessageResponseHandle *handle, const uint8_t *data, size_t data_length)
#define FLUTTER_ENGINE_VERSION
const char FlTextDirection FlAssertiveness assertiveness
const gchar FlBinaryMessengerMessageHandler handler
const uint8_t uint32_t uint32_t GError ** error
G_BEGIN_DECLS FlutterViewId view_id
HWND(* FlutterPlatformViewFactory)(const FlutterPlatformViewCreationParameters *)
FlutterDesktopBinaryReply callback
#define FML_DCHECK(condition)
instancetype messageChannelWithName:binaryMessenger:codec:(NSString *name,[binaryMessenger] NSObject< FlutterBinaryMessenger > *messenger,[codec] NSObject< FlutterMessageCodec > *codec)
void(* rootIsolateCreateCallback)(void *_Nullable)
NSString * lookupKeyForAsset:fromPackage:(NSString *asset,[fromPackage] NSString *package)
NSString * lookupKeyForAsset:(NSString *asset)
instancetype displayLinkWithView:(NSView *view)
FlutterBinaryMessageHandler handler
NSObject< FlutterBinaryMessenger > * binaryMessenger
NSObject * publishedValue
instancetype errorWithCode:message:details:(NSString *code,[message] NSString *_Nullable message,[details] id _Nullable details)
instancetype methodCallWithMethodName:arguments:(NSString *method,[arguments] id _Nullable arguments)
void setMethodCallHandler:(FlutterMethodCallHandler _Nullable handler)
instancetype methodChannelWithName:binaryMessenger:codec:(NSString *name,[binaryMessenger] NSObject< FlutterBinaryMessenger > *messenger,[codec] NSObject< FlutterMethodCodec > *codec)
void registerWithRegistrar:delegate:(nonnull id< FlutterPluginRegistrar > registrar,[delegate] nullable id< FlutterMouseCursorPluginDelegate > delegate)
instancetype typedDataWithBytes:(NSData *data)
Converts between the time representation used by Flutter Engine and CAMediaTime.
uint64_t CAMediaTimeToEngineTime:(CFTimeInterval time)
void waitForVSync:(uintptr_t baton)
void onPreEngineRestart()
FlutterViewIdentifier viewIdentifier
void onAccessibilityStatusChanged:(BOOL enabled)
FlutterBinaryMessengerRelay * _binaryMessenger
FlutterViewController * viewController
FlutterMethodChannel * _platformViewsChannel
_FlutterEngineAOTData * _aotData
std::unique_ptr< flutter::FlutterCompositor > _macOSCompositor
static const int kMainThreadPriority
static void OnPlatformMessage(const FlutterPlatformMessage *message, void *user_data)
FlutterPlatformViewController * _platformViewController
FlutterBasicMessageChannel * _accessibilityChannel
FlutterBasicMessageChannel * _settingsChannel
static FlutterLocale FlutterLocaleFromNSLocale(NSLocale *locale)
BOOL _allowHeadlessExecution
NSMutableDictionary< NSString *, FlutterEngineHandlerInfo * > * _messengerHandlers
FlutterBinaryMessengerConnection _currentMessengerConnection
FlutterMethodChannel * _platformChannel
NSString *const kFlutterLifecycleChannel
static NSString *const kEnhancedUserInterfaceNotification
The private notification for voice over.
NSMapTable< NSNumber *, FlutterVSyncWaiter * > * _vsyncWaiters
FlutterViewIdentifier _nextViewIdentifier
NSString *const kFlutterPlatformChannel
FlutterMethodChannel * _screenshotChannel
FlutterTextInputPlugin * _textInputPlugin
FlutterDartProject * _project
NSMapTable * _viewControllers
FlutterCompositor _compositor
__weak FlutterView * _lastViewWithPointerEvent
FlutterKeyboardManager * _keyboardManager
FlutterWindowController * _windowController
FlutterTerminationCallback _terminator
constexpr char kTextPlainFormat[]
Clipboard plain text format.
__weak FlutterEngine * _flutterEngine
FlutterBinaryMessengerRelay * _binaryMessenger
static NSString *const kEnhancedUserInterfaceKey
NSString *const kFlutterSettingsChannel
NS_ASSUME_NONNULL_BEGIN typedef void(^ FlutterTerminationCallback)(id _Nullable sender)
constexpr int64_t kFlutterImplicitViewId
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot data
std::vector< std::string > GetSwitchesFromEnvironment()
instancetype sharedInstance()
impeller::ShaderType type
void * user_data
The |FlutterAddViewInfo.user_data|.
FlutterBackingStoreCreateCallback create_backing_store_callback
bool avoid_backing_store_cache
size_t struct_size
This size of this struct. Must be sizeof(FlutterCompositor).
FlutterPresentViewCallback present_view_callback
FlutterBackingStoreCollectCallback collect_backing_store_callback
size_t struct_size
The size of this struct. Must be sizeof(FlutterCustomTaskRunners).
FlutterEngineAOTDataSourceType type
const char * elf_path
Absolute path to an ELF library file.
size_t height
The height of the display, in physical pixels.
double device_pixel_ratio
size_t struct_size
The size of this struct. Must be sizeof(FlutterEngineDisplay).
size_t width
The width of the display, in physical pixels.
FlutterEngineDisplayId display_id
Function-pointer-based versions of the APIs above.
const char * language_code
size_t struct_size
This size of this struct. Must be sizeof(FlutterLocale).
const char * country_code
const char * variant_code
FlutterPlatformMessageCallback platform_message_callback
FlutterLogMessageCallback log_message_callback
FlutterViewFocusChangeRequestCallback view_focus_change_request_callback
VsyncCallback vsync_callback
OnPreEngineRestartCallback on_pre_engine_restart_callback
FlutterEngineAOTData aot_data
const char *const * dart_entrypoint_argv
size_t struct_size
The size of this struct. Must be sizeof(FlutterProjectArgs).
FlutterUpdateSemanticsCallback2 update_semantics_callback2
const char *const * command_line_argv
const char * icu_data_path
bool shutdown_dart_vm_when_done
const char * custom_dart_entrypoint
const FlutterCustomTaskRunners * custom_task_runners
int command_line_argc
The command line argument count used to initialize the project.
VoidCallback root_isolate_create_callback
const FlutterCompositor * compositor
FlutterRemoveViewCallback remove_view_callback
A batch of updates to semantics nodes and custom actions.
size_t struct_size
The size of this struct. Must be sizeof(FlutterTaskRunnerDescription).
FlutterViewFocusState state
The focus state of the view.
size_t struct_size
The size of this struct. Must be sizeof(FlutterWindowMetricsEvent).
size_t min_height_constraint
size_t min_width_constraint
size_t max_width_constraint
size_t max_height_constraint