]> granicus.if.org Git - handbrake/commitdiff
MacGui: update preset when navigating with the keyboard arrows, fix #452
authorDamiano Galassi <damiog@gmail.com>
Wed, 20 Dec 2017 21:06:38 +0000 (22:06 +0100)
committerDamiano Galassi <damiog@gmail.com>
Wed, 20 Dec 2017 21:06:38 +0000 (22:06 +0100)
macosx/HBAddPresetController.m
macosx/HBController.m
macosx/HBPresetsViewController.h
macosx/HBPresetsViewController.m

index 4ff228d015222f223aee26196a8bd4e4c5d45442..76ce3facd54cc28f993976137d8a38eaf2c6d423 100644 (file)
@@ -252,7 +252,7 @@ typedef NS_ENUM(NSUInteger, HBAddPresetControllerMode) {
         [newPreset cleanUp];
 
         self.preset = [newPreset copy];
-        [self.selectedCategory insertObject:self.preset inChildrenAtIndex:0];
+        [self.selectedCategory insertObject:self.preset inChildrenAtIndex:self.selectedCategory.countOfChildren];
 
         [self.window orderOut:nil];
         [NSApp endSheet:self.window returnCode:NSModalResponseContinue];
index 95cda984f5c337dccb7092cd3bbc6e71af432d3a..575270c71f4b71f4a96440b08a96a571e3078414 100644 (file)
 
 - (void)selectionDidChange
 {
-    [self applyPreset:fPresetsView.selectedPreset];
+    if (fPresetsView.selectedPreset != self.currentPreset || self.edited)
+    {
+        [self applyPreset:fPresetsView.selectedPreset];
+    }
 }
 
 #pragma mark -  Presets
 - (IBAction)selectDefaultPreset:(id)sender
 {
     [self applyPreset:presetManager.defaultPreset];
-    [fPresetsView setSelection:_currentPreset];
+    fPresetsView.selectedPreset = presetManager.defaultPreset;
 }
 
 - (IBAction)insertCategory:(id)sender
     HBPreset *preset = [sender representedObject];
 
     [self applyPreset:preset];
-    [fPresetsView setSelection:preset];
+    fPresetsView.selectedPreset = preset;
 }
 
 @end
index 11390e452b37ba71bfa218d40110a4186046ae95..e3cca8cd13ce6754aca597620a58a90cfed5087c 100644 (file)
@@ -27,13 +27,9 @@ NS_ASSUME_NONNULL_BEGIN
 - (IBAction)exportPreset:(id)sender;
 - (IBAction)importPreset:(id)sender;
 
-- (void)deselect;
-- (void)setSelection:(HBPreset *)preset;
-
 - (IBAction)insertCategory:(id)sender;
 
-@property (nonatomic, readonly) HBPreset *selectedPreset;
-@property (nonatomic, readonly) NSUInteger indexOfSelectedItem;
+@property (nonatomic, readwrite) HBPreset *selectedPreset;
 
 @property (nonatomic, readwrite, getter=isEnabled) BOOL enabled;
 @property (nonatomic, readwrite) BOOL showHeader;
index 7763a3a56b8171d97dd7d2e90453b9c8a5bd5881..b9a975c91295f6e1607971b281d705830a2626cc 100644 (file)
@@ -12,6 +12,9 @@
 // drag and drop pasteboard type
 #define kHandBrakePresetPBoardType @"handBrakePresetPBoardType"
 
+// KVO Context
+static void *HBPresetsViewControllerContext = &HBPresetsViewControllerContext;
+
 @interface HBPresetCellView : NSTableCellView
 @end
 
@@ -44,6 +47,7 @@
 @interface HBPresetsViewController () <NSOutlineViewDelegate>
 
 @property (nonatomic, strong) HBPresetsManager *presets;
+@property (nonatomic, readwrite) HBPreset *selectedPresetInternal;
 @property (nonatomic, unsafe_unretained) IBOutlet NSTreeController *treeController;
 
 @property (nonatomic, strong) IBOutlet NSTextField *headerLabel;
@@ -59,7 +63,8 @@
  */
 @property (nonatomic, strong) NSMutableArray *expandedNodes;
 
-@property (unsafe_unretained) IBOutlet NSOutlineView *outlineView;
+@property (nonatomic, unsafe_unretained) IBOutlet NSOutlineView *outlineView;
+
 
 @end
 
@@ -71,6 +76,7 @@
     if (self)
     {
         _presets = presetManager;
+        _selectedPresetInternal = presetManager.defaultPreset;
         _expandedNodes = [[NSArray arrayWithArray:[[NSUserDefaults standardUserDefaults]
                                                    objectForKey:@"HBPreviewViewExpandedStatus"]] mutableCopy];
     }
     // Re-expand the items
     [self expandNodes:[self.treeController.arrangedObjects childNodes]];
 
-    [self.treeController setSelectionIndexPath:[self.presets indexPathOfPreset:self.presets.defaultPreset]];
+    [self.treeController setSelectionIndexPath:[self.presets indexPathOfPreset:self.selectedPreset]];
 
     // Update header state
     self.showHeader = _showHeader;
+
+    [self.treeController addObserver:self forKeyPath:@"selectedObjects" options:NSKeyValueObservingOptionNew context:HBPresetsViewControllerContext];
+}
+
+- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
+{
+    if (context == HBPresetsViewControllerContext)
+    {
+        HBPreset *selectedNode = [[self.treeController selectedObjects] firstObject];
+        if (selectedNode && selectedNode.isLeaf && selectedNode != self.selectedPresetInternal)
+        {
+            self.selectedPresetInternal = selectedNode;
+            [self.delegate selectionDidChange];
+        }
+    }
+    else
+    {
+        [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
+    }
 }
 
 - (BOOL)validateUserInterfaceItem:(id < NSValidatedUserInterfaceItem >)anItem
         }
     }
 
-
     return YES;
 }
 
 - (IBAction)setDefault:(id)sender
 {
     HBPreset *selectedNode = [[self.treeController selectedObjects] firstObject];
-    if ([[selectedNode valueForKey:@"isLeaf"] boolValue])
+    if (selectedNode.isLeaf)
     {
         self.presets.defaultPreset = selectedNode;
     }
 }
 
-- (void)deselect
+- (void)setSelectedPreset:(HBPreset *)selectedPreset
 {
-    [self.treeController setSelectionIndexPath:nil];
+    _selectedPresetInternal = selectedPreset;
+    [self setSelection:selectedPreset];
+}
+
+- (HBPreset *)selectedPreset
+{
+    return _selectedPresetInternal;
 }
 
 - (void)setSelection:(HBPreset *)preset
     }
 }
 
-- (HBPreset *)selectedPreset
-{
-    HBPreset *selectedNode = [[self.treeController selectedObjects] firstObject];
-    if ([[selectedNode valueForKey:@"isLeaf"] boolValue])
-    {
-        return selectedNode;
-    }
-    else
-    {
-        return self.presets.defaultPreset;
-    }
-}
-
 - (IBAction)updateBuiltInPresets:(id)sender
 {
     [self.presets generateBuiltInPresets];