LibGUI: Make CheckBox height adapt to the current font size

This commit is contained in:
Andreas Kling 2023-03-03 10:57:12 +01:00
parent fdcdc14fce
commit ee001cc417
2 changed files with 48 additions and 18 deletions

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2018-2023, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2022, the SerenityOS developers.
*
* SPDX-License-Identifier: BSD-2-Clause
@ -16,10 +16,6 @@ REGISTER_WIDGET(GUI, CheckBox)
namespace GUI {
static constexpr int s_box_width = 13;
static constexpr int s_box_height = 13;
static constexpr int s_horizontal_padding = 6;
CheckBox::CheckBox(String text)
: AbstractButton(move(text))
{
@ -30,8 +26,34 @@ CheckBox::CheckBox(String text)
{ CheckBoxPosition::Left, "Left" },
{ CheckBoxPosition::Right, "Right" });
set_min_size({ 22, 22 });
set_preferred_size({ SpecialDimension::OpportunisticGrow, 22 });
set_min_size(SpecialDimension::Shrink, SpecialDimension::Shrink);
set_preferred_size(SpecialDimension::OpportunisticGrow, SpecialDimension::Shrink);
}
int CheckBox::gap_between_box_and_rect() const
{
return 6;
}
int CheckBox::horizontal_padding() const
{
return 2;
}
Gfx::IntRect CheckBox::box_rect() const
{
int box_size = max(13, height() - 10);
Gfx::IntRect box_rect {
0,
height() / 2 - box_size / 2 - 1,
box_size,
box_size,
};
if (m_checkbox_position == CheckBoxPosition::Right)
box_rect.set_right_without_resize(rect().right());
return box_rect;
}
void CheckBox::paint_event(PaintEvent& event)
@ -39,12 +61,13 @@ void CheckBox::paint_event(PaintEvent& event)
Painter painter(*this);
painter.add_clip_rect(event.rect());
auto box_rect = this->box_rect();
auto text_rect = rect();
if (m_checkbox_position == CheckBoxPosition::Left)
text_rect.set_left(s_box_width + s_horizontal_padding);
text_rect.set_left(box_rect.right() + 1 + gap_between_box_and_rect());
text_rect.set_width(static_cast<int>(ceilf(font().width(text()))));
text_rect.set_top(height() / 2 - static_cast<int>(ceilf(font().glyph_height()) / 2));
text_rect.set_height(static_cast<int>(ceilf(font().glyph_height())));
text_rect.set_top(height() / 2 - static_cast<int>(ceilf(font().pixel_size()) / 2));
text_rect.set_height(static_cast<int>(ceilf(font().pixel_size())));
if (fill_with_background_color())
painter.fill_rect(rect(), palette().window());
@ -52,13 +75,6 @@ void CheckBox::paint_event(PaintEvent& event)
if (is_enabled() && is_hovered())
painter.fill_rect(rect(), palette().hover_highlight());
Gfx::IntRect box_rect {
0, height() / 2 - s_box_height / 2 - 1,
s_box_width, s_box_height
};
if (m_checkbox_position == CheckBoxPosition::Right)
box_rect.set_right_without_resize(rect().right());
Gfx::StylePainter::paint_check_box(painter, box_rect, palette(), is_enabled(), is_checked(), is_being_pressed());
paint_text(painter, text_rect, font(), Gfx::TextAlignment::TopLeft);
@ -85,7 +101,15 @@ void CheckBox::set_autosize(bool autosize)
void CheckBox::size_to_fit()
{
set_fixed_width(s_box_width + static_cast<int>(ceilf(font().width(text()))) + s_horizontal_padding * 2);
set_fixed_width(box_rect().width() + gap_between_box_and_rect() + static_cast<int>(ceilf(font().width(text()))) + horizontal_padding() * 2);
}
Optional<UISize> CheckBox::calculated_min_size() const
{
auto const& font = this->font();
int width = box_rect().width();
int height = max(22, max(static_cast<int>(ceilf(font.pixel_size())) + 8, box_rect().height()));
return UISize(width, height);
}
}

View file

@ -29,6 +29,8 @@ public:
CheckBoxPosition checkbox_position() const { return m_checkbox_position; }
void set_checkbox_position(CheckBoxPosition value) { m_checkbox_position = value; }
virtual Optional<UISize> calculated_min_size() const override;
protected:
explicit CheckBox(String = {});
@ -41,6 +43,10 @@ private:
virtual void paint_event(PaintEvent&) override;
Gfx::IntRect box_rect() const;
int gap_between_box_and_rect() const;
int horizontal_padding() const;
bool m_autosize { false };
CheckBoxPosition m_checkbox_position { CheckBoxPosition::Left };
};