From 64ba41ea132b4572880826d1381ae1661edef7b9 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sun, 20 Dec 2020 12:35:10 +0100 Subject: [PATCH] LibGUI: Make GUI::Label auto-sizing declarative You can now set the "autosize" property on a GUI::Label and it will automatically update its width preference to fit the text. --- Applications/FileManager/FileManagerWindow.gml | 2 +- Applications/FileManager/main.cpp | 3 --- Libraries/LibGUI/Label.cpp | 12 ++++++++++++ Libraries/LibGUI/Label.h | 6 +++++- 4 files changed, 18 insertions(+), 5 deletions(-) diff --git a/Applications/FileManager/FileManagerWindow.gml b/Applications/FileManager/FileManagerWindow.gml index 3e38c907f0..e2e67d4499 100644 --- a/Applications/FileManager/FileManagerWindow.gml +++ b/Applications/FileManager/FileManagerWindow.gml @@ -13,8 +13,8 @@ visible: false @GUI::Label { - name: "location_label" text: "Location: " + autosize: true } @GUI::TextBox { diff --git a/Applications/FileManager/main.cpp b/Applications/FileManager/main.cpp index 99d387217d..e7ce374760 100644 --- a/Applications/FileManager/main.cpp +++ b/Applications/FileManager/main.cpp @@ -300,9 +300,6 @@ int run_in_windowed_mode(RefPtr config, String initial_locatio auto& location_toolbar = (GUI::ToolBar&)*widget.find_descendant_by_name("location_toolbar"); location_toolbar.layout()->set_margins({ 6, 3, 6, 3 }); - auto& location_label = (GUI::Label&)*widget.find_descendant_by_name("location_label"); - location_label.size_to_fit(); - auto& location_textbox = (GUI::TextBox&)*widget.find_descendant_by_name("location_textbox"); auto& breadcrumb_toolbar = (GUI::ToolBar&)*widget.find_descendant_by_name("breadcrumb_toolbar"); diff --git a/Libraries/LibGUI/Label.cpp b/Libraries/LibGUI/Label.cpp index a9904c20e9..878925fd55 100644 --- a/Libraries/LibGUI/Label.cpp +++ b/Libraries/LibGUI/Label.cpp @@ -42,12 +42,22 @@ Label::Label(const StringView& text) set_foreground_role(Gfx::ColorRole::WindowText); REGISTER_STRING_PROPERTY("text", text, set_text); + REGISTER_BOOL_PROPERTY("autosize", is_autosize, set_autosize); } Label::~Label() { } +void Label::set_autosize(bool autosize) +{ + if (m_autosize == autosize) + return; + m_autosize = autosize; + if (m_autosize) + size_to_fit(); +} + void Label::set_icon(const Gfx::Bitmap* icon) { if (m_icon == icon) @@ -61,6 +71,8 @@ void Label::set_text(const StringView& text) if (text == m_text) return; m_text = text; + if (m_autosize) + size_to_fit(); update(); } diff --git a/Libraries/LibGUI/Label.h b/Libraries/LibGUI/Label.h index bcc95f60bd..12d2e57278 100644 --- a/Libraries/LibGUI/Label.h +++ b/Libraries/LibGUI/Label.h @@ -49,7 +49,8 @@ public: bool should_stretch_icon() const { return m_should_stretch_icon; } void set_should_stretch_icon(bool b) { m_should_stretch_icon = b; } - void size_to_fit(); + bool is_autosize() const { return m_autosize; } + void set_autosize(bool); protected: explicit Label(const StringView& text = {}); @@ -57,10 +58,13 @@ protected: virtual void paint_event(PaintEvent&) override; private: + void size_to_fit(); + String m_text; RefPtr m_icon; Gfx::TextAlignment m_text_alignment { Gfx::TextAlignment::Center }; bool m_should_stretch_icon { false }; + bool m_autosize { false }; }; }