2 // ScreenshotCollectionViewItem.swift
5 // Created by Hori,Masaki on 2016/12/29.
6 // Copyright © 2016年 Hori,Masaki. All rights reserved.
12 final class ScreenshotCollectionViewItem: NSCollectionViewItem {
14 @IBOutlet weak var imageBox: NSBox!
15 @IBOutlet weak var nameField: NSTextField!
16 @IBOutlet weak var nameBox: NSBox!
18 var info: ScreenshotInformation? {
20 return representedObject as? ScreenshotInformation
23 var imageFrame: NSRect {
25 guard let imageView = imageView
26 else { fatalError("ScreenshotCollectionViewItem: imageView is nil") }
28 let frame = centerFitRect(imageView.image, target: imageView.frame)
30 return view.convert(frame, from: imageBox)
33 override var isSelected: Bool {
36 (imageBox.fillColor, nameField.textColor, nameBox.fillColor) = {
40 return (NSColor.controlHighlightColor, NSColor.white, NSColor.alternateSelectedControlColor)
44 return (NSColor.white, NSColor.black, NSColor.white)
50 private func centerFitRect(_ image: NSImage?, target: NSRect) -> NSRect {
52 guard let image = image else { return target }
54 let imageSize = image.size
56 var ratio: CGFloat = 1
57 let ratioX = target.size.height / imageSize.height
58 let ratioY = target.size.width / imageSize.width
67 let fitSize = NSSize(width: imageSize.width * ratio, height: imageSize.height * ratio)
68 let left = (target.size.width - fitSize.width) * 0.5
69 let bottom = (target.size.height - fitSize.height) * 0.5
71 return NSRect(x: left, y: bottom, width: fitSize.width, height: fitSize.height)
75 extension ScreenshotCollectionViewItem: QLPreviewItem {
77 var previewItemURL: URL! {