From 76daac9bf15b6c9bd7f3f072e6bf9fe20c0512eb Mon Sep 17 00:00:00 2001 From: Grim Maple Date: Thu, 18 May 2023 22:46:24 +0300 Subject: [PATCH] Fix deprecations for 2.103; Add `decodeCSSColor` --- src/dlangui/core/settings.d | 1 - src/dlangui/graphics/colors.d | 106 ++++++++++++++++++++++----------- src/dlangui/widgets/appframe.d | 8 +-- src/dlangui/widgets/metadata.d | 10 ++-- 4 files changed, 79 insertions(+), 46 deletions(-) diff --git a/src/dlangui/core/settings.d b/src/dlangui/core/settings.d index c21e8a8a5..f458e56c5 100644 --- a/src/dlangui/core/settings.d +++ b/src/dlangui/core/settings.d @@ -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) { diff --git a/src/dlangui/graphics/colors.d b/src/dlangui/graphics/colors.d index faa303903..0d3927c7e 100644 --- a/src/dlangui/graphics/colors.d +++ b/src/dlangui/graphics/colors.d @@ -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") @@ -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; @@ -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 -} - diff --git a/src/dlangui/widgets/appframe.d b/src/dlangui/widgets/appframe.d index 7c2acd50b..890c70193 100644 --- a/src/dlangui/widgets/appframe.d +++ b/src/dlangui/widgets/appframe.d @@ -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); @@ -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; } } } diff --git a/src/dlangui/widgets/metadata.d b/src/dlangui/widgets/metadata.d index 793bd49e6..bd873f7ac 100644 --- a/src/dlangui/widgets/metadata.d +++ b/src/dlangui/widgets/metadata.d @@ -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 ~ " ), ";