forked from johnno1962/GitDiff
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGitDiffColorsWindowController.m
More file actions
168 lines (131 loc) · 5.42 KB
/
GitDiffColorsWindowController.m
File metadata and controls
168 lines (131 loc) · 5.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
//
// GitDiffColorsWindowController.m
// GitDiff
//
// Created by Allen Wu on 8/17/14.
//
//
#import "GitDiffColorsWindowController.h"
static NSString *const GitDiffModifiedColorKey = @"GitDiffModifiedColor";
static NSString *const GitDiffAddedColorKey = @"GitDiffAddedColor";
static NSString *const GitDiffDeletedColorKey = @"GitDiffDeletedColor";
static NSString *const GitDiffPopoverColorKey = @"GitDiffPopoverColor";
static NSString *const GitDiffChangedColorKey = @"GitDiffChangedColor";
@interface GitDiffColorsWindowController ()
{
NSDictionary *_pluginDefaults;
NSUserDefaults *_userDefaults;
}
@property (weak) IBOutlet NSColorWell *modifiedColorWell;
@property (weak) IBOutlet NSColorWell *addedColorWell;
@property (weak) IBOutlet NSColorWell *deletedColorWell;
@property (weak) IBOutlet NSColorWell *popoverColorWell;
@property (weak) IBOutlet NSColorWell *changedColorWell;
@end
@implementation GitDiffColorsWindowController
- (instancetype)initWithPluginBundle:(NSBundle *)bundle {
NSString *nibPath = [bundle pathForResource:@"GitDiff" ofType:@"nib"];
if (!nibPath) {
if ( [[NSAlert alertWithMessageText:@"GitDiff Plugin:"
defaultButton:@"OK" alternateButton:@"Goto GitHub" otherButton:nil
informativeTextWithFormat:@"Could not load colors interface. GitDiff will not work correctly. This is a problem when installing using Alcatraz related to Xcode6. Please download GitDiff and build from the sources on GitHub. This will install the plugin manually."]
runModal] == NSAlertAlternateReturn )
[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:@"https://github.com/johnno1962/GitDiff"]];
return nil;
}
self = [super initWithWindowNibPath:nibPath owner:self];
if (self)
{
NSString *pluginDefaultsPath = [bundle pathForResource:@"Defaults" ofType:@"plist"];
_pluginDefaults = [NSDictionary dictionaryWithContentsOfFile:pluginDefaultsPath];
_userDefaults = [NSUserDefaults standardUserDefaults];
[self loadWindow];
}
return self;
}
- (void)awakeFromNib {
[self loadColors];
}
#pragma mark - Actions
- (IBAction)gitDiffColorWellChanged:(id)sender
{
[self saveColors];
}
- (IBAction)gitDiffColorResetPressed:(id)sender
{
[self resetColors];
}
#pragma mark - Acessors
- (NSColor *)modifiedColor {
return [self colorForKey:GitDiffModifiedColorKey];
}
- (NSColor *)addedColor {
return [self colorForKey:GitDiffAddedColorKey];
}
- (NSColor *)deletedColor {
return [self colorForKey:GitDiffDeletedColorKey];
}
- (NSColor *)popoverColor {
return [self colorForKey:GitDiffPopoverColorKey];
}
- (NSColor *)changedColor {
return [self colorForKey:GitDiffChangedColorKey];
}
#pragma mark - Colors
- (void)resetColors
{
self.modifiedColorWell.color = [self colorFromPlistString:_pluginDefaults[GitDiffModifiedColorKey]];
self.addedColorWell.color = [self colorFromPlistString:_pluginDefaults[GitDiffAddedColorKey]];
self.deletedColorWell.color = [self colorFromPlistString:_pluginDefaults[GitDiffDeletedColorKey]];
self.popoverColorWell.color = [self colorFromPlistString:_pluginDefaults[GitDiffPopoverColorKey]];
self.changedColorWell.color = [self colorFromPlistString:_pluginDefaults[GitDiffChangedColorKey]];
[self saveColors];
}
- (void)loadColors
{
self.modifiedColorWell.color = [self colorForKey:GitDiffModifiedColorKey];
self.addedColorWell.color = [self colorForKey:GitDiffAddedColorKey];
self.deletedColorWell.color = [self colorForKey:GitDiffDeletedColorKey];
self.popoverColorWell.color = [self colorForKey:GitDiffPopoverColorKey];
self.changedColorWell.color = [self colorForKey:GitDiffChangedColorKey];
}
- (void)saveColors
{
[_userDefaults setObject:[self plistStringFromColor:self.modifiedColorWell.color] forKey:GitDiffModifiedColorKey];
[_userDefaults setObject:[self plistStringFromColor:self.addedColorWell.color] forKey:GitDiffAddedColorKey];
[_userDefaults setObject:[self plistStringFromColor:self.deletedColorWell.color] forKey:GitDiffDeletedColorKey];
[_userDefaults setObject:[self plistStringFromColor:self.popoverColorWell.color] forKey:GitDiffPopoverColorKey];
[_userDefaults setObject:[self plistStringFromColor:self.changedColorWell.color] forKey:GitDiffChangedColorKey];
}
#pragma mark - Helpers
- (NSColor *)colorFromPlistString:(NSString *)colorString
{
NSArray* colorStringComponents = [colorString componentsSeparatedByString:@" "];
CGFloat r = 0.0f,
g = 0.0f,
b = 0.0f,
a = 0.0f;
if ( colorStringComponents.count == 4 ) {
r = [colorStringComponents[0] floatValue];
g = [colorStringComponents[1] floatValue];
b = [colorStringComponents[2] floatValue];
a = [colorStringComponents[3] floatValue];
}
return [NSColor colorWithRed:r green:g blue:b alpha:a];
}
- (NSString *)plistStringFromColor:(NSColor *)color
{
CGFloat r, g, b, a;
[[color colorUsingColorSpace:[NSColorSpace deviceRGBColorSpace]] getRed:&r green:&g blue:&b alpha:&a];
return [NSString stringWithFormat:@"%.3f %.3f %.3f %.3f", r, g, b, a];
}
- (NSColor *)colorForKey:(NSString *)key
{
if ( [_userDefaults stringForKey:key] ) {
return [self colorFromPlistString:[_userDefaults stringForKey:key]];
}
else {
return [self colorFromPlistString:_pluginDefaults[key]];
}
}
@end