@@ -711,29 +711,46 @@ - (BOOL)running {
711
711
return _engine != nullptr ;
712
712
}
713
713
714
+ - (void )updateDisplayConfig : (NSNotification *)notification {
715
+ [self updateDisplayConfig ];
716
+ }
717
+
714
718
- (void )updateDisplayConfig {
715
719
if (!_engine) {
716
720
return ;
717
721
}
718
722
719
- CVDisplayLinkRef displayLinkRef;
720
- CGDirectDisplayID mainDisplayID = CGMainDisplayID ();
721
- CVDisplayLinkCreateWithCGDisplay (mainDisplayID, &displayLinkRef);
722
- CVTime nominal = CVDisplayLinkGetNominalOutputVideoRefreshPeriod (displayLinkRef);
723
- if (!(nominal.flags & kCVTimeIsIndefinite )) {
724
- double refreshRate = static_cast <double >(nominal.timeScale ) / nominal.timeValue ;
723
+ std::vector<FlutterEngineDisplay> displays;
724
+ for (NSScreen * screen : [NSScreen screens ]) {
725
+ CGDirectDisplayID displayID =
726
+ static_cast <CGDirectDisplayID>([screen.deviceDescription[@" NSScreenNumber" ] integerValue ]);
725
727
726
728
FlutterEngineDisplay display;
727
729
display.struct_size = sizeof (display);
728
- display.display_id = mainDisplayID;
729
- display.refresh_rate = round (refreshRate);
730
+ display.display_id = displayID;
731
+ display.single_display = false ;
732
+ display.width = static_cast <size_t >(screen.frame .size .width );
733
+ display.height = static_cast <size_t >(screen.frame .size .height );
734
+ display.device_pixel_ratio = screen.backingScaleFactor ;
735
+
736
+ CVDisplayLinkRef displayLinkRef = nil ;
737
+ CVReturn error = CVDisplayLinkCreateWithCGDisplay (displayID, &displayLinkRef);
738
+
739
+ if (error == 0 ) {
740
+ CVTime nominal = CVDisplayLinkGetNominalOutputVideoRefreshPeriod (displayLinkRef);
741
+ if (!(nominal.flags & kCVTimeIsIndefinite )) {
742
+ double refreshRate = static_cast <double >(nominal.timeScale ) / nominal.timeValue ;
743
+ display.refresh_rate = round (refreshRate);
744
+ }
745
+ CVDisplayLinkRelease (displayLinkRef);
746
+ } else {
747
+ display.refresh_rate = 0 ;
748
+ }
730
749
731
- std::vector<FlutterEngineDisplay> displays = {display};
732
- _embedderAPI.NotifyDisplayUpdate (_engine, kFlutterEngineDisplaysUpdateTypeStartup ,
733
- displays.data (), displays.size ());
750
+ displays.push_back (display);
734
751
}
735
-
736
- CVDisplayLinkRelease (displayLinkRef );
752
+ _embedderAPI. NotifyDisplayUpdate (_engine, kFlutterEngineDisplaysUpdateTypeStartup ,
753
+ displays. data (), displays. size () );
737
754
}
738
755
739
756
- (void )onSettingsChanged : (NSNotification *)notification {
@@ -782,14 +799,15 @@ - (void)updateWindowMetricsForViewController:(FlutterViewController*)viewControl
782
799
CGRect scaledBounds = [view convertRectToBacking: view.bounds];
783
800
CGSize scaledSize = scaledBounds.size ;
784
801
double pixelRatio = view.bounds .size .width == 0 ? 1 : scaledSize.width / view.bounds .size .width ;
785
-
802
+ auto displayId = [view.window.screen.deviceDescription[ @" NSScreenNumber " ] integerValue ];
786
803
const FlutterWindowMetricsEvent windowMetricsEvent = {
787
804
.struct_size = sizeof (windowMetricsEvent),
788
805
.width = static_cast <size_t >(scaledSize.width ),
789
806
.height = static_cast <size_t >(scaledSize.height ),
790
807
.pixel_ratio = pixelRatio,
791
808
.left = static_cast <size_t >(scaledBounds.origin .x ),
792
809
.top = static_cast <size_t >(scaledBounds.origin .y ),
810
+ .display_id = static_cast <uint64_t >(displayId),
793
811
};
794
812
_embedderAPI.SendWindowMetricsEvent (_engine, &windowMetricsEvent);
795
813
}
@@ -964,6 +982,14 @@ - (void)setUpNotificationCenterListeners {
964
982
selector: @selector (applicationWillTerminate: )
965
983
name: NSApplicationWillTerminateNotification
966
984
object: nil ];
985
+ [center addObserver: self
986
+ selector: @selector (windowDidChangeScreen: )
987
+ name: NSWindowDidChangeScreenNotification
988
+ object: nil ];
989
+ [center addObserver: self
990
+ selector: @selector (updateDisplayConfig: )
991
+ name: NSApplicationDidChangeScreenParametersNotification
992
+ object: nil ];
967
993
}
968
994
969
995
- (void )addInternalPlugins {
@@ -987,6 +1013,16 @@ - (void)applicationWillTerminate:(NSNotification*)notification {
987
1013
[self shutDownEngine ];
988
1014
}
989
1015
1016
+ - (void )windowDidChangeScreen : (NSNotification *)notification {
1017
+ // Update window metric for all view controllers since the display_id has
1018
+ // changed.
1019
+ NSEnumerator * viewControllerEnumerator = [_viewControllers objectEnumerator ];
1020
+ FlutterViewController* nextViewController;
1021
+ while ((nextViewController = [viewControllerEnumerator nextObject ])) {
1022
+ [self updateWindowMetricsForViewController: nextViewController];
1023
+ }
1024
+ }
1025
+
990
1026
- (void )onAccessibilityStatusChanged : (NSNotification *)notification {
991
1027
BOOL enabled = [notification.userInfo[kEnhancedUserInterfaceKey ] boolValue ];
992
1028
NSEnumerator * viewControllerEnumerator = [_viewControllers objectEnumerator ];
0 commit comments