Skip to content

Commit

Permalink
feat: SplashScreen Background Color preference support
Browse files Browse the repository at this point in the history
Closes #1254.
  • Loading branch information
dpogue committed Mar 20, 2024
1 parent c97845a commit e10fd3d
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 16 deletions.
4 changes: 3 additions & 1 deletion CordovaLib/Classes/Public/CDVViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
69 changes: 55 additions & 14 deletions lib/prepare.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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: [{
Expand All @@ -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));
}
}

/**
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"colors" : [
{
"color" : {
"platform" : "ios",
"reference" : "systemBackgroundColor"
},
"idiom" : "universal"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
</scenes>
<resources>
<image name="LaunchStoryboard" width="1366" height="1366"/>
<namedColor name="BackgroundColor">
<namedColor name="SplashScreenBackgroundColor">
<color white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
</namedColor>
</resources>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"colors" : [
{
"color" : {
"platform" : "ios",
"reference" : "systemBackgroundColor"
},
"idiom" : "universal"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}

0 comments on commit e10fd3d

Please sign in to comment.