From 9a513a9a56b21849c84e68dc72cf3af579ee3e88 Mon Sep 17 00:00:00 2001 From: Jonas Kvinge Date: Wed, 17 Dec 2025 23:14:57 +0100 Subject: [PATCH] AutoExpandingTreeView: Scroll if cursor is out of visible area Fixes #1489 --- src/widgets/autoexpandingtreeview.cpp | 25 ++++++++++++++++++++++++- src/widgets/autoexpandingtreeview.h | 3 ++- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/src/widgets/autoexpandingtreeview.cpp b/src/widgets/autoexpandingtreeview.cpp index 96b35f967..db95c4924 100644 --- a/src/widgets/autoexpandingtreeview.cpp +++ b/src/widgets/autoexpandingtreeview.cpp @@ -2,7 +2,7 @@ * Strawberry Music Player * This file was part of Clementine. * Copyright 2010, David Sansome - * Copyright 2018-2021, Jonas Kvinge + * Copyright 2018-2025, Jonas Kvinge * * Strawberry is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -191,3 +191,26 @@ void AutoExpandingTreeView::DownAndFocus() { setCurrentIndex(moveCursor(QAbstractItemView::MoveDown, Qt::NoModifier)); setFocus(); } + +void AutoExpandingTreeView::currentChanged(const QModelIndex ¤t_index, const QModelIndex &previous_index) { + + QTreeView::currentChanged(current_index, previous_index); + + // Ensure the newly selected item is visible after keyboard navigation. + // This fixes the issue where the cursor highlight disappears off-screen when using arrow keys to navigate through expanded lists. + if (current_index.isValid()) { + const QRect current_index_rect = visualRect(current_index); + const QRect viewport_rect = viewport()->rect(); + + // Calculate if we need to scroll to keep the item visible + // If the item extends below the viewport, scroll it to the bottom + // If the item extends above the viewport, scroll it to the top + if (current_index_rect.bottom() > viewport_rect.bottom()) { + scrollTo(current_index, QAbstractItemView::PositionAtBottom); + } + else if (current_index_rect.top() < viewport_rect.top()) { + scrollTo(current_index, QAbstractItemView::PositionAtTop); + } + } + +} diff --git a/src/widgets/autoexpandingtreeview.h b/src/widgets/autoexpandingtreeview.h index 1d5b856ba..5dabbfbd0 100644 --- a/src/widgets/autoexpandingtreeview.h +++ b/src/widgets/autoexpandingtreeview.h @@ -2,7 +2,7 @@ * Strawberry Music Player * This file was part of Clementine. * Copyright 2010, David Sansome - * Copyright 2018-2021, Jonas Kvinge + * Copyright 2018-2025, Jonas Kvinge * * Strawberry is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -55,6 +55,7 @@ class AutoExpandingTreeView : public QTreeView { protected: // QAbstractItemView void reset() override; + void currentChanged(const QModelIndex ¤t_index, const QModelIndex &previous_index) override; // QWidget void mousePressEvent(QMouseEvent *event) override;