Fix deprecations for 2.103; Add `decodeCSSColor`

This commit is contained in:
Grim Maple 2023-05-18 22:46:24 +03:00
parent 475e0bba7c
commit 76daac9bf1
4 changed files with 79 additions and 46 deletions

View File

@ -79,7 +79,6 @@ class SettingsFile {
_setting = settings; _setting = settings;
//_setting.apply(settings); //_setting.apply(settings);
} }
alias setting this;
/// create settings file object; if filename is provided, attempts to load settings from file /// create settings file object; if filename is provided, attempts to load settings from file
this(string filename = null) { this(string filename = null) {

View File

@ -319,8 +319,77 @@ bool isFullyTransparentColor(uint color) pure nothrow {
/// decode color string supported formats: #RGB, #ARGB, #RRGGBB, #AARRGGBB, rgb(r,g,b), rgba(r,g,b,a), rgba(r,g,b,a%) /// decode color string supported formats: #RGB, #ARGB, #RRGGBB, #AARRGGBB, rgb(r,g,b), rgba(r,g,b,a), rgba(r,g,b,a%)
//TODO: the name doesn't match function //TODO: the name doesn't match function
uint decodeHexColor(string s, uint defValue = 0) { //pure uint decodeHexColor(string s, uint defValue = 0) @safe
{
s = s.strip.toLower; s = s.strip.toLower;
return decodeColorInternal(s, defValue);
}
///
@safe unittest
{
assert(decodeHexColor("") == 0);
assert(decodeHexColor("@null") == COLOR_TRANSPARENT);
assert(decodeHexColor("trAnsParent") == COLOR_TRANSPARENT);
assert(decodeHexColor("grAy") == 0x808080);
assert(decodeHexColor("#8B008B") == 0x8B008B);
assert(decodeHexColor("#fFf") == 0xfff);
assert(decodeHexColor("#f0F0") == 0xf0f0);
assert(decodeHexColor("#80ff0000") == 0x80ff0000);
assert(decodeHexColor("rgba(255, 0, 0,.5 )") == 0x80ff0000);
assert(decodeHexColor("rgba(255,0, 0, 50%)") == 0x80ff0000);
assert(decodeHexColor("rgba(255,0, 0, 100%)") == 0xff0000);
assert(decodeHexColor("rgba(255,0, 0, 0%)") == 0x00000000);
assert(decodeHexColor("rgb(255,255, 255)") == 0xffffff);
assert(decodeHexColor("rgba(255,0, 0, 150%)") == 0xff0000); // invalid input
assert(decodeHexColor("rgba(255,0, 0, -34%)") == 0x00000000); // invalid input
assert(decodeHexColor("rgb(321,321,321)") == 0xffffff); // invalid input
assert(decodeHexColor("not_valid_color_name") == 0x00000000); // invalid input, return def value
assert(decodeHexColor("#80ff00000") == 0x000000000); // invalid input, return def value
assert(decodeHexColor("#f0") == 0x00000000); // invalid input, return def value
assert(decodeHexColor("rgba(255,255, 255, 10)") == 0xffffff); // invalid input
assert(decodeHexColor("rgba(444,0, 0, -5)") == 0x00000000); // invalid input
}
uint decodeCSSColor(string s, uint defValue = 0) @safe
{
s = s.strip.toLower;
if (s.startsWith("#")) {
if (s.length.among(7, 9)) { //#RRGGBB #AARRGGBB
//s = s[1 .. $];
import std.stdio : writeln;
auto d = s[1 .. $];
auto color = parse!uint(d, 16); //RGB(A) by default
if (s.length == 5)
{ //RGBA
color = ((color & 0xF00) >> 4) | ((color & 0xF0) << 8) | ((color & 0xF) << 20);
}
else if (s.length == 9)
{ //RRGGBBAA
color = ((color & 0xFF) << 24) | (color >> 8);
}
return color;
}
return defValue;
}
return decodeColorInternal(s, defValue);
}
///
@safe unittest
{
assert(decodeCSSColor("gray") == Color.gray);
assert(decodeCSSColor("#AABBCC80") == 0x80AABBCC);
}
@safe unittest
{
assert(decodeCSSColor("#AABBCC80") == decodeColorInternal("#80AABBCC"));
assert(decodeCSSColor("#FF000080") == decodeColorInternal("#80FF0000"));
assert(decodeCSSColor("#0000FF80") == decodeColorInternal("#800000FF"));
}
private uint decodeColorInternal(string s, uint defValue = 0) @safe
{
if (s.empty) if (s.empty)
return defValue; return defValue;
if (s == "@null" || s == "transparent") if (s == "@null" || s == "transparent")
@ -329,15 +398,6 @@ uint decodeHexColor(string s, uint defValue = 0) { //pure
if (s.length.among(4, 5, 7, 9)) { //#RGB #ARGB #RRGGBB #AARRGGBB if (s.length.among(4, 5, 7, 9)) { //#RGB #ARGB #RRGGBB #AARRGGBB
s = s[1 .. $]; s = s[1 .. $];
auto color = parse!uint(s, 16); //RGB(A) by default auto color = parse!uint(s, 16); //RGB(A) by default
if (s.length == 4)
{ //ARGB
color = ((color & 0xF00) >> 4) | ((color & 0xF0) << 8) | ((color & 0xF) << 20);
}
else if (s.length == 8)
{ //AARRGGBB
color = ((color & 0xFF00) >> 8) | ((color & 0xFF) << 24) | (
(color & 0xFF0000) >> 8) | ((color & 0xFF000000) >> 24);
}
return color; return color;
} }
return defValue; return defValue;
@ -383,29 +443,3 @@ uint decodeHexColor(string s, uint defValue = 0) { //pure
} }
return defValue; return defValue;
} }
unittest
{
static assert(decodeHexColor("") == 0);
static assert(decodeHexColor("@null") == COLOR_TRANSPARENT);
static assert(decodeHexColor("trAnsParent") == COLOR_TRANSPARENT);
static assert(decodeHexColor("grAy") == 0x808080);
static assert(decodeHexColor("#8B008B") == 0x8B008B);
static assert(decodeHexColor("#fFf") == 0xfff);
static assert(decodeHexColor("#f0F0") == 0xf0f0);
static assert(decodeHexColor("#80ff0000") == 0x80ff0000);
static assert(decodeHexColor("rgba(255, 0, 0,.5 )") == 0x80ff0000);
static assert(decodeHexColor("rgba(255,0, 0, 50%)") == 0x80ff0000);
static assert(decodeHexColor("rgba(255,0, 0, 100%)") == 0xff0000);
static assert(decodeHexColor("rgba(255,0, 0, 0%)") == 0x00000000);
static assert(decodeHexColor("rgb(255,255, 255)") == 0xffffff);
static assert(decodeHexColor("rgba(255,0, 0, 150%)") == 0xff0000); // invalid input
static assert(decodeHexColor("rgba(255,0, 0, -34%)") == 0x00000000); // invalid input
static assert(decodeHexColor("rgb(321,321,321)") == 0xffffff); // invalid input
static assert(decodeHexColor("not_valid_color_name") == 0x00000000); // invalid input, return def value
static assert(decodeHexColor("#80ff00000") == 0x000000000); // invalid input, return def value
static assert(decodeHexColor("#f0") == 0x00000000); // invalid input, return def value
static assert(decodeHexColor("rgba(255,255, 255, 10)") == 0xffffff); // invalid input
static assert(decodeHexColor("rgba(444,0, 0, -5)") == 0x00000000); // invalid input
}

View File

@ -118,7 +118,7 @@ class AppFrame : VerticalLayout, MenuItemClickHandler, MenuItemActionHandler {
bool applyShortcutsSettings() { bool applyShortcutsSettings() {
if (shortcutSettings.loaded) { if (shortcutSettings.loaded) {
foreach(key, value; _shortcutSettings.map) { foreach(key, value; _shortcutSettings.setting.map) {
int actionId = actionNameToId(key); int actionId = actionNameToId(key);
if (actionId == 0) { if (actionId == 0) {
Log.e("applyShortcutsSettings: Unknown action name: ", key); Log.e("applyShortcutsSettings: Unknown action name: ", key);
@ -153,20 +153,20 @@ class AppFrame : VerticalLayout, MenuItemClickHandler, MenuItemActionHandler {
/// set shortcut settings from actions and save to file - useful for initial settings file version creation /// set shortcut settings from actions and save to file - useful for initial settings file version creation
bool saveShortcutsSettings(const(Action)[] actions) { bool saveShortcutsSettings(const(Action)[] actions) {
shortcutSettings.clear(); shortcutSettings.setting.clear();
foreach(a; actions) { foreach(a; actions) {
string name = actionIdToName(a.id); string name = actionIdToName(a.id);
if (name) { if (name) {
const(Accelerator)[] acc = a.accelerators; const(Accelerator)[] acc = a.accelerators;
if (acc.length > 0) { if (acc.length > 0) {
if (acc.length == 1) { if (acc.length == 1) {
_shortcutSettings[name] = acc[0].toString; _shortcutSettings.setting[name] = acc[0].toString;
} else { } else {
string[] array; string[] array;
foreach(accel; acc) { foreach(accel; acc) {
array ~= accel.toString; array ~= accel.toString;
} }
_shortcutSettings[name] = array; _shortcutSettings.setting[name] = array;
} }
} }
} }

View File

@ -163,15 +163,15 @@ private string[] generatePropertyTypeList(alias T)() {
foreach(m; __traits(allMembers, T)) { foreach(m; __traits(allMembers, T)) {
static if (__traits(compiles, (typeof(__traits(getMember, T, m))))){ static if (__traits(compiles, (typeof(__traits(getMember, T, m))))){
//static if (is (typeof(__traits(getMember, T, m)) == function)) { //static if (is (typeof(__traits(getMember, T, m)) == function)) {
static if (__traits(isVirtualFunction, __traits(getMember, T, m))) {// static if (__traits(isVirtualMethod, __traits(getMember, T, m))) {//
import std.traits : MemberFunctionsTuple; import std.traits : MemberFunctionsTuple;
alias overloads = typeof(__traits(getVirtualFunctions, T, m)); alias overloads = typeof(__traits(getVirtualMethods, T, m));
static if (overloads.length == 2) { static if (overloads.length == 2) {
static if (isPublicPropertyFunction!(__traits(getVirtualFunctions, T, m)[0]) && isPublicPropertyFunction!(__traits(getVirtualFunctions, T, m)[1])) { static if (isPublicPropertyFunction!(__traits(getVirtualMethods, T, m)[0]) && isPublicPropertyFunction!(__traits(getVirtualMethods, T, m)[1])) {
//pragma(msg, m ~ " isPublicPropertyFunction0=" ~ isPublicPropertyFunction!(__traits(getVirtualFunctions, T, m)[0]).stringof); //pragma(msg, m ~ " isPublicPropertyFunction0=" ~ isPublicPropertyFunction!(__traits(getVirtualFunctions, T, m)[0]).stringof);
//pragma(msg, m ~ " isPublicPropertyFunction1=" ~ isPublicPropertyFunction!(__traits(getVirtualFunctions, T, m)[1]).stringof); //pragma(msg, m ~ " isPublicPropertyFunction1=" ~ isPublicPropertyFunction!(__traits(getVirtualFunctions, T, m)[1]).stringof);
immutable getterType = markupPropertyType!(__traits(getVirtualFunctions, T, m)[0]); immutable getterType = markupPropertyType!(__traits(getVirtualMethods, T, m)[0]);
immutable setterType = markupPropertyType!(__traits(getVirtualFunctions, T, m)[1]); immutable setterType = markupPropertyType!(__traits(getVirtualMethods, T, m)[1]);
static if (getterType && setterType && getterType == setterType) { static if (getterType && setterType && getterType == setterType) {
//pragma(msg, "markup property found: " ~ getterType ~ " " ~ m.stringof); //pragma(msg, "markup property found: " ~ getterType ~ " " ~ m.stringof);
properties ~= "WidgetPropertyMetadata( typeid(" ~ getterType ~ "), " ~ m.stringof ~ " ), "; properties ~= "WidgetPropertyMetadata( typeid(" ~ getterType ~ "), " ~ m.stringof ~ " ), ";