1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-14 05:44:58 +00:00

LibGUI: Fix bad title alignment in GroupBox

Make a separate rect for the text and use IntRect::centered_within()
to sidestep any subpixel jitter. This way it looks good with both bitmap
and vector fonts.
This commit is contained in:
Andreas Kling 2023-05-15 14:15:53 +02:00
parent 20e2c9a7dd
commit 4d0d0a99b4

View file

@ -43,8 +43,14 @@ void GroupBox::paint_event(PaintEvent& event)
Gfx::StylePainter::paint_frame(painter, frame_rect, palette(), Gfx::FrameStyle::SunkenBox);
if (!m_title.is_empty()) {
Gfx::IntRect text_rect { 6, 1, font().width_rounded_up(m_title) + 6, font().pixel_size_rounded_up() };
painter.fill_rect(text_rect, palette().button());
// Fill with button background behind the text (covering the frame).
Gfx::IntRect text_background_rect { 6, 1, font().width_rounded_up(m_title) + 6, font().pixel_size_rounded_up() };
painter.fill_rect(text_background_rect, palette().button());
// Center text within button background rect to ensure symmetric padding on both sides.
// Note that we don't use TextAlignment::Center here to avoid subpixel jitter.
Gfx::IntRect text_rect { 0, 0, text_background_rect.width() - 6, text_background_rect.height() };
text_rect.center_within(text_background_rect);
painter.draw_text(text_rect, m_title, Gfx::TextAlignment::CenterLeft, palette().button_text());
}
}