1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-14 09:04:59 +00:00

LibGUI/Widget: Add helpers to set the background color

This commit is contained in:
david072 2023-11-11 13:38:35 +01:00 committed by Andreas Kling
parent e4019ba9dc
commit 999a44969d
2 changed files with 25 additions and 9 deletions

View file

@ -152,15 +152,12 @@ Widget::Widget()
register_property(
"background_color", [this]() -> JsonValue { return palette().color(background_role()).to_deprecated_string(); },
[this](auto& value) {
auto c = Color::from_string(value.to_deprecated_string());
if (c.has_value()) {
auto _palette = palette();
_palette.set_color(background_role(), c.value());
set_palette(_palette);
return true;
}
return false;
[this](JsonValue const& value) {
auto color_str = String::from_deprecated_string(value.to_deprecated_string());
if (color_str.is_error())
return false;
return set_background_color(color_str.release_value());
});
register_property(
@ -1085,6 +1082,22 @@ void Widget::set_foreground_role(ColorRole role)
update();
}
bool Widget::set_background_color(String color_str)
{
auto color = Color::from_string(color_str.to_deprecated_string());
if (!color.has_value())
return false;
set_background_color(color.release_value());
return true;
}
void Widget::set_background_color(Gfx::Color color)
{
auto _palette = palette();
_palette.set_color(background_role(), color);
set_palette(_palette);
}
Gfx::Palette Widget::palette() const
{
return Gfx::Palette(*m_palette);

View file

@ -254,6 +254,9 @@ public:
Gfx::ColorRole foreground_role() const { return m_foreground_role; }
void set_foreground_role(Gfx::ColorRole);
bool set_background_color(String);
void set_background_color(Gfx::Color);
void set_autofill(bool b) { set_fill_with_background_color(b); }
Window* window()