Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support for dark mode images and semantic colors (#11097)
* feat(ios): add support for specifying dark mode images Implements part of TIMOB-27126 * feat: add support for semantic colors This adds a cross platfom method for loading semanitc colors, on iOS 11+ we will use the native Ti.UI.iOS.fetchSemanticColor to load the right color, in all other cases we use the Ti.UI.semanticColorType and the provided json file to obtain the correct value Fixes TIMOB-27126 * fix: correct check for iOS namespace * build: apply rollup configuration to xcode project build * fix: fall back to json file when using below ios 13 * docs: correct summary * test: add tests * fix(android): define getter/setter for semanticcolortype property Due to the way Ti.UI works on Android we cant reliably track the changes, by implementing the property with the get/set syntax we can track the property changes ourselves and reliably return the correct result * feat: support setting alpha per color * refactor: allow setting alpha as a 0-100 range * fix(ios): guard in sdk 11 check * docs: add docs
- Loading branch information
1 parent
da842f4
commit 1a8ae85
Showing
9 changed files
with
311 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
// Load extensions to polyfill our own APIs | ||
import './ti.blob'; | ||
import './ti.ui'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/** | ||
* Appcelerator Titanium Mobile | ||
* Copyright (c) 2019 by Axway, Inc. All Rights Reserved. | ||
* Licensed under the terms of the Apache Public License | ||
* Please see the LICENSE included with this distribution for details. | ||
*/ | ||
|
||
let colorset; | ||
let osVersion; | ||
|
||
// As Android passes a new instance of Ti.UI to every JS file we can't just | ||
// Ti.UI within this file, we must call kroll.binding to get the Titanium | ||
// namespace that is passed in with require and that deal with the .UI | ||
// namespace that is on that directly. | ||
let uiModule = Ti.UI; | ||
if (Ti.Android) { | ||
uiModule = kroll.binding('Titanium').Titanium.UI; | ||
} | ||
|
||
uiModule.SEMANTIC_COLOR_TYPE_LIGHT = 'light'; | ||
uiModule.SEMANTIC_COLOR_TYPE_DARK = 'dark'; | ||
|
||
// We need to track this manually with a getter/setter | ||
// due to the same reasons we use uiModule instead of Ti.UI | ||
let currentColorType = uiModule.SEMANTIC_COLOR_TYPE_LIGHT; | ||
Object.defineProperty(uiModule, 'semanticColorType', { | ||
get: () => { | ||
return currentColorType; | ||
}, | ||
set: (colorType) => { | ||
currentColorType = colorType; | ||
} | ||
}); | ||
|
||
uiModule.fetchSemanticColor = function fetchSemanticColor (colorName) { | ||
if (!osVersion) { | ||
osVersion = parseInt(Ti.Platform.version.split('.')[0]); | ||
} | ||
|
||
if (Ti.App.iOS && osVersion >= 13) { | ||
return Ti.UI.iOS.fetchSemanticColor(colorName); | ||
} else { | ||
if (!colorset) { | ||
try { | ||
colorset = require('/semantic.colors.json'); // eslint-disable-line import/no-absolute-path | ||
} catch (error) { | ||
console.error('Failed to require colors file at /semantic.colors.json'); | ||
return; | ||
} | ||
} | ||
try { | ||
return colorset[colorName][uiModule.semanticColorType].color || colorset[colorName][uiModule.semanticColorType]; | ||
} catch (error) { | ||
console.log(`Failed to lookup color for ${colorName}`); | ||
} | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"textColor": { | ||
"dark": "#ff85e2", | ||
"light": "#ff1f1f" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* Appcelerator Titanium Mobile | ||
* Copyright (c) 2011-Present by Appcelerator, Inc. All Rights Reserved. | ||
* Licensed under the terms of the Apache Public License | ||
* Please see the LICENSE included with this distribution for details. | ||
*/ | ||
/* eslint-env mocha */ | ||
/* eslint no-unused-expressions: "off", import/no-absolute-path: "off" */ | ||
'use strict'; | ||
const should = require('./utilities/assertions'); | ||
|
||
describe('Titanium.UI', function () { | ||
it('.SEMANTIC_COLOR_TYPE_DARK', function () { | ||
should(Ti.UI).have.a.constant('SEMANTIC_COLOR_TYPE_DARK').which.is.a.string; | ||
}); | ||
|
||
it('.SEMANTIC_COLOR_TYPE_LIGHT', function () { | ||
should(Ti.UI).have.a.constant('SEMANTIC_COLOR_TYPE_LIGHT').which.is.a.string; | ||
}); | ||
|
||
it('semanticColorType default', function () { | ||
should(Ti.UI.semanticColorType).eql(Ti.UI.SEMANTIC_COLOR_TYPE_LIGHT); | ||
}); | ||
|
||
it('fetchSemanticColor', function () { | ||
var isiOS13 = (Ti.Platform.osname === 'iphone' || Ti.Platform.osname === 'ipad') && (parseInt(Ti.Platform.version.split('.')[0]) >= 13); | ||
const semanticColors = require('/semantic.colors.json'); | ||
|
||
if (isiOS13) { | ||
should(Ti.UI.fetchSemanticColor('textColor')).be.an.string; | ||
} else { | ||
should(Ti.UI.fetchSemanticColor('textColor')).equal(semanticColors.textColor.light); | ||
Ti.UI.semanticColorType = Ti.UI.SEMANTIC_COLOR_TYPE_DARK; | ||
should(Ti.UI.fetchSemanticColor('textColor')).equal(semanticColors.textColor.dark); | ||
|
||
} | ||
}); | ||
}); |