Allow custom override to utilize KeyWindow for default fallback colorScheme (#44383)

Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/44383

This diff allows the default fallback style to be grabbed from KeyWindow. Previously with the TraitCollections being passed in from overridden views it was not getting the accurate system fallback.

We need this for Twilight, which is adopting a Light/Dark mode toggle. Previously when setColorScheme was getting called it would modify overrideUserInterfaceStyle and that would serve as the "default fallback" for future setColorScheme calls. setColorScheme shouldn't be setting the defaults, it should be setting the user-session theme preference.

Changelog:
[Internal] [Changed] - Added option for treating the KeyWindows's userInterfaceStyle as the source of truth for the system's dark/light mode.

Differential Revision: D56868862

fbshipit-source-id: 229894947baed65ef15cece5bece120e8497462f
This commit is contained in:
Jesse Watts-Russell
2024-05-03 22:15:29 -07:00
committed by Facebook GitHub Bot
parent 002396beae
commit 377c2f5d88
2 changed files with 10 additions and 2 deletions
@@ -13,6 +13,7 @@
RCT_EXTERN void RCTEnableAppearancePreference(BOOL enabled);
RCT_EXTERN void RCTOverrideAppearancePreference(NSString *const);
RCT_EXTERN void RCTUseKeyWindowForSystemStyle(BOOL useMainScreen);
RCT_EXTERN NSString *RCTCurrentOverrideAppearancePreference();
RCT_EXTERN NSString *RCTColorSchemePreference(UITraitCollection *traitCollection);
@@ -31,6 +31,12 @@ void RCTOverrideAppearancePreference(NSString *const colorSchemeOverride)
sColorSchemeOverride = colorSchemeOverride;
}
static BOOL sUseKeyWindowForSystemStyle = NO;
void RCTUseKeyWindowForSystemStyle(BOOL useMainScreen)
{
sUseKeyWindowForSystemStyle = useMainScreen;
}
NSString *RCTCurrentOverrideAppearancePreference()
{
return sColorSchemeOverride;
@@ -56,8 +62,9 @@ NSString *RCTColorSchemePreference(UITraitCollection *traitCollection)
// Return the default if the app doesn't allow different color schemes.
return RCTAppearanceColorSchemeLight;
}
return appearances[@(traitCollection.userInterfaceStyle)] ?: RCTAppearanceColorSchemeLight;
UIUserInterfaceStyle systemStyle = sUseKeyWindowForSystemStyle ? RCTKeyWindow().traitCollection.userInterfaceStyle
: traitCollection.userInterfaceStyle;
return appearances[@(systemStyle)] ?: RCTAppearanceColorSchemeLight;
}
@interface RCTAppearance () <NativeAppearanceSpec>