Skip to content

Commit f4ac8ae

Browse files
committed
Change the way SPTextView loads theme colors to prevent loading invalid colors (which could have been stored in prefs before the previous commit) (part of #2963)
1 parent 7f43f5e commit f4ac8ae

File tree

2 files changed

+72
-31
lines changed

2 files changed

+72
-31
lines changed

Source/SPEditorPreferencePane.m

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#import "SPColorWellCell.h"
3434
#import "SPAlertSheets.h"
3535
#import "SPCategoryAdditions.h"
36+
#import "SPFunctions.h"
3637

3738
// Constants
3839
static NSString *SPSaveColorScheme = @"SaveColorScheme";
@@ -222,8 +223,6 @@ - (IBAction)duplicateTheme:(id)sender
222223
}
223224
}
224225

225-
NSBeep();
226-
227226
[editThemeListTable reloadData];
228227
}
229228

@@ -256,8 +255,6 @@ - (IBAction)removeTheme:(id)sender
256255
}
257256
}
258257

259-
NSBeep();
260-
261258
[editThemeListTable reloadData];
262259
}
263260

@@ -288,17 +285,26 @@ - (IBAction)setDefaultColors:(id)sender
288285
[[NSColorPanel sharedColorPanel] close];
289286

290287
[prefs setObject:SPDefaultColorSchemeName forKey:SPCustomQueryEditorThemeName];
291-
[prefs setObject:[NSArchiver archivedDataWithRootObject:[NSColor colorWithDeviceRed:0.000f green:0.455f blue:0.000f alpha:1.000f]] forKey:SPCustomQueryEditorCommentColor];
292-
[prefs setObject:[NSArchiver archivedDataWithRootObject:[NSColor colorWithDeviceRed:0.769f green:0.102f blue:0.086f alpha:1.000f]] forKey:SPCustomQueryEditorQuoteColor];
293-
[prefs setObject:[NSArchiver archivedDataWithRootObject:[NSColor colorWithDeviceRed:0.200f green:0.250f blue:1.000f alpha:1.000f]] forKey:SPCustomQueryEditorSQLKeywordColor];
294-
[prefs setObject:[NSArchiver archivedDataWithRootObject:[NSColor colorWithDeviceRed:0.000f green:0.000f blue:0.658f alpha:1.000f]] forKey:SPCustomQueryEditorBacktickColor];
295-
[prefs setObject:[NSArchiver archivedDataWithRootObject:[NSColor colorWithDeviceRed:0.506f green:0.263f blue:0.000f alpha:1.000f]] forKey:SPCustomQueryEditorNumericColor];
296-
[prefs setObject:[NSArchiver archivedDataWithRootObject:[NSColor colorWithDeviceRed:0.500f green:0.500f blue:0.500f alpha:1.000f]] forKey:SPCustomQueryEditorVariableColor];
297-
[prefs setObject:[NSArchiver archivedDataWithRootObject:[NSColor colorWithDeviceRed:0.950f green:0.950f blue:0.950f alpha:1.000f]] forKey:SPCustomQueryEditorHighlightQueryColor];
298-
[prefs setObject:[NSArchiver archivedDataWithRootObject:[NSColor colorWithDeviceRed:0.7098f green:0.8352f blue:1.000f alpha:1.000f]] forKey:SPCustomQueryEditorSelectionColor];
299-
[prefs setObject:[NSArchiver archivedDataWithRootObject:[NSColor blackColor]] forKey:SPCustomQueryEditorTextColor];
300-
[prefs setObject:[NSArchiver archivedDataWithRootObject:[NSColor blackColor]] forKey:SPCustomQueryEditorCaretColor];
301-
[prefs setObject:[NSArchiver archivedDataWithRootObject:[NSColor whiteColor]] forKey:SPCustomQueryEditorBackgroundColor];
288+
289+
NSDictionary *vendorDefaults = [prefs volatileDomainForName:NSRegistrationDomain]; // corresponds to -registerDefaults: in the app controller
290+
291+
NSArray *copyKeys = @[
292+
SPCustomQueryEditorCommentColor,
293+
SPCustomQueryEditorQuoteColor,
294+
SPCustomQueryEditorSQLKeywordColor,
295+
SPCustomQueryEditorBacktickColor,
296+
SPCustomQueryEditorNumericColor,
297+
SPCustomQueryEditorVariableColor,
298+
SPCustomQueryEditorHighlightQueryColor,
299+
SPCustomQueryEditorSelectionColor,
300+
SPCustomQueryEditorTextColor,
301+
SPCustomQueryEditorCaretColor,
302+
SPCustomQueryEditorBackgroundColor,
303+
];
304+
305+
for(NSString *key in copyKeys) {
306+
[prefs setObject:[vendorDefaults objectForKey:key] forKey:key];
307+
}
302308

303309
[colorSettingTableView setBackgroundColor:[NSUnarchiver unarchiveObjectWithData:[prefs dataForKey:SPCustomQueryEditorBackgroundColor]]];
304310
[colorSettingTableView reloadData];

Source/SPTextView.m

Lines changed: 51 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ @interface SPTextView (Private_API)
8585

8686
NSInteger _alphabeticSort(id string1, id string2, void *reverse);
8787
#ifndef SP_CODA
88+
- (void)_setTextSelectionColor:(NSColor *)newSelectionColor;
8889
- (void)_setTextSelectionColor:(NSColor *)newSelectionColor onBackgroundColor:(NSColor *)aBackgroundColor;
8990
#endif
9091
- (void)_positionCompletionPopup:(SPNarrowDownCompletion *)aPopup relativeToTextAtLocation:(NSUInteger)aLocation;
@@ -185,27 +186,56 @@ - (void) awakeFromNib
185186
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(boundsDidChangeNotification:) name:NSViewBoundsDidChangeNotification object:[scrollView contentView]];
186187

187188
#ifndef SP_CODA
188-
[self setQueryHiliteColor:[NSUnarchiver unarchiveObjectWithData:[prefs dataForKey:SPCustomQueryEditorHighlightQueryColor]]];
189-
NSColor *backgroundColor = [NSUnarchiver unarchiveObjectWithData:[prefs dataForKey:SPCustomQueryEditorBackgroundColor]];
190-
[self setQueryEditorBackgroundColor:backgroundColor];
191-
[self setBackgroundColor:backgroundColor];
192-
[self setCommentColor:[NSUnarchiver unarchiveObjectWithData:[prefs dataForKey:SPCustomQueryEditorCommentColor]]];
193-
[self setQuoteColor:[NSUnarchiver unarchiveObjectWithData:[prefs dataForKey:SPCustomQueryEditorQuoteColor]]];
194-
[self setKeywordColor:[NSUnarchiver unarchiveObjectWithData:[prefs dataForKey:SPCustomQueryEditorSQLKeywordColor]]];
195-
[self setBacktickColor:[NSUnarchiver unarchiveObjectWithData:[prefs dataForKey:SPCustomQueryEditorBacktickColor]]];
196-
[self setNumericColor:[NSUnarchiver unarchiveObjectWithData:[prefs dataForKey:SPCustomQueryEditorNumericColor]]];
197-
[self setVariableColor:[NSUnarchiver unarchiveObjectWithData:[prefs dataForKey:SPCustomQueryEditorVariableColor]]];
198-
[self setOtherTextColor:[NSUnarchiver unarchiveObjectWithData:[prefs dataForKey:SPCustomQueryEditorTextColor]]];
199-
[self setTextColor:otherTextColor];
200-
[self setInsertionPointColor:[NSUnarchiver unarchiveObjectWithData:[prefs dataForKey:SPCustomQueryEditorCaretColor]]];
189+
{
190+
struct csItem {
191+
NSString *p;
192+
SEL m;
193+
} colorSetup[] = {
194+
{ .p = SPCustomQueryEditorHighlightQueryColor, .m = @selector(setQueryHiliteColor:) },
195+
{ .p = SPCustomQueryEditorBackgroundColor, .m = @selector(setQueryEditorBackgroundColor:) },
196+
{ .p = SPCustomQueryEditorBackgroundColor, .m = @selector(setBackgroundColor:) },
197+
{ .p = SPCustomQueryEditorCommentColor, .m = @selector(setCommentColor:) },
198+
{ .p = SPCustomQueryEditorQuoteColor, .m = @selector(setQuoteColor:) },
199+
{ .p = SPCustomQueryEditorSQLKeywordColor, .m = @selector(setKeywordColor:) },
200+
{ .p = SPCustomQueryEditorBacktickColor, .m = @selector(setBacktickColor:) },
201+
{ .p = SPCustomQueryEditorNumericColor, .m = @selector(setNumericColor:) },
202+
{ .p = SPCustomQueryEditorVariableColor, .m = @selector(setVariableColor:) },
203+
{ .p = SPCustomQueryEditorTextColor, .m = @selector(setOtherTextColor:) },
204+
{ .p = SPCustomQueryEditorTextColor, .m = @selector(setTextColor:) },
205+
{ .p = SPCustomQueryEditorCaretColor, .m = @selector(setInsertionPointColor:) },
206+
{ .p = SPCustomQueryEditorSelectionColor, .m = @selector(_setTextSelectionColor:) },
207+
{ .p = nil, .m = NULL } // stop key
208+
};
209+
210+
struct csItem *item = &colorSetup[0];
211+
212+
NSDictionary *vendorDefaults = [prefs volatileDomainForName:NSRegistrationDomain]; //prefs from -registerDefaults: in app controller
213+
214+
do {
215+
NSData *colorData = [prefs dataForKey:item->p];
216+
NSColor *color;
217+
BOOL canRetry = YES;
218+
retry:
219+
if(colorData && (color = [NSUnarchiver unarchiveObjectWithData:colorData])) {
220+
[self performSelector:item->m withObject:color];
221+
}
222+
else if(canRetry) {
223+
// #2963: previous versions of SP would accept invalid data (resulting in `nil`) and store it in prefs,
224+
// so if loading failed use the default color instead (`nil` would cause exceptions later on)
225+
colorData = [vendorDefaults objectForKey:item->p];
226+
canRetry = NO;
227+
SPLog(@"user defaults contains invalid value for theme color '%@'! (retrying with default value)", item->p);
228+
goto retry;
229+
}
230+
} while((++item)->p);
231+
}
232+
201233
[self setShouldHiliteQuery:[prefs boolForKey:SPCustomQueryHighlightCurrentQuery]];
202234

203-
[self _setTextSelectionColor:[NSUnarchiver unarchiveObjectWithData:[prefs dataForKey:SPCustomQueryEditorSelectionColor]] onBackgroundColor:backgroundColor];
204-
205235
[self setAutomaticDashSubstitutionEnabled:NO]; // prevents -- from becoming —, the em dash.
206236
[self setAutomaticQuoteSubstitutionEnabled:NO]; // prevents ' and " from becoming ‘, ’ and “, ” respectively.
207237

208-
// Register observers for the when editor background colors preference changes
238+
// Register observers for the when editor colors preference changes
209239
[prefs addObserver:self forKeyPath:SPCustomQueryEditorSelectionColor options:NSKeyValueObservingOptionNew context:NULL];
210240
[prefs addObserver:self forKeyPath:SPCustomQueryEditorCaretColor options:NSKeyValueObservingOptionNew context:NULL];
211241
[prefs addObserver:self forKeyPath:SPCustomQueryEditorFont options:NSKeyValueObservingOptionNew context:NULL];
@@ -3736,6 +3766,11 @@ - (void)_setTextSelectionColor:(NSColor *)newSelectionColor onBackgroundColor:(N
37363766
// Set the selection colour
37373767
[self setSelectedTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:newSelectionColor, NSBackgroundColorAttributeName, nil]];
37383768
}
3769+
3770+
- (void)_setTextSelectionColor:(NSColor *)newSelectionColor
3771+
{
3772+
[self _setTextSelectionColor:newSelectionColor onBackgroundColor:[self backgroundColor]];
3773+
}
37393774
#endif
37403775

37413776
/**

0 commit comments

Comments
 (0)