Skip to content

Commit 889c350

Browse files
committed
Move the ref drag & drop support inside the History controller
1 parent 8d20dad commit 889c350

File tree

3 files changed

+121
-125
lines changed

3 files changed

+121
-125
lines changed

Classes/Controllers/PBGitHistoryController.m

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#import "GitXCommitCopier.h"
2626
#import "NSSplitView+GitX.h"
2727
#import "PBGitRevisionRow.h"
28+
#import "PBGitRevisionCell.h"
2829
#import "PBRefMenuItem.h"
2930
#import "PBGitStash.h"
3031

@@ -157,6 +158,8 @@ - (void)loadView {
157158
// Add a menu that allows a user to select which columns to view
158159
[[commitList headerView] setMenu:[self tableColumnMenu]];
159160

161+
[commitList registerForDraggedTypes:[NSArray arrayWithObject:@"PBGitRef"]];
162+
160163
[upperToolbarView setTopShade:237/255.0f bottomShade:216/255.0f];
161164
[scopeBarView setTopColor:[NSColor colorWithCalibratedHue:0.579 saturation:0.068 brightness:0.898 alpha:1.000]
162165
bottomColor:[NSColor colorWithCalibratedHue:0.579 saturation:0.119 brightness:0.765 alpha:1.000]];
@@ -609,6 +612,123 @@ - (void) diffFilesAction:(id)sender
609612
[PBDiffWindowController showDiffWindowWithFiles:[sender representedObject] fromCommit:self.selectedCommits.firstObject diffCommit:nil];
610613
}
611614

615+
#pragma mark -
616+
#pragma mark History table view delegate
617+
618+
- (BOOL)tableView:(NSTableView *)tv writeRowsWithIndexes:(NSIndexSet *)rowIndexes toPasteboard:(NSPasteboard*)pboard
619+
{
620+
NSPoint location = [(PBCommitList *)tv mouseDownPoint];
621+
NSInteger row = [tv rowAtPoint:location];
622+
NSInteger column = [tv columnAtPoint:location];
623+
624+
PBGitRevisionCell *cell = (PBGitRevisionCell *)[tv viewAtColumn:column row:row makeIfNecessary:NO];
625+
PBGitCommit *commit = [[commitController arrangedObjects] objectAtIndex:row];
626+
627+
int index = -1;
628+
if ([cell respondsToSelector:@selector(indexAtX:)]) {
629+
NSRect cellFrame = [tv frameOfCellAtColumn:column row:row];
630+
CGFloat deltaX = location.x - cellFrame.origin.x;
631+
index = [cell indexAtX:deltaX];
632+
}
633+
634+
if (index != -1) {
635+
PBGitRef *ref = [[commit refs] objectAtIndex:index];
636+
if ([ref isTag] || [ref isRemoteBranch])
637+
return NO;
638+
639+
if ([[[repository headRef] ref] isEqualToRef:ref])
640+
return NO;
641+
642+
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:[NSArray arrayWithObjects:[NSNumber numberWithInteger:row], [NSNumber numberWithInt:index], NULL]];
643+
[pboard declareTypes:[NSArray arrayWithObject:@"PBGitRef"] owner:self];
644+
[pboard setData:data forType:@"PBGitRef"];
645+
} else {
646+
[pboard declareTypes:[NSArray arrayWithObject:NSStringPboardType] owner:self];
647+
648+
NSString *info = nil;
649+
if (column == [tv columnWithIdentifier:@"ShortSHAColumn"]) {
650+
info = [commit shortName];
651+
} else {
652+
info = [NSString stringWithFormat:@"%@ (%@)", [commit shortName], [commit subject]];
653+
}
654+
655+
[pboard setString:info forType:NSStringPboardType];
656+
}
657+
658+
return YES;
659+
}
660+
661+
- (NSDragOperation)tableView:(NSTableView*)tv
662+
validateDrop:(id <NSDraggingInfo>)info
663+
proposedRow:(NSInteger)row
664+
proposedDropOperation:(NSTableViewDropOperation)operation
665+
{
666+
if (operation == NSTableViewDropAbove)
667+
return NSDragOperationNone;
668+
669+
NSPasteboard *pboard = [info draggingPasteboard];
670+
if ([pboard dataForType:@"PBGitRef"])
671+
return NSDragOperationMove;
672+
673+
return NSDragOperationNone;
674+
}
675+
676+
- (BOOL)tableView:(NSTableView *)aTableView
677+
acceptDrop:(id <NSDraggingInfo>)info
678+
row:(NSInteger)row
679+
dropOperation:(NSTableViewDropOperation)operation
680+
{
681+
if (operation != NSTableViewDropOn)
682+
return NO;
683+
684+
NSPasteboard *pboard = [info draggingPasteboard];
685+
NSData *data = [pboard dataForType:@"PBGitRef"];
686+
if (!data)
687+
return NO;
688+
689+
NSArray *numbers = [NSKeyedUnarchiver unarchiveObjectWithData:data];
690+
int oldRow = [[numbers objectAtIndex:0] intValue];
691+
if (oldRow == row)
692+
return NO;
693+
694+
int oldRefIndex = [[numbers objectAtIndex:1] intValue];
695+
PBGitCommit *oldCommit = [[commitController arrangedObjects] objectAtIndex:oldRow];
696+
PBGitRef *ref = [[oldCommit refs] objectAtIndex:oldRefIndex];
697+
698+
PBGitCommit *dropCommit = [[commitController arrangedObjects] objectAtIndex:row];
699+
700+
NSString *subject = [dropCommit subject];
701+
if ([subject length] > 99)
702+
subject = [[subject substringToIndex:99] stringByAppendingString:@""];
703+
704+
NSAlert *alert = [[NSAlert alloc] init];
705+
alert.messageText = [NSString stringWithFormat:NSLocalizedString(@"Move %@: %@", @""), [ref refishType], [ref shortName]];
706+
alert.informativeText = [NSString stringWithFormat:NSLocalizedString(@"Move the %@ to point to the commit: %@", @""), [ref refishType], subject];
707+
708+
[alert addButtonWithTitle:NSLocalizedString(@"Move", @"Move branch label - default button")];
709+
[alert addButtonWithTitle:NSLocalizedString(@"Cancel", @"Move branch label - cancel button")];
710+
711+
PBGitWindowController *wc = self.windowController;
712+
[wc confirmDialog:alert
713+
suppressionIdentifier:kDialogAcceptDroppedRef
714+
forAction:^{
715+
NSError *error = nil;
716+
if (![wc.repository updateReference:ref toPointAtCommit:dropCommit error:&error]) {
717+
[wc showErrorSheet:error];
718+
return;
719+
}
720+
721+
[dropCommit addRef:ref];
722+
[oldCommit removeRef:ref];
723+
}];
724+
725+
return YES;
726+
}
727+
728+
729+
#pragma mark -
730+
#pragma mark File browser
731+
612732
- (NSMenu *)contextMenuForTreeView
613733
{
614734
NSArray *filePaths = [[treeController selectedObjects] valueForKey:@"fullPath"];

Classes/Controllers/PBRefController.m

Lines changed: 0 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,9 @@
1414
#import "PBGitRevSpecifier.h"
1515
#import "PBGitStash.h"
1616
#import "GitXCommitCopier.h"
17-
#import "PBCommitList.h"
18-
#import "PBGitHistoryController.h"
19-
20-
2117

2218
@implementation PBRefController
2319

24-
- (void)awakeFromNib
25-
{
26-
[commitList registerForDraggedTypes:[NSArray arrayWithObject:@"PBGitRef"]];
27-
}
28-
2920
#pragma mark Contextual menus
3021

3122
- (NSArray<NSMenuItem *> *) menuItemsForRef:(PBGitRef *)ref
@@ -47,121 +38,6 @@ - (void)awakeFromNib
4738
return [self menuItemsForCommits:@[[commits objectAtIndex:rowIndex]]];
4839
}
4940

50-
51-
52-
# pragma mark Tableview delegate methods
53-
54-
- (BOOL)tableView:(NSTableView *)tv writeRowsWithIndexes:(NSIndexSet *)rowIndexes toPasteboard:(NSPasteboard*)pboard
55-
{
56-
57-
NSPoint location = [(PBCommitList *)tv mouseDownPoint];
58-
NSInteger row = [tv rowAtPoint:location];
59-
NSInteger column = [tv columnAtPoint:location];
60-
61-
PBGitRevisionCell *cell = (PBGitRevisionCell *)[tv viewAtColumn:column row:row makeIfNecessary:NO];
62-
PBGitCommit *commit = [[commitController arrangedObjects] objectAtIndex:row];
63-
64-
int index = -1;
65-
if ([cell respondsToSelector:@selector(indexAtX:)]) {
66-
NSRect cellFrame = [tv frameOfCellAtColumn:column row:row];
67-
CGFloat deltaX = location.x - cellFrame.origin.x;
68-
index = [cell indexAtX:deltaX];
69-
}
70-
71-
if (index != -1) {
72-
PBGitRef *ref = [[commit refs] objectAtIndex:index];
73-
if ([ref isTag] || [ref isRemoteBranch])
74-
return NO;
75-
76-
if ([[[historyController.repository headRef] ref] isEqualToRef:ref])
77-
return NO;
78-
79-
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:[NSArray arrayWithObjects:[NSNumber numberWithInteger:row], [NSNumber numberWithInt:index], NULL]];
80-
[pboard declareTypes:[NSArray arrayWithObject:@"PBGitRef"] owner:self];
81-
[pboard setData:data forType:@"PBGitRef"];
82-
} else {
83-
[pboard declareTypes:[NSArray arrayWithObject:NSStringPboardType] owner:self];
84-
85-
NSString *info = nil;
86-
if (column == [tv columnWithIdentifier:@"ShortSHAColumn"]) {
87-
info = [commit shortName];
88-
} else {
89-
info = [NSString stringWithFormat:@"%@ (%@)", [commit shortName], [commit subject]];
90-
}
91-
92-
[pboard setString:info forType:NSStringPboardType];
93-
}
94-
95-
return YES;
96-
}
97-
98-
- (NSDragOperation)tableView:(NSTableView*)tv
99-
validateDrop:(id <NSDraggingInfo>)info
100-
proposedRow:(NSInteger)row
101-
proposedDropOperation:(NSTableViewDropOperation)operation
102-
{
103-
if (operation == NSTableViewDropAbove)
104-
return NSDragOperationNone;
105-
106-
NSPasteboard *pboard = [info draggingPasteboard];
107-
if ([pboard dataForType:@"PBGitRef"])
108-
return NSDragOperationMove;
109-
110-
return NSDragOperationNone;
111-
}
112-
113-
- (BOOL)tableView:(NSTableView *)aTableView
114-
acceptDrop:(id <NSDraggingInfo>)info
115-
row:(NSInteger)row
116-
dropOperation:(NSTableViewDropOperation)operation
117-
{
118-
if (operation != NSTableViewDropOn)
119-
return NO;
120-
121-
NSPasteboard *pboard = [info draggingPasteboard];
122-
NSData *data = [pboard dataForType:@"PBGitRef"];
123-
if (!data)
124-
return NO;
125-
126-
NSArray *numbers = [NSKeyedUnarchiver unarchiveObjectWithData:data];
127-
int oldRow = [[numbers objectAtIndex:0] intValue];
128-
if (oldRow == row)
129-
return NO;
130-
131-
int oldRefIndex = [[numbers objectAtIndex:1] intValue];
132-
PBGitCommit *oldCommit = [[commitController arrangedObjects] objectAtIndex:oldRow];
133-
PBGitRef *ref = [[oldCommit refs] objectAtIndex:oldRefIndex];
134-
135-
PBGitCommit *dropCommit = [[commitController arrangedObjects] objectAtIndex:row];
136-
137-
NSString *subject = [dropCommit subject];
138-
if ([subject length] > 99)
139-
subject = [[subject substringToIndex:99] stringByAppendingString:@""];
140-
141-
NSAlert *alert = [[NSAlert alloc] init];
142-
alert.messageText = [NSString stringWithFormat:NSLocalizedString(@"Move %@: %@", @""), [ref refishType], [ref shortName]];
143-
alert.informativeText = [NSString stringWithFormat:NSLocalizedString(@"Move the %@ to point to the commit: %@", @""), [ref refishType], subject];
144-
145-
[alert addButtonWithTitle:NSLocalizedString(@"Move", @"Move branch label - default button")];
146-
[alert addButtonWithTitle:NSLocalizedString(@"Cancel", @"Move branch label - cancel button")];
147-
148-
PBGitWindowController *wc = historyController.windowController;
149-
[wc confirmDialog:alert
150-
suppressionIdentifier:kDialogAcceptDroppedRef
151-
forAction:^{
152-
NSError *error = nil;
153-
if (![wc.repository updateReference:ref toPointAtCommit:dropCommit error:&error]) {
154-
[wc showErrorSheet:error];
155-
return;
156-
}
157-
158-
[dropCommit addRef:ref];
159-
[oldCommit removeRef:ref];
160-
}];
161-
162-
return YES;
163-
}
164-
16541
- (void)dealloc {
16642
historyController = nil;
16743
}

Resources/XIBs/PBGitHistoryView.xib

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -537,7 +537,7 @@
537537
<connections>
538538
<outlet property="contextMenuDelegate" destination="231" id="DtG-Dl-ygU"/>
539539
<outlet property="controller" destination="-2" id="111"/>
540-
<outlet property="dataSource" destination="231" id="445"/>
540+
<outlet property="dataSource" destination="-2" id="Gol-fU-kRr"/>
541541
<outlet property="delegate" destination="-2" id="444"/>
542542
<outlet property="menu" destination="eTP-cx-OCg" id="xzi-Ng-ecd"/>
543543
<outlet property="searchController" destination="423" id="436"/>

0 commit comments

Comments
 (0)