Skip to content
Permalink
Browse files

Fix some code style issues with KVO in indexes controller

  • Loading branch information
dmoagx committed Nov 10, 2018
1 parent 3074e64 commit df10e204431c40967aa4d3377a5c5334f39b7fa9
Showing with 37 additions and 28 deletions.
  1. +1 −1 Source/SPIndexesController.h
  2. +36 −22 Source/SPIndexesController.m
  3. +0 −5 Source/SPTableStructure.m
@@ -35,7 +35,7 @@
@class SPMySQLConnection;
@class SPTableView;

@interface SPIndexesController : NSWindowController <NSTableViewDelegate, NSTableViewDataSource>
@interface SPIndexesController : NSWindowController <NSTableViewDelegate, NSTableViewDataSource, NSComboBoxCellDataSource>
{
// Controllers
IBOutlet SPDatabaseDocument *dbDocument;
@@ -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;
@@ -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 -
@@ -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];
@@ -477,7 +484,7 @@ - (id)comboBoxCell:(NSComboBoxCell *)comboBoxCell objectValueForItemAtIndex:(NSI

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

SPClear(availableFields);
[availableFields release];

return name;
}
@@ -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];
}
}

@@ -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];
}
@@ -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
@@ -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);

2 comments on commit df10e20

@derek778

This comment has been minimized.

Copy link

@derek778 derek778 replied Dec 11, 2018

mark

@dmoagx

This comment has been minimized.

Copy link
Member Author

@dmoagx dmoagx replied Dec 12, 2018

jeff

Please sign in to comment.
You can’t perform that action at this time.