From cca510162ed3c1f720f0d2ef11849ab755d25484 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sat, 25 May 2019 20:15:52 +0200 Subject: [PATCH] GButton: Align the button text according to text_alignment(). Added a Rect::align_within(other_rect, alignment) helper that seemed useful. --- LibGUI/GButton.cpp | 4 ++-- SharedGraphics/Rect.cpp | 20 ++++++++++++++++++++ SharedGraphics/Rect.h | 7 +++++-- 3 files changed, 27 insertions(+), 4 deletions(-) diff --git a/LibGUI/GButton.cpp b/LibGUI/GButton.cpp index 7cafa76f1a..62e521702d 100644 --- a/LibGUI/GButton.cpp +++ b/LibGUI/GButton.cpp @@ -52,8 +52,8 @@ void GButton::paint_event(GPaintEvent& event) Rect text_rect { 0, 0, font.width(text()), font.glyph_height() }; if (text_rect.width() > content_rect.width()) text_rect.set_width(content_rect.width()); - text_rect.center_within(content_rect); - paint_text(painter, text_rect, font, text_alignment()); + text_rect.align_within(content_rect, text_alignment()); + paint_text(painter, text_rect, font, TextAlignment::Center); } void GButton::click() diff --git a/SharedGraphics/Rect.cpp b/SharedGraphics/Rect.cpp index d97ce129dd..6013dc6343 100644 --- a/SharedGraphics/Rect.cpp +++ b/SharedGraphics/Rect.cpp @@ -76,3 +76,23 @@ Vector Rect::shatter(const Rect& hammer) const return pieces; } + +void Rect::align_within(const Rect& other, TextAlignment alignment) +{ + switch (alignment) { + case TextAlignment::Center: + center_within(other); + return; + case TextAlignment::TopLeft: + set_location(other.location()); + return; + case TextAlignment::CenterLeft: + set_x(other.x()); + center_vertically_within(other); + return; + case TextAlignment::CenterRight: + set_x(other.x() + other.width() - width()); + center_vertically_within(other); + return; + } +} diff --git a/SharedGraphics/Rect.h b/SharedGraphics/Rect.h index 7c7ce72be2..05695b6996 100644 --- a/SharedGraphics/Rect.h +++ b/SharedGraphics/Rect.h @@ -1,8 +1,9 @@ #pragma once -#include "Point.h" -#include "Size.h" #include +#include +#include +#include struct WSAPI_Rect; @@ -207,6 +208,8 @@ public: Point bottom_left() const { return { left(), bottom() }; } Point bottom_right() const { return { right(), bottom() }; } + void align_within(const Rect&, TextAlignment); + void center_within(const Rect& other) { center_horizontally_within(other);