summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSam Atkins <atkinssj@serenityos.org>2022-01-06 16:59:58 +0000
committerAndreas Kling <kling@serenityos.org>2022-01-16 11:17:03 +0100
commitdd17df76e9d02b2969b1c4771134a30a4a844e31 (patch)
treeb4632d64dbd23c05edfc6f7ed0075c082c8d1b29
parent49011e2030ea4dc8d4405fef53a79927b6ac545b (diff)
downloadserenity-dd17df76e9d02b2969b1c4771134a30a4a844e31.zip
LibGUI: Make button icons settable in GML
`Widget::load_from_gml()` doesn't yet return `ErrorOr`, so we log a warning message if loading the icon fails.
-rw-r--r--Userland/Libraries/LibGUI/Button.cpp12
-rw-r--r--Userland/Libraries/LibGUI/Button.h1
2 files changed, 13 insertions, 0 deletions
diff --git a/Userland/Libraries/LibGUI/Button.cpp b/Userland/Libraries/LibGUI/Button.cpp
index 456191fd4b..7cd7d440a0 100644
--- a/Userland/Libraries/LibGUI/Button.cpp
+++ b/Userland/Libraries/LibGUI/Button.cpp
@@ -29,6 +29,8 @@ Button::Button(String text)
"button_style", button_style, set_button_style, Gfx::ButtonStyle,
{ Gfx::ButtonStyle::Normal, "Normal" },
{ Gfx::ButtonStyle::Coolbar, "Coolbar" });
+
+ REGISTER_STRING_PROPERTY("icon", icon, set_icon_from_path);
}
Button::~Button()
@@ -140,6 +142,16 @@ void Button::set_icon(RefPtr<Gfx::Bitmap>&& icon)
update();
}
+void Button::set_icon_from_path(String const& path)
+{
+ auto maybe_bitmap = Gfx::Bitmap::try_load_from_file(path);
+ if (maybe_bitmap.is_error()) {
+ dbgln("Unable to load bitmap `{}` for button icon", path);
+ return;
+ }
+ set_icon(maybe_bitmap.release_value());
+}
+
bool Button::is_uncheckable() const
{
if (!m_action)
diff --git a/Userland/Libraries/LibGUI/Button.h b/Userland/Libraries/LibGUI/Button.h
index 26d1588ed8..0ab17fd88d 100644
--- a/Userland/Libraries/LibGUI/Button.h
+++ b/Userland/Libraries/LibGUI/Button.h
@@ -21,6 +21,7 @@ public:
virtual ~Button() override;
void set_icon(RefPtr<Gfx::Bitmap>&&);
+ void set_icon_from_path(String const&);
const Gfx::Bitmap* icon() const { return m_icon.ptr(); }
Gfx::Bitmap* icon() { return m_icon.ptr(); }