Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Move some badly misplaced KVO code
  • Loading branch information
dmoagx committed May 6, 2018
1 parent 2252b66 commit 0e5c658
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 22 deletions.
8 changes: 0 additions & 8 deletions Source/SPDatabaseDocument.m
Expand Up @@ -6395,7 +6395,6 @@ - (void)_addPreferenceObservers
// Register observers for when the DisplayTableViewVerticalGridlines preference changes
[prefs addObserver:self forKeyPath:SPDisplayTableViewVerticalGridlines options:NSKeyValueObservingOptionNew context:NULL];
[prefs addObserver:tableSourceInstance forKeyPath:SPDisplayTableViewVerticalGridlines options:NSKeyValueObservingOptionNew context:NULL];
[prefs addObserver:tableContentInstance forKeyPath:SPDisplayTableViewVerticalGridlines options:NSKeyValueObservingOptionNew context:NULL];
[prefs addObserver:customQueryInstance forKeyPath:SPDisplayTableViewVerticalGridlines options:NSKeyValueObservingOptionNew context:NULL];
[prefs addObserver:tableRelationsInstance forKeyPath:SPDisplayTableViewVerticalGridlines options:NSKeyValueObservingOptionNew context:NULL];
[prefs addObserver:[SPQueryController sharedQueryController] forKeyPath:SPDisplayTableViewVerticalGridlines options:NSKeyValueObservingOptionNew context:NULL];
Expand All @@ -6404,9 +6403,6 @@ - (void)_addPreferenceObservers
[prefs addObserver:tableSourceInstance forKeyPath:SPUseMonospacedFonts options:NSKeyValueObservingOptionNew context:NULL];
[prefs addObserver:[SPQueryController sharedQueryController] forKeyPath:SPUseMonospacedFonts options:NSKeyValueObservingOptionNew context:NULL];

[prefs addObserver:tableContentInstance forKeyPath:SPGlobalResultTableFont options:NSKeyValueObservingOptionNew context:NULL];
[prefs addObserver:tableContentInstance forKeyPath:SPDisplayBinaryDataAsHex options:NSKeyValueObservingOptionNew context:NULL];

// Register observers for when the logging preference changes
[prefs addObserver:[SPQueryController sharedQueryController] forKeyPath:SPConsoleEnableLogging options:NSKeyValueObservingOptionNew context:NULL];

Expand All @@ -6424,13 +6420,9 @@ - (void)_removePreferenceObservers

[prefs removeObserver:tableSourceInstance forKeyPath:SPUseMonospacedFonts];

[prefs removeObserver:tableContentInstance forKeyPath:SPGlobalResultTableFont];
[prefs removeObserver:tableContentInstance forKeyPath:SPDisplayBinaryDataAsHex];

[prefs removeObserver:customQueryInstance forKeyPath:SPDisplayTableViewVerticalGridlines];
[prefs removeObserver:tableRelationsInstance forKeyPath:SPDisplayTableViewVerticalGridlines];
[prefs removeObserver:tableSourceInstance forKeyPath:SPDisplayTableViewVerticalGridlines];
[prefs removeObserver:tableContentInstance forKeyPath:SPDisplayTableViewVerticalGridlines];

[prefs removeObserver:[SPQueryController sharedQueryController] forKeyPath:SPUseMonospacedFonts];
[prefs removeObserver:[SPQueryController sharedQueryController] forKeyPath:SPConsoleEnableLogging];
Expand Down
51 changes: 37 additions & 14 deletions Source/SPTableContent.m
Expand Up @@ -65,6 +65,12 @@
#import <SPMySQL/SPMySQL.h>
#include <stdlib.h>

/**
* This is the unique KVO context of code that resides in THIS class.
* Do not try to give it to other classes, ESPECIALLY NOT child classes!
*/
static void *TableContentKVOContext = &TableContentKVOContext;

#ifndef SP_CODA
static NSString *SPTableFilterSetDefaultOperator = @"SPTableFilterSetDefaultOperator";
#endif
Expand Down Expand Up @@ -287,6 +293,10 @@ - (void)awakeFromNib
// filterTableDefaultOperator = [[self escapeFilterTableDefaultOperator:nil] retain];
#endif

[prefs addObserver:self forKeyPath:SPDisplayTableViewVerticalGridlines options:NSKeyValueObservingOptionNew context:TableContentKVOContext];
[prefs addObserver:self forKeyPath:SPGlobalResultTableFont options:NSKeyValueObservingOptionNew context:TableContentKVOContext];
[prefs addObserver:self forKeyPath:SPDisplayBinaryDataAsHex options:NSKeyValueObservingOptionNew context:TableContentKVOContext];

// Add observers for document task activity
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(startDocumentTaskForTab:)
Expand Down Expand Up @@ -4197,22 +4207,28 @@ - (void)documentWillClose:(NSNotification *)notification
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
#ifndef SP_CODA /* observe pref changes */
// Display table veiew vertical gridlines preference changed
if ([keyPath isEqualToString:SPDisplayTableViewVerticalGridlines]) {
[tableContentView setGridStyleMask:([[change objectForKey:NSKeyValueChangeNewKey] boolValue]) ? NSTableViewSolidVerticalGridLineMask : NSTableViewGridNone];
[filterTableView setGridStyleMask:([[change objectForKey:NSKeyValueChangeNewKey] boolValue]) ? NSTableViewSolidVerticalGridLineMask : NSTableViewGridNone];
}
// Table font preference changed
else if ([keyPath isEqualToString:SPGlobalResultTableFont]) {
NSFont *tableFont = [NSUnarchiver unarchiveObjectWithData:[change objectForKey:NSKeyValueChangeNewKey]];
// a parent class (or cocoa) can also use KVO, so we need to watch out to only catch those KVO messages we requested
if(context == TableContentKVOContext) {
// Display table veiew vertical gridlines preference changed
if ([keyPath isEqualToString:SPDisplayTableViewVerticalGridlines]) {
[tableContentView setGridStyleMask:([[change objectForKey:NSKeyValueChangeNewKey] boolValue]) ? NSTableViewSolidVerticalGridLineMask : NSTableViewGridNone];
[filterTableView setGridStyleMask:([[change objectForKey:NSKeyValueChangeNewKey] boolValue]) ? NSTableViewSolidVerticalGridLineMask : NSTableViewGridNone];
}
// Table font preference changed
else if ([keyPath isEqualToString:SPGlobalResultTableFont]) {
NSFont *tableFont = [NSUnarchiver unarchiveObjectWithData:[change objectForKey:NSKeyValueChangeNewKey]];

[tableContentView setRowHeight:2.0f + NSSizeToCGSize([@"{ǞṶḹÜ∑zgyf" sizeWithAttributes:@{NSFontAttributeName : tableFont}]).height];
[tableContentView setFont:tableFont];
[tableContentView reloadData];
[tableContentView setRowHeight:2.0f + NSSizeToCGSize([@"{ǞṶḹÜ∑zgyf" sizeWithAttributes:@{NSFontAttributeName : tableFont}]).height];
[tableContentView setFont:tableFont];
[tableContentView reloadData];
}
// Display binary data as Hex
else if ([keyPath isEqualToString:SPDisplayBinaryDataAsHex] && [tableContentView numberOfRows] > 0) {
[tableContentView reloadData];
}
}
// Display binary data as Hex
else if ([keyPath isEqualToString:SPDisplayBinaryDataAsHex] && [tableContentView numberOfRows] > 0) {
[tableContentView reloadData];
else {
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
#endif
}
Expand Down Expand Up @@ -5461,6 +5477,13 @@ - (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];

if(_mainNibLoaded) {
//TODO this should be changed to the variant with …context: after 10.6 support is removed!
[prefs removeObserver:self forKeyPath:SPGlobalResultTableFont];
[prefs removeObserver:self forKeyPath:SPDisplayBinaryDataAsHex];
[prefs removeObserver:self forKeyPath:SPDisplayTableViewVerticalGridlines];
}

// Cancel previous performSelector: requests on ourselves and the table view
// to prevent crashes for deferred actions
[NSObject cancelPreviousPerformRequestsWithTarget:self];
Expand Down

0 comments on commit 0e5c658

Please sign in to comment.