Skip to content

Instantly share code, notes, and snippets.

@lukele
Last active August 2, 2017 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save lukele/ba02ef12886ede03d16b1276ae979b26 to your computer and use it in GitHub Desktop.
Save lukele/ba02ef12886ede03d16b1276ae979b26 to your computer and use it in GitHub Desktop.
- (void)MASetPreferencesController:(NSWindowController *)windowController {
[self MASetPreferencesController:windowController];
if(!windowController) {
return;
}
NSToolbarItem *toolbarItem = [[NSToolbarItem alloc] initWithItemIdentifier:@"gpgmail"];
toolbarItem.label = @"GPGMail";
toolbarItem.image = [NSImage imageNamed:@"GPGMail"];
NSToolbar *toolbar = [[windowController window] toolbar];
BOOL alreadySetup = NO;
for(NSToolbarItem *toolbarItem in [toolbar items]) {
if([[toolbarItem itemIdentifier] isEqualToString:@"gpgmail"]) {
alreadySetup = YES;
break;
}
}
if(!alreadySetup) {
[toolbar insertItemWithItemIdentifier:@"gpgmail" atIndex:[[toolbar items] count]];
}
NSArray *topLevelObjects = nil;
NSView *currentView = nil;
[[GPGMailBundle bundle] loadNibNamed:@"GPGMailPreferences" owner:nil topLevelObjects:&topLevelObjects];
for(id object in topLevelObjects) {
if([object isKindOfClass:NSClassFromString(@"GMSpecialBox")]) {
currentView = object;
break;
}
}
NSTabViewItem *item = [[NSTabViewItem alloc] initWithIdentifier:@"gpgmail"];
NSViewController *GMPreferencesController = [[NSViewController alloc] init];
GMPreferencesController.view = currentView;
item.view = GMPreferencesController.view;
NSTabViewController *controller = (NSTabViewController *)[windowController contentViewController];
item.viewController = GMPreferencesController;
[controller addTabViewItem:item];
NSRect frame = [[windowController window] frame];
float width = 0.0f;
NSArray *subviews = [[[toolbar valueForKey:@"_toolbarView"] subviews][0] subviews];
for (NSView *view in subviews) {
width += view.frame.size.width;
}
// Add padding to fit them all.
// Migt not be necessary to adjust the frame.
frame.size = NSMakeSize(width > frame.size.width ? width : frame.size.width, frame.size.height);
[[windowController window] setMinSize:frame.size];
[[windowController window] setFrame:frame display:YES];
// Fixing the width constraint is however necessary.
[[[[[windowController window] contentView] widthAnchor] constraintEqualToConstant:width] setActive:YES];
}
- (NSToolbarItem *)MAToolbar:(NSToolbar *)toolbar
itemForItemIdentifier:(NSString *)itemIdentifier
willBeInsertedIntoToolbar:(BOOL)flag {
if([[itemIdentifier lowercaseString] isEqualToString:@"gpgmail"]) {
NSToolbarItem *toolbarItem = [[NSToolbarItem alloc] initWithItemIdentifier:@"gpgmail"];
toolbarItem.label = @"GPGMail";
toolbarItem.image = [NSImage imageNamed:@"GPGMail"];
toolbarItem.target = self;
toolbarItem.action = @selector(showGPGMailPreferences:);
return toolbarItem;
}
id ret = [self MAToolbar:toolbar itemForItemIdentifier:itemIdentifier willBeInsertedIntoToolbar:flag];
return ret;
}
- (void)showGPGMailPreferences:(id)item {
NSWindowController *preferencesController = [(id)[NSClassFromString(@"MailApp") sharedApplication] preferencesController];
[[(NSTabViewController *)[preferencesController contentViewController] tabView] selectTabViewItemWithIdentifier:[item itemIdentifier]];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment