88static void *const UnstagedFileSelectedContext = @" UnstagedFileSelectedContext" ;
99static void *const CachedFileSelectedContext = @" CachedFileSelectedContext" ;
1010
11+ @interface PBRefreshCoalescer : NSObject
12+ - (instancetype )initWithDeliveryHandler : (void (^)(void ))deliveryHandler ;
13+ - (void )requestRefresh ;
14+ - (void )cancel ;
15+ @end
16+
1117@interface PBWebChangesController () <PBNativeContentViewDelegate>
1218@property (nonatomic ) NSUInteger contextLines;
1319@property (nonatomic ) NSTextField *contextValueLabel;
20+ @property (nonatomic ) PBRefreshCoalescer *refreshCoalescer;
1421@end
1522
1623@implementation PBWebChangesController
@@ -19,6 +26,10 @@ - (void)awakeFromNib
1926{
2027 [super awakeFromNib ];
2128 self.nativeView .delegate = self;
29+ __weak typeof (self) weakSelf = self;
30+ self.refreshCoalescer = [[PBRefreshCoalescer alloc ] initWithDeliveryHandler: ^{
31+ [weakSelf refresh ];
32+ }];
2233 NSNumber *savedContext = [[NSUserDefaults standardUserDefaults ] objectForKey: @" PBStageDiffContextLines" ];
2334 self.contextLines = savedContext ? savedContext.unsignedIntegerValue : 3 ;
2435 NSView *accessory = [[NSView alloc ] initWithFrame: NSMakeRect (0 , 0 , 100 , 34 )];
@@ -59,6 +70,8 @@ - (void)contextLinesChanged:(NSStepper *)sender
5970
6071- (void )closeView
6172{
73+ [self .refreshCoalescer cancel ];
74+ self.refreshCoalescer = nil ;
6275 [unstagedFilesController removeObserver: self forKeyPath: @" selection" ];
6376 [stagedFilesController removeObserver: self forKeyPath: @" selection" ];
6477 [super closeView ];
@@ -72,7 +85,7 @@ - (void)didLoad
7285- (void )observeValueForKeyPath : (NSString *)keyPath ofObject : (id )object change : (NSDictionary *)change context : (void *)context
7386{
7487 if (context == UnstagedFileSelectedContext || context == CachedFileSelectedContext) {
75- [self refresh ];
88+ [self .refreshCoalescer requestRefresh ];
7689 return ;
7790 }
7891 [super observeValueForKeyPath: keyPath ofObject: object change: change context: context];
@@ -95,8 +108,11 @@ - (void)refresh
95108 NSMutableArray <NSDictionary *> *sections = [NSMutableArray array ];
96109 for (PBChangedFile *file in stagedFilesController.selectedObjects ) [sections addObject: [self sectionForFile: file staged: YES ]];
97110 for (PBChangedFile *file in unstagedFilesController.selectedObjects ) [sections addObject: [self sectionForFile: file staged: NO ]];
98- if (sections.count == 0 ) [self .nativeView showMessage: @" No file selected" ];
99- else [self .nativeView showDiffSections: sections];
111+ NSLog (@" [GitX] Rendering %lu selected stage-diff sections" , (unsigned long )sections.count );
112+ if (sections.count == 0 )
113+ [self .nativeView showMessage: @" No file selected" ];
114+ else
115+ [self .nativeView showDiffSections: sections];
100116}
101117
102118- (void )showMultiple : (NSArray *)files
@@ -112,23 +128,23 @@ - (void)setStateMessage:(NSString *)state
112128- (void )nativeContentView : (PBNativeContentView *)view performDiffAction : (NSString *)action patch : (NSString *)patch
113129{
114130 if ([action isEqualToString: @" stage" ]) {
131+ NSLog (@" [GitX] Applying a partial stage patch" );
115132 [controller.index applyPatch: patch stage: YES reverse: NO ];
116- [self refresh ];
117133 } else if ([action isEqualToString: @" unstage" ]) {
134+ NSLog (@" [GitX] Applying a partial unstage patch" );
118135 [controller.index applyPatch: patch stage: YES reverse: YES ];
119- [self refresh ];
120136 } else if ([action isEqualToString: @" discard" ]) {
121137 NSAlert *alert = [[NSAlert alloc ] init ];
122138 alert.messageText = NSLocalizedString(@" Discard hunk" , @" " );
123139 alert.informativeText = NSLocalizedString(@" Are you sure you wish to discard this hunk? This operation cannot be undone." , @" " );
124140 [alert addButtonWithTitle: NSLocalizedString(@" Discard" , @" " )];
125141 [alert addButtonWithTitle: NSLocalizedString(@" Cancel" , @" " )];
126- [alert beginSheetModalForWindow: self .view.window completionHandler: ^( NSModalResponse response) {
127- if (response == NSAlertFirstButtonReturn ) {
128- [ self ->controller.index applyPatch: patch stage: NO reverse: YES ];
129- [self refresh ];
130- }
131- }];
142+ [alert beginSheetModalForWindow: self .view.window
143+ completionHandler: ^( NSModalResponse response ) {
144+ if (response == NSAlertFirstButtonReturn ) {
145+ [self ->controller.index applyPatch: patch stage: NO reverse: YES ];
146+ }
147+ }];
132148 }
133149}
134150
0 commit comments