summaryrefslogtreecommitdiff
path: root/Libraries
diff options
context:
space:
mode:
authorSergey Bugaev <bugaevc@gmail.com>2020-01-22 18:07:23 +0300
committerAndreas Kling <kling@serenityos.org>2020-01-22 21:22:23 +0100
commit2ccad40a1632c51e5d82459213df818cb2316fb8 (patch)
tree0ea139e79f68ad2e71b77c7cf1cbc60109018731 /Libraries
parentb4923938e101ffbf574c1b50c5d2acf0db0127e7 (diff)
downloadserenity-2ccad40a1632c51e5d82459213df818cb2316fb8.zip
LibGUI: Add GListView::index_at_event_position()
And port GListView::mousedown_event() to it.
Diffstat (limited to 'Libraries')
-rw-r--r--Libraries/LibGUI/GListView.cpp26
-rw-r--r--Libraries/LibGUI/GListView.h3
2 files changed, 20 insertions, 9 deletions
diff --git a/Libraries/LibGUI/GListView.cpp b/Libraries/LibGUI/GListView.cpp
index b65d2780d3..c6dae846c2 100644
--- a/Libraries/LibGUI/GListView.cpp
+++ b/Libraries/LibGUI/GListView.cpp
@@ -84,7 +84,20 @@ Rect GListView::content_rect(const GModelIndex& index) const
return content_rect(index.row());
}
-Point GListView::adjusted_position(const Point& position)
+GModelIndex GListView::index_at_event_position(const Point& point) const
+{
+ ASSERT(model());
+
+ auto adjusted_position = this->adjusted_position(point);
+ for (int row = 0, row_count = model()->row_count(); row < row_count; ++row) {
+ if (!content_rect(row).contains(adjusted_position))
+ continue;
+ return model()->index(row, m_model_column);
+ }
+ return {};
+}
+
+Point GListView::adjusted_position(const Point& position) const
{
return position.translated(horizontal_scrollbar().value() - frame_thickness(), vertical_scrollbar().value() - frame_thickness());
}
@@ -97,18 +110,15 @@ void GListView::mousedown_event(GMouseEvent& event)
if (event.button() != GMouseButton::Left)
return;
- auto adjusted_position = this->adjusted_position(event.position());
- for (int row = 0, row_count = model()->row_count(); row < row_count; ++row) {
- if (!content_rect(row).contains(adjusted_position))
- continue;
- auto index = model()->index(row, m_model_column);
+ auto index = index_at_event_position(event.position());
+ if (index.is_valid()) {
if (event.modifiers() & Mod_Ctrl)
selection().toggle(index);
else
selection().set(index);
- return;
+ } else {
+ selection().clear();
}
- selection().clear();
}
void GListView::paint_event(GPaintEvent& event)
diff --git a/Libraries/LibGUI/GListView.h b/Libraries/LibGUI/GListView.h
index ac2a0f983d..ff5a3d79fe 100644
--- a/Libraries/LibGUI/GListView.h
+++ b/Libraries/LibGUI/GListView.h
@@ -49,8 +49,9 @@ public:
void scroll_into_view(const GModelIndex&, Orientation);
- Point adjusted_position(const Point&);
+ Point adjusted_position(const Point&) const;
+ GModelIndex index_at_event_position(const Point&) const;
virtual Rect content_rect(const GModelIndex&) const override;
int model_column() const { return m_model_column; }