diff options
author | Max <post@wickenrode.com> | 2018-01-13 04:13:17 +0100 |
---|---|---|
committer | Max <post@wickenrode.com> | 2018-01-13 04:13:17 +0100 |
commit | f4ac8ae36e216f2b678e46f7c8f98e5a55c40c76 (patch) | |
tree | fbe96e8bbdba3057a2c769aeff9f01d90619978a /Source | |
parent | 7f43f5eb60ba409af7b56b185802f6bac1abe8e7 (diff) | |
download | sequelpro-f4ac8ae36e216f2b678e46f7c8f98e5a55c40c76.tar.gz sequelpro-f4ac8ae36e216f2b678e46f7c8f98e5a55c40c76.tar.bz2 sequelpro-f4ac8ae36e216f2b678e46f7c8f98e5a55c40c76.zip |
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)
Diffstat (limited to 'Source')
-rw-r--r-- | Source/SPEditorPreferencePane.m | 36 | ||||
-rw-r--r-- | Source/SPTextView.m | 67 |
2 files changed, 72 insertions, 31 deletions
diff --git a/Source/SPEditorPreferencePane.m b/Source/SPEditorPreferencePane.m index 605d8a8b..c09e1fe8 100644 --- a/Source/SPEditorPreferencePane.m +++ b/Source/SPEditorPreferencePane.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 @@ static NSString *SPCustomColorSchemeNameLC = @"user-defined"; } } - NSBeep(); - [editThemeListTable reloadData]; } @@ -256,8 +255,6 @@ static NSString *SPCustomColorSchemeNameLC = @"user-defined"; } } - NSBeep(); - [editThemeListTable reloadData]; } @@ -288,17 +285,26 @@ static NSString *SPCustomColorSchemeNameLC = @"user-defined"; [[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]; diff --git a/Source/SPTextView.m b/Source/SPTextView.m index 36bd005e..f6ee4efc 100644 --- a/Source/SPTextView.m +++ b/Source/SPTextView.m @@ -85,6 +85,7 @@ 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 @@ static inline NSPoint SPPointOnLine(NSPoint a, NSPoint b, CGFloat t) { return NS [[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 @@ NSInteger _alphabeticSort(id string1, id string2, void *reverse) // Set the selection colour [self setSelectedTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:newSelectionColor, NSBackgroundColorAttributeName, nil]]; } + +- (void)_setTextSelectionColor:(NSColor *)newSelectionColor +{ + [self _setTextSelectionColor:newSelectionColor onBackgroundColor:[self backgroundColor]]; +} #endif /** |