import Cocoa
import Quartz
-class ScreenshotCollectionViewItem: NSCollectionViewItem {
- @IBOutlet weak var imageBox: NSBox!
- @IBOutlet weak var nameField: NSTextField!
- @IBOutlet weak var nameBox: NSBox!
+final class ScreenshotCollectionViewItem: NSCollectionViewItem {
+
+ @IBOutlet private weak var imageBox: NSBox!
+ @IBOutlet private weak var nameField: NSTextField!
+ @IBOutlet private weak var nameBox: NSBox!
var info: ScreenshotInformation? {
+
return representedObject as? ScreenshotInformation
}
+
var imageFrame: NSRect {
- guard let imageView = imageView
- else { fatalError("ScreenshotCollectionViewItem: imageView is nil") }
+
+ guard let imageView = imageView else {
+
+ fatalError("ScreenshotCollectionViewItem: imageView is nil")
+ }
+
let frame = centerFitRect(imageView.image, target: imageView.frame)
+
return view.convert(frame, from: imageBox)
}
override var isSelected: Bool {
+
didSet {
+
(imageBox.fillColor, nameField.textColor, nameBox.fillColor) = {
+
if isSelected {
- return (NSColor.controlHighlightColor, NSColor.white, NSColor.alternateSelectedControlColor)
+
+ return (.controlHighlightColor, .white, .alternateSelectedControlColor)
+
} else {
- return (NSColor.white, NSColor.black, NSColor.white)
+
+ return (.white, .black, .white)
}
}()
}
}
private func centerFitRect(_ image: NSImage?, target: NSRect) -> NSRect {
- guard let image = image else { return target }
+
+ guard let image = image else {
+
+ return target
+ }
+
let imageSize = image.size
- var ratio: CGFloat = 1
let ratioX = target.size.height / imageSize.height
let ratioY = target.size.width / imageSize.width
- if ratioX > ratioY {
- ratio = ratioY
- } else {
- ratio = ratioX
- }
+ let ratio = min(ratioY, ratioX)
let fitSize = NSSize(width: imageSize.width * ratio, height: imageSize.height * ratio)
let left = (target.size.width - fitSize.width) * 0.5
let bottom = (target.size.height - fitSize.height) * 0.5
+
return NSRect(x: left, y: bottom, width: fitSize.width, height: fitSize.height)
}
}
-extension ScreenshotCollectionViewItem: QLPreviewItem {
+extension ScreenshotCollectionViewItem: QLPreviewItem, Previewable {
+
var previewItemURL: URL! {
+
return info?.url
}
}
+
+extension ScreenshotCollectionViewItem: NibLoadable {}