summaryrefslogtreecommitdiffstats
path: root/macosx/HBThumbnailItemView.m
blob: eb6e6646f7c504284fcf6bee304739f450081944 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/*
 Copyright (C) 2017 Apple Inc. All Rights Reserved.
 See LICENSE.txt for this sample’s licensing information
 
 Abstract:
 Custom NSScrubberItemView used to display an image.
 */

#import "HBThumbnailItemView.h"

@interface HBThumbnailItemView ()

@property (nonatomic, strong) NSImageView *imageView;

@end

#pragma mark -

@implementation HBThumbnailItemView

@synthesize thumbnail = _thumbnail;

- (instancetype)initWithFrame:(NSRect)frameRect
{
    self = [super initWithFrame:frameRect];
    if (self != nil)
    {
        _thumbnail = [[NSImage alloc] initWithSize:frameRect.size];
        _imageView = [NSImageView imageViewWithImage:_thumbnail];
        [_imageView setAutoresizingMask:(NSAutoresizingMaskOptions)(NSViewWidthSizable | NSViewHeightSizable)];

        [self addSubview:_imageView];
    }
    
    return self;
}

- (void)updateLayer
{
    self.layer.backgroundColor = NSColor.controlColor.CGColor;
}

- (void)layout
{
    [super layout];
    _imageView.frame = self.bounds;
}

- (NSImage *)thumbnail
{
    return _imageView.image;
}

- (void)setThumbnail:(NSImage *)thumbnail
{
    _imageView.hidden = NO;
    _imageView.image = thumbnail;
}

- (void)setThumbnailIndex:(NSUInteger)thumbnailIndex
{
    _thumbnailIndex = thumbnailIndex;

    _imageView.hidden = YES;

     [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