From e8c1288e2c21bbc71e430a1c9699ae09a77f3c62 Mon Sep 17 00:00:00 2001 From: thankyouverycool <66646555+thankyouverycool@users.noreply.github.com> Date: Sun, 4 Apr 2021 11:12:17 -0400 Subject: LibGUI: Add autosize to CheckBox --- Userland/Libraries/LibGUI/CheckBox.cpp | 19 ++++++++++++++++++- Userland/Libraries/LibGUI/CheckBox.h | 7 +++++++ 2 files changed, 25 insertions(+), 1 deletion(-) (limited to 'Userland') diff --git a/Userland/Libraries/LibGUI/CheckBox.cpp b/Userland/Libraries/LibGUI/CheckBox.cpp index b2f4a10456..25688567ee 100644 --- a/Userland/Libraries/LibGUI/CheckBox.cpp +++ b/Userland/Libraries/LibGUI/CheckBox.cpp @@ -37,10 +37,13 @@ namespace GUI { static const int s_box_width = 13; static const int s_box_height = 13; +static const int s_horizontal_padding = 4; CheckBox::CheckBox(String text) : AbstractButton(move(text)) { + REGISTER_BOOL_PROPERTY("autosize", is_autosize, set_autosize); + set_min_width(32); set_fixed_height(22); } @@ -55,7 +58,7 @@ void CheckBox::paint_event(PaintEvent& event) painter.add_clip_rect(event.rect()); auto text_rect = rect(); - text_rect.set_left(s_box_width + 4); + text_rect.set_left(s_box_width + s_horizontal_padding); text_rect.set_width(font().width(text())); text_rect.set_top(height() / 2 - font().glyph_height() / 2); text_rect.set_height(font().glyph_height()); @@ -86,4 +89,18 @@ void CheckBox::click(unsigned) set_checked(!is_checked()); } +void CheckBox::set_autosize(bool autosize) +{ + if (m_autosize == autosize) + return; + m_autosize = autosize; + if (m_autosize) + size_to_fit(); +} + +void CheckBox::size_to_fit() +{ + set_fixed_width(s_box_width + font().width(text()) + s_horizontal_padding * 2); +} + } diff --git a/Userland/Libraries/LibGUI/CheckBox.h b/Userland/Libraries/LibGUI/CheckBox.h index 733a264321..4568a6a2dd 100644 --- a/Userland/Libraries/LibGUI/CheckBox.h +++ b/Userland/Libraries/LibGUI/CheckBox.h @@ -38,14 +38,21 @@ public: virtual void click(unsigned modifiers = 0) override; + bool is_autosize() const { return m_autosize; } + void set_autosize(bool); + private: explicit CheckBox(String = {}); + void size_to_fit(); + // These don't make sense for a check box, so hide them. using AbstractButton::auto_repeat_interval; using AbstractButton::set_auto_repeat_interval; virtual void paint_event(PaintEvent&) override; + + bool m_autosize { false }; }; } -- cgit v1.2.3