Skip to content

Instantly share code, notes, and snippets.

@Kentzo
Created February 8, 2010 16:21
Show Gist options
  • Select an option

  • Save Kentzo/298308 to your computer and use it in GitHub Desktop.

Select an option

Save Kentzo/298308 to your computer and use it in GitHub Desktop.
@implementation CustomToolBarButtonExtension
+ (BOOL)loadExtension:(NSError**)error { // Is called in +load function
Class origClass = NSClassFromString(@"ToolbarController");
class_addMethodsFromClass(origClass, self);
BOOL result = [origClass jr_swizzleMethod:@selector(toolbar:itemForItemIdentifier:willBeInsertedIntoToolbar:)
withMethod:@selector(SWMToolbar:itemForItemIdentifier:willBeInsertedIntoToolbar:) error:error] &&
[origClass jr_swizzleMethod:@selector(toolbarAllowedItemIdentifiers:)
withMethod:@selector(SWMToolbarAllowedItemIdentifiers:) error:error];
[[NSNotificationCenter defaultCenter] addObserver:[SWMPluginController sharedInstance]
selector:@selector(safariToolbarWillAddItem:)
name:NSToolbarWillAddItemNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:[SWMPluginController sharedInstance]
selector:@selector(safariToolbarDidRemoveItem:)
name:NSToolbarDidRemoveItemNotification
object:nil];
if ([[SWMPluginController sharedInstance] toolbarButtonShouldBeInserted]) {
NSDocumentController* documentContr = [NSDocumentController sharedDocumentController];
NSWindowController* windowContr = [[[documentContr documents] objectAtIndex:0] browserWindowController];
NSWindow* window = [windowContr window];
NSToolbar* toolbar = [window toolbar];
[toolbar _userInsertItemWithItemIdentifier:[[SWMPluginController sharedInstance] toolbarButtonIdentifier]
atIndex:[[SWMPluginController sharedInstance] toolbarButtonIndex]];
// [toolbar insertItemWithItemIdentifier:[[SWMPluginController sharedInstance] toolbarButtonIdentifier]
// atIndex:[[SWMPluginController sharedInstance] toolbarButtonIndex]];
}
return result;
}
+ (void)unloadExtension {
Class origClass = NSClassFromString(@"ToolbarController");
[origClass jr_swizzleMethod:@selector(toolbar:itemForItemIdentifier:willBeInsertedIntoToolbar:)
withMethod:@selector(SWMToolbar:itemForItemIdentifier:willBeInsertedIntoToolbar:) error:nil];
[origClass jr_swizzleMethod:@selector(toolbarAllowedItemIdentifiers:)
withMethod:@selector(SWMToolbarAllowedItemIdentifiers:) error:nil];
[[NSNotificationCenter defaultCenter] removeObserver:[SWMPluginController sharedInstance] name:NSToolbarWillAddItemNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:[SWMPluginController sharedInstance] name:NSToolbarDidRemoveItemNotification object:nil];
}
- (BrowserToolbarItem*)SWMToolbar:(BrowserToolbar*)toolbar itemForItemIdentifier:(NSString*)identifier willBeInsertedIntoToolbar:(BOOL)willBeInserted {
static BrowserToolbarItem* SWMToolBarItem = nil;
if (SWMToolBarItem == nil) {
NSButton* SWMButton = [[SWMPluginController sharedInstance] toolbarButton];
SWMToolBarItem = [[NSClassFromString(@"BrowserToolbarItem") alloc]
initWithItemIdentifier:[[SWMPluginController sharedInstance] toolbarButtonIdentifier]
target:self
button:SWMButton];
}
if ([identifier isEqualToString:[[SWMPluginController sharedInstance] toolbarButtonIdentifier]]) {
return SWMToolBarItem;
}
else {
[self SWMToolbar:toolbar itemForItemIdentifier:identifier willBeInsertedIntoToolbar:willBeInserted];
}
}
- (NSArray*)SWMToolbarAllowedItemIdentifiers:(BrowserToolbar*)toolbar {
return [[self SWMToolbarAllowedItemIdentifiers:toolbar] arrayByAddingObject:[[SWMPluginController sharedInstance] toolbarButtonIdentifier]];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment