Skip to content

Commit 13190c0

Browse files
committed
Added color preference window
1 parent f6c189e commit 13190c0

File tree

6 files changed

+339
-45
lines changed

6 files changed

+339
-45
lines changed

Classes/GitDiff.mm

+32-14
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
#import "GitDiff.h"
1414
#import <objc/runtime.h>
15+
#import "GitDiffColorsWindowController.h"
1516

1617
extern "C" {
1718
#import "DiffMatchPatch.h"
@@ -24,11 +25,10 @@
2425

2526
@interface GitDiff()
2627

27-
@property IBOutlet NSColorWell *modifiedColor, *addedColor, *deletedColor, *popoverColor, *changedColor;
28-
2928
@property NSMutableDictionary *diffsByFile;
3029
@property Class sourceDocClass;
3130
@property NSTextView *popover;
31+
@property GitDiffColorsWindowController *colorsWindowController;
3232

3333
@end
3434

@@ -38,19 +38,16 @@ + (void)pluginDidLoad:(NSBundle *)plugin
3838
{
3939
static dispatch_once_t onceToken;
4040
dispatch_once(&onceToken, ^{
41-
4241
gitDiffPlugin = [[self alloc] init];
42+
gitDiffPlugin.colorsWindowController = [[GitDiffColorsWindowController alloc] initWithPluginBundle:plugin];
4343
gitDiffPlugin.diffsByFile = [NSMutableDictionary new];
44-
45-
if ( ![NSBundle loadNibNamed:@"GitDiff" owner:gitDiffPlugin] )
46-
NSLog( @"GitDiff Plugin: Could not load colors interface." );
47-
44+
45+
[gitDiffPlugin insertMenuItems];
46+
4847
gitDiffPlugin.popover = [[NSTextView alloc] initWithFrame:NSZeroRect];
4948
gitDiffPlugin.popover.wantsLayer = YES;
5049
gitDiffPlugin.popover.layer.cornerRadius = 6.0;
5150

52-
gitDiffPlugin.popover.backgroundColor = gitDiffPlugin.popoverColor.color;
53-
5451
gitDiffPlugin.sourceDocClass = NSClassFromString(@"IDESourceCodeDocument");
5552
[self swizzleClass:[NSDocument class]
5653
exchange:@selector(_finishSavingToURL:ofType:forSaveOperation:changeCount:)
@@ -82,6 +79,26 @@ + (void)swizzleClass:(Class)aClass exchange:(SEL)origMethod with:(SEL)altMethod
8279
class_getInstanceMethod(aClass, altMethod));
8380
}
8481

82+
- (void)insertMenuItems
83+
{
84+
NSMenu *editorMenu = [[[NSApp mainMenu] itemWithTitle:@"Edit"] submenu];
85+
86+
if ( editorMenu ) {
87+
NSMenuItem *menuItem = [[NSMenuItem alloc] initWithTitle:@"GitDiff Colors..."
88+
action:@selector(gitDiffColorsMenuItemSelected:)
89+
keyEquivalent:@""];
90+
menuItem.target = self;
91+
92+
[editorMenu addItem:[NSMenuItem separatorItem]];
93+
[editorMenu addItem:menuItem];
94+
}
95+
}
96+
97+
- (void)gitDiffColorsMenuItemSelected:(id)sender
98+
{
99+
[gitDiffPlugin.colorsWindowController showWindow:self];
100+
}
101+
85102
@end
86103

87104
#import <map>
@@ -270,7 +287,7 @@ - (void)gitdiff_drawLineNumbersInSidebarRect:(CGRect)rect
270287
for ( NSUInteger i=0 ; i<indexCount ; i++ ) {
271288
NSUInteger line = indexes[i];
272289
NSColor *highlight = !exists( diffs->added, line ) ? nil :
273-
exists( diffs->modified, line ) ? gitDiffPlugin.modifiedColor.color : gitDiffPlugin.addedColor.color;
290+
exists( diffs->modified, line ) ? gitDiffPlugin.colorsWindowController.modifiedColor : gitDiffPlugin.colorsWindowController.addedColor;
274291
CGRect a0, a1;
275292

276293
if ( highlight ) {
@@ -281,7 +298,7 @@ - (void)gitdiff_drawLineNumbersInSidebarRect:(CGRect)rect
281298
NSRectFill( a0 );
282299
}
283300
else if ( exists( diffs->deleted, line ) ) {
284-
[gitDiffPlugin.deletedColor.color setFill];
301+
[gitDiffPlugin.colorsWindowController.deletedColor setFill];
285302
[self getParagraphRect:&a0 firstLineRect:&a1 forLineNumber:line];
286303
a0.size.height = 1.;
287304
NSRectFill( a0 );
@@ -302,6 +319,7 @@ - (id)gitdiff_annotationAtSidebarPoint:(CGPoint)p0
302319
{
303320
id annotation = [self gitdiff_annotationAtSidebarPoint:p0];
304321
NSTextView *popover = gitDiffPlugin.popover;
322+
popover.backgroundColor = gitDiffPlugin.colorsWindowController.popoverColor;
305323

306324
if ( !annotation && p0.x < self.sidebarWidth ) {
307325
GitFileDiffs *diffs = [self gitDiffs];
@@ -320,7 +338,7 @@ - (id)gitdiff_annotationAtSidebarPoint:(CGPoint)p0
320338
NSString *before = [NSString stringWithUTF8String:deleted.c_str()];
321339

322340
if ( exists( diffs->added, start ) ) {
323-
NSDictionary *attributes = @{NSForegroundColorAttributeName : gitDiffPlugin.changedColor.color};
341+
NSDictionary *attributes = @{NSForegroundColorAttributeName : gitDiffPlugin.colorsWindowController.changedColor};
324342
NSMutableAttributedString *attrstr = [[NSMutableAttributedString alloc] init];
325343

326344
std::string added = diffs->added[start];
@@ -389,7 +407,7 @@ - (void)gitdiff_drawKnobSlotInRect:(CGRect)a0 highlight:(char)a1
389407
for ( const auto &added : diffs->added ) {
390408
NSUInteger line = added.first;
391409
NSColor *highlight = exists( diffs->modified, line ) ?
392-
gitDiffPlugin.modifiedColor.color : gitDiffPlugin.addedColor.color;
410+
gitDiffPlugin.colorsWindowController.modifiedColor : gitDiffPlugin.colorsWindowController.addedColor;
393411

394412
[highlight setFill];
395413
NSRectFill( NSMakeRect(0, line*scale, 3., 1.) );
@@ -398,7 +416,7 @@ - (void)gitdiff_drawKnobSlotInRect:(CGRect)a0 highlight:(char)a1
398416
for ( const auto &deleted : diffs->deleted ) {
399417
NSUInteger line = deleted.first;
400418
if ( !exists( diffs->added, line ) ) {
401-
[gitDiffPlugin.deletedColor.color setFill];
419+
[gitDiffPlugin.colorsWindowController.deletedColor setFill];
402420
NSRectFill( NSMakeRect(0, line*scale, 3., 1.) );
403421
}
404422
}

0 commit comments

Comments
 (0)