Skip to content

Commit

Permalink
LibGUI: Add foreground_role and background_role property to GUI::Widget
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
DoubleNegation authored and awesomekling committed Jun 30, 2021
1 parent d0c7a48 commit d19edb0
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions Userland/Libraries/LibGUI/Widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <LibGfx/Font.h>
#include <LibGfx/FontDatabase.h>
#include <LibGfx/Palette.h>
#include <LibGfx/SystemTheme.h>
#include <unistd.h>

REGISTER_CORE_OBJECT(GUI, Widget)
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit d19edb0

Please sign in to comment.