Skip to content

Commit

Permalink
Fix deprecations for 2.103; Add decodeCSSColor
Browse files Browse the repository at this point in the history
  • Loading branch information
GrimMaple committed May 18, 2023
1 parent 475e0bb commit 76daac9
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 46 deletions.
1 change: 0 additions & 1 deletion src/dlangui/core/settings.d
Expand Up @@ -79,7 +79,6 @@ class SettingsFile {
_setting = settings;
//_setting.apply(settings);
}
alias setting this;

/// create settings file object; if filename is provided, attempts to load settings from file
this(string filename = null) {
Expand Down
106 changes: 70 additions & 36 deletions src/dlangui/graphics/colors.d
Expand Up @@ -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%)
//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;
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)
return defValue;
if (s == "@null" || s == "transparent")
Expand All @@ -329,15 +398,6 @@ uint decodeHexColor(string s, uint defValue = 0) { //pure
if (s.length.among(4, 5, 7, 9)) { //#RGB #ARGB #RRGGBB #AARRGGBB
s = s[1 .. $];
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 defValue;
Expand Down Expand Up @@ -383,29 +443,3 @@ uint decodeHexColor(string s, uint defValue = 0) { //pure
}
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
}

8 changes: 4 additions & 4 deletions src/dlangui/widgets/appframe.d
Expand Up @@ -118,7 +118,7 @@ class AppFrame : VerticalLayout, MenuItemClickHandler, MenuItemActionHandler {

bool applyShortcutsSettings() {
if (shortcutSettings.loaded) {
foreach(key, value; _shortcutSettings.map) {
foreach(key, value; _shortcutSettings.setting.map) {
int actionId = actionNameToId(key);
if (actionId == 0) {
Log.e("applyShortcutsSettings: Unknown action name: ", key);
Expand Down Expand Up @@ -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
bool saveShortcutsSettings(const(Action)[] actions) {
shortcutSettings.clear();
shortcutSettings.setting.clear();
foreach(a; actions) {
string name = actionIdToName(a.id);
if (name) {
const(Accelerator)[] acc = a.accelerators;
if (acc.length > 0) {
if (acc.length == 1) {
_shortcutSettings[name] = acc[0].toString;
_shortcutSettings.setting[name] = acc[0].toString;
} else {
string[] array;
foreach(accel; acc) {
array ~= accel.toString;
}
_shortcutSettings[name] = array;
_shortcutSettings.setting[name] = array;
}
}
}
Expand Down
10 changes: 5 additions & 5 deletions src/dlangui/widgets/metadata.d
Expand Up @@ -163,15 +163,15 @@ private string[] generatePropertyTypeList(alias T)() {
foreach(m; __traits(allMembers, T)) {
static if (__traits(compiles, (typeof(__traits(getMember, T, m))))){
//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;
alias overloads = typeof(__traits(getVirtualFunctions, T, m));
alias overloads = typeof(__traits(getVirtualMethods, T, m));
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 ~ " isPublicPropertyFunction1=" ~ isPublicPropertyFunction!(__traits(getVirtualFunctions, T, m)[1]).stringof);
immutable getterType = markupPropertyType!(__traits(getVirtualFunctions, T, m)[0]);
immutable setterType = markupPropertyType!(__traits(getVirtualFunctions, T, m)[1]);
immutable getterType = markupPropertyType!(__traits(getVirtualMethods, T, m)[0]);
immutable setterType = markupPropertyType!(__traits(getVirtualMethods, T, m)[1]);
static if (getterType && setterType && getterType == setterType) {
//pragma(msg, "markup property found: " ~ getterType ~ " " ~ m.stringof);
properties ~= "WidgetPropertyMetadata( typeid(" ~ getterType ~ "), " ~ m.stringof ~ " ), ";
Expand Down

0 comments on commit 76daac9

Please sign in to comment.