2015-01-25 08:02:47 +00:00
/ * HBTitleSelectionController . h
This file is part of the HandBrake source code .
Homepage : < http : // handbrake . fr / > .
It may be used under the terms of the GNU General Public License . * /
# import "HBTitleSelectionController.h"
2024-07-17 15:40:47 +02:00
# import "HBTitleSelectionRangeController.h"
2024-02-26 17:01:35 +01:00
# import "HBTableView.h"
2016-02-26 09:50:16 +01:00
2018-06-08 16:59:25 +02:00
@ import HandBrakeKit ;
2015-01-25 08:02:47 +00:00
2015-10-25 07:03:36 +01:00
@ interface HBTitleSelection : NSObject
2024-07-17 15:40:47 +02:00
2015-10-25 07:03:36 +01:00
@ property ( nonatomic , readonly ) HBTitle * title ;
2024-02-26 17:01:35 +01:00
@ property ( nonatomic , readonly ) BOOL enabled ;
2015-10-25 07:03:36 +01:00
@ property ( nonatomic , readonly , assign ) NSUndoManager * undo ;
2024-07-17 15:40:47 +02:00
2015-10-25 07:03:36 +01:00
@ end
@ implementation HBTitleSelection
2024-07-17 15:40:47 +02:00
2015-10-25 07:03:36 +01:00
- ( instancetype ) initWithTitle : ( HBTitle * ) title undo : ( NSUndoManager * ) undo
{
if ( self = [ super init ] )
{
_title = title ;
2024-02-26 17:01:35 +01:00
_enabled = YES ;
2015-10-25 07:03:36 +01:00
_undo = undo ;
}
return self ;
}
2024-02-26 17:01:35 +01:00
- ( void ) setEnabled : ( BOOL ) enabled
2015-10-25 07:03:36 +01:00
{
2024-02-26 17:01:35 +01:00
if ( enabled ! = _enabled )
2015-10-25 07:03:36 +01:00
{
2024-02-26 17:01:35 +01:00
[ [ self . undo prepareWithInvocationTarget : self ] setEnabled : _enabled ] ;
2015-10-25 07:03:36 +01:00
}
2024-02-26 17:01:35 +01:00
_enabled = enabled ;
2015-10-25 07:03:36 +01:00
}
2024-07-17 15:40:47 +02:00
2015-10-25 07:03:36 +01:00
@ end
2015-01-25 08:02:47 +00:00
2024-12-05 08:58:42 +01:00
@ interface HBTitleSelectionController ( ) < NSTableViewDataSource , NSTableViewDelegate , NSMenuItemValidation >
2015-01-25 08:02:47 +00:00
2024-02-26 17:01:35 +01:00
@ property ( nonatomic , weak ) IBOutlet HBTableView * tableView ;
2024-07-17 15:40:47 +02:00
@ property ( nonatomic , weak ) IBOutlet NSView * rangeView ;
2017-02-04 14:04:37 +01:00
@ property ( nonatomic , strong ) IBOutlet NSArrayController * arrayController ;
2024-02-26 17:01:35 +01:00
2024-07-17 15:40:47 +02:00
@ property ( nonatomic , readonly ) HBTitleSelectionRangeController * selectionRangeController ;
2015-10-25 07:03:36 +01:00
@ property ( nonatomic , readwrite ) NSArray < HBTitleSelection * > * titles ;
2024-02-26 17:01:35 +01:00
@ property ( nonatomic , readonly , weak ) id < HBTitleSelectionDelegate > delegate ;
2024-07-17 15:40:47 +02:00
2016-02-15 11:13:39 +01:00
@ property ( nonatomic , readonly ) NSString * message ;
2015-01-25 08:02:47 +00:00
@ end
@ implementation HBTitleSelectionController
2019-08-12 10:19:00 +02:00
- ( instancetype ) initWithTitles : ( NSArray < HBTitle * > * ) titles presetName : ( NSString * ) presetName delegate : ( id < HBTitleSelectionDelegate > ) delegate
2015-01-25 08:02:47 +00:00
{
self = [ super initWithWindowNibName : @ "HBTitleSelection" ] ;
if ( self )
{
2024-07-17 15:40:47 +02:00
_selectionRangeController = [ [ HBTitleSelectionRangeController alloc ] initWithTitles : titles ] ;
2015-01-25 08:02:47 +00:00
_delegate = delegate ;
2018-06-09 10:06:52 +02:00
_message = [ NSString stringWithFormat : NSLocalizedString ( @ "Select the titles to add to the queue using the %@ preset:" , @ "Titles selection sheet -> informative text" ) , presetName ] ;
2015-01-25 08:02:47 +00:00
2015-10-25 07:03:36 +01:00
NSMutableArray < HBTitleSelection * > * array = [ [ NSMutableArray alloc ] init ] ;
for ( HBTitle * title in titles )
2015-01-25 08:02:47 +00:00
{
2015-10-25 07:03:36 +01:00
[ array addObject : [ [ HBTitleSelection alloc ] initWithTitle : title undo : self . window . undoManager ] ] ;
2015-01-25 08:02:47 +00:00
}
2015-10-25 07:03:36 +01:00
self . titles = [ array copy ] ;
2015-01-25 08:02:47 +00:00
}
return self ;
}
2017-02-24 09:26:08 +01:00
- ( void ) windowDidLoad
{
NSSortDescriptor * mySortDescriptor = [ [ NSSortDescriptor alloc ] initWithKey : @ "title.index" ascending : YES ] ;
self . arrayController . sortDescriptors = [ NSArray arrayWithObject : mySortDescriptor ] ;
2024-07-17 15:40:47 +02:00
self . selectionRangeController . view . autoresizingMask = NSViewWidthSizable | NSViewHeightSizable ;
self . selectionRangeController . view . frame = NSMakeRect ( 0 , 0 , self . rangeView . frame . size . width , self . rangeView . frame . size . height ) ;
[ self . rangeView addSubview : self . selectionRangeController . view ] ;
self . selectionRangeController . range . undo = self . window . undoManager ;
2017-02-24 09:26:08 +01:00
}
2024-02-26 17:01:35 +01:00
- ( IBAction ) enable : ( id ) sender
{
NSIndexSet * indexes = self . tableView . targetedRowIndexes ;
for ( HBTitleSelection * title in [ self . arrayController . arrangedObjects objectsAtIndexes : indexes ] )
{
title . enabled = YES ;
}
}
- ( IBAction ) disable : ( id ) sender
{
NSIndexSet * indexes = self . tableView . targetedRowIndexes ;
for ( HBTitleSelection * title in [ self . arrayController . arrangedObjects objectsAtIndexes : indexes ] )
{
title . enabled = NO ;
}
}
- ( IBAction ) enableAll : ( id ) sender
{
for ( HBTitleSelection * title in self . titles )
{
title . enabled = YES ;
}
}
- ( IBAction ) disableAll : ( id ) sender
2015-01-25 08:02:47 +00:00
{
2015-10-25 07:03:36 +01:00
for ( HBTitleSelection * title in self . titles )
2015-01-25 08:02:47 +00:00
{
2024-02-26 17:01:35 +01:00
title . enabled = NO ;
}
}
- ( IBAction ) invertState : ( id ) sender
{
for ( HBTitleSelection * title in self . titles )
{
title . enabled = ! title . enabled ;
2015-01-25 08:02:47 +00:00
}
}
- ( IBAction ) add : ( id ) sender
{
2017-02-04 14:04:37 +01:00
NSMutableArray < HBTitle * > * titles = [ NSMutableArray array ] ;
[ self . arrayController . arrangedObjects enumerateObjectsUsingBlock : ^ ( HBTitleSelection * obj , NSUInteger idx , BOOL * stop ) {
2024-02-26 17:01:35 +01:00
if ( obj . enabled )
2015-01-25 08:02:47 +00:00
{
2017-02-04 14:04:37 +01:00
[ titles addObject : obj . title ] ;
2015-01-25 08:02:47 +00:00
}
} ] ;
2024-07-17 15:40:47 +02:00
HBTitleSelectionRange * range = self . selectionRangeController . enabled ?
[ self . selectionRangeController . range copy ] : nil ;
[ self . delegate didSelectTitles : titles range : range ] ;
2015-01-25 08:02:47 +00:00
}
- ( IBAction ) cancel : ( id ) sender
{
2024-07-17 15:40:47 +02:00
[ self . delegate didSelectTitles : @ [ ] range : nil ] ;
2015-01-25 08:02:47 +00:00
}
2024-02-26 17:01:35 +01:00
- ( BOOL ) validateMenuItem : ( NSMenuItem * ) menuItem
{
SEL action = menuItem . action ;
if ( action = = @ selector ( enable : ) || action = = @ selector ( disable : ) )
{
NSIndexSet * targetedRowIndexes = self . tableView . targetedRowIndexes ;
if ( targetedRowIndexes . count = = 1 )
{
NSUInteger index = targetedRowIndexes . firstIndex ;
HBTitleSelection * title = [ self . arrayController . arrangedObjects objectAtIndex : index ] ;
if ( ( action = = @ selector ( enable : ) && title . enabled ) ||
( action = = @ selector ( disable : ) && ! title . enabled ) )
{
return NO ;
}
}
else
{
return targetedRowIndexes . count > 0 ;
}
}
return YES ;
}
2015-01-25 08:02:47 +00:00
@ end
2018-10-16 13:57:40 +02:00
@ interface HBTitleSelectionController ( TouchBar ) < NSTouchBarProvider , NSTouchBarDelegate >
@ 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 : ) ] ;
2018-11-15 13:00:25 +01:00
[ cancelButton . widthAnchor constraintGreaterThanOrEqualToConstant : 160 ] . active = YES ;
2018-10-16 13:57:40 +02:00
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 : ) ] ;
2018-11-15 13:00:25 +01:00
[ addButton . widthAnchor constraintGreaterThanOrEqualToConstant : 160 ] . active = YES ;
2018-10-16 13:57:40 +02:00
addButton . keyEquivalent = @ "\r" ;
addItem . view = addButton ;
NSGroupTouchBarItem * item = [ NSGroupTouchBarItem groupItemWithIdentifier : identifier items : @ [ cancelItem , addItem ] ] ;
return item ;
}
return nil ;
}
@ end