Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tracks view: keep current item visible when the view shrinks vertically #11273

Merged
merged 2 commits into from
Jan 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions src/widget/wtracktableview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -844,6 +844,47 @@ void WTrackTableView::keyPressEvent(QKeyEvent* event) {
}
}

void WTrackTableView::resizeEvent(QResizeEvent* event) {
// When the tracks view shrinks in height, e.g. when other skin regions expand,
// and if the row was visible before resizing, scroll to it afterwards.

// these heights are the actual inner region without header, scrollbars and padding
int oldHeight = event->oldSize().height();
int newHeight = event->size().height();

if (newHeight >= oldHeight) {
QTableView::resizeEvent(event);
return;
}

QModelIndex currIndex = currentIndex();
int currRow = currIndex.row();
int rHeight = rowHeight(currRow);

if (currRow < 0 || rHeight == 0) { // true if currIndex is invalid
QTableView::resizeEvent(event);
return;
}

// y-pos of the top edge, negative value means above viewport boundary
int posInView = rowViewportPosition(currRow);
// Check if the row is visible.
// Note: don't use viewport()->height() because that may already have changed
bool rowWasVisible = posInView > 0 && posInView - rHeight < oldHeight;

QTableView::resizeEvent(event);

if (!rowWasVisible) {
return;
}

// Check if the item is fully visible. If not, scroll to show it
posInView = rowViewportPosition(currRow);
if (posInView - rHeight < 0 || posInView + rHeight > newHeight) {
scrollTo(currIndex);
}
}

void WTrackTableView::hideOrRemoveSelectedTracks() {
QModelIndexList indices = selectionModel()->selectedRows();
if (indices.isEmpty()) {
Expand Down
1 change: 1 addition & 0 deletions src/widget/wtracktableview.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class WTrackTableView : public WLibraryTableView {
bool hasFocus() const override;
void setFocus() override;
void keyPressEvent(QKeyEvent* event) override;
void resizeEvent(QResizeEvent* event) override;
void activateSelectedTrack() override;
void loadSelectedTrackToGroup(const QString& group, bool play) override;
void assignNextTrackColor() override;
Expand Down