diff --git a/src/Project.cpp b/src/Project.cpp index 071a505e2..2d27e8b66 100644 --- a/src/Project.cpp +++ b/src/Project.cpp @@ -5447,14 +5447,16 @@ void AudacityProject::SetTrackPan(WaveTrack * wt, LWSlider * slider) /// Removes the specified track. Called from HandleClosing. void AudacityProject::RemoveTrack(Track * toRemove) { - // If it was focused, reassign focus to the next or, if - // unavailable, the previous track. - if (mTrackPanel->GetFocusedTrack() == toRemove) { - Track *t = mTracks->GetNext(toRemove, true); - if (t == NULL) { - t = mTracks->GetPrev(toRemove, true); + // If it was focused, then new focus is the next or, if + // unavailable, the previous track. (The new focus is set + // after the track has been removed.) + bool toRemoveWasFocused = mTrackPanel->GetFocusedTrack() == toRemove; + Track* newFocus{}; + if (toRemoveWasFocused) { + newFocus = mTracks->GetNext(toRemove, true); + if (!newFocus) { + newFocus = mTracks->GetPrev(toRemove, true); } - mTrackPanel->SetFocusedTrack(t); // It's okay if this is NULL } wxString name = toRemove->GetName(); @@ -5473,9 +5475,9 @@ void AudacityProject::RemoveTrack(Track * toRemove) if (partner) { mTracks->Remove(partner); } - - if (mTracks->IsEmpty()) { - mTrackPanel->SetFocusedTrack(NULL); + + if (toRemoveWasFocused) { + mTrackPanel->SetFocusedTrack(newFocus); } PushState( diff --git a/src/TrackPanelAx.cpp b/src/TrackPanelAx.cpp index 354a08c2d..7e9127aa0 100644 --- a/src/TrackPanelAx.cpp +++ b/src/TrackPanelAx.cpp @@ -49,8 +49,14 @@ TrackPanelAx::~TrackPanelAx() std::shared_ptr TrackPanelAx::GetFocus() { auto focusedTrack = mFocusedTrack.lock(); - if( !focusedTrack ) - focusedTrack = SetFocus(); + if( !focusedTrack ) { + TrackListIterator iter( mTrackPanel->GetTracks() ); + focusedTrack = Track::Pointer( iter.First() ); + // only call SetFocus if the focus has changed to avoid + // unnecessary focus events + if (focusedTrack) + focusedTrack = SetFocus(); + } if( !TrackNum( focusedTrack ) ) { @@ -104,6 +110,14 @@ std::shared_ptr TrackPanelAx::SetFocus( std::shared_ptr track ) num ); } } + else + { + NotifyEvent(wxACC_EVENT_OBJECT_FOCUS, + mTrackPanel, + wxOBJID_CLIENT, + wxACC_SELF); + } + #endif return track;