diff options
author | Damiano Galassi <[email protected]> | 2017-11-24 18:01:43 +0100 |
---|---|---|
committer | Damiano Galassi <[email protected]> | 2017-11-24 18:01:43 +0100 |
commit | 68c7758a7cf0a397b6dfc584792a8958ddd01322 (patch) | |
tree | 14babe731d94d2ace06e2b1058e13cf8e0221581 /macosx/HBPresetsMenuBuilder.m | |
parent | b73dabc600420663a5962d3da5decdd8c3a7bbd1 (diff) |
MacGui: add a popup button to select the preset in the main window.
Diffstat (limited to 'macosx/HBPresetsMenuBuilder.m')
-rw-r--r-- | macosx/HBPresetsMenuBuilder.m | 103 |
1 files changed, 103 insertions, 0 deletions
diff --git a/macosx/HBPresetsMenuBuilder.m b/macosx/HBPresetsMenuBuilder.m new file mode 100644 index 000000000..1bdb9ba2d --- /dev/null +++ b/macosx/HBPresetsMenuBuilder.m @@ -0,0 +1,103 @@ +// +// HBPresetsMenuBuilder.m +// HandBrake +// +// Created by Damiano Galassi on 24/11/2017. +// + +#import "HBPresetsMenuBuilder.h" + +@interface HBPresetsMenuBuilder () + +@property (nonatomic, readonly) CGFloat size; +@property (nonatomic, readonly) HBPresetsManager *manager; +@property (nonatomic, readonly) SEL action; + +@end + +@implementation HBPresetsMenuBuilder + +- (instancetype)initWithMenu:(NSMenu *)menu action:(SEL)action size:(CGFloat)size presetsManager:(HBPresetsManager *)manager +{ + self = [super init]; + if (self) + { + _menu = menu; + _size = size; + _manager = manager; + _action = action; + + [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(build) name:HBPresetsChangedNotification object:nil]; + } + + return self; +} + +- (void)dealloc +{ + [[NSNotificationCenter defaultCenter] removeObserver:self]; +} + +/** + * Adds the presets list to the menu. + */ +- (void)build +{ + // First we remove all the preset menu items + // inserted previously + NSArray *menuItems = [self.menu.itemArray copy]; + for (NSMenuItem *item in menuItems) + { + if (item.tag == 2) + { + [self.menu removeItem:item]; + } + } + + BOOL builtInSeparatorInserted = NO; + for (HBPreset *preset in self.manager.root.children) + { + if (preset.isBuiltIn == NO && builtInSeparatorInserted == NO) + { + [self.menu addItem:[NSMenuItem separatorItem]]; + builtInSeparatorInserted = YES; + } + [self.menu addItem:[self buildMenuItemWithPreset:preset]]; + } +} + +- (NSMenuItem *)buildMenuItemWithPreset:(HBPreset *)preset +{ + NSMenuItem *item = [[NSMenuItem alloc] init]; + item.title = preset.name; + item.toolTip = preset.presetDescription; + item.tag = 2; + + if (preset.isLeaf) + { + item.action = self.action; + item.representedObject = preset; + + // Make the default preset font bold. + if ([preset isEqualTo:self.manager.defaultPreset]) + { + NSAttributedString *newTitle = [[NSAttributedString alloc] initWithString:preset.name + attributes:@{NSFontAttributeName: [NSFont boldSystemFontOfSize:self.size]}]; + [item setAttributedTitle:newTitle]; + } + } + else + { + NSMenu *menu = [[NSMenu alloc] init]; + for (HBPreset *childPreset in preset.children) + { + [menu addItem:[self buildMenuItemWithPreset:childPreset]]; + } + + item.submenu = menu; + } + + return item; +} + +@end |