1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 18:47:34 +00:00

LibGUI: Add on_glyph_double_clicked() callback to GlyphMapWidget

This will be used by CharacterMap.

In implementing this, extracted the logic for finding which glyph is at
a given position within the widget.
This commit is contained in:
Sam Atkins 2022-01-11 20:10:32 +00:00 committed by Andreas Kling
parent 2327ea8970
commit ff500ffcc4
2 changed files with 27 additions and 6 deletions

View file

@ -135,16 +135,25 @@ void GlyphMapWidget::paint_event(PaintEvent& event)
painter.draw_focus_rect(get_outer_rect(m_active_glyph), Gfx::Color::Black);
}
Optional<int> GlyphMapWidget::glyph_at_position(Gfx::IntPoint position) const
{
Gfx::IntPoint map_offset { frame_thickness() - horizontal_scrollbar().value(), frame_thickness() - vertical_scrollbar().value() };
auto map_position = position - map_offset;
auto col = (map_position.x() - 1) / ((font().max_glyph_width() + m_horizontal_spacing));
auto row = (map_position.y() - 1) / ((font().glyph_height() + m_vertical_spacing));
auto glyph = row * columns() + col;
if (row >= 0 && row < rows() && col >= 0 && col < columns() && glyph < m_glyph_count)
return glyph;
return {};
}
void GlyphMapWidget::mousedown_event(MouseEvent& event)
{
Frame::mousedown_event(event);
Gfx::IntPoint map_offset { frame_thickness() - horizontal_scrollbar().value(), frame_thickness() - vertical_scrollbar().value() };
auto map_position = event.position() - map_offset;
auto col = (map_position.x() - 1) / ((font().max_glyph_width() + m_horizontal_spacing));
auto row = (map_position.y() - 1) / ((font().glyph_height() + m_vertical_spacing));
auto glyph = row * columns() + col;
if (row >= 0 && row < rows() && col >= 0 && col < columns() && glyph < m_glyph_count) {
if (auto maybe_glyph = glyph_at_position(event.position()); maybe_glyph.has_value()) {
auto glyph = maybe_glyph.value();
if (event.shift())
m_selection.extend_to(glyph);
else {
@ -155,6 +164,15 @@ void GlyphMapWidget::mousedown_event(MouseEvent& event)
}
}
void GlyphMapWidget::doubleclick_event(MouseEvent& event)
{
Widget::doubleclick_event(event);
if (on_glyph_double_clicked) {
if (auto maybe_glyph = glyph_at_position(event.position()); maybe_glyph.has_value())
on_glyph_double_clicked(maybe_glyph.value());
}
}
void GlyphMapWidget::keydown_event(KeyEvent& event)
{
Frame::keydown_event(event);