| 141 | } |
| 142 | |
| 143 | void TrackUtilities::DoRemoveTrack(AudacityProject &project, Track &toRemove) |
| 144 | { |
| 145 | auto &tracks = TrackList::Get(project); |
| 146 | auto &trackFocus = TrackFocus::Get(project); |
| 147 | |
| 148 | const auto iter = tracks.Find(&toRemove); |
| 149 | |
| 150 | // If it was focused, then NEW focus is the next or, if |
| 151 | // unavailable, the previous track. (The NEW focus is set |
| 152 | // after the track has been removed.) |
| 153 | bool toRemoveWasFocused = trackFocus.Get() == *iter; |
| 154 | std::optional<decltype(iter)> newFocus{}; |
| 155 | if (toRemoveWasFocused) { |
| 156 | auto iterNext = iter, |
| 157 | iterPrev = iter; |
| 158 | newFocus.emplace(++iterNext); |
| 159 | if (!**newFocus) |
| 160 | newFocus.emplace(--iterPrev); |
| 161 | } |
| 162 | |
| 163 | wxString name = toRemove.GetName(); |
| 164 | |
| 165 | tracks.Remove(**iter); |
| 166 | |
| 167 | if (toRemoveWasFocused) |
| 168 | trackFocus.Set(**newFocus); |
| 169 | |
| 170 | ProjectHistory::Get(project).PushState( |
| 171 | XO("Removed track '%s'.").Format(name), |
| 172 | XO("Track Remove")); |
| 173 | } |
| 174 | |
| 175 | void TrackUtilities::DoMoveTrack( |
| 176 | AudacityProject &project, Track& target, MoveChoice choice) |