From 452150c632c1f2ee63839ae7597eb7e511cde6e9 Mon Sep 17 00:00:00 2001 From: martinfalisse Date: Mon, 3 Jan 2022 10:52:38 +0100 Subject: [PATCH] LibGUI: Bring entire cell into view after auto scroll into view On account of row and column headers, when a user navigates to a cell (for example in the spreadsheet application) that is outside of the view, the cell is not properly aligned and so is partially cut-off. This fix takes into account the row and column headers when calculating the Rect to pass to the scroll_into_view function. --- Userland/Libraries/LibGUI/AbstractTableView.cpp | 13 ++++++++++++- Userland/Libraries/LibGUI/AbstractTableView.h | 1 + 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/Userland/Libraries/LibGUI/AbstractTableView.cpp b/Userland/Libraries/LibGUI/AbstractTableView.cpp index 52d92f4632..ac872fda81 100644 --- a/Userland/Libraries/LibGUI/AbstractTableView.cpp +++ b/Userland/Libraries/LibGUI/AbstractTableView.cpp @@ -276,7 +276,7 @@ void AbstractTableView::scroll_into_view(const ModelIndex& index, bool scroll_ho Gfx::IntRect rect; switch (selection_behavior()) { case SelectionBehavior::SelectItems: - rect = content_rect(index); + rect = cell_rect(index.row(), index.column()); break; case SelectionBehavior::SelectRows: rect = row_rect(index.row()); @@ -324,6 +324,17 @@ Gfx::IntRect AbstractTableView::content_rect(const ModelIndex& index) const return content_rect(index.row(), index.column()); } +Gfx::IntRect AbstractTableView::cell_rect(int row, int column) const +{ + auto cell_rect = this->content_rect(row, column); + if (row_header().is_visible()) + cell_rect.set_left(cell_rect.left() - row_header().width()); + if (column_header().is_visible()) + cell_rect.set_top(cell_rect.top() - column_header().height()); + + return cell_rect; +} + Gfx::IntRect AbstractTableView::row_rect(int item_index) const { return { row_header().is_visible() ? row_header().width() : 0, diff --git a/Userland/Libraries/LibGUI/AbstractTableView.h b/Userland/Libraries/LibGUI/AbstractTableView.h index 916ecf05c2..612c4b5625 100644 --- a/Userland/Libraries/LibGUI/AbstractTableView.h +++ b/Userland/Libraries/LibGUI/AbstractTableView.h @@ -51,6 +51,7 @@ public: virtual Gfx::IntRect content_rect(const ModelIndex&) const override; Gfx::IntRect content_rect(int row, int column) const; + Gfx::IntRect cell_rect(int row, int column) const; Gfx::IntRect row_rect(int item_index) const; virtual Gfx::IntRect paint_invalidation_rect(ModelIndex const& index) const override;