Skip to content

Commit 0e5c658

Browse files
committed
Move some badly misplaced KVO code
1 parent 2252b66 commit 0e5c658

File tree

2 files changed

+37
-22
lines changed

2 files changed

+37
-22
lines changed

Source/SPDatabaseDocument.m

-8
Original file line numberDiff line numberDiff line change
@@ -6395,7 +6395,6 @@ - (void)_addPreferenceObservers
63956395
// Register observers for when the DisplayTableViewVerticalGridlines preference changes
63966396
[prefs addObserver:self forKeyPath:SPDisplayTableViewVerticalGridlines options:NSKeyValueObservingOptionNew context:NULL];
63976397
[prefs addObserver:tableSourceInstance forKeyPath:SPDisplayTableViewVerticalGridlines options:NSKeyValueObservingOptionNew context:NULL];
6398-
[prefs addObserver:tableContentInstance forKeyPath:SPDisplayTableViewVerticalGridlines options:NSKeyValueObservingOptionNew context:NULL];
63996398
[prefs addObserver:customQueryInstance forKeyPath:SPDisplayTableViewVerticalGridlines options:NSKeyValueObservingOptionNew context:NULL];
64006399
[prefs addObserver:tableRelationsInstance forKeyPath:SPDisplayTableViewVerticalGridlines options:NSKeyValueObservingOptionNew context:NULL];
64016400
[prefs addObserver:[SPQueryController sharedQueryController] forKeyPath:SPDisplayTableViewVerticalGridlines options:NSKeyValueObservingOptionNew context:NULL];
@@ -6404,9 +6403,6 @@ - (void)_addPreferenceObservers
64046403
[prefs addObserver:tableSourceInstance forKeyPath:SPUseMonospacedFonts options:NSKeyValueObservingOptionNew context:NULL];
64056404
[prefs addObserver:[SPQueryController sharedQueryController] forKeyPath:SPUseMonospacedFonts options:NSKeyValueObservingOptionNew context:NULL];
64066405

6407-
[prefs addObserver:tableContentInstance forKeyPath:SPGlobalResultTableFont options:NSKeyValueObservingOptionNew context:NULL];
6408-
[prefs addObserver:tableContentInstance forKeyPath:SPDisplayBinaryDataAsHex options:NSKeyValueObservingOptionNew context:NULL];
6409-
64106406
// Register observers for when the logging preference changes
64116407
[prefs addObserver:[SPQueryController sharedQueryController] forKeyPath:SPConsoleEnableLogging options:NSKeyValueObservingOptionNew context:NULL];
64126408

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

64256421
[prefs removeObserver:tableSourceInstance forKeyPath:SPUseMonospacedFonts];
64266422

6427-
[prefs removeObserver:tableContentInstance forKeyPath:SPGlobalResultTableFont];
6428-
[prefs removeObserver:tableContentInstance forKeyPath:SPDisplayBinaryDataAsHex];
6429-
64306423
[prefs removeObserver:customQueryInstance forKeyPath:SPDisplayTableViewVerticalGridlines];
64316424
[prefs removeObserver:tableRelationsInstance forKeyPath:SPDisplayTableViewVerticalGridlines];
64326425
[prefs removeObserver:tableSourceInstance forKeyPath:SPDisplayTableViewVerticalGridlines];
6433-
[prefs removeObserver:tableContentInstance forKeyPath:SPDisplayTableViewVerticalGridlines];
64346426

64356427
[prefs removeObserver:[SPQueryController sharedQueryController] forKeyPath:SPUseMonospacedFonts];
64366428
[prefs removeObserver:[SPQueryController sharedQueryController] forKeyPath:SPConsoleEnableLogging];

Source/SPTableContent.m

+37-14
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,12 @@
6565
#import <SPMySQL/SPMySQL.h>
6666
#include <stdlib.h>
6767

68+
/**
69+
* This is the unique KVO context of code that resides in THIS class.
70+
* Do not try to give it to other classes, ESPECIALLY NOT child classes!
71+
*/
72+
static void *TableContentKVOContext = &TableContentKVOContext;
73+
6874
#ifndef SP_CODA
6975
static NSString *SPTableFilterSetDefaultOperator = @"SPTableFilterSetDefaultOperator";
7076
#endif
@@ -287,6 +293,10 @@ - (void)awakeFromNib
287293
// filterTableDefaultOperator = [[self escapeFilterTableDefaultOperator:nil] retain];
288294
#endif
289295

296+
[prefs addObserver:self forKeyPath:SPDisplayTableViewVerticalGridlines options:NSKeyValueObservingOptionNew context:TableContentKVOContext];
297+
[prefs addObserver:self forKeyPath:SPGlobalResultTableFont options:NSKeyValueObservingOptionNew context:TableContentKVOContext];
298+
[prefs addObserver:self forKeyPath:SPDisplayBinaryDataAsHex options:NSKeyValueObservingOptionNew context:TableContentKVOContext];
299+
290300
// Add observers for document task activity
291301
[[NSNotificationCenter defaultCenter] addObserver:self
292302
selector:@selector(startDocumentTaskForTab:)
@@ -4197,22 +4207,28 @@ - (void)documentWillClose:(NSNotification *)notification
41974207
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
41984208
{
41994209
#ifndef SP_CODA /* observe pref changes */
4200-
// Display table veiew vertical gridlines preference changed
4201-
if ([keyPath isEqualToString:SPDisplayTableViewVerticalGridlines]) {
4202-
[tableContentView setGridStyleMask:([[change objectForKey:NSKeyValueChangeNewKey] boolValue]) ? NSTableViewSolidVerticalGridLineMask : NSTableViewGridNone];
4203-
[filterTableView setGridStyleMask:([[change objectForKey:NSKeyValueChangeNewKey] boolValue]) ? NSTableViewSolidVerticalGridLineMask : NSTableViewGridNone];
4204-
}
4205-
// Table font preference changed
4206-
else if ([keyPath isEqualToString:SPGlobalResultTableFont]) {
4207-
NSFont *tableFont = [NSUnarchiver unarchiveObjectWithData:[change objectForKey:NSKeyValueChangeNewKey]];
4210+
// a parent class (or cocoa) can also use KVO, so we need to watch out to only catch those KVO messages we requested
4211+
if(context == TableContentKVOContext) {
4212+
// Display table veiew vertical gridlines preference changed
4213+
if ([keyPath isEqualToString:SPDisplayTableViewVerticalGridlines]) {
4214+
[tableContentView setGridStyleMask:([[change objectForKey:NSKeyValueChangeNewKey] boolValue]) ? NSTableViewSolidVerticalGridLineMask : NSTableViewGridNone];
4215+
[filterTableView setGridStyleMask:([[change objectForKey:NSKeyValueChangeNewKey] boolValue]) ? NSTableViewSolidVerticalGridLineMask : NSTableViewGridNone];
4216+
}
4217+
// Table font preference changed
4218+
else if ([keyPath isEqualToString:SPGlobalResultTableFont]) {
4219+
NSFont *tableFont = [NSUnarchiver unarchiveObjectWithData:[change objectForKey:NSKeyValueChangeNewKey]];
42084220

4209-
[tableContentView setRowHeight:2.0f + NSSizeToCGSize([@"{ǞṶḹÜ∑zgyf" sizeWithAttributes:@{NSFontAttributeName : tableFont}]).height];
4210-
[tableContentView setFont:tableFont];
4211-
[tableContentView reloadData];
4221+
[tableContentView setRowHeight:2.0f + NSSizeToCGSize([@"{ǞṶḹÜ∑zgyf" sizeWithAttributes:@{NSFontAttributeName : tableFont}]).height];
4222+
[tableContentView setFont:tableFont];
4223+
[tableContentView reloadData];
4224+
}
4225+
// Display binary data as Hex
4226+
else if ([keyPath isEqualToString:SPDisplayBinaryDataAsHex] && [tableContentView numberOfRows] > 0) {
4227+
[tableContentView reloadData];
4228+
}
42124229
}
4213-
// Display binary data as Hex
4214-
else if ([keyPath isEqualToString:SPDisplayBinaryDataAsHex] && [tableContentView numberOfRows] > 0) {
4215-
[tableContentView reloadData];
4230+
else {
4231+
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
42164232
}
42174233
#endif
42184234
}
@@ -5461,6 +5477,13 @@ - (void)dealloc
54615477
{
54625478
[[NSNotificationCenter defaultCenter] removeObserver:self];
54635479

5480+
if(_mainNibLoaded) {
5481+
//TODO this should be changed to the variant with …context: after 10.6 support is removed!
5482+
[prefs removeObserver:self forKeyPath:SPGlobalResultTableFont];
5483+
[prefs removeObserver:self forKeyPath:SPDisplayBinaryDataAsHex];
5484+
[prefs removeObserver:self forKeyPath:SPDisplayTableViewVerticalGridlines];
5485+
}
5486+
54645487
// Cancel previous performSelector: requests on ourselves and the table view
54655488
// to prevent crashes for deferred actions
54665489
[NSObject cancelPreviousPerformRequestsWithTarget:self];

0 commit comments

Comments
 (0)