From 56eba94dbe3ea9db60e7b8aaeb3a8959fa52194f Mon Sep 17 00:00:00 2001 From: Alex Antonyuk Date: Sat, 21 Mar 2015 00:37:29 +0200 Subject: [PATCH] - added overlay NSImageView for displaying screenshot previews. fix #230 --- .../Controllers/ATZPackageTableViewDelegate.m | 56 +++---------------- Alcatraz/Views/ATZPackageListTableCellView.h | 2 +- .../Views/ATZPackageListTableCellView.xib | 15 ++++- Alcatraz/Views/ATZPreviewImageButton.m | 24 -------- 4 files changed, 22 insertions(+), 75 deletions(-) diff --git a/Alcatraz/Controllers/ATZPackageTableViewDelegate.m b/Alcatraz/Controllers/ATZPackageTableViewDelegate.m index 37e2589..86295b2 100644 --- a/Alcatraz/Controllers/ATZPackageTableViewDelegate.m +++ b/Alcatraz/Controllers/ATZPackageTableViewDelegate.m @@ -86,14 +86,13 @@ - (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn [view.descriptionField setStringValue:package.summary]; [view.linkButton setImage:[self tableView:tableView websiteImageForTableColumn:tableColumn row:row]]; [view.linkButton setTitle:[self tableView:tableView displayWebsiteForTableColumn:tableColumn row:row]]; - [view.typeImageView setImage:[self tableView:tableView packageTypeImageForTableColumn:tableColumn row:row]]; [view.installButton setTitle:([package isInstalled] ? @"REMOVE" : @"INSTALL")]; BOOL hasImage = package.screenshotPath != nil; [view.previewButton setFullSize:hasImage]; [view.previewButton setHidden:!hasImage]; view.screenshotPath = package.screenshotPath; if (package.screenshotPath) { - [view.previewButton setImage:[ATZScreenshotsStorage cachedImageForPackage:package]]; + view.screenshotImageView.image = [ATZScreenshotsStorage cachedImageForPackage:package]; } ATZFillableButton* installButton = (ATZFillableButton*)view.installButton; [installButton setButtonBorderStyle:ATZFillableButtonTypeInstall]; @@ -149,57 +148,20 @@ - (NSImage *)tableView:(NSTableView*)tableView websiteImageForTableColumn:(NSTab return [[Alcatraz sharedPlugin].bundle imageForResource:websiteImageName]; } -- (NSImage *)tableView:(NSTableView*)tableView packageTypeImageForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row { - NSString* typeImageName = nil; - ATZPackage* package = [self tableView:tableView objectValueForTableColumn:tableColumn row:row]; - if ([package isKindOfClass:[ATZPlugin class]]) { - typeImageName = @"740-gear"; - } else if ([package isKindOfClass:[ATZTemplate class]]) { - typeImageName = @"808-documents"; - } else if ([package isKindOfClass:[ATZColorScheme class]]) { - typeImageName = @"837-palette"; - } else { - return nil; - } - if ([package isInstalled]) - typeImageName = [NSString stringWithFormat:@"%@-selected", typeImageName]; - - return [self selectionTemplateImageNamed:typeImageName]; -} - -- (NSImage *)selectionTemplateImageNamed:(NSString *)imageName { - NSImage *template = [[Alcatraz sharedPlugin].bundle imageForResource:imageName]; - NSSize size = [template size]; - - NSImage *copiedImage = [template copy]; - [copiedImage setTemplate:NO]; - [copiedImage lockFocus]; - [[NSColor selectedItemColor] set]; - NSRectFillUsingOperation(NSMakeRect(0, 0, size.width, size.height), NSCompositeSourceAtop); - [copiedImage unlockFocus]; - - return copiedImage; -} - - (void)loadImagesForVisibleRowsInTableView:(NSTableView *)tableView { NSRange range = [tableView rowsInRect:tableView.visibleRect]; for (NSInteger row = range.location; row < range.location + range.length; ++row) { ATZPackageListTableCellView *view = [tableView viewAtColumn:0 row:row makeIfNecessary:NO]; ATZPackage *package = self.filteredPackages[row]; - if (package.screenshotPath) { - [view.previewButton setImage:[ATZScreenshotsStorage cachedImageForPackage:package]]; - if (!view.previewButton.image) { - __block NSButton* imageButton = view.previewButton; - [ATZScreenshotsStorage fetchAndCacheImageForPackage:package progress:NULL completion:^(ATZPackage *pkg, NSImage *image) { - if ([view.titleField.stringValue isEqualToString:pkg.name]) { - imageButton.image = image; - [tableView noteHeightOfRowsWithIndexesChanged:[NSIndexSet indexSetWithIndex:row]]; - } - }]; - } - } else { - [view.previewButton setImage:nil]; + if (package.screenshotPath && !view.screenshotImageView.image) { + NSImageView __weak *imageView = view.screenshotImageView; + [ATZScreenshotsStorage fetchAndCacheImageForPackage:package progress:NULL completion:^(ATZPackage *pkg, NSImage *image) { + if ([view.titleField.stringValue isEqualToString:pkg.name]) { + imageView.image = image; + [tableView noteHeightOfRowsWithIndexesChanged:[NSIndexSet indexSetWithIndex:row]]; + } + }]; } } } diff --git a/Alcatraz/Views/ATZPackageListTableCellView.h b/Alcatraz/Views/ATZPackageListTableCellView.h index d27aee8..b98dcd1 100644 --- a/Alcatraz/Views/ATZPackageListTableCellView.h +++ b/Alcatraz/Views/ATZPackageListTableCellView.h @@ -30,7 +30,7 @@ @property (nonatomic, weak) IBOutlet NSButton* linkButton; @property (nonatomic, weak) IBOutlet NSButton* installButton; @property (nonatomic, weak) IBOutlet ATZPreviewImageButton* previewButton; -@property (nonatomic, weak) IBOutlet NSImageView* typeImageView; @property (nonatomic, copy) NSString *screenshotPath; +@property (nonatomic, weak) IBOutlet NSImageView *screenshotImageView; @end diff --git a/Alcatraz/Views/ATZPackageListTableCellView.xib b/Alcatraz/Views/ATZPackageListTableCellView.xib index 2e3cc8a..25eae39 100644 --- a/Alcatraz/Views/ATZPackageListTableCellView.xib +++ b/Alcatraz/Views/ATZPackageListTableCellView.xib @@ -1,8 +1,8 @@ - + - + @@ -58,19 +58,25 @@ + + + + + + @@ -78,9 +84,11 @@ + + @@ -88,6 +96,7 @@ + diff --git a/Alcatraz/Views/ATZPreviewImageButton.m b/Alcatraz/Views/ATZPreviewImageButton.m index b284e82..53df8c6 100644 --- a/Alcatraz/Views/ATZPreviewImageButton.m +++ b/Alcatraz/Views/ATZPreviewImageButton.m @@ -40,25 +40,6 @@ - (instancetype)initWithCoder:(NSCoder *)coder { return self; } -- (void)drawRect:(NSRect)dirtyRect { - if (!self.image) - return; - NSSize imageSize = self.image.size; - CGFloat x, y, width, height; - if (imageSize.width >= self.bounds.size.width) { - width = imageSize.width; - height = imageSize.height; - } else { - width = self.bounds.size.width; - height = (width/imageSize.width) * imageSize.height; - } - x = (self.bounds.size.width - width)/2; - y = (ATZPreviewImageHeight - height)/2; - NSRect imageRect = NSMakeRect(x, y, width, height); - [self.image drawInRect:imageRect fromRect:NSZeroRect operation:NSCompositeDestinationAtop - fraction:1.f respectFlipped:YES hints:nil]; -} - - (NSSize)intrinsicContentSize { if (self.image || [self isFullSize]) return NSMakeSize(self.bounds.size.width, ATZPreviewImageHeight); @@ -66,9 +47,4 @@ - (NSSize)intrinsicContentSize { return NSZeroSize; } -- (void)setImage:(NSImage *)image { - [image lockFocusFlipped:YES]; - [super setImage:image]; -} - @end