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/English.lproj/MainWindow.xib | 43 +++++++----- macosx/HBAppDelegate.m | 71 ++------------------ macosx/HBController.m | 13 +++- macosx/HBPresetsMenuBuilder.h | 23 +++++++ macosx/HBPresetsMenuBuilder.m | 103 +++++++++++++++++++++++++++++ macosx/HandBrake.xcodeproj/project.pbxproj | 6 ++ 6 files changed, 179 insertions(+), 80 deletions(-) create mode 100644 macosx/HBPresetsMenuBuilder.h create mode 100644 macosx/HBPresetsMenuBuilder.m (limited to 'macosx') diff --git a/macosx/English.lproj/MainWindow.xib b/macosx/English.lproj/MainWindow.xib index d31cfe14c..a22b4b131 100644 --- a/macosx/English.lproj/MainWindow.xib +++ b/macosx/English.lproj/MainWindow.xib @@ -32,6 +32,7 @@ + @@ -54,7 +55,7 @@ - + @@ -356,21 +357,9 @@ - + - - - - - - - - - - - - @@ -578,6 +567,30 @@ Blu-ray and DVD sources often have multiple titles, the longest of which is typi + + + + + + + + + + + + + + + + NSIsNotNil + + + + @@ -613,7 +626,7 @@ Blu-ray and DVD sources often have multiple titles, the longest of which is typi - + diff --git a/macosx/HBAppDelegate.m b/macosx/HBAppDelegate.m index 97627194b..32253ed54 100644 --- a/macosx/HBAppDelegate.m +++ b/macosx/HBAppDelegate.m @@ -9,6 +9,7 @@ #import "HBUtilities.h" #import "HBPresetsManager.h" #import "HBPreset.h" +#import "HBPresetsMenuBuilder.h" #import "HBPreferencesController.h" #import "HBQueueController.h" @@ -23,6 +24,7 @@ @interface HBAppDelegate () @property (nonatomic, strong) HBPresetsManager *presetsManager; +@property (nonatomic, strong) HBPresetsMenuBuilder *presetsMenuBuilder; @property (unsafe_unretained) IBOutlet NSMenu *presetsMenu; @property (nonatomic, strong) HBPreferencesController *preferencesController; @@ -80,9 +82,12 @@ - (void)applicationDidFinishLaunching:(NSNotification *)notification { - [self buildPresetsMenu]; - [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(buildPresetsMenu) name:HBPresetsChangedNotification object:nil]; + self.presetsMenuBuilder = [[HBPresetsMenuBuilder alloc] initWithMenu:self.presetsMenu + action:@selector(selectPresetFromMenu:) + size:[NSFont systemFontSize] + presetsManager:self.presetsManager]; + [self.presetsMenuBuilder build]; // Get the number of HandBrake instances currently running NSUInteger instances = [NSRunningApplication runningApplicationsWithBundleIdentifier:[[NSBundle mainBundle] bundleIdentifier]].count; @@ -286,68 +291,6 @@ #pragma mark - Presets Menu actions -/** - * Adds the presets list to the menu. - */ -- (void)buildPresetsMenu -{ - // First we remove all the preset menu items - // inserted previously - NSArray *menuItems = [self.presetsMenu.itemArray copy]; - for (NSMenuItem *item in menuItems) - { - if (item.tag != -1) - { - [self.presetsMenu removeItem:item]; - } - } - - BOOL builtInSeparatorInserted = NO; - for (HBPreset *preset in self.presetsManager.root.children) - { - if (preset.isBuiltIn == NO && builtInSeparatorInserted == NO) - { - [self.presetsMenu addItem:[NSMenuItem separatorItem]]; - builtInSeparatorInserted = YES; - } - [self.presetsMenu 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 = @selector(selectPresetFromMenu:); - item.representedObject = preset; - - // Make the default preset font bold. - if ([preset isEqualTo:self.presetsManager.defaultPreset]) - { - NSAttributedString *newTitle = [[NSAttributedString alloc] initWithString:preset.name - attributes:@{NSFontAttributeName: [NSFont boldSystemFontOfSize:14]}]; - [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; -} - /** * We use this method to recreate new, updated factory presets */ diff --git a/macosx/HBController.m b/macosx/HBController.m index 32995f738..42488ccba 100644 --- a/macosx/HBController.m +++ b/macosx/HBController.m @@ -14,6 +14,7 @@ #import "HBPresetsManager.h" #import "HBPreset.h" #import "HBMutablePreset.h" +#import "HBPresetsMenuBuilder.h" #import "HBSummaryViewController.h" #import "HBPictureViewController.h" @@ -99,6 +100,9 @@ IBOutlet NSDrawer * fPresetDrawer; } +@property (nonatomic, strong) HBPresetsMenuBuilder *presetsMenuBuilder; +@property (nonatomic, strong) IBOutlet NSPopUpButton *presetsPopup; + @property (nonatomic, strong) HBSummaryViewController *summaryController; @property (nonatomic, strong) IBOutlet NSTabViewItem *summaryTab; @@ -282,7 +286,14 @@ forKeyPath:@"values.HBShowAdvancedTab" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionInitial context:NULL]; - + + + self.presetsMenuBuilder = [[HBPresetsMenuBuilder alloc] initWithMenu:self.presetsPopup.menu + action:@selector(selectPresetFromMenu:) + size:[NSFont smallSystemFontSize] + presetsManager:presetManager]; + [self.presetsMenuBuilder build]; + [self.window recalculateKeyViewLoop]; } diff --git a/macosx/HBPresetsMenuBuilder.h b/macosx/HBPresetsMenuBuilder.h new file mode 100644 index 000000000..d7b66aa04 --- /dev/null +++ b/macosx/HBPresetsMenuBuilder.h @@ -0,0 +1,23 @@ +// +// HBPresetsMenuBuilder.h +// HandBrake +// +// Created by Damiano Galassi on 24/11/2017. +// + +#import + +@import HandBrakeKit; + +@interface HBPresetsMenuBuilder : NSObject + +@property (nonatomic, readonly) NSMenu *menu; + +- (instancetype)initWithMenu:(NSMenu *)menu action:(SEL)action size:(CGFloat)size presetsManager:(HBPresetsManager *)manager; + +/** + * Adds the presets list to the menu. + */ +- (void)build; + +@end 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 diff --git a/macosx/HandBrake.xcodeproj/project.pbxproj b/macosx/HandBrake.xcodeproj/project.pbxproj index be8fc167b..32ac5fcd6 100644 --- a/macosx/HandBrake.xcodeproj/project.pbxproj +++ b/macosx/HandBrake.xcodeproj/project.pbxproj @@ -186,6 +186,7 @@ A932E26C1988334B0047D13E /* AudioDefaults.xib in Resources */ = {isa = PBXBuildFile; fileRef = A932E26A1988334B0047D13E /* AudioDefaults.xib */; }; A9350F501CCA7F490089F970 /* HBQTKitPlayer.m in Sources */ = {isa = PBXBuildFile; fileRef = A9350F4E1CCA7C3B0089F970 /* HBQTKitPlayer.m */; }; A937EECB1C6C7C0300EEAE6D /* dsa_pub.pem in Resources */ = {isa = PBXBuildFile; fileRef = A937EECA1C6C7C0300EEAE6D /* dsa_pub.pem */; }; + A939DD8B1FC8826A00135F2A /* HBPresetsMenuBuilder.m in Sources */ = {isa = PBXBuildFile; fileRef = A939DD8A1FC8826A00135F2A /* HBPresetsMenuBuilder.m */; }; A93B49221DA3AA6900DD70A3 /* HBToolbarBadgedItem.m in Sources */ = {isa = PBXBuildFile; fileRef = A93B49211DA3AA6900DD70A3 /* HBToolbarBadgedItem.m */; }; A93E0ED31972957000FD67FB /* HBVideoController.m in Sources */ = {isa = PBXBuildFile; fileRef = A93E0ED11972957000FD67FB /* HBVideoController.m */; }; A93E0ED71972958C00FD67FB /* Video.xib in Resources */ = {isa = PBXBuildFile; fileRef = A93E0ED51972958C00FD67FB /* Video.xib */; }; @@ -453,6 +454,8 @@ A9350F4D1CCA7C3B0089F970 /* HBQTKitPlayer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HBQTKitPlayer.h; sourceTree = ""; }; A9350F4E1CCA7C3B0089F970 /* HBQTKitPlayer.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = HBQTKitPlayer.m; sourceTree = ""; }; A937EECA1C6C7C0300EEAE6D /* dsa_pub.pem */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = dsa_pub.pem; sourceTree = ""; }; + A939DD891FC8826A00135F2A /* HBPresetsMenuBuilder.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = HBPresetsMenuBuilder.h; sourceTree = ""; }; + A939DD8A1FC8826A00135F2A /* HBPresetsMenuBuilder.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = HBPresetsMenuBuilder.m; sourceTree = ""; }; A93B0DF61C804CF50051A3FA /* NSDictionary+HBAdditions.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSDictionary+HBAdditions.h"; sourceTree = ""; }; A93B0DF71C804CF50051A3FA /* NSDictionary+HBAdditions.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSDictionary+HBAdditions.m"; sourceTree = ""; }; A93B49201DA3AA6900DD70A3 /* HBToolbarBadgedItem.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HBToolbarBadgedItem.h; sourceTree = ""; }; @@ -1188,6 +1191,8 @@ 273F209C14ADBE670021BE6D /* HBOutputPanelController.m */, 273F209F14ADBE670021BE6D /* HBPreferencesController.h */, 273F20A014ADBE670021BE6D /* HBPreferencesController.m */, + A939DD891FC8826A00135F2A /* HBPresetsMenuBuilder.h */, + A939DD8A1FC8826A00135F2A /* HBPresetsMenuBuilder.m */, ); name = Controllers; sourceTree = ""; @@ -1571,6 +1576,7 @@ A9706CB71AC1437800BAEAA8 /* HBExceptionAlertController.m in Sources */, A93B49221DA3AA6900DD70A3 /* HBToolbarBadgedItem.m in Sources */, A91AD3401FB5D2FC00AEA822 /* HBAddCategoryController.m in Sources */, + A939DD8B1FC8826A00135F2A /* HBPresetsMenuBuilder.m in Sources */, A92268781A6E555500A8D5C5 /* HBAppDelegate.m in Sources */, A98036CD1CCA91DD007661AA /* HBAVPlayer.m in Sources */, A9BC24C91A69293E007DC41A /* HBAttributedStringAdditions.m in Sources */, -- cgit v1.2.3