From ff500ffcc4f7dbb866bad36132e16169a50059b4 Mon Sep 17 00:00:00 2001 From: Sam Atkins Date: Tue, 11 Jan 2022 20:10:32 +0000 Subject: [PATCH] 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. --- Userland/Libraries/LibGUI/GlyphMapWidget.cpp | 30 ++++++++++++++++---- Userland/Libraries/LibGUI/GlyphMapWidget.h | 3 ++ 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/Userland/Libraries/LibGUI/GlyphMapWidget.cpp b/Userland/Libraries/LibGUI/GlyphMapWidget.cpp index 8a4fabdbd0..6b379d2dba 100644 --- a/Userland/Libraries/LibGUI/GlyphMapWidget.cpp +++ b/Userland/Libraries/LibGUI/GlyphMapWidget.cpp @@ -135,16 +135,25 @@ void GlyphMapWidget::paint_event(PaintEvent& event) painter.draw_focus_rect(get_outer_rect(m_active_glyph), Gfx::Color::Black); } +Optional 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); diff --git a/Userland/Libraries/LibGUI/GlyphMapWidget.h b/Userland/Libraries/LibGUI/GlyphMapWidget.h index 1ba13a9632..ed258225ad 100644 --- a/Userland/Libraries/LibGUI/GlyphMapWidget.h +++ b/Userland/Libraries/LibGUI/GlyphMapWidget.h @@ -60,16 +60,19 @@ public: int columns() const { return m_columns; } Function on_active_glyph_changed; + Function on_glyph_double_clicked; private: GlyphMapWidget(); virtual void paint_event(PaintEvent&) override; virtual void mousedown_event(MouseEvent&) override; + virtual void doubleclick_event(MouseEvent&) override; virtual void keydown_event(KeyEvent&) override; virtual void resize_event(ResizeEvent&) override; virtual void did_change_font() override; Gfx::IntRect get_outer_rect(int glyph) const; + Optional glyph_at_position(Gfx::IntPoint) const; void recalculate_content_size();