Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: SplashScreen Background Color preference support #1411

Merged
merged 1 commit into from
Mar 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions CordovaLib/Classes/Public/CDVViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ Licensed to the Apache Software Foundation (ASF) under one
#import <Cordova/NSDictionary+CordovaPreferences.h>
#import "CDVCommandDelegateImpl.h"

UIColor* defaultBackgroundColor(void) {
if (@available(iOS 13.0, *)) {
return UIColor.systemBackgroundColor;
} else {
return UIColor.whiteColor;
}
}

@interface CDVViewController () <CDVWebViewEngineConfigurationDelegate> { }

@property (nonatomic, readwrite, strong) NSXMLParser* configParser;
Expand Down Expand Up @@ -293,9 +301,12 @@ - (void)viewDidLoad
}
// /////////////////

UIColor* bgColor = [UIColor colorNamed:@"BackgroundColor"] ?: UIColor.whiteColor;
[self.launchView setBackgroundColor:bgColor];
UIColor* bgDefault = defaultBackgroundColor();
UIColor* bgColor = [UIColor colorNamed:@"BackgroundColor"] ?: bgDefault;
UIColor* bgSplash = [UIColor colorNamed:@"SplashScreenBackgroundColor"] ?: bgColor;

[self.webView setBackgroundColor:bgColor];
[self.launchView setBackgroundColor:bgSplash];
}

-(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
}
}
4 changes: 2 additions & 2 deletions templates/project/__PROJECT_NAME__/CDVLaunchScreen.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
<rect key="frame" x="0.0" y="0.0" width="414" height="896"/>
</imageView>
</subviews>
<color key="backgroundColor" name="BackgroundColor"/>
<color key="backgroundColor" name="SplashScreenBackgroundColor"/>
<constraints>
<constraint firstAttribute="trailing" secondItem="2ns-9I-Qjs" secondAttribute="trailing" id="FZL-3Z-NFz"/>
<constraint firstItem="2ns-9I-Qjs" firstAttribute="bottom" secondItem="Ze5-6b-2t3" secondAttribute="bottom" id="L9l-pw-wXj"/>
Expand All @@ -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
}
}
Loading