fix: selected torrents when moving rows (#5991)

* Fix selected torrents when moving rows

* code review: keep selected torrents when toggling group presentation
This commit is contained in:
Cœur 2023-09-11 04:39:01 +02:00 committed by GitHub
parent d566695b33
commit 92c8c41c0b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 11 deletions

View File

@ -3237,6 +3237,9 @@ void onTorrentCompletenessChanged(tr_torrent* tor, tr_completeness status, bool
//set all groups as expanded //set all groups as expanded
[self.fTableView removeAllCollapsedGroups]; [self.fTableView removeAllCollapsedGroups];
// we need to remember selected values
NSArray<Torrent*>* selectedTorrents = self.fTableView.selectedTorrents;
beganUpdates = YES; beganUpdates = YES;
[self.fTableView beginUpdates]; [self.fTableView beginUpdates];
@ -3280,6 +3283,8 @@ void onTorrentCompletenessChanged(tr_torrent* tor, tr_completeness status, bool
for (TorrentGroup* group in self.fDisplayedTorrents) for (TorrentGroup* group in self.fDisplayedTorrents)
[self.fTableView expandItem:group]; [self.fTableView expandItem:group];
} }
self.fTableView.selectedTorrents = selectedTorrents;
} }
//sort the torrents (won't sort the groups, though) //sort the torrents (won't sort the groups, though)

View File

@ -17,9 +17,7 @@ extern const CGFloat kGroupSeparatorHeight;
- (void)removeAllCollapsedGroups; - (void)removeAllCollapsedGroups;
- (void)saveCollapsedGroups; - (void)saveCollapsedGroups;
- (void)restoreSelectionIndexes; @property(nonatomic) NSArray<Torrent*>* selectedTorrents;
@property(nonatomic, readonly) NSArray<Torrent*>* selectedTorrents;
- (NSRect)iconRectForRow:(NSInteger)row; - (NSRect)iconRectForRow:(NSInteger)row;

View File

@ -125,8 +125,9 @@ static NSTimeInterval const kToggleProgressSeconds = 0.175;
//make sure we don't lose selection on manual reloads //make sure we don't lose selection on manual reloads
- (void)reloadData - (void)reloadData
{ {
NSArray<Torrent*>* selectedTorrents = self.selectedTorrents;
[super reloadData]; [super reloadData];
[self restoreSelectionIndexes]; self.selectedTorrents = selectedTorrents;
} }
- (void)reloadVisibleRows - (void)reloadVisibleRows
@ -185,8 +186,6 @@ static NSTimeInterval const kToggleProgressSeconds = 0.175;
} }
} }
}]; }];
[self restoreSelectionIndexes];
} }
- (BOOL)isGroupCollapsed:(NSInteger)value - (BOOL)isGroupCollapsed:(NSInteger)value
@ -559,6 +558,16 @@ static NSTimeInterval const kToggleProgressSeconds = 0.175;
return torrents; return torrents;
} }
- (void)setSelectedTorrents:(NSArray<Torrent*>*)selectedTorrents
{
NSMutableIndexSet* selectedIndexes = [NSMutableIndexSet new];
for (Torrent* i in selectedTorrents)
{
[selectedIndexes addIndex:[self rowForItem:i]];
}
[self selectRowIndexes:selectedIndexes byExtendingSelection:NO];
}
- (NSMenu*)menuForEvent:(NSEvent*)event - (NSMenu*)menuForEvent:(NSEvent*)event
{ {
NSInteger row = [self rowAtPoint:[self convertPoint:event.locationInWindow fromView:nil]]; NSInteger row = [self rowAtPoint:[self convertPoint:event.locationInWindow fromView:nil]];
@ -577,11 +586,6 @@ static NSTimeInterval const kToggleProgressSeconds = 0.175;
} }
} }
- (void)restoreSelectionIndexes
{
[self selectRowIndexes:self.fSelectedRowIndexes byExtendingSelection:NO];
}
//make sure that the pause buttons become orange when holding down the option key //make sure that the pause buttons become orange when holding down the option key
- (void)flagsChanged:(NSEvent*)event - (void)flagsChanged:(NSEvent*)event
{ {