Skip to content

Commit

Permalink
Fix some code style issues with KVO in indexes controller
Browse files Browse the repository at this point in the history
  • Loading branch information
dmoagx committed Nov 10, 2018
1 parent 3074e64 commit df10e20
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 28 deletions.
2 changes: 1 addition & 1 deletion Source/SPIndexesController.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
@class SPMySQLConnection;
@class SPTableView;

@interface SPIndexesController : NSWindowController <NSTableViewDelegate, NSTableViewDataSource>
@interface SPIndexesController : NSWindowController <NSTableViewDelegate, NSTableViewDataSource, NSComboBoxCellDataSource>
{
// Controllers
IBOutlet SPDatabaseDocument *dbDocument;
Expand Down
58 changes: 36 additions & 22 deletions Source/SPIndexesController.m
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@
static const NSString *SPNewIndexStorageType = @"IndexStorageType";
static const NSString *SPNewIndexKeyBlockSize = @"IndexKeyBlockSize";

/**
* 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 *IndexesControllerKVOContext = &IndexesControllerKVOContext;

@interface SPIndexesController ()

- (BOOL)_isFullTextIndexSelected;
Expand Down Expand Up @@ -125,7 +131,8 @@ - (void)awakeFromNib
[[fieldColumn dataCell] setFont:useMonospacedFont ? [NSFont fontWithName:SPDefaultMonospacedFontName size:monospacedFontSize] : [NSFont systemFontOfSize:[NSFont smallSystemFontSize]]];
}

[prefs addObserver:self forKeyPath:SPDisplayTableViewVerticalGridlines options:NSKeyValueObservingOptionNew context:NULL];
[prefs addObserver:self forKeyPath:SPDisplayTableViewVerticalGridlines options:NSKeyValueObservingOptionNew context:IndexesControllerKVOContext];
[prefs addObserver:self forKeyPath:SPUseMonospacedFonts options:NSKeyValueObservingOptionNew context:IndexesControllerKVOContext];
}

#pragma mark -
Expand Down Expand Up @@ -196,7 +203,7 @@ - (IBAction)addIndex:(id)sender
// If no initial field has been selected yet - all fields are indexed - add the first field.
if (!initialField) initialField = [fields objectAtIndex:0];

if (indexedFieldNames) SPClear(indexedFieldNames);
[indexedFieldNames release];

// Reset the indexed columns
[indexedFields removeAllObjects];
Expand Down Expand Up @@ -477,7 +484,7 @@ - (id)comboBoxCell:(NSComboBoxCell *)comboBoxCell objectValueForItemAtIndex:(NSI

NSString *name = [[availableFields objectAtIndex:index] objectForKey:@"name"];

SPClear(availableFields);
[availableFields release];

return name;
}
Expand Down Expand Up @@ -640,29 +647,35 @@ - (void)removeIndexSheetDidEnd:(NSAlert *)alert returnCode:(NSInteger)returnCode
*/
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
// Display table veiew vertical gridlines preference changed
if ([keyPath isEqualToString:SPDisplayTableViewVerticalGridlines]) {
[indexesTableView setGridStyleMask:([[change objectForKey:NSKeyValueChangeNewKey] boolValue]) ? NSTableViewSolidVerticalGridLineMask : NSTableViewGridNone];
}
// Use monospaced fonts preference changed
else if ([keyPath isEqualToString:SPUseMonospacedFonts]) {
// 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 == IndexesControllerKVOContext) {
// Display table veiew vertical gridlines preference changed
if ([keyPath isEqualToString:SPDisplayTableViewVerticalGridlines]) {
[indexesTableView setGridStyleMask:([[change objectForKey:NSKeyValueChangeNewKey] boolValue]) ? NSTableViewSolidVerticalGridLineMask : NSTableViewGridNone];
}
// Use monospaced fonts preference changed
else if ([keyPath isEqualToString:SPUseMonospacedFonts]) {

BOOL useMonospacedFont = [[change objectForKey:NSKeyValueChangeNewKey] boolValue];
CGFloat monospacedFontSize = [prefs floatForKey:SPMonospacedFontSize] > 0 ? [prefs floatForKey:SPMonospacedFontSize] : [NSFont smallSystemFontSize];
BOOL useMonospacedFont = [[change objectForKey:NSKeyValueChangeNewKey] boolValue];
CGFloat monospacedFontSize = [prefs floatForKey:SPMonospacedFontSize] > 0 ? [prefs floatForKey:SPMonospacedFontSize] : [NSFont smallSystemFontSize];

for (NSTableColumn *indexColumn in [indexesTableView tableColumns])
{
[[indexColumn dataCell] setFont:useMonospacedFont ? [NSFont fontWithName:SPDefaultMonospacedFontName size:monospacedFontSize] : [NSFont systemFontOfSize:[NSFont smallSystemFontSize]]];
}
for (NSTableColumn *indexColumn in [indexesTableView tableColumns])
{
[[indexColumn dataCell] setFont:useMonospacedFont ? [NSFont fontWithName:SPDefaultMonospacedFontName size:monospacedFontSize] : [NSFont systemFontOfSize:[NSFont smallSystemFontSize]]];
}

for (NSTableColumn *indexColumn in [indexedColumnsTableView tableColumns])
{
[[indexColumn dataCell] setFont:useMonospacedFont ? [NSFont fontWithName:SPDefaultMonospacedFontName size:monospacedFontSize] : [NSFont systemFontOfSize:[NSFont smallSystemFontSize]]];
}
for (NSTableColumn *indexColumn in [indexedColumnsTableView tableColumns])
{
[[indexColumn dataCell] setFont:useMonospacedFont ? [NSFont fontWithName:SPDefaultMonospacedFontName size:monospacedFontSize] : [NSFont systemFontOfSize:[NSFont smallSystemFontSize]]];
}

[indexesTableView reloadData];
[indexesTableView reloadData];

[self _reloadIndexedColumnsTableData];
[self _reloadIndexedColumnsTableData];
}
}
else {
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
}

Expand Down Expand Up @@ -1093,7 +1106,8 @@ - (void)dealloc

if (indexedFields) SPClear(indexedFields);

[prefs removeObserver:self forKeyPath:SPDisplayTableViewVerticalGridlines];
[prefs removeObserver:self forKeyPath:SPDisplayTableViewVerticalGridlines]; //TODO: update to ...context: variant after 10.6
[prefs removeObserver:self forKeyPath:SPUseMonospacedFonts]; //TODO: update to ...context: variant after 10.6

[super dealloc];
}
Expand Down
5 changes: 0 additions & 5 deletions Source/SPTableStructure.m
Original file line number Diff line number Diff line change
Expand Up @@ -248,8 +248,6 @@ - (void)awakeFromNib
name:SPDocumentTaskEndNotification
object:tableDocumentInstance];

[prefs addObserver:indexesController forKeyPath:SPUseMonospacedFonts options:NSKeyValueObservingOptionNew context:NULL];

// Init the view column submenu according to saved hidden status;
// menu items are identified by their tag number which represents the initial column index
for (NSMenuItem *item in [viewColumnsMenu itemArray]) [item setState:NSOnState]; // Set all items to NSOnState
Expand Down Expand Up @@ -2568,9 +2566,6 @@ - (void)menuNeedsUpdate:(SPIdMenu *)menu
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
#ifndef SP_CODA
[prefs removeObserver:indexesController forKeyPath:SPUseMonospacedFonts];
#endif

SPClear(tableFields);
SPClear(oldRow);
Expand Down

2 comments on commit df10e20

@derek0x
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mark

@dmoagx
Copy link
Member Author

@dmoagx dmoagx commented on df10e20 Dec 12, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

jeff

Please sign in to comment.