From 68c7758a7cf0a397b6dfc584792a8958ddd01322 Mon Sep 17 00:00:00 2001 From: Damiano Galassi Date: Fri, 24 Nov 2017 18:01:43 +0100 Subject: MacGui: add a popup button to select the preset in the main window. --- macosx/HBPresetsMenuBuilder.m | 103 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 macosx/HBPresetsMenuBuilder.m (limited to 'macosx/HBPresetsMenuBuilder.m') 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 -- cgit v1.2.3