Permalink
Browse files

Change the way SPTextView loads theme colors to prevent loading inval…

…id colors (which could have been stored in prefs before the previous commit) (part of #2963)
  • Loading branch information...
dmoagx committed Jan 13, 2018
1 parent 7f43f5e commit f4ac8ae36e216f2b678e46f7c8f98e5a55c40c76
Showing with 72 additions and 31 deletions.
  1. +21 −15 Source/SPEditorPreferencePane.m
  2. +51 −16 Source/SPTextView.m
@@ -33,6 +33,7 @@
#import "SPColorWellCell.h"
#import "SPAlertSheets.h"
#import "SPCategoryAdditions.h"
#import "SPFunctions.h"
// Constants
static NSString *SPSaveColorScheme = @"SaveColorScheme";
@@ -222,8 +223,6 @@ - (IBAction)duplicateTheme:(id)sender
}
}
NSBeep();
[editThemeListTable reloadData];
}
@@ -256,8 +255,6 @@ - (IBAction)removeTheme:(id)sender
}
}
NSBeep();
[editThemeListTable reloadData];
}
@@ -288,17 +285,26 @@ - (IBAction)setDefaultColors:(id)sender
[[NSColorPanel sharedColorPanel] close];
[prefs setObject:SPDefaultColorSchemeName forKey:SPCustomQueryEditorThemeName];
[prefs setObject:[NSArchiver archivedDataWithRootObject:[NSColor colorWithDeviceRed:0.000f green:0.455f blue:0.000f alpha:1.000f]] forKey:SPCustomQueryEditorCommentColor];
[prefs setObject:[NSArchiver archivedDataWithRootObject:[NSColor colorWithDeviceRed:0.769f green:0.102f blue:0.086f alpha:1.000f]] forKey:SPCustomQueryEditorQuoteColor];
[prefs setObject:[NSArchiver archivedDataWithRootObject:[NSColor colorWithDeviceRed:0.200f green:0.250f blue:1.000f alpha:1.000f]] forKey:SPCustomQueryEditorSQLKeywordColor];
[prefs setObject:[NSArchiver archivedDataWithRootObject:[NSColor colorWithDeviceRed:0.000f green:0.000f blue:0.658f alpha:1.000f]] forKey:SPCustomQueryEditorBacktickColor];
[prefs setObject:[NSArchiver archivedDataWithRootObject:[NSColor colorWithDeviceRed:0.506f green:0.263f blue:0.000f alpha:1.000f]] forKey:SPCustomQueryEditorNumericColor];
[prefs setObject:[NSArchiver archivedDataWithRootObject:[NSColor colorWithDeviceRed:0.500f green:0.500f blue:0.500f alpha:1.000f]] forKey:SPCustomQueryEditorVariableColor];
[prefs setObject:[NSArchiver archivedDataWithRootObject:[NSColor colorWithDeviceRed:0.950f green:0.950f blue:0.950f alpha:1.000f]] forKey:SPCustomQueryEditorHighlightQueryColor];
[prefs setObject:[NSArchiver archivedDataWithRootObject:[NSColor colorWithDeviceRed:0.7098f green:0.8352f blue:1.000f alpha:1.000f]] forKey:SPCustomQueryEditorSelectionColor];
[prefs setObject:[NSArchiver archivedDataWithRootObject:[NSColor blackColor]] forKey:SPCustomQueryEditorTextColor];
[prefs setObject:[NSArchiver archivedDataWithRootObject:[NSColor blackColor]] forKey:SPCustomQueryEditorCaretColor];
[prefs setObject:[NSArchiver archivedDataWithRootObject:[NSColor whiteColor]] forKey:SPCustomQueryEditorBackgroundColor];
NSDictionary *vendorDefaults = [prefs volatileDomainForName:NSRegistrationDomain]; // corresponds to -registerDefaults: in the app controller
NSArray *copyKeys = @[
SPCustomQueryEditorCommentColor,
SPCustomQueryEditorQuoteColor,
SPCustomQueryEditorSQLKeywordColor,
SPCustomQueryEditorBacktickColor,
SPCustomQueryEditorNumericColor,
SPCustomQueryEditorVariableColor,
SPCustomQueryEditorHighlightQueryColor,
SPCustomQueryEditorSelectionColor,
SPCustomQueryEditorTextColor,
SPCustomQueryEditorCaretColor,
SPCustomQueryEditorBackgroundColor,
];
for(NSString *key in copyKeys) {
[prefs setObject:[vendorDefaults objectForKey:key] forKey:key];
}
[colorSettingTableView setBackgroundColor:[NSUnarchiver unarchiveObjectWithData:[prefs dataForKey:SPCustomQueryEditorBackgroundColor]]];
[colorSettingTableView reloadData];
View
@@ -85,6 +85,7 @@ @interface SPTextView (Private_API)
NSInteger _alphabeticSort(id string1, id string2, void *reverse);
#ifndef SP_CODA
- (void)_setTextSelectionColor:(NSColor *)newSelectionColor;
- (void)_setTextSelectionColor:(NSColor *)newSelectionColor onBackgroundColor:(NSColor *)aBackgroundColor;
#endif
- (void)_positionCompletionPopup:(SPNarrowDownCompletion *)aPopup relativeToTextAtLocation:(NSUInteger)aLocation;
@@ -185,27 +186,56 @@ - (void) awakeFromNib
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(boundsDidChangeNotification:) name:NSViewBoundsDidChangeNotification object:[scrollView contentView]];
#ifndef SP_CODA
[self setQueryHiliteColor:[NSUnarchiver unarchiveObjectWithData:[prefs dataForKey:SPCustomQueryEditorHighlightQueryColor]]];
NSColor *backgroundColor = [NSUnarchiver unarchiveObjectWithData:[prefs dataForKey:SPCustomQueryEditorBackgroundColor]];
[self setQueryEditorBackgroundColor:backgroundColor];
[self setBackgroundColor:backgroundColor];
[self setCommentColor:[NSUnarchiver unarchiveObjectWithData:[prefs dataForKey:SPCustomQueryEditorCommentColor]]];
[self setQuoteColor:[NSUnarchiver unarchiveObjectWithData:[prefs dataForKey:SPCustomQueryEditorQuoteColor]]];
[self setKeywordColor:[NSUnarchiver unarchiveObjectWithData:[prefs dataForKey:SPCustomQueryEditorSQLKeywordColor]]];
[self setBacktickColor:[NSUnarchiver unarchiveObjectWithData:[prefs dataForKey:SPCustomQueryEditorBacktickColor]]];
[self setNumericColor:[NSUnarchiver unarchiveObjectWithData:[prefs dataForKey:SPCustomQueryEditorNumericColor]]];
[self setVariableColor:[NSUnarchiver unarchiveObjectWithData:[prefs dataForKey:SPCustomQueryEditorVariableColor]]];
[self setOtherTextColor:[NSUnarchiver unarchiveObjectWithData:[prefs dataForKey:SPCustomQueryEditorTextColor]]];
[self setTextColor:otherTextColor];
[self setInsertionPointColor:[NSUnarchiver unarchiveObjectWithData:[prefs dataForKey:SPCustomQueryEditorCaretColor]]];
{
struct csItem {
NSString *p;
SEL m;
} colorSetup[] = {
{ .p = SPCustomQueryEditorHighlightQueryColor, .m = @selector(setQueryHiliteColor:) },
{ .p = SPCustomQueryEditorBackgroundColor, .m = @selector(setQueryEditorBackgroundColor:) },
{ .p = SPCustomQueryEditorBackgroundColor, .m = @selector(setBackgroundColor:) },
{ .p = SPCustomQueryEditorCommentColor, .m = @selector(setCommentColor:) },
{ .p = SPCustomQueryEditorQuoteColor, .m = @selector(setQuoteColor:) },
{ .p = SPCustomQueryEditorSQLKeywordColor, .m = @selector(setKeywordColor:) },
{ .p = SPCustomQueryEditorBacktickColor, .m = @selector(setBacktickColor:) },
{ .p = SPCustomQueryEditorNumericColor, .m = @selector(setNumericColor:) },
{ .p = SPCustomQueryEditorVariableColor, .m = @selector(setVariableColor:) },
{ .p = SPCustomQueryEditorTextColor, .m = @selector(setOtherTextColor:) },
{ .p = SPCustomQueryEditorTextColor, .m = @selector(setTextColor:) },
{ .p = SPCustomQueryEditorCaretColor, .m = @selector(setInsertionPointColor:) },
{ .p = SPCustomQueryEditorSelectionColor, .m = @selector(_setTextSelectionColor:) },
{ .p = nil, .m = NULL } // stop key
};
struct csItem *item = &colorSetup[0];
NSDictionary *vendorDefaults = [prefs volatileDomainForName:NSRegistrationDomain]; //prefs from -registerDefaults: in app controller
do {
NSData *colorData = [prefs dataForKey:item->p];
NSColor *color;
BOOL canRetry = YES;
retry:
if(colorData && (color = [NSUnarchiver unarchiveObjectWithData:colorData])) {
[self performSelector:item->m withObject:color];
}
else if(canRetry) {
// #2963: previous versions of SP would accept invalid data (resulting in `nil`) and store it in prefs,
// so if loading failed use the default color instead (`nil` would cause exceptions later on)
colorData = [vendorDefaults objectForKey:item->p];
canRetry = NO;
SPLog(@"user defaults contains invalid value for theme color '%@'! (retrying with default value)", item->p);
goto retry;
}
} while((++item)->p);
}
[self setShouldHiliteQuery:[prefs boolForKey:SPCustomQueryHighlightCurrentQuery]];
[self _setTextSelectionColor:[NSUnarchiver unarchiveObjectWithData:[prefs dataForKey:SPCustomQueryEditorSelectionColor]] onBackgroundColor:backgroundColor];
[self setAutomaticDashSubstitutionEnabled:NO]; // prevents -- from becoming —, the em dash.
[self setAutomaticQuoteSubstitutionEnabled:NO]; // prevents ' and " from becoming ‘, ’ and “, ” respectively.
// Register observers for the when editor background colors preference changes
// Register observers for the when editor colors preference changes
[prefs addObserver:self forKeyPath:SPCustomQueryEditorSelectionColor options:NSKeyValueObservingOptionNew context:NULL];
[prefs addObserver:self forKeyPath:SPCustomQueryEditorCaretColor options:NSKeyValueObservingOptionNew context:NULL];
[prefs addObserver:self forKeyPath:SPCustomQueryEditorFont options:NSKeyValueObservingOptionNew context:NULL];
@@ -3736,6 +3766,11 @@ - (void)_setTextSelectionColor:(NSColor *)newSelectionColor onBackgroundColor:(N
// Set the selection colour
[self setSelectedTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:newSelectionColor, NSBackgroundColorAttributeName, nil]];
}
- (void)_setTextSelectionColor:(NSColor *)newSelectionColor
{
[self _setTextSelectionColor:newSelectionColor onBackgroundColor:[self backgroundColor]];
}
#endif
/**

0 comments on commit f4ac8ae

Please sign in to comment.