From 95b6c984350f150b0d34fdc0ad726c9593b68a84 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Wed, 16 Sep 2020 15:28:16 +0200 Subject: [PATCH] LibGUI: Fix TreeView scrolling to top when clicking sub-items This code was confusing two different versions of scroll_into_view that were getting mixed up due to member function shadowing. Adding an "override" to the subclass declaration exposed the problem. With this fixed, we no longer lose our scroll position wildly when using the mouse to select TreeView items. --- Applications/FileManager/main.cpp | 4 ++-- Libraries/LibGUI/TreeView.cpp | 12 ++++++------ Libraries/LibGUI/TreeView.h | 2 +- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/Applications/FileManager/main.cpp b/Applications/FileManager/main.cpp index 005eca136e..7e87f4d8b6 100644 --- a/Applications/FileManager/main.cpp +++ b/Applications/FileManager/main.cpp @@ -277,7 +277,7 @@ int run_in_windowed_mode(RefPtr config, String initial_locatio // Reselect the existing folder in the tree. auto new_index = directories_model->index(current_path, GUI::FileSystemModel::Column::Name); tree_view.selection().set(new_index); - tree_view.scroll_into_view(new_index, Orientation::Vertical); + tree_view.scroll_into_view(new_index, false, true); tree_view.update(); directory_view.refresh(); @@ -549,7 +549,7 @@ int run_in_windowed_mode(RefPtr config, String initial_locatio auto new_index = directories_model->index(new_path, GUI::FileSystemModel::Column::Name); if (new_index.is_valid()) { tree_view.selection().set(new_index); - tree_view.scroll_into_view(new_index, Orientation::Vertical); + tree_view.scroll_into_view(new_index, false, true); tree_view.update(); } diff --git a/Libraries/LibGUI/TreeView.cpp b/Libraries/LibGUI/TreeView.cpp index 37afdd18a9..25f7ef5677 100644 --- a/Libraries/LibGUI/TreeView.cpp +++ b/Libraries/LibGUI/TreeView.cpp @@ -360,7 +360,7 @@ void TreeView::paint_event(PaintEvent& event) }); } -void TreeView::scroll_into_view(const ModelIndex& a_index, Orientation orientation) +void TreeView::scroll_into_view(const ModelIndex& a_index, bool scroll_horizontally, bool scroll_vertically) { if (!a_index.is_valid()) return; @@ -372,7 +372,7 @@ void TreeView::scroll_into_view(const ModelIndex& a_index, Orientation orientati } return IterationDecision::Continue; }); - ScrollableWidget::scroll_into_view(found_rect, orientation); + ScrollableWidget::scroll_into_view(found_rect, scroll_horizontally, scroll_vertically); } void TreeView::did_update_model(unsigned flags) @@ -442,7 +442,7 @@ void TreeView::keydown_event(KeyEvent& event) } if (cursor_index.is_valid() && cursor_index.parent().is_valid()) { selection().set(cursor_index.parent()); - scroll_into_view(selection().first(), Orientation::Vertical); + scroll_into_view(selection().first(), false, true); return; } } @@ -461,7 +461,7 @@ void TreeView::keydown_event(KeyEvent& event) } selection().set(model()->index(0, model()->tree_column(), cursor_index)); - scroll_into_view(selection().first(), Orientation::Vertical); + scroll_into_view(selection().first(), false, true); return; } } @@ -494,7 +494,7 @@ void TreeView::move_cursor(CursorMovement movement, SelectionUpdate) }); if (found_index.is_valid()) { selection().set(found_index); - scroll_into_view(selection().first(), Orientation::Vertical); + scroll_into_view(selection().first(), false, true); update(); } break; @@ -512,7 +512,7 @@ void TreeView::move_cursor(CursorMovement movement, SelectionUpdate) }); if (found_index.is_valid()) { selection().set(found_index); - scroll_into_view(selection().first(), Orientation::Vertical); + scroll_into_view(selection().first(), false, true); update(); } return; diff --git a/Libraries/LibGUI/TreeView.h b/Libraries/LibGUI/TreeView.h index 4e4e39fb44..3b3f049370 100644 --- a/Libraries/LibGUI/TreeView.h +++ b/Libraries/LibGUI/TreeView.h @@ -36,7 +36,7 @@ class TreeView : public AbstractTableView { public: virtual ~TreeView() override; - virtual void scroll_into_view(const ModelIndex&, Gfx::Orientation); + virtual void scroll_into_view(const ModelIndex&, bool scroll_horizontally, bool scroll_vertically) override; virtual int item_count() const override; virtual void toggle_index(const ModelIndex&) override;