From 8150402d59c01f1a00587b7d169390710c4af58f Mon Sep 17 00:00:00 2001 From: Damiano Galassi Date: Tue, 16 Oct 2018 13:57:40 +0200 Subject: [PATCH] MacGui: improve touch bar support, add touch bars to the add titles to queue and add preset sheets. --- macosx/HBAddPresetController.m | 48 +++++++++++++++++++++++++++++ macosx/HBController.m | 17 +++++++--- macosx/HBCore.h | 7 +++++ macosx/HBPresetsViewController.m | 1 + macosx/HBPreviewGenerator.m | 6 +++- macosx/HBTitleSelectionController.m | 48 +++++++++++++++++++++++++++++ 6 files changed, 122 insertions(+), 5 deletions(-) diff --git a/macosx/HBAddPresetController.m b/macosx/HBAddPresetController.m index ab011155b..b29c516c5 100644 --- a/macosx/HBAddPresetController.m +++ b/macosx/HBAddPresetController.m @@ -258,3 +258,51 @@ typedef NS_ENUM(NSUInteger, HBAddPresetControllerMode) { } @end + +@interface HBAddPresetController (TouchBar) +@end + +@implementation HBAddPresetController (TouchBar) + +@dynamic touchBar; + +static NSTouchBarItemIdentifier HBTouchBarGroup = @"fr.handbrake.buttonsGroup"; +static NSTouchBarItemIdentifier HBTouchBarAdd = @"fr.handbrake.openSource"; +static NSTouchBarItemIdentifier HBTouchBarCancel = @"fr.handbrake.addToQueue"; + +- (NSTouchBar *)makeTouchBar +{ + NSTouchBar *bar = [[NSTouchBar alloc] init]; + bar.delegate = self; + + bar.defaultItemIdentifiers = @[HBTouchBarGroup, NSTouchBarItemIdentifierOtherItemsProxy]; + bar.principalItemIdentifier = HBTouchBarGroup; + + return bar; +} + +- (NSTouchBarItem *)touchBar:(NSTouchBar *)touchBar makeItemForIdentifier:(NSTouchBarItemIdentifier)identifier +{ + if ([identifier isEqualTo:HBTouchBarGroup]) + { + NSCustomTouchBarItem *cancelItem = [[NSCustomTouchBarItem alloc] initWithIdentifier:HBTouchBarAdd]; + cancelItem.customizationLabel = NSLocalizedString(@"Cancel", @"Touch bar"); + NSButton *cancelButton = [NSButton buttonWithTitle:NSLocalizedString(@"Cancel", @"Touch bar") target:self action:@selector(cancel:)]; + [cancelButton.widthAnchor constraintGreaterThanOrEqualToConstant:200].active = YES; + cancelItem.view = cancelButton; + + NSCustomTouchBarItem *addItem = [[NSCustomTouchBarItem alloc] initWithIdentifier:HBTouchBarCancel]; + addItem.customizationLabel = NSLocalizedString(@"Add Preset", @"Touch bar"); + NSButton *addButton = [NSButton buttonWithTitle:NSLocalizedString(@"Add Preset", @"Touch bar") target:self action:@selector(add:)]; + [addButton.widthAnchor constraintGreaterThanOrEqualToConstant:200].active = YES; + addButton.keyEquivalent = @"\r"; + addItem.view = addButton; + + NSGroupTouchBarItem *item = [NSGroupTouchBarItem groupItemWithIdentifier:identifier items:@[cancelItem, addItem]]; + return item; + } + + return nil; +} + +@end diff --git a/macosx/HBController.m b/macosx/HBController.m index c4a0c70e5..dec7906ee 100644 --- a/macosx/HBController.m +++ b/macosx/HBController.m @@ -226,8 +226,6 @@ static void *HBControllerQueueCoreContext = &HBControllerQueueCoreContext; fPresetsView = [[HBPresetsViewController alloc] initWithPresetManager:presetManager]; fPresetsView.delegate = self; - fPresetsView.showHeader = YES; - // Set up the presets popover self.presetsPopover = [[NSPopover alloc] init]; @@ -1544,6 +1542,7 @@ static NSTouchBarItemIdentifier HBTouchBarMain = @"fr.handbrake.mainWindowTouchB static NSTouchBarItemIdentifier HBTouchBarOpen = @"fr.handbrake.openSource"; static NSTouchBarItemIdentifier HBTouchBarAddToQueue = @"fr.handbrake.addToQueue"; +static NSTouchBarItemIdentifier HBTouchBarAddTitlesToQueue = @"fr.handbrake.addTitlesToQueue"; static NSTouchBarItemIdentifier HBTouchBarRip = @"fr.handbrake.rip"; static NSTouchBarItemIdentifier HBTouchBarPause = @"fr.handbrake.pause"; static NSTouchBarItemIdentifier HBTouchBarPreview = @"fr.handbrake.preview"; @@ -1553,10 +1552,10 @@ static NSTouchBarItemIdentifier HBTouchBarPreview = @"fr.handbrake.preview"; NSTouchBar *bar = [[NSTouchBar alloc] init]; bar.delegate = self; - bar.defaultItemIdentifiers = @[HBTouchBarOpen, NSTouchBarItemIdentifierFixedSpaceSmall, HBTouchBarAddToQueue, NSTouchBarItemIdentifierFixedSpaceLarge, HBTouchBarRip, HBTouchBarPause, NSTouchBarItemIdentifierFixedSpaceLarge, HBTouchBarPreview]; + bar.defaultItemIdentifiers = @[HBTouchBarOpen, NSTouchBarItemIdentifierFixedSpaceSmall, HBTouchBarAddToQueue, NSTouchBarItemIdentifierFixedSpaceLarge, HBTouchBarRip, HBTouchBarPause, NSTouchBarItemIdentifierFixedSpaceLarge, HBTouchBarPreview, NSTouchBarItemIdentifierOtherItemsProxy]; bar.customizationIdentifier = HBTouchBarMain; - bar.customizationAllowedItemIdentifiers = @[HBTouchBarOpen, HBTouchBarAddToQueue, HBTouchBarRip, HBTouchBarPause, HBTouchBarPreview, NSTouchBarItemIdentifierFixedSpaceSmall, NSTouchBarItemIdentifierFixedSpaceLarge, NSTouchBarItemIdentifierFlexibleSpace]; + bar.customizationAllowedItemIdentifiers = @[HBTouchBarOpen, HBTouchBarAddToQueue, HBTouchBarAddTitlesToQueue, HBTouchBarRip, HBTouchBarPause, HBTouchBarPreview, NSTouchBarItemIdentifierFixedSpaceSmall, NSTouchBarItemIdentifierFixedSpaceLarge, NSTouchBarItemIdentifierFlexibleSpace]; return bar; } @@ -1583,6 +1582,16 @@ static NSTouchBarItemIdentifier HBTouchBarPreview = @"fr.handbrake.preview"; item.view = button; return item; } + else if ([identifier isEqualTo:HBTouchBarAddTitlesToQueue]) + { + NSCustomTouchBarItem *item = [[NSCustomTouchBarItem alloc] initWithIdentifier:identifier]; + item.customizationLabel = NSLocalizedString(@"Add Titles To Queue", @"Touch bar"); + + NSButton *button = [NSButton buttonWithTitle:NSLocalizedString(@"Add Titles To Queue", @"Touch bar") target:self action:@selector(addTitlesToQueue:)]; + + item.view = button; + return item; + } else if ([identifier isEqualTo:HBTouchBarRip]) { NSCustomTouchBarItem *item = [[NSCustomTouchBarItem alloc] initWithIdentifier:identifier]; diff --git a/macosx/HBCore.h b/macosx/HBCore.h index fd802efa2..eb4d3920b 100644 --- a/macosx/HBCore.h +++ b/macosx/HBCore.h @@ -49,6 +49,13 @@ typedef void (^HBCoreCompletionHandler)(HBCoreResult result); * HBCore is an Objective-C interface to the low-level HandBrake library. * HBCore monitors state changes of libhb. It can also be used * to implement properties that can be directly bound to elements of the gui. + * + * Instance methods must be called on the same queue as the queue + * passed to initWithLogLevel:queue: + * Convenience inits use the main queue by default. + * + * copyImageAtIndex: can be called on a different queue, + * but the caller must ensure the validity of the title. */ @interface HBCore : NSObject diff --git a/macosx/HBPresetsViewController.m b/macosx/HBPresetsViewController.m index 0fe2fa47f..470776b9b 100644 --- a/macosx/HBPresetsViewController.m +++ b/macosx/HBPresetsViewController.m @@ -79,6 +79,7 @@ static void *HBPresetsViewControllerContext = &HBPresetsViewControllerContext; _selectedPresetInternal = presetManager.defaultPreset; _expandedNodes = [[NSArray arrayWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:@"HBPreviewViewExpandedStatus"]] mutableCopy]; + _showHeader = YES; } return self; } diff --git a/macosx/HBPreviewGenerator.m b/macosx/HBPreviewGenerator.m index 6ac4e76bd..fa486832c 100644 --- a/macosx/HBPreviewGenerator.m +++ b/macosx/HBPreviewGenerator.m @@ -18,6 +18,7 @@ @property (nonatomic, readonly) NSCache *smallPreviewsCache; @property (nonatomic, readonly) dispatch_semaphore_t sem; +@property (nonatomic, readonly) _Atomic bool invalidated; @property (nonatomic, strong) HBCore *core; @@ -59,6 +60,8 @@ - (void)dealloc { + _invalidated = true; + [[NSNotificationCenter defaultCenter] removeObserver:self]; [[NSRunLoop mainRunLoop] cancelPerformSelectorsWithTarget:self]; [self.core cancelEncode]; @@ -154,7 +157,8 @@ - (void)copySmallImageAtIndex:(NSUInteger)index completionHandler:(void (^)(__nullable CGImageRef result))handler { dispatch_semaphore_wait(_sem, DISPATCH_TIME_FOREVER); - if (index >= self.imagesCount) + + if (_invalidated || index >= self.imagesCount) { handler(NULL); dispatch_semaphore_signal(_sem); diff --git a/macosx/HBTitleSelectionController.m b/macosx/HBTitleSelectionController.m index b0bf0a2fd..001e9c4d6 100644 --- a/macosx/HBTitleSelectionController.m +++ b/macosx/HBTitleSelectionController.m @@ -98,3 +98,51 @@ } @end + +@interface HBTitleSelectionController (TouchBar) +@end + +@implementation HBTitleSelectionController (TouchBar) + +@dynamic touchBar; + +static NSTouchBarItemIdentifier HBTouchBarGroup = @"fr.handbrake.buttonsGroup"; +static NSTouchBarItemIdentifier HBTouchBarAdd = @"fr.handbrake.openSource"; +static NSTouchBarItemIdentifier HBTouchBarCancel = @"fr.handbrake.addToQueue"; + +- (NSTouchBar *)makeTouchBar +{ + NSTouchBar *bar = [[NSTouchBar alloc] init]; + bar.delegate = self; + + bar.defaultItemIdentifiers = @[HBTouchBarGroup]; + bar.principalItemIdentifier = HBTouchBarGroup; + + return bar; +} + +- (NSTouchBarItem *)touchBar:(NSTouchBar *)touchBar makeItemForIdentifier:(NSTouchBarItemIdentifier)identifier +{ + if ([identifier isEqualTo:HBTouchBarGroup]) + { + NSCustomTouchBarItem *cancelItem = [[NSCustomTouchBarItem alloc] initWithIdentifier:HBTouchBarAdd]; + cancelItem.customizationLabel = NSLocalizedString(@"Cancel", @"Touch bar"); + NSButton *cancelButton = [NSButton buttonWithTitle:NSLocalizedString(@"Cancel", @"Touch bar") target:self action:@selector(cancel:)]; + [cancelButton.widthAnchor constraintGreaterThanOrEqualToConstant:200].active = YES; + cancelItem.view = cancelButton; + + NSCustomTouchBarItem *addItem = [[NSCustomTouchBarItem alloc] initWithIdentifier:HBTouchBarCancel]; + addItem.customizationLabel = NSLocalizedString(@"Add To Queue", @"Touch bar"); + NSButton *addButton = [NSButton buttonWithTitle:NSLocalizedString(@"Add To Queue", @"Touch bar") target:self action:@selector(add:)]; + [addButton.widthAnchor constraintGreaterThanOrEqualToConstant:200].active = YES; + addButton.keyEquivalent = @"\r"; + addItem.view = addButton; + + NSGroupTouchBarItem *item = [NSGroupTouchBarItem groupItemWithIdentifier:identifier items:@[cancelItem, addItem]]; + return item; + } + + return nil; +} + +@end -- 2.40.0