mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 08:57:34 +00:00
LibGUI: Add autosize to CheckBox
This commit is contained in:
parent
8afe013069
commit
e8c1288e2c
2 changed files with 25 additions and 1 deletions
|
@ -37,10 +37,13 @@ namespace GUI {
|
||||||
|
|
||||||
static const int s_box_width = 13;
|
static const int s_box_width = 13;
|
||||||
static const int s_box_height = 13;
|
static const int s_box_height = 13;
|
||||||
|
static const int s_horizontal_padding = 4;
|
||||||
|
|
||||||
CheckBox::CheckBox(String text)
|
CheckBox::CheckBox(String text)
|
||||||
: AbstractButton(move(text))
|
: AbstractButton(move(text))
|
||||||
{
|
{
|
||||||
|
REGISTER_BOOL_PROPERTY("autosize", is_autosize, set_autosize);
|
||||||
|
|
||||||
set_min_width(32);
|
set_min_width(32);
|
||||||
set_fixed_height(22);
|
set_fixed_height(22);
|
||||||
}
|
}
|
||||||
|
@ -55,7 +58,7 @@ void CheckBox::paint_event(PaintEvent& event)
|
||||||
painter.add_clip_rect(event.rect());
|
painter.add_clip_rect(event.rect());
|
||||||
|
|
||||||
auto text_rect = 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_width(font().width(text()));
|
||||||
text_rect.set_top(height() / 2 - font().glyph_height() / 2);
|
text_rect.set_top(height() / 2 - font().glyph_height() / 2);
|
||||||
text_rect.set_height(font().glyph_height());
|
text_rect.set_height(font().glyph_height());
|
||||||
|
@ -86,4 +89,18 @@ void CheckBox::click(unsigned)
|
||||||
set_checked(!is_checked());
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -38,14 +38,21 @@ public:
|
||||||
|
|
||||||
virtual void click(unsigned modifiers = 0) override;
|
virtual void click(unsigned modifiers = 0) override;
|
||||||
|
|
||||||
|
bool is_autosize() const { return m_autosize; }
|
||||||
|
void set_autosize(bool);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
explicit CheckBox(String = {});
|
explicit CheckBox(String = {});
|
||||||
|
|
||||||
|
void size_to_fit();
|
||||||
|
|
||||||
// These don't make sense for a check box, so hide them.
|
// These don't make sense for a check box, so hide them.
|
||||||
using AbstractButton::auto_repeat_interval;
|
using AbstractButton::auto_repeat_interval;
|
||||||
using AbstractButton::set_auto_repeat_interval;
|
using AbstractButton::set_auto_repeat_interval;
|
||||||
|
|
||||||
virtual void paint_event(PaintEvent&) override;
|
virtual void paint_event(PaintEvent&) override;
|
||||||
|
|
||||||
|
bool m_autosize { false };
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue