diff options
author | Damiano Galassi <[email protected]> | 2020-01-20 12:12:27 +0100 |
---|---|---|
committer | Damiano Galassi <[email protected]> | 2020-01-20 12:12:27 +0100 |
commit | 063e587158fead42f198f73fd16f42e091c536f4 (patch) | |
tree | dfb3f6910dd69ddcbe03c5a9ee762a794c46988e | |
parent | 97424db9a6b116054ec31f4a6f1aee8a95e090da (diff) |
MacGui: add a Save menu item in the presets menu to save the changes to the selected preset. Fix a bunch of presets issues and weird behaviour.
-rw-r--r-- | macosx/Base.lproj/MainMenu.xib | 28 | ||||
-rw-r--r-- | macosx/HBAddPresetController.m | 1 | ||||
-rw-r--r-- | macosx/HBController.m | 129 | ||||
-rw-r--r-- | macosx/HBMutablePreset.h | 6 | ||||
-rw-r--r-- | macosx/HBMutablePreset.m | 6 | ||||
-rw-r--r-- | macosx/HBPresetsManager.h | 2 | ||||
-rw-r--r-- | macosx/HBPresetsManager.m | 16 | ||||
-rw-r--r-- | macosx/HBPresetsMenuBuilder.m | 4 | ||||
-rw-r--r-- | macosx/HBPresetsViewController.m | 29 | ||||
-rw-r--r-- | macosx/HBRenamePresetController.m | 14 | ||||
-rw-r--r-- | macosx/HBTreeNode.h | 14 | ||||
-rw-r--r-- | macosx/HBTreeNode.m | 54 |
12 files changed, 192 insertions, 111 deletions
diff --git a/macosx/Base.lproj/MainMenu.xib b/macosx/Base.lproj/MainMenu.xib index 26adb990c..18b2642ef 100644 --- a/macosx/Base.lproj/MainMenu.xib +++ b/macosx/Base.lproj/MainMenu.xib @@ -1,8 +1,8 @@ <?xml version="1.0" encoding="UTF-8"?> -<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="14490.70" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none"> +<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="15705" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES"> <dependencies> <deployment identifier="macosx"/> - <plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="14490.70"/> + <plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="15705"/> </dependencies> <objects> <customObject id="-2" userLabel="File's Owner" customClass="NSApplication"> @@ -331,7 +331,7 @@ <action selector="showAddPresetPanel:" target="-1" id="83j-3C-fCg"/> </connections> </menuItem> - <menuItem title="New Category" tag="-1" keyEquivalent="N" id="wvb-60-cWL"> + <menuItem title="New Category…" tag="-1" keyEquivalent="N" id="wvb-60-cWL"> <connections> <action selector="insertCategory:" target="-1" id="JL7-bI-H97"/> </connections> @@ -339,20 +339,25 @@ <menuItem isSeparatorItem="YES" tag="-1" id="1954"> <modifierMask key="keyEquivalentModifierMask" command="YES"/> </menuItem> - <menuItem title="Make Default Preset" tag="-1" id="frQ-v5-pRa" userLabel="Make Default Preset"> + <menuItem title="Save" tag="-1" id="lab-Ly-3p3"> <connections> - <action selector="setDefaultPreset:" target="-1" id="adZ-ic-MKn"/> + <action selector="savePreset:" target="-1" id="bp1-35-D63"/> </connections> </menuItem> - <menuItem isSeparatorItem="YES" tag="-1" id="2954"> - <modifierMask key="keyEquivalentModifierMask" command="YES"/> - </menuItem> - <menuItem title="Rename Preset…" tag="-1" id="1GQ-n3-jfY"> + <menuItem title="Rename…" tag="-1" id="1GQ-n3-jfY"> <connections> <action selector="showRenamePresetPanel:" target="-1" id="gRr-Jl-kZO"/> </connections> </menuItem> - <menuItem title="Delete Preset" tag="-1" id="CN3-Rh-gVf"> + <menuItem title="Make Default" tag="-1" id="frQ-v5-pRa" userLabel="Make Default Preset"> + <connections> + <action selector="setDefaultPreset:" target="-1" id="adZ-ic-MKn"/> + </connections> + </menuItem> + <menuItem isSeparatorItem="YES" tag="-1" id="2954"> + <modifierMask key="keyEquivalentModifierMask" command="YES"/> + </menuItem> + <menuItem title="Delete" tag="-1" id="CN3-Rh-gVf"> <connections> <action selector="deletePreset:" target="-1" id="ZGV-Ji-MGc"/> </connections> @@ -380,7 +385,7 @@ <menuItem isSeparatorItem="YES" tag="-1" id="ymY-bE-5EP"> <modifierMask key="keyEquivalentModifierMask" command="YES"/> </menuItem> - <menuItem title="Select Default Preset" tag="-1" id="2421"> + <menuItem title="Select Default" tag="-1" id="2421"> <connections> <action selector="selectDefaultPreset:" target="-1" id="2eH-zk-T3n"/> </connections> @@ -461,6 +466,7 @@ </menu> </menuItem> </items> + <point key="canvasLocation" x="-169" y="-21"/> </menu> <customObject id="4963" customClass="SPUStandardUpdaterController"/> <customObject id="6lr-Yy-GMc" customClass="HBAppDelegate"> diff --git a/macosx/HBAddPresetController.m b/macosx/HBAddPresetController.m index 960697f42..5bbcca0b0 100644 --- a/macosx/HBAddPresetController.m +++ b/macosx/HBAddPresetController.m @@ -60,6 +60,7 @@ typedef NS_ENUM(NSUInteger, HBAddPresetControllerMode) { { NSParameterAssert(preset); _mutablePreset = [preset mutableCopy]; + [_mutablePreset resetBuiltInAndDefaultState]; _manager = manager; _width = customWidth; _height = customHeight; diff --git a/macosx/HBController.m b/macosx/HBController.m index 0a23528a9..780cbb359 100644 --- a/macosx/HBController.m +++ b/macosx/HBController.m @@ -113,14 +113,14 @@ static void *HBControllerLogLevelContext = &HBControllerLogLevelContext; @property (nonatomic, nullable) HBJob *job; @property (nonatomic, nullable) HBAutoNamer *autoNamer; -/// The current selected preset. +/// The selected preset. +@property (nonatomic, nullable, strong) HBPreset *selectedPreset; + +/// The current modified preset. @property (nonatomic, strong) HBPreset *currentPreset; /// The current destination. -@property (nonatomic, strong) NSURL *currentDestination; - -/// Whether the job has been edited after a preset was applied. -@property (nonatomic) BOOL edited; +@property (nonatomic, strong) NSURL *destinationURL; /// The HBCore used for scanning. @property (nonatomic, strong) HBCore *core; @@ -178,6 +178,7 @@ static void *HBControllerLogLevelContext = &HBControllerLogLevelContext; _queue = queue; presetManager = manager; + _selectedPreset = manager.defaultPreset; _currentPreset = manager.defaultPreset; _scanSpecificTitleIdx = 1; @@ -190,20 +191,20 @@ static void *HBControllerLogLevelContext = &HBControllerLogLevelContext; NSData *bookmark = [NSUserDefaults.standardUserDefaults objectForKey:HBLastDestinationDirectoryBookmark]; if (bookmark) { - _currentDestination = [HBUtilities URLFromBookmark:bookmark]; + _destinationURL = [HBUtilities URLFromBookmark:bookmark]; } #else _currentDestination = [NSUserDefaults.standardUserDefaults URLForKey:HBLastDestinationDirectoryURL]; #endif - if (!_currentDestination || [[NSFileManager defaultManager] fileExistsAtPath:_currentDestination.path isDirectory:nil] == NO) + if (!_destinationURL || [NSFileManager.defaultManager fileExistsAtPath:_destinationURL.path isDirectory:nil] == NO) { - _currentDestination = [NSURL fileURLWithPath:[NSSearchPathForDirectoriesInDomains(NSMoviesDirectory, NSUserDomainMask, YES) firstObject] + _destinationURL = [NSURL fileURLWithPath:[NSSearchPathForDirectoriesInDomains(NSMoviesDirectory, NSUserDomainMask, YES) firstObject] isDirectory:YES]; } #ifdef __SANDBOX_ENABLED__ - [_currentDestination startAccessingSecurityScopedResource]; + [_destinationURL startAccessingSecurityScopedResource]; #endif } @@ -568,7 +569,7 @@ static void *HBControllerLogLevelContext = &HBControllerLogLevelContext; } if (action == @selector(selectPresetFromMenu:)) { - if ([menuItem.representedObject isEqualTo:self.currentPreset] && self.edited == NO) + if ([menuItem.representedObject isEqualTo:self.selectedPreset]) { menuItem.state = NSControlStateValueOn; } @@ -578,19 +579,23 @@ static void *HBControllerLogLevelContext = &HBControllerLogLevelContext; } return (self.job != nil); } - if (action == @selector(exportPreset:)) - { - return [fPresetsView validateUserInterfaceItem:menuItem] && self.job != nil; - } - if (action == @selector(selectDefaultPreset:) || - action == @selector(insertCategory:)) + if (action == @selector(exportPreset:) || + action == @selector(selectDefaultPreset:)) { return self.job != nil; } if (action == @selector(deletePreset:) || action == @selector(setDefaultPreset:)) { - return self.job != nil && self.edited == NO;//fixme + return self.job != nil && self.selectedPreset; + } + if (action == @selector(savePreset:)) + { + return self.job != nil && self.selectedPreset && self.selectedPreset.isBuiltIn == NO; + } + if (action == @selector(showRenamePresetPanel:)) + { + return self.selectedPreset && self.selectedPreset.isBuiltIn == NO; } return YES; @@ -783,6 +788,8 @@ static void *HBControllerLogLevelContext = &HBControllerLogLevelContext; { job.title = titles.firstObject; } + + self.selectedPreset = nil; self.job = job; handler(YES); @@ -808,7 +815,7 @@ static void *HBControllerLogLevelContext = &HBControllerLogLevelContext; } HBJob *job = [[HBJob alloc] initWithTitle:title andPreset:self.currentPreset]; - job.outputURL = self.currentDestination; + job.outputURL = self.destinationURL; // If the source is not a stream, and autonaming is disabled, // keep the existing file name. @@ -974,7 +981,7 @@ static void *HBControllerLogLevelContext = &HBControllerLogLevelContext; if (result == NSModalResponseOK) { self.job.outputURL = panel.URL; - self.currentDestination = panel.URL; + self.destinationURL = panel.URL; // Save this path to the prefs so that on next browse destination window it opens there [NSUserDefaults.standardUserDefaults setObject:[HBUtilities bookmarkFromURL:panel.URL] @@ -1016,7 +1023,6 @@ static void *HBControllerLogLevelContext = &HBControllerLogLevelContext; { self.job.presetName = [NSString stringWithFormat:@"%@ %@", self.job.presetName, NSLocalizedString(@"(Modified)", @"Main Window -> preset modified")]; } - self.edited = YES; } } @@ -1195,7 +1201,7 @@ static void *HBControllerLogLevelContext = &HBControllerLogLevelContext; for (HBTitle *title in titles) { HBJob *job = [[HBJob alloc] initWithTitle:title andPreset:preset]; - job.outputURL = self.currentDestination; + job.outputURL = self.destinationURL; job.outputFileName = job.defaultName; job.title = nil; [jobs addObject:job]; @@ -1281,6 +1287,7 @@ static void *HBControllerLogLevelContext = &HBControllerLogLevelContext; - (void)selectionDidChange { + self.selectedPreset = fPresetsView.selectedPreset; [self applyPreset:fPresetsView.selectedPreset]; } @@ -1306,42 +1313,41 @@ static void *HBControllerLogLevelContext = &HBControllerLogLevelContext; } else { - fPresetsView.selectedPreset = _currentPreset; [self.presetsPopover close]; } } } -- (void)setCurrentPreset:(HBPreset *)currentPreset +- (void)setSelectedPreset:(HBPreset *)selectedPreset { - NSParameterAssert(currentPreset); - - if (currentPreset != _currentPreset) + if (selectedPreset != _selectedPreset) { NSUndoManager *undo = self.window.undoManager; - [[undo prepareWithInvocationTarget:self] setCurrentPreset:_currentPreset]; + [[undo prepareWithInvocationTarget:self] setSelectedPreset:_selectedPreset]; - _currentPreset = currentPreset; + _selectedPreset = selectedPreset; } } -- (void)setEdited:(BOOL)edited +- (void)setCurrentPreset:(HBPreset *)currentPreset { - if (edited != _edited) + NSParameterAssert(currentPreset); + + if (currentPreset != _currentPreset) { NSUndoManager *undo = self.window.undoManager; - [[undo prepareWithInvocationTarget:self] setEdited:_edited]; + [[undo prepareWithInvocationTarget:self] setCurrentPreset:_currentPreset]; - _edited = edited; + _currentPreset = currentPreset; } } -- (void)reloadPreset:(id)sender +- (IBAction)reloadPreset:(id)sender { - // Reload the currently selected preset if it is selected. - if (self.currentPreset != NULL) + HBPreset *preset = self.selectedPreset ? self.selectedPreset : self.currentPreset; + if (preset) { - [self applyPreset:self.currentPreset]; + [self applyPreset:preset]; } } @@ -1352,7 +1358,6 @@ static void *HBControllerLogLevelContext = &HBControllerLogLevelContext; if (self.job) { self.currentPreset = preset; - self.edited = NO; // Remove the job observer so we don't update the file name // too many times while the preset is being applied @@ -1387,45 +1392,36 @@ static void *HBControllerLogLevelContext = &HBControllerLogLevelContext; [self.window beginSheet:addPresetController.window completionHandler:^(NSModalResponse returnCode) { if (returnCode == NSModalResponseOK) { + self.selectedPreset = addPresetController.preset; + [self applyPreset:addPresetController.preset]; self->fPresetsView.selectedPreset = addPresetController.preset; - [self applyPreset:self->fPresetsView.selectedPreset]; - [[NSNotificationCenter defaultCenter] postNotificationName:HBPresetsChangedNotification object:nil]; } }]; } -- (HBPreset *)createPresetFromCurrentSettings +- (HBMutablePreset *)createPresetFromCurrentSettings { HBMutablePreset *preset = [self.currentPreset mutableCopy]; - - // Set whether or not this is a user preset or factory 0 is factory, 1 is user - preset[@"Type"] = @1; - preset[@"Default"] = @NO; - [self.job writeToPreset:preset]; - - return [preset copy]; + return preset; } - (IBAction)showRenamePresetPanel:(id)sender { [self.window HB_endEditing]; - fPresetsView.selectedPreset = _currentPreset; - __block HBRenamePresetController *renamePresetController = [[HBRenamePresetController alloc] initWithPreset:self.currentPreset + __block HBRenamePresetController *renamePresetController = [[HBRenamePresetController alloc] initWithPreset:self.selectedPreset presetManager:presetManager]; [self.window beginSheet:renamePresetController.window completionHandler:^(NSModalResponse returnCode) { if (returnCode == NSModalResponseOK) { - [self applyPreset:self->fPresetsView.selectedPreset]; - [[NSNotificationCenter defaultCenter] postNotificationName:HBPresetsChangedNotification object:nil]; + self.job.presetName = renamePresetController.preset.name; } renamePresetController = nil; }]; } -#pragma mark - -#pragma mark Import Export Preset(s) +#pragma mark - Import Export Preset(s) - (IBAction)exportPreset:(id)sender { @@ -1437,25 +1433,39 @@ static void *HBControllerLogLevelContext = &HBControllerLogLevelContext; [fPresetsView importPreset:sender]; } -#pragma mark - -#pragma mark Preset Menu +#pragma mark - Preset Menu - (IBAction)selectDefaultPreset:(id)sender { + self.selectedPreset = presetManager.defaultPreset; [self applyPreset:presetManager.defaultPreset]; fPresetsView.selectedPreset = presetManager.defaultPreset; } - (IBAction)setDefaultPreset:(id)sender { - fPresetsView.selectedPreset = _currentPreset; - [fPresetsView setDefault:sender]; + [presetManager setDefaultPreset:self.selectedPreset]; +} + +- (IBAction)savePreset:(id)sender +{ + [self.window HB_endEditing]; + + NSIndexPath *indexPath = [presetManager indexPathOfPreset:self.selectedPreset]; + HBMutablePreset *preset = [self createPresetFromCurrentSettings]; + preset.name = self.selectedPreset.name; + preset.isDefault = self.selectedPreset.isDefault; + + [presetManager replacePresetAtIndexPath:indexPath withPreset:preset]; + + self.job.presetName = preset.name; + self.selectedPreset = preset; + fPresetsView.selectedPreset = preset; } - (IBAction)deletePreset:(id)sender { - HBPreset *preset = [sender representedObject]; - [fPresetsView deletePreset:preset]; + [fPresetsView deletePreset:self]; } - (IBAction)insertCategory:(id)sender @@ -1468,6 +1478,7 @@ static void *HBControllerLogLevelContext = &HBControllerLogLevelContext; // Retrieve the preset stored in the NSMenuItem HBPreset *preset = [sender representedObject]; + self.selectedPreset = preset; [self applyPreset:preset]; fPresetsView.selectedPreset = preset; } diff --git a/macosx/HBMutablePreset.h b/macosx/HBMutablePreset.h index 12feacb8d..80d37ebaa 100644 --- a/macosx/HBMutablePreset.h +++ b/macosx/HBMutablePreset.h @@ -14,11 +14,11 @@ NS_ASSUME_NONNULL_BEGIN */ @interface HBMutablePreset : HBPreset -/** - * Removes unknown keys and normalizes values. - */ +/// Removes unknown keys and normalizes values. - (void)cleanUp; +- (void)resetBuiltInAndDefaultState; + - (void)setObject:(id)obj forKey:(NSString *)key; - (void)setObject:(id)obj forKeyedSubscript:(NSString *)key; diff --git a/macosx/HBMutablePreset.m b/macosx/HBMutablePreset.m index 0fddc6a40..bec3f80b7 100644 --- a/macosx/HBMutablePreset.m +++ b/macosx/HBMutablePreset.m @@ -10,6 +10,7 @@ @property (nonatomic, strong, nullable) NSMutableDictionary *content; - (void)cleanUp; +- (void)resetBuiltInAndDefaultState; @end @@ -30,4 +31,9 @@ [super cleanUp]; } +- (void)resetBuiltInAndDefaultState +{ + [super resetBuiltInAndDefaultState]; +} + @end diff --git a/macosx/HBPresetsManager.h b/macosx/HBPresetsManager.h index 0c11818d6..ce32ed89d 100644 --- a/macosx/HBPresetsManager.h +++ b/macosx/HBPresetsManager.h @@ -59,6 +59,8 @@ extern NSString *HBPresetsChangedNotification; */ - (void)deletePresetAtIndexPath:(NSIndexPath *)idx; +- (void)replacePresetAtIndexPath:(NSIndexPath *)idx withPreset:(HBPreset *)preset; + /** * Returns the index path of the specified object. * diff --git a/macosx/HBPresetsManager.m b/macosx/HBPresetsManager.m index f8aede8a1..d1a11cb56 100644 --- a/macosx/HBPresetsManager.m +++ b/macosx/HBPresetsManager.m @@ -47,12 +47,12 @@ NSString *HBPresetsChangedNotification = @"HBPresetsChangedNotification"; #pragma mark - HBTreeNode delegate -- (void)nodeDidChange:(id)node +- (void)nodeDidChange:(HBTreeNode *)node { - [[NSNotificationCenter defaultCenter] postNotificationName:HBPresetsChangedNotification object:nil]; + [NSNotificationCenter.defaultCenter postNotificationName:HBPresetsChangedNotification object:nil]; } -- (void)treeDidRemoveNode:(id)node +- (void)treeDidRemoveNode:(HBTreeNode *)node { if (node == self.defaultPreset) { @@ -275,6 +275,11 @@ typedef NS_ENUM(NSUInteger, HBPresetLoadingResult) { [self.root removeObjectAtIndexPath:idx]; } +- (void)replacePresetAtIndexPath:(NSIndexPath *)idx withPreset:(HBPreset *)preset +{ + [self.root replaceObjectAtIndexPath:idx withObject:preset]; +} + - (NSIndexPath *)indexPathOfPreset:(HBPreset *)preset { return [self.root indexPathOfObject:preset]; @@ -318,7 +323,10 @@ typedef NS_ENUM(NSUInteger, HBPresetLoadingResult) { defaultAlreadySetted = YES; } - [obj setIsDefault:NO]; + if ([obj isDefault]) + { + [obj setIsDefault:NO]; + } }]; if (defaultAlreadySetted) diff --git a/macosx/HBPresetsMenuBuilder.m b/macosx/HBPresetsMenuBuilder.m index 931d8c6fa..4db86f8d6 100644 --- a/macosx/HBPresetsMenuBuilder.m +++ b/macosx/HBPresetsMenuBuilder.m @@ -26,7 +26,7 @@ _manager = manager; _action = action; - [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(build) name:HBPresetsChangedNotification object:nil]; + [NSNotificationCenter.defaultCenter addObserver:self selector:@selector(build) name:HBPresetsChangedNotification object:nil]; } return self; @@ -34,7 +34,7 @@ - (void)dealloc { - [[NSNotificationCenter defaultCenter] removeObserver:self]; + [NSNotificationCenter.defaultCenter removeObserver:self]; } /** diff --git a/macosx/HBPresetsViewController.m b/macosx/HBPresetsViewController.m index f71c1bac4..4ce075293 100644 --- a/macosx/HBPresetsViewController.m +++ b/macosx/HBPresetsViewController.m @@ -112,7 +112,7 @@ static void *HBPresetsViewControllerContext = &HBPresetsViewControllerContext; { if (context == HBPresetsViewControllerContext) { - HBPreset *selectedNode = [[self.treeController selectedObjects] firstObject]; + HBPreset *selectedNode = self.treeController.selectedObjects.firstObject; if (selectedNode && selectedNode.isLeaf && selectedNode != self.selectedPresetInternal) { self.selectedPresetInternal = selectedNode; @@ -158,7 +158,7 @@ static void *HBPresetsViewControllerContext = &HBPresetsViewControllerContext; - (IBAction)exportPreset:(id)sender { // Find the current selection, it can be a category too. - HBPreset *selectedPreset = [[[self.treeController selectedObjects] firstObject] copy]; + HBPreset *selectedPreset = [self.treeController.selectedObjects.firstObject copy]; // Open a panel to let the user choose where and how to save the export file NSSavePanel *panel = [NSSavePanel savePanel]; @@ -265,7 +265,7 @@ static void *HBPresetsViewControllerContext = &HBPresetsViewControllerContext; - (IBAction)clicked:(id)sender { - if (self.delegate && [[self.treeController.selectedObjects firstObject] isLeaf]) + if (self.delegate && [self.treeController.selectedObjects.firstObject isLeaf]) { [self.delegate selectionDidChange]; } @@ -273,10 +273,9 @@ static void *HBPresetsViewControllerContext = &HBPresetsViewControllerContext; - (IBAction)renamed:(id)sender { - if (self.delegate && [[self.treeController.selectedObjects firstObject] isLeaf]) + if (self.delegate && [self.treeController.selectedObjects.firstObject isLeaf]) { [self.delegate selectionDidChange]; - [NSNotificationCenter.defaultCenter postNotificationName:HBPresetsChangedNotification object:nil]; } } @@ -294,14 +293,23 @@ static void *HBPresetsViewControllerContext = &HBPresetsViewControllerContext; { // Alert user before deleting preset NSAlert *alert = [[NSAlert alloc] init]; - alert.messageText = NSLocalizedString(@"Are you sure you want to permanently delete the selected preset?", @"Delete preset alert -> message"); + alert.alertStyle = NSAlertStyleCritical; alert.informativeText = NSLocalizedString(@"You can't undo this action.", @"Delete preset alert -> informative text"); - [alert addButtonWithTitle:NSLocalizedString(@"Delete Preset", @"Delete preset alert -> first button")]; + + if (self.treeController.selectedObjects.count > 1) + { + alert.messageText = NSLocalizedString(@"Are you sure you want to permanently delete the selected presets?", @"Delete preset alert -> message"); + [alert addButtonWithTitle:NSLocalizedString(@"Delete Presets", @"Delete preset alert -> first button")]; + } + else + { + alert.messageText = NSLocalizedString(@"Are you sure you want to permanently delete the selected preset?", @"Delete preset alert -> message"); + [alert addButtonWithTitle:NSLocalizedString(@"Delete Preset", @"Delete preset alert -> first button")]; + } + [alert addButtonWithTitle:NSLocalizedString(@"Cancel", @"Delete preset alert -> second button")]; - alert.alertStyle = NSAlertStyleCritical; NSInteger status = [alert runModal]; - if (status == NSAlertFirstButtonReturn) { for (NSIndexPath *indexPath in self.treeController.selectionIndexPaths.reverseObjectEnumerator) @@ -327,11 +335,10 @@ static void *HBPresetsViewControllerContext = &HBPresetsViewControllerContext; - (IBAction)setDefault:(id)sender { - HBPreset *selectedNode = [[self.treeController selectedObjects] firstObject]; + HBPreset *selectedNode = self.treeController.selectedObjects.firstObject; if (selectedNode.isLeaf) { self.presets.defaultPreset = selectedNode; - [NSNotificationCenter.defaultCenter postNotificationName:HBPresetsChangedNotification object:nil]; } } diff --git a/macosx/HBRenamePresetController.m b/macosx/HBRenamePresetController.m index 33a2f8ec5..7f95d63b4 100644 --- a/macosx/HBRenamePresetController.m +++ b/macosx/HBRenamePresetController.m @@ -44,14 +44,14 @@ self.name.placeholderString = self.preset.name; self.name.stringValue = self.preset.name; - [[NSNotificationCenter defaultCenter] addObserver:self - selector:@selector(controlTextDidChange:) - name:NSControlTextDidChangeNotification object:nil]; + [NSNotificationCenter.defaultCenter addObserver:self + selector:@selector(controlTextDidChange:) + name:NSControlTextDidChangeNotification object:nil]; } - (void)dealloc { - [[NSNotificationCenter defaultCenter] removeObserver:self name:NSControlTextDidChangeNotification object:nil]; + [NSNotificationCenter.defaultCenter removeObserver:self name:NSControlTextDidChangeNotification object:nil]; } - (void)controlTextDidChange:(NSNotification *)obj { @@ -68,13 +68,13 @@ if (self.name.stringValue.length == 0) { NSAlert *alert = [[NSAlert alloc] init]; - [alert setMessageText:NSLocalizedString(@"The preset name cannot be empty.", @"Rename preset window -> name alert message")]; - [alert setInformativeText:NSLocalizedString(@"Please enter a name.", @"Rename preset window -> name alert informative text")]; + alert.messageText = NSLocalizedString(@"The preset name cannot be empty.", @"Rename preset window -> name alert message"); + alert.informativeText = NSLocalizedString(@"Please enter a name.", @"Rename preset window -> name alert informative text"); [alert runModal]; } else { - [self.preset setName:self.name.stringValue]; + self.preset.name = self.name.stringValue; [self.window.sheetParent endSheet:self.window returnCode:NSModalResponseOK]; } } diff --git a/macosx/HBTreeNode.h b/macosx/HBTreeNode.h index 4b0afcb12..d52590d68 100644 --- a/macosx/HBTreeNode.h +++ b/macosx/HBTreeNode.h @@ -8,6 +8,8 @@ NS_ASSUME_NONNULL_BEGIN +@class HBTreeNode; + /** * Notify a delegate that something changed in the tree. * KVO observing a tree looks complicated and expensive, so this is a lightweight @@ -15,10 +17,10 @@ NS_ASSUME_NONNULL_BEGIN */ @protocol HBTreeNodeDelegate <NSObject> -- (void)nodeDidChange:(id)node; +- (void)nodeDidChange:(HBTreeNode *)node; @optional -- (void)treeDidRemoveNode:(id)node; +- (void)treeDidRemoveNode:(HBTreeNode *)node; @end @@ -28,7 +30,7 @@ NS_ASSUME_NONNULL_BEGIN @interface HBTreeNode : NSObject <NSCopying> // NSTreeController required properties -@property (nonatomic, readonly) NSMutableArray *children; +@property (nonatomic, readonly) NSMutableArray<HBTreeNode *> *children; @property (nonatomic) BOOL isLeaf; @property (nonatomic, weak) id<HBTreeNodeDelegate> delegate; @@ -56,11 +58,17 @@ NS_ASSUME_NONNULL_BEGIN */ - (void)removeObjectAtIndexPath:(NSIndexPath *)idx; +/// Replaces the object at the specified index path. +/// @param idx the NSIndexPath of the object to replace +/// @param object the new object +- (void)replaceObjectAtIndexPath:(NSIndexPath *)idx withObject:(HBTreeNode *)object; + // KVC Accessor Methods @property (nonatomic, readonly) NSUInteger countOfChildren; - (id)objectInChildrenAtIndex:(NSUInteger)index; - (void)insertObject:(HBTreeNode *)presetObject inChildrenAtIndex:(NSUInteger)index; +- (void)replaceObjectInChildrenAtIndex:(NSUInteger)index withObject:(HBTreeNode *)object; - (void)removeObjectFromChildrenAtIndex:(NSUInteger)index; @end diff --git a/macosx/HBTreeNode.m b/macosx/HBTreeNode.m index e02d3e70f..ac45323c8 100644 --- a/macosx/HBTreeNode.m +++ b/macosx/HBTreeNode.m @@ -32,6 +32,15 @@ return node; } +- (void)setDelegate:(id<HBTreeNodeDelegate>)delegate +{ + _delegate = delegate; + for (HBTreeNode *node in self.children) + { + node.delegate = delegate; + } +} + - (NSUInteger)countOfChildren { return self.children.count; @@ -39,28 +48,30 @@ - (id)objectInChildrenAtIndex:(NSUInteger)index { - return (self.children)[index]; + return self.children[index]; } -- (void)insertObject:(HBTreeNode *)presetObject inChildrenAtIndex:(NSUInteger)index +- (void)insertObject:(HBTreeNode *)object inChildrenAtIndex:(NSUInteger)index { - [self.children insertObject:presetObject atIndex:index]; - [presetObject setDelegate:self.delegate]; - - for (HBTreeNode *node in self.children) - { - node.delegate = self.delegate; - } - + [self.children insertObject:object atIndex:index]; + object.delegate = self.delegate; [self.delegate nodeDidChange:self]; } - (void)removeObjectFromChildrenAtIndex:(NSUInteger)index { + self.children[index].delegate = nil; [self.children removeObjectAtIndex:index]; [self.delegate nodeDidChange:self]; } +- (void)replaceObjectInChildrenAtIndex:(NSUInteger)index withObject:(HBTreeNode *)object +{ + [self.children replaceObjectAtIndex:index withObject:object]; + object.delegate = self.delegate; + [self.delegate nodeDidChange:self]; +} + #pragma mark - Enumeration - (void)enumerateObjectsUsingBlock:(void (^)(id obj, NSIndexPath *idx, BOOL *stop))block @@ -133,7 +144,7 @@ if (parentNode.children.count > currIdx) { - parentNode = (parentNode.children)[currIdx]; + parentNode = parentNode.children[currIdx]; } } @@ -152,4 +163,25 @@ } } +- (void)replaceObjectAtIndexPath:(NSIndexPath *)idx withObject:(HBTreeNode *)object +{ + HBTreeNode *parentNode = self; + + // Find the object parent array + NSUInteger currIdx = 0; + NSUInteger i = 0; + for (i = 0; i < idx.length - 1; i++) + { + currIdx = [idx indexAtPosition:i]; + + if (parentNode.children.count > currIdx) + { + parentNode = parentNode.children[currIdx]; + } + } + + NSUInteger insertionIndex = [idx indexAtPosition:idx.length - 1]; + [parentNode replaceObjectInChildrenAtIndex:insertionIndex withObject:object]; +} + @end |