diff options
author | Maciej <sppmacd@pm.me> | 2022-03-01 21:17:57 +0100 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2022-03-17 17:05:56 +0100 |
commit | a95794a35624e48a2bc4357a6c47da09801355b1 (patch) | |
tree | 2298e281b1fce74d134fce280e96c3f20b95c904 | |
parent | bee9cd21136308003fe932bb02e2729af12e2458 (diff) | |
download | serenity-a95794a35624e48a2bc4357a6c47da09801355b1.zip |
LibGUI: Clamp selection when drag-selecting over out of range area
-rw-r--r-- | Userland/Libraries/LibGUI/GlyphMapWidget.cpp | 30 | ||||
-rw-r--r-- | Userland/Libraries/LibGUI/GlyphMapWidget.h | 1 |
2 files changed, 21 insertions, 10 deletions
diff --git a/Userland/Libraries/LibGUI/GlyphMapWidget.cpp b/Userland/Libraries/LibGUI/GlyphMapWidget.cpp index 9e26303ede..5f9a45dd69 100644 --- a/Userland/Libraries/LibGUI/GlyphMapWidget.cpp +++ b/Userland/Libraries/LibGUI/GlyphMapWidget.cpp @@ -149,6 +149,18 @@ Optional<int> GlyphMapWidget::glyph_at_position(Gfx::IntPoint position) const return {}; } +int GlyphMapWidget::glyph_at_position_clamped(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 = clamp((map_position.x() - 1) / ((font().max_glyph_width() + m_horizontal_spacing)), 0, columns() - 1); + auto row = clamp((map_position.y() - 1) / ((font().glyph_height() + m_vertical_spacing)), 0, rows() - 1); + auto glyph = row * columns() + col + m_active_range.first; + if (row == rows() - 1) + glyph = min(glyph, m_glyph_count + m_active_range.first - 1); + return glyph; +} + void GlyphMapWidget::mousedown_event(MouseEvent& event) { Frame::mousedown_event(event); @@ -172,13 +184,13 @@ void GlyphMapWidget::mouseup_event(GUI::MouseEvent& event) if (!m_in_drag_select) return; - - if (auto maybe_glyph = glyph_at_position(event.position()); maybe_glyph.has_value()) { + auto constrained = event.position().constrained(rect().shrunken(0, frame_thickness() * 2)); + if (auto maybe_glyph = glyph_at_position(constrained); maybe_glyph.has_value()) { auto glyph = maybe_glyph.value(); m_selection.extend_to(glyph); - m_in_drag_select = false; set_active_glyph(glyph, ShouldResetSelection::No); } + m_in_drag_select = false; } void GlyphMapWidget::mousemove_event(GUI::MouseEvent& event) @@ -188,13 +200,11 @@ void GlyphMapWidget::mousemove_event(GUI::MouseEvent& event) if (!m_in_drag_select) return; - if (auto maybe_glyph = glyph_at_position(event.position()); maybe_glyph.has_value()) { - auto glyph = maybe_glyph.value(); - m_selection.extend_to(glyph); - set_active_glyph(glyph, ShouldResetSelection::No); - scroll_to_glyph(glyph); - update(); - } + auto glyph = glyph_at_position_clamped(event.position()); + m_selection.extend_to(glyph); + set_active_glyph(glyph, ShouldResetSelection::No); + scroll_to_glyph(glyph); + update(); } void GlyphMapWidget::doubleclick_event(MouseEvent& event) diff --git a/Userland/Libraries/LibGUI/GlyphMapWidget.h b/Userland/Libraries/LibGUI/GlyphMapWidget.h index 7fc07e1f50..b5ac0b8df5 100644 --- a/Userland/Libraries/LibGUI/GlyphMapWidget.h +++ b/Userland/Libraries/LibGUI/GlyphMapWidget.h @@ -80,6 +80,7 @@ private: Gfx::IntRect get_outer_rect(int glyph) const; Optional<int> glyph_at_position(Gfx::IntPoint) const; + int glyph_at_position_clamped(Gfx::IntPoint) const; void recalculate_content_size(); |