]> granicus.if.org Git - handbrake/commitdiff
MacGui: improve preview window touch bar.
authorDamiano Galassi <damiog@gmail.com>
Sun, 11 Nov 2018 11:07:13 +0000 (12:07 +0100)
committerDamiano Galassi <damiog@gmail.com>
Sun, 11 Nov 2018 11:07:13 +0000 (12:07 +0100)
macosx/HBController.m
macosx/HBPictureHUDController.m
macosx/HBPreviewGenerator.h
macosx/HBPreviewGenerator.m
macosx/HBThumbnailItemView.m

index 79c2638e7967c921820cf394e7427f43ebbb1617..c489ae2fbc87d28f5556e8e8c5d4950411642f4e 100644 (file)
@@ -859,6 +859,7 @@ static void *HBControllerQueueCoreContext = &HBControllerQueueCoreContext;
     }
     else
     {
+        [fPreviewController.generator invalidate];
         fPreviewController.generator = nil;
         self.summaryController.generator = nil;
     }
index dcf4944e07578345f6b1267497f1cf33dabc0610..bcd17f613792489506e0d7ed830cd152be6c4263 100644 (file)
@@ -30,6 +30,7 @@
 - (void)_touchBar_reloadScrubberData;
 - (void)_touchBar_updateScrubberSelectedIndex:(NSUInteger)selectedIndex;
 - (void)_touchBar_updateFitToView:(BOOL)fitToView;
+- (void)_touchBar_validateUserInterfaceItems;
 @end
 
 
@@ -82,6 +83,7 @@
     if (@available(macOS 10.12.2, *))
     {
         [self _touchBar_reloadScrubberData];
+        [self _touchBar_validateUserInterfaceItems];
     }
 }
 
     }
 }
 
+- (BOOL)validateUserIterfaceItemForAction:(SEL)action
+{
+    if (action == @selector(createMoviePreview:) || action == @selector(toggleScaleToScreen:))
+    {
+        return self.generator != nil;
+    }
+    return YES;
+}
+
 - (IBAction)previewDurationPopUpChanged:(id)sender
 {
     [[NSUserDefaults standardUserDefaults] setObject:self.durationPopUp.titleOfSelectedItem forKey:@"PreviewLength"];
@@ -230,7 +241,7 @@ NSString *thumbnailScrubberItemIdentifier = @"thumbnailItem";
 static NSTouchBarItemIdentifier HBTouchBarMain = @"fr.handbrake.previewWindowTouchBar";
 
 static NSTouchBarItemIdentifier HBTouchBarRip = @"fr.handbrake.rip";
-static NSTouchBarItemIdentifier HBTouchBarScrubber = @"fr.handbrake.scrubbe";
+static NSTouchBarItemIdentifier HBTouchBarScrubber = @"fr.handbrake.scrubber";
 static NSTouchBarItemIdentifier HBTouchBarFitToScreen = @"fr.handbrake.fitToScreen";
 
 @dynamic touchBar;
@@ -330,4 +341,17 @@ static NSTouchBarItemIdentifier HBTouchBarFitToScreen = @"fr.handbrake.fitToScre
     }
 }
 
+- (void)_touchBar_validateUserInterfaceItems
+{
+    for (NSTouchBarItemIdentifier identifier in self.touchBar.itemIdentifiers) {
+        NSTouchBarItem *item = [self.touchBar itemForIdentifier:identifier];
+        NSView *view = item.view;
+        if ([view isKindOfClass:[NSButton class]]) {
+            NSButton *button = (NSButton *)view;
+            BOOL enabled = [self validateUserIterfaceItemForAction:button.action];
+            button.enabled = enabled;
+        }
+    }
+}
+
 @end
index dcf5f6953b55ff8be060b5694c969e9e1584cd91..617ad32f38f72aa93bb7a95750450173a199a772 100644 (file)
@@ -29,6 +29,11 @@ NS_ASSUME_NONNULL_BEGIN
 - (instancetype)init NS_UNAVAILABLE;
 - (instancetype)initWithCore:(HBCore *)core job:(HBJob *)job NS_DESIGNATED_INITIALIZER;
 
+/**
+ * Wait until all the asyncronous operations are done.
+ */
+- (void)invalidate;
+
 #pragma mark - Still image generator
 
 /**
index fa486832c53be7ff7c18f5916b5d2379202be34a..9f1057b35f1746c4a3524e3d88003b502221c72f 100644 (file)
@@ -17,7 +17,8 @@
 @property (nonatomic, readonly) NSCache<NSNumber *, id> *previewsCache;
 @property (nonatomic, readonly) NSCache<NSNumber *, id> *smallPreviewsCache;
 
-@property (nonatomic, readonly) dispatch_semaphore_t sem;
+@property (nonatomic, readonly) dispatch_queue_t queue;
+@property (nonatomic, readonly) dispatch_group_t group;
 @property (nonatomic, readonly) _Atomic bool invalidated;
 
 @property (nonatomic, strong) HBCore *core;
@@ -50,7 +51,8 @@
 
         _imagesCount = [_scanCore imagesCountForTitle:self.job.title];
 
-        _sem = dispatch_semaphore_create(4);
+        _queue = dispatch_queue_create("fr.handbrake.PreviewQueue", DISPATCH_QUEUE_SERIAL);
+        _group = dispatch_group_create();
 
         [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(imagesSettingsDidChange) name:HBPictureChangedNotification object:job.picture];
         [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(imagesSettingsDidChange) name:HBFiltersChangedNotification object:job.filters];
 
 - (void)copySmallImageAtIndex:(NSUInteger)index completionHandler:(void (^)(__nullable CGImageRef result))handler
 {
-    dispatch_semaphore_wait(_sem, DISPATCH_TIME_FOREVER);
-
     if (_invalidated || index >= self.imagesCount)
     {
         handler(NULL);
-        dispatch_semaphore_signal(_sem);
         return;
     }
 
-    CGImageRef image;
+    dispatch_group_async(_group, _queue,^{
 
-    // First try to look in the small previews cache
-    image = (__bridge CGImageRef)([_smallPreviewsCache objectForKey:@(index)]);
+        if (self->_invalidated || index >= self.imagesCount)
+        {
+            handler(NULL);
+            return;
+        }
 
-    if (image != NULL)
-    {
-        handler(image);
-        dispatch_semaphore_signal(_sem);
-        return;
-    }
+        CGImageRef image;
 
-    // Else try the normal cache
-    image = (__bridge CGImageRef)([_previewsCache objectForKey:@(index)]);
+        // First try to look in the small previews cache
+        image = (__bridge CGImageRef)([self->_smallPreviewsCache objectForKey:@(index)]);
 
-    if (image == NULL)
-    {
-        image = (CGImageRef)[self.scanCore copyImageAtIndex:index
-                                                      forTitle:self.job.title
-                                                  pictureFrame:self.job.picture
-                                                   deinterlace:NO
-                                                        rotate:self.job.filters.rotate
-                                                       flipped:self.job.filters.flip];
-        CFAutorelease(image);
-    }
+        if (image != NULL)
+        {
+            handler(image);
+            return;
+        }
 
-    if (image != NULL)
-    {
-        CGImageRef scaledImage = CreateScaledCGImageFromCGImage(image, 30);
-        // The cost is the number of pixels of the image
-        NSUInteger previewCost = CGImageGetWidth(scaledImage) * CGImageGetHeight(scaledImage);
-        [self.smallPreviewsCache setObject:(__bridge id)(scaledImage) forKey:@(index) cost:previewCost];
-        handler(scaledImage);
-        dispatch_semaphore_signal(_sem);
-        return;
-    }
+        // Else try the normal cache
+        image = (__bridge CGImageRef)([self->_previewsCache objectForKey:@(index)]);
+
+        if (image == NULL)
+        {
+            image = (CGImageRef)[self.scanCore copyImageAtIndex:index
+                                                       forTitle:self.job.title
+                                                   pictureFrame:self.job.picture
+                                                    deinterlace:NO
+                                                         rotate:self.job.filters.rotate
+                                                        flipped:self.job.filters.flip];
+            CFAutorelease(image);
+        }
 
-    handler(NULL);
-    dispatch_semaphore_signal(_sem);
+        if (image != NULL)
+        {
+            CGImageRef scaledImage = CreateScaledCGImageFromCGImage(image, 30);
+            // The cost is the number of pixels of the image
+            NSUInteger previewCost = CGImageGetWidth(scaledImage) * CGImageGetHeight(scaledImage);
+            [self.smallPreviewsCache setObject:(__bridge id)(scaledImage) forKey:@(index) cost:previewCost];
+            handler(scaledImage);
+            return;
+        }
+
+        handler(NULL);
+    });
 }
 
 #pragma mark -
     }
 }
 
+- (void)invalidate
+{
+    _invalidated = true;
+    dispatch_group_wait(_group, DISPATCH_TIME_FOREVER);
+}
+
 @end
index 9a96bda0fb3c5523ba01024148197ed290e3ed81..21f8e7ac6d95d643e2f2006bdf4ded32d96e4c38 100644 (file)
 
     _imageView.hidden = YES;
 
-    dispatch_async(dispatch_get_global_queue(QOS_CLASS_BACKGROUND, 0), ^{
-        HBPreviewGenerator *generator = self.generator;
-
-        [generator copySmallImageAtIndex:thumbnailIndex completionHandler:^(CGImageRef  _Nullable result)
-         {
-             if (result != NULL)
-             {
-                 NSSize size = NSMakeSize(CGImageGetWidth(result), CGImageGetHeight(result));
-                 NSImage *thumbnail = [[NSImage alloc] initWithCGImage:result size:size];
-
-                 dispatch_async(dispatch_get_main_queue(), ^{
-                     [self setThumbnail:thumbnail];
-                 });
-             }
-             else
-             {
-                 dispatch_async(dispatch_get_main_queue(), ^{
-                     [self setThumbnail:nil];
-                 });
-             }
-         }];
-    });
+     [self.generator copySmallImageAtIndex:thumbnailIndex completionHandler:^(CGImageRef  _Nullable result)
+      {
+          if (result != NULL)
+          {
+              NSSize size = NSMakeSize(CGImageGetWidth(result), CGImageGetHeight(result));
+              NSImage *thumbnail = [[NSImage alloc] initWithCGImage:result size:size];
+
+              dispatch_async(dispatch_get_main_queue(), ^{
+                  [self setThumbnail:thumbnail];
+              });
+          }
+          else
+          {
+              dispatch_async(dispatch_get_main_queue(), ^{
+                  [self setThumbnail:nil];
+              });
+          }
+      }];
 }
 
 @end