GButton: Align the button text according to text_alignment().

Added a Rect::align_within(other_rect, alignment) helper that seemed useful.
This commit is contained in:
Andreas Kling 2019-05-25 20:15:52 +02:00
parent 75b0e5cce5
commit cca510162e
3 changed files with 27 additions and 4 deletions

View file

@ -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()

View file

@ -76,3 +76,23 @@ Vector<Rect> 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;
}
}

View file

@ -1,8 +1,9 @@
#pragma once
#include "Point.h"
#include "Size.h"
#include <AK/AKString.h>
#include <SharedGraphics/Point.h>
#include <SharedGraphics/Size.h>
#include <SharedGraphics/TextAlignment.h>
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);