diff options
author | thankyouverycool <66646555+thankyouverycool@users.noreply.github.com> | 2021-04-04 11:12:17 -0400 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-04-06 22:24:05 +0200 |
commit | e8c1288e2c21bbc71e430a1c9699ae09a77f3c62 (patch) | |
tree | 0cf5e060868e0dcd727c17c3f015e18dab2fd64b /Userland/Libraries/LibGUI/CheckBox.cpp | |
parent | 8afe013069ef1058d93fd443de1babab3454f8ad (diff) | |
download | serenity-e8c1288e2c21bbc71e430a1c9699ae09a77f3c62.zip |
LibGUI: Add autosize to CheckBox
Diffstat (limited to 'Userland/Libraries/LibGUI/CheckBox.cpp')
-rw-r--r-- | Userland/Libraries/LibGUI/CheckBox.cpp | 19 |
1 files changed, 18 insertions, 1 deletions
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); +} + } |