]> granicus.if.org Git - handbrake/commitdiff
MacGui: improve queue init.
authorDamiano Galassi <damiog@gmail.com>
Thu, 25 Jul 2019 06:10:15 +0000 (08:10 +0200)
committerDamiano Galassi <damiog@gmail.com>
Thu, 25 Jul 2019 06:10:15 +0000 (08:10 +0200)
macosx/HBQueueController.m
macosx/HBQueueItemWorkingView.m

index 54b6f1a0301edf4cc6144d3cbbf8e514f96651bf..259f251e7f658b9f41137cc861c6419a87fef2d1 100644 (file)
@@ -52,12 +52,56 @@ static void *HBControllerQueueCoreContext = &HBControllerQueueCoreContext;
 
     if (self = [super initWithWindowNibName:@"Queue"])
     {
-        // Load the dockTile and instiante initial text fields
+        // Load the dockTile and instantiate initial text fields
         _dockTile = [[HBDockTile alloc] initWithDockTile:NSApplication.sharedApplication.dockTile
                                                   image:NSApplication.sharedApplication.applicationIconImage];
 
         // Init state
         _queue = queue;
+        _queue.undoManager = [[NSUndoManager alloc] init];
+
+        [NSNotificationCenter.defaultCenter addObserverForName:HBQueueLowSpaceAlertNotification object:_queue queue:NSOperationQueue.mainQueue usingBlock:^(NSNotification * _Nonnull note) {
+            [self queueLowDiskSpaceAlert];
+        }];
+
+        [NSNotificationCenter.defaultCenter addObserverForName:HBQueueDidCompleteNotification object:_queue queue:NSOperationQueue.mainQueue usingBlock:^(NSNotification * _Nonnull note) {
+            [self queueCompletedAlerts];
+        }];
+
+        [NSNotificationCenter.defaultCenter addObserverForName:HBQueueProgressNotification object:_queue queue:NSOperationQueue.mainQueue usingBlock:^(NSNotification * _Nonnull note) {
+            // Update dock icon
+            double progress = [note.userInfo[HBQueueProgressNotificationPercentKey] doubleValue];
+
+#define dockTileUpdateFrequency 0.1f
+
+            if (self.dockIconProgress < 100.0 * progress)
+            {
+                double hours = [note.userInfo[HBQueueProgressNotificationHoursKey] doubleValue];
+                double minutes = [note.userInfo[HBQueueProgressNotificationMinutesKey] doubleValue];
+                double seconds = [note.userInfo[HBQueueProgressNotificationSecondsKey] doubleValue];
+
+                [self.dockTile updateDockIcon:progress hours:hours minutes:minutes seconds:seconds];
+                self.dockIconProgress += dockTileUpdateFrequency;
+            }
+        }];
+
+        [NSNotificationCenter.defaultCenter addObserverForName:HBQueueDidCompleteItemNotification object:_queue queue:NSOperationQueue.mainQueue usingBlock:^(NSNotification * _Nonnull note) {
+            // Restore dock icon
+            [self.dockTile updateDockIcon:-1.0 withETA:@""];
+            self.dockIconProgress = 0;
+
+            // Run the per item notification and actions
+            HBQueueItem *item = note.userInfo[HBQueueItemNotificationItemKey];
+            if (item.state == HBQueueItemStateCompleted)
+            {
+                [self sendToExternalApp:item];
+            }
+
+            if (item.state == HBQueueItemStateCompleted || item.state == HBQueueItemStateFailed)
+            {
+                [self itemCompletedAlerts:item];
+            }
+        }];
 
         NSUserNotificationCenter.defaultUserNotificationCenter.delegate = self;
     }
@@ -65,6 +109,11 @@ static void *HBControllerQueueCoreContext = &HBControllerQueueCoreContext;
     return self;
 }
 
+- (NSUndoManager *)windowWillReturnUndoManager:(NSWindow *)window
+{
+    return _queue.undoManager;
+}
+
 - (void)windowDidLoad
 {
     if (@available (macOS 10.12, *))
@@ -72,8 +121,6 @@ static void *HBControllerQueueCoreContext = &HBControllerQueueCoreContext;
         self.window.tabbingMode = NSWindowTabbingModeDisallowed;
     }
 
-    _queue.undoManager = self.window.undoManager;
-
     // Set up the child view controllers
     _splitViewController = [[NSSplitViewController alloc] init];
     _splitViewController.splitView = _splitView;
@@ -98,9 +145,8 @@ static void *HBControllerQueueCoreContext = &HBControllerQueueCoreContext;
     _splitViewController.splitView.identifier = @"HBQueueSplitViewIdentifier";
 
     self.window.contentViewController = _splitViewController;
-
     self.window.frameAutosaveName = @"HBQueueWindowFrameAutosave";
-    [self.window setFrameFromString: @"HBQueueWindowFrameAutosave"];
+    [self.window setFrameFromString:@"HBQueueWindowFrameAutosave"];
 
     // Set up observers
     [self.queue.core addObserver:self forKeyPath:@"state"
@@ -109,51 +155,6 @@ static void *HBControllerQueueCoreContext = &HBControllerQueueCoreContext;
     [self.queue addObserver:self forKeyPath:@"pendingItemsCount"
                    options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionInitial
                    context:HBControllerQueueCoreContext];
-
-    [NSNotificationCenter.defaultCenter addObserverForName:HBQueueLowSpaceAlertNotification object:_queue queue:NSOperationQueue.mainQueue usingBlock:^(NSNotification * _Nonnull note) {
-        [self queueLowDiskSpaceAlert];
-    }];
-
-    [NSNotificationCenter.defaultCenter addObserverForName:HBQueueDidCompleteNotification object:_queue queue:NSOperationQueue.mainQueue usingBlock:^(NSNotification * _Nonnull note) {
-        // Since there are no more items to encode, go to queueCompletedAlerts
-        // for user specified alerts after queue completed
-        [self queueCompletedAlerts];
-    }];
-
-    [NSNotificationCenter.defaultCenter addObserverForName:HBQueueProgressNotification object:_queue queue:NSOperationQueue.mainQueue usingBlock:^(NSNotification * _Nonnull note) {
-        // Update dock icon
-        double progress = [note.userInfo[HBQueueProgressNotificationPercentKey] doubleValue];
-
-#define dockTileUpdateFrequency 0.1f
-
-        if (self.dockIconProgress < 100.0 * progress)
-        {
-            double hours = [note.userInfo[HBQueueProgressNotificationHoursKey] doubleValue];
-            double minutes = [note.userInfo[HBQueueProgressNotificationMinutesKey] doubleValue];
-            double seconds = [note.userInfo[HBQueueProgressNotificationSecondsKey] doubleValue];
-
-            [self.dockTile updateDockIcon:progress hours:hours minutes:minutes seconds:seconds];
-            self.dockIconProgress += dockTileUpdateFrequency;
-        }
-    }];
-
-    [NSNotificationCenter.defaultCenter addObserverForName:HBQueueDidCompleteItemNotification object:_queue queue:NSOperationQueue.mainQueue usingBlock:^(NSNotification * _Nonnull note) {
-        // Restore dock icon
-        [self.dockTile updateDockIcon:-1.0 withETA:@""];
-        self.dockIconProgress = 0;
-
-        // Run the per item notification and actions
-        HBQueueItem *item = note.userInfo[HBQueueItemNotificationItemKey];
-        if (item.state == HBQueueItemStateCompleted)
-        {
-            [self sendToExternalApp:item];
-        }
-
-        if (item.state == HBQueueItemStateCompleted || item.state == HBQueueItemStateFailed)
-        {
-            [self itemCompletedAlerts:item];
-        }
-    }];
 }
 
 - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
@@ -450,10 +451,12 @@ static void *HBControllerQueueCoreContext = &HBControllerQueueCoreContext;
 
 #pragma mark - Encode Done Actions
 
+NSString * const HBQueueItemNotificationPathKey = @"HBQueueItemNotificationPathKey";
+
 - (void)userNotificationCenter:(NSUserNotificationCenter *)center didActivateNotification:(NSUserNotification *)notification
 {
     // Show the file in Finder when a done notification was clicked.
-    NSString *path = notification.userInfo[@"Path"];
+    NSString *path = notification.userInfo[HBQueueItemNotificationPathKey];
     if ([path isKindOfClass:[NSString class]] && path.length)
     {
         NSURL *fileURL = [NSURL fileURLWithPath:path];
@@ -469,7 +472,7 @@ static void *HBControllerQueueCoreContext = &HBControllerQueueCoreContext;
     notification.soundName = playSound ? NSUserNotificationDefaultSoundName : nil;
     notification.hasActionButton = YES;
     notification.actionButtonTitle = NSLocalizedString(@"Show", @"Notification -> Show in Finder");
-    notification.userInfo = @{ @"Path": fileURL.path };
+    notification.userInfo = @{ HBQueueItemNotificationPathKey: fileURL.path };
 
     [[NSUserNotificationCenter defaultUserNotificationCenter] deliverNotification:notification];
 }
index 4a217a78a686cc40d8fd83dd732f481721673210..91e26a028c17e587745ff8d66fe393bdd859ab7a 100644 (file)
 
 @implementation HBQueueItemWorkingView
 
+- (void)setUpObservers
+{
+    NSNotificationCenter * __weak center = NSNotificationCenter.defaultCenter;
+
+    self.progressToken = [center addObserverForName:HBQueueProgressNotification
+                                             object:nil
+                                              queue:NSOperationQueue.mainQueue usingBlock:^(NSNotification * _Nonnull note)
+                          {
+                              NSString *progressInfo = note.userInfo[HBQueueProgressNotificationInfoKey];
+                              double progress = [note.userInfo[HBQueueProgressNotificationPercentKey] doubleValue];
+
+                              self.progressField.attributedStringValue = progressInfo.HB_smallMonospacedString;
+                              self.progressBar.doubleValue = progress;
+                          }];
+
+    self.completedToken = [center addObserverForName:HBQueueDidCompleteItemNotification
+                                              object:nil
+                                               queue:NSOperationQueue.mainQueue usingBlock:^(NSNotification * _Nonnull note)
+                           {
+                               HBQueueItem *completedItem = note.userInfo[HBQueueItemNotificationItemKey];
+                               if (completedItem == self.item) {
+                                   [self removeObservers];
+                               }
+                           }];
+}
+
+- (void)removeObservers
+{
+    if (self.progressToken)
+    {
+        [NSNotificationCenter.defaultCenter removeObserver:self.progressToken];
+        self.progressToken = nil;
+    }
+    if (self.completedToken)
+    {
+        [NSNotificationCenter.defaultCenter removeObserver:self.completedToken];
+        self.completedToken = nil;
+    }
+}
+
+- (void)dealloc
+{
+    [self removeObservers];
+}
+
 - (void)setItem:(HBQueueItem *)item
 {
     [super setItem:item];
 
     if (item.state == HBQueueItemStateWorking)
     {
-        NSNotificationCenter * __weak center = NSNotificationCenter.defaultCenter;
-
-        self.progressToken = [center addObserverForName:HBQueueProgressNotification
-                                                 object:nil
-                                                  queue:NSOperationQueue.mainQueue usingBlock:^(NSNotification * _Nonnull note)
-        {
-            NSString *progressInfo = note.userInfo[HBQueueProgressNotificationInfoKey];
-            double progress = [note.userInfo[HBQueueProgressNotificationPercentKey] doubleValue];
-
-            self.progressField.attributedStringValue = progressInfo.HB_smallMonospacedString;
-            self.progressBar.doubleValue = progress;
-        }];
-
-        self.completedToken = [center addObserverForName:HBQueueDidCompleteItemNotification
-                                                  object:nil
-                                                   queue:NSOperationQueue.mainQueue usingBlock:^(NSNotification * _Nonnull note)
-        {
-            HBQueueItem *completedItem = note.userInfo[HBQueueItemNotificationItemKey];
-            if (completedItem == self.item) {
-                [center removeObserver:self.progressToken];
-                [center removeObserver:self.completedToken];
-            }
-        }];
+        [self removeObservers];
+        [self setUpObservers];
     }
 }