From d19edb076228009f53f74439a9e88113ef7c885f Mon Sep 17 00:00:00 2001 From: DoubleNegation <43982164+DoubleNegation@users.noreply.github.com> Date: Mon, 28 Jun 2021 13:30:28 +0200 Subject: [PATCH] LibGUI: Add foreground_role and background_role property to GUI::Widget These properties allow GML files to specify a Gfx::ColorRole instead of a color, so that the effective color of the Widget is resolved using the system theme. --- Userland/Libraries/LibGUI/Widget.cpp | 45 ++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/Userland/Libraries/LibGUI/Widget.cpp b/Userland/Libraries/LibGUI/Widget.cpp index 8dcb9a5062ed20..026bab275befd2 100644 --- a/Userland/Libraries/LibGUI/Widget.cpp +++ b/Userland/Libraries/LibGUI/Widget.cpp @@ -21,6 +21,7 @@ #include #include #include +#include #include REGISTER_CORE_OBJECT(GUI, Widget) @@ -136,6 +137,50 @@ Widget::Widget() } return false; }); + + register_property( + "foreground_role", [this]() -> JsonValue { return Gfx::to_string(foreground_role()); }, + [this](auto& value) { + if (!value.is_string()) + return false; + auto str = value.as_string(); + if (str == "NoRole") { + set_foreground_role(Gfx::ColorRole::NoRole); + return true; + } +#undef __ENUMERATE_COLOR_ROLE +#define __ENUMERATE_COLOR_ROLE(role) \ + else if (str == #role) \ + { \ + set_foreground_role(Gfx::ColorRole::role); \ + return true; \ + } + ENUMERATE_COLOR_ROLES(__ENUMERATE_COLOR_ROLE) +#undef __ENUMERATE_COLOR_ROLE + return false; + }); + + register_property( + "background_role", [this]() -> JsonValue { return Gfx::to_string(background_role()); }, + [this](auto& value) { + if (!value.is_string()) + return false; + auto str = value.as_string(); + if (str == "NoRole") { + set_background_role(Gfx::ColorRole::NoRole); + return true; + } +#undef __ENUMERATE_COLOR_ROLE +#define __ENUMERATE_COLOR_ROLE(role) \ + else if (str == #role) \ + { \ + set_background_role(Gfx::ColorRole::role); \ + return true; \ + } + ENUMERATE_COLOR_ROLES(__ENUMERATE_COLOR_ROLE) +#undef __ENUMERATE_COLOR_ROLE + return false; + }); } Widget::~Widget()