Skip to content

Commit

Permalink
Fix various ARC issues (erm... I think?) in FindFix.
Browse files Browse the repository at this point in the history
  • Loading branch information
tom-seddon committed Mar 12, 2014
1 parent 77f857a commit 3d4589d
Showing 1 changed file with 30 additions and 38 deletions.
68 changes: 30 additions & 38 deletions XCFixin_FindFix/XCFixin_FindFix.m
Original file line number Diff line number Diff line change
Expand Up @@ -47,45 +47,36 @@ static void DumpSubviews(NSView *view, NSString *prefix)
}
}

static NSView *GetFindBarStackView(NSView *findBarView)
static NSArray *RemoveOptionsFromSuperview(NSViewController *findBar)
{
NSArray *subviews = [findBarView subviews];
NSView *stackView = [subviews objectAtIndex:0];
NSView *optionsCtrl = MSGSEND(NSView *, findBar, optionsCtrl);

return stackView;
}

static void ForEachOption(id optionsCtrl, void (*fn)(id option, id context), id context)
{
(*fn)(MSGSEND(NSView *, optionsCtrl, matchingStyleView), context);
(*fn)(MSGSEND(NSView *, optionsCtrl, hitsMustContainView), context);
(*fn)(MSGSEND(NSView *, optionsCtrl, matchCaseView), context);
(*fn)(MSGSEND(NSView *, optionsCtrl, wrapView), context);
}

static void RemoveOptionFromSuperview(id option, id context)
{
[option removeFromSuperview];
}

// RemoveOptionsFromSuperview and AddOptionToFindBar must be called as a
// pair, because they contain matching release and retain calls.

static void RemoveOptionsFromSuperview(id optionsCtrl, NSView *findBarView)
{
ForEachOption(optionsCtrl, &RemoveOptionFromSuperview, nil);
}

static void AddOptionToView(id option, id view)
{
[view addSubview:option];
NSView *matchingStyleView = MSGSEND(NSView *, optionsCtrl, matchingStyleView);
NSView *hitsMustContainView = MSGSEND(NSView *, optionsCtrl, hitsMustContainView);
NSView *matchCaseView = MSGSEND(NSView *, optionsCtrl, matchCaseView);
NSView *wrapView = MSGSEND(NSView *, optionsCtrl, wrapView);

NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:4];

[array addObject:matchingStyleView];
[array addObject:hitsMustContainView];
[array addObject:matchCaseView];
[array addObject:wrapView];

for (NSView *view in array)
[view removeFromSuperview];

return array;
}

static void AddOptionsToFindBar(id optionsCtrl, NSView *findBarView)
static void AddOptionsToFindBar(NSViewController *findBar, NSArray *options)
{
NSView *findBarStackView = GetFindBarStackView(findBarView);
NSView *findBarView = [findBar view];
NSArray *findBarSubviews = [findBarView subviews];
NSView *findBarStackView = [findBarSubviews objectAtIndex:0];

ForEachOption(optionsCtrl, &AddOptionToView, findBarStackView);
for (NSView *view in options)
[findBarStackView addSubview:view];
}

static void overrideViewDidInstall(id self, SEL _cmd)
Expand Down Expand Up @@ -119,10 +110,9 @@ static void overrideViewDidInstall(id self, SEL _cmd)
// DumpSubviews(view, @"");
// NSLog(@"End FindBar subviews.");

id optionsCtrl = MSGSEND(id, self, optionsCtrl);
NSArray *views = RemoveOptionsFromSuperview(self);

RemoveOptionsFromSuperview(optionsCtrl, [self view]);
AddOptionsToFindBar(optionsCtrl, [self view]);
AddOptionsToFindBar(self, views);
}
else
{
Expand Down Expand Up @@ -175,13 +165,15 @@ static void overrideSetFinderMode(id self, SEL _cmd, unsigned long long newFinde
unsigned long long oldFinderMode = MSGSEND(unsigned long long, self, finderMode);
if (newFinderMode != oldFinderMode)
{
NSArray *views = nil;

if (gIsXcode5)
RemoveOptionsFromSuperview(MSGSEND(id, self, optionsCtrl), [self view]);
views = RemoveOptionsFromSuperview(self);

((void (*)(id, SEL, unsigned long long))gOriginalSetFinderMode)(self, _cmd, newFinderMode);

if (gIsXcode5)
AddOptionsToFindBar(MSGSEND(id, self, optionsCtrl), [self view]);
AddOptionsToFindBar(self, views);
}
}

Expand Down

0 comments on commit 3d4589d

Please sign in to comment.