From e10fd3de29aa58c9d45b218846e03bd03a04be55 Mon Sep 17 00:00:00 2001 From: Darryl Pogue Date: Tue, 19 Mar 2024 21:40:58 -0700 Subject: [PATCH] feat: SplashScreen Background Color preference support Closes #1254. --- CordovaLib/Classes/Public/CDVViewController.m | 4 +- lib/prepare.js | 69 +++++++++++++++---- .../Contents.json | 15 ++++ .../CDVLaunchScreen.storyboard | 2 +- .../Contents.json | 15 ++++ 5 files changed, 89 insertions(+), 16 deletions(-) create mode 100644 templates/project/__PROJECT_NAME__/Assets.xcassets/SplashScreenBackgroundColor.colorset/Contents.json create mode 100644 tests/spec/unit/fixtures/ios-config-xml/SampleApp/Assets.xcassets/SplashScreenBackgroundColor.colorset/Contents.json diff --git a/CordovaLib/Classes/Public/CDVViewController.m b/CordovaLib/Classes/Public/CDVViewController.m index dd8ba165e5..1bfdaa95c5 100644 --- a/CordovaLib/Classes/Public/CDVViewController.m +++ b/CordovaLib/Classes/Public/CDVViewController.m @@ -294,8 +294,10 @@ - (void)viewDidLoad // ///////////////// UIColor* bgColor = [UIColor colorNamed:@"BackgroundColor"] ?: UIColor.whiteColor; - [self.launchView setBackgroundColor:bgColor]; [self.webView setBackgroundColor:bgColor]; + + bgColor = [UIColor colorNamed:@"SplashScreenBackgroundColor"] ?: UIColor.whiteColor; + [self.launchView setBackgroundColor:bgColor]; } -(void)viewWillAppear:(BOOL)animated diff --git a/lib/prepare.js b/lib/prepare.js index cfaab02c2e..df88fd6c57 100644 --- a/lib/prepare.js +++ b/lib/prepare.js @@ -467,6 +467,21 @@ function getBackgroundColorDir (projectRoot, platformProjDir) { } } +/** + * Returns the directory for the SplashScreenBackgroundColor.colorset asset, or + * null if no xcassets exist. + * + * @param {string} projectRoot The project's root directory + * @param {string} platformProjDir The platform's project directory + */ +function getSplashScreenBackgroundColorDir (projectRoot, platformProjDir) { + if (folderExists(path.join(projectRoot, platformProjDir, 'Assets.xcassets/'))) { + return path.join(platformProjDir, 'Assets.xcassets', 'SplashScreenBackgroundColor.colorset'); + } else { + return null; + } +} + function colorPreferenceToComponents (pref) { if (!pref || !pref.match(/^(#[0-9A-Fa-f]{3}|(0x|#)([0-9A-Fa-f]{2})?[0-9A-Fa-f]{6})$/)) { return { @@ -517,11 +532,12 @@ function colorPreferenceToComponents (pref) { * @param {Object} locations A dictionary containing useful location paths */ function updateBackgroundColor (cordovaProject, locations) { + const platformProjDir = path.relative(cordovaProject.root, locations.xcodeCordovaProj); + const pref = cordovaProject.projectConfig.getPreference('BackgroundColor', 'ios') || ''; + const splashPref = cordovaProject.projectConfig.getPreference('SplashScreenBackgroundColor', 'ios') || pref; - const platformProjDir = path.relative(cordovaProject.root, locations.xcodeCordovaProj); const backgroundColorDir = getBackgroundColorDir(cordovaProject.root, platformProjDir); - if (backgroundColorDir) { const contentsJSON = { colors: [{ @@ -538,6 +554,24 @@ function updateBackgroundColor (cordovaProject, locations) { fs.writeFileSync(path.join(cordovaProject.root, backgroundColorDir, 'Contents.json'), JSON.stringify(contentsJSON, null, 2)); } + + const splashBackgroundColorDir = getSplashScreenBackgroundColorDir(cordovaProject.root, platformProjDir); + if (splashBackgroundColorDir) { + const contentsJSON = { + colors: [{ + idiom: 'universal', + color: colorPreferenceToComponents(splashPref) + }], + info: { + author: 'Xcode', + version: 1 + } + }; + + events.emit('verbose', 'Updating Splash Screen Background Color color set Contents.json'); + fs.writeFileSync(path.join(cordovaProject.root, splashBackgroundColorDir, 'Contents.json'), + JSON.stringify(contentsJSON, null, 2)); + } } /** @@ -549,24 +583,31 @@ function updateBackgroundColor (cordovaProject, locations) { */ function cleanBackgroundColor (projectRoot, projectConfig, locations) { const platformProjDir = path.relative(projectRoot, locations.xcodeCordovaProj); - const backgroundColorDir = getBackgroundColorDir(projectRoot, platformProjDir); - if (backgroundColorDir) { - const contentsJSON = { - colors: [{ - idiom: 'universal', - color: colorPreferenceToComponents(null) - }], - info: { - author: 'Xcode', - version: 1 - } - }; + const contentsJSON = { + colors: [{ + idiom: 'universal', + color: colorPreferenceToComponents(null) + }], + info: { + author: 'Xcode', + version: 1 + } + }; + const backgroundColorDir = getBackgroundColorDir(projectRoot, platformProjDir); + if (backgroundColorDir) { events.emit('verbose', 'Cleaning Background Color color set Contents.json'); fs.writeFileSync(path.join(projectRoot, backgroundColorDir, 'Contents.json'), JSON.stringify(contentsJSON, null, 2)); } + + const splashBackgroundColorDir = getSplashScreenBackgroundColorDir(projectRoot, platformProjDir); + if (splashBackgroundColorDir) { + events.emit('verbose', 'Cleaning Splash Screen Background Color color set Contents.json'); + fs.writeFileSync(path.join(projectRoot, splashBackgroundColorDir, 'Contents.json'), + JSON.stringify(contentsJSON, null, 2)); + } } function updateFileResources (cordovaProject, locations) { diff --git a/templates/project/__PROJECT_NAME__/Assets.xcassets/SplashScreenBackgroundColor.colorset/Contents.json b/templates/project/__PROJECT_NAME__/Assets.xcassets/SplashScreenBackgroundColor.colorset/Contents.json new file mode 100644 index 0000000000..462830f8c6 --- /dev/null +++ b/templates/project/__PROJECT_NAME__/Assets.xcassets/SplashScreenBackgroundColor.colorset/Contents.json @@ -0,0 +1,15 @@ +{ + "colors" : [ + { + "color" : { + "platform" : "ios", + "reference" : "systemBackgroundColor" + }, + "idiom" : "universal" + } + ], + "info" : { + "author" : "xcode", + "version" : 1 + } +} diff --git a/templates/project/__PROJECT_NAME__/CDVLaunchScreen.storyboard b/templates/project/__PROJECT_NAME__/CDVLaunchScreen.storyboard index 924392e345..f2481e3421 100644 --- a/templates/project/__PROJECT_NAME__/CDVLaunchScreen.storyboard +++ b/templates/project/__PROJECT_NAME__/CDVLaunchScreen.storyboard @@ -58,7 +58,7 @@ - + diff --git a/tests/spec/unit/fixtures/ios-config-xml/SampleApp/Assets.xcassets/SplashScreenBackgroundColor.colorset/Contents.json b/tests/spec/unit/fixtures/ios-config-xml/SampleApp/Assets.xcassets/SplashScreenBackgroundColor.colorset/Contents.json new file mode 100644 index 0000000000..462830f8c6 --- /dev/null +++ b/tests/spec/unit/fixtures/ios-config-xml/SampleApp/Assets.xcassets/SplashScreenBackgroundColor.colorset/Contents.json @@ -0,0 +1,15 @@ +{ + "colors" : [ + { + "color" : { + "platform" : "ios", + "reference" : "systemBackgroundColor" + }, + "idiom" : "universal" + } + ], + "info" : { + "author" : "xcode", + "version" : 1 + } +}