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

LibGUI: Recalculate GlyphMapWidget content size when changing font

Previously, if it was displaying N glyphs per line, then you changed
font to one that was a drastically different size, it would continue to
display N glyphs per line until you resized the window. Now, we
immediately recalculate how many to show, so that they fill the
available width. :^)
This commit is contained in:
Sam Atkins 2022-01-09 17:42:57 +00:00 committed by Andreas Kling
parent 21a24c36a8
commit 15a7dfbc30
2 changed files with 19 additions and 12 deletions

View file

@ -60,18 +60,7 @@ GlyphMapWidget::~GlyphMapWidget()
void GlyphMapWidget::resize_event(ResizeEvent& event)
{
int event_width = event.size().width() - vertical_scrollbar().width() - (frame_thickness() * 2) - m_horizontal_spacing;
int event_height = event.size().height() - (frame_thickness() * 2);
m_visible_glyphs = (event_width * event_height) / (font().max_glyph_width() * font().glyph_height());
m_columns = max(event_width / (font().max_glyph_width() + m_horizontal_spacing), 1);
m_rows = ceil_div(m_glyph_count, m_columns);
int content_width = columns() * (font().max_glyph_width() + m_horizontal_spacing);
int content_height = rows() * (font().glyph_height() + m_vertical_spacing) + frame_thickness();
set_content_size({ content_width, content_height });
scroll_to_glyph(m_active_glyph);
recalculate_content_size();
AbstractScrollableWidget::resize_event(event);
}
@ -245,6 +234,7 @@ void GlyphMapWidget::keydown_event(KeyEvent& event)
void GlyphMapWidget::did_change_font()
{
recalculate_content_size();
vertical_scrollbar().set_step(font().glyph_height() + m_vertical_spacing);
set_active_glyph('A');
}
@ -262,4 +252,20 @@ void GlyphMapWidget::scroll_to_glyph(int glyph)
scroll_into_view(scroll_rect, true, true);
}
void GlyphMapWidget::recalculate_content_size()
{
auto inner_rect = frame_inner_rect();
int event_width = inner_rect.width() - vertical_scrollbar().width() - m_horizontal_spacing;
int event_height = inner_rect.height();
m_visible_glyphs = (event_width * event_height) / (font().max_glyph_width() * font().glyph_height());
m_columns = max(event_width / (font().max_glyph_width() + m_horizontal_spacing), 1);
m_rows = ceil_div(m_glyph_count, m_columns);
int content_width = columns() * (font().max_glyph_width() + m_horizontal_spacing);
int content_height = rows() * (font().glyph_height() + m_vertical_spacing) + frame_thickness();
set_content_size({ content_width, content_height });
scroll_to_glyph(m_active_glyph);
}
}