5 // Created by Hori,Masaki on 2017/01/02.
6 // Copyright © 2017年 Hori,Masaki. All rights reserved.
11 final class ImageView: NSView {
13 var images: [NSImage] = [] {
15 didSet { needsDisplay = true }
18 var imageRect: NSRect {
20 if images.isEmpty { return .zero }
22 let bounds = self.bounds
23 let offset = bounds.width * 0.1 / 2 / 3
24 let border = offset * 3
25 let rect = bounds.insetBy(dx: border, dy: border)
26 let size = images[0].size
27 let ratioX = rect.height / size.height
28 let ratioY = rect.width / size.width
29 let ratio = ratioX > ratioY ? ratioY : ratioX
30 let drawSize = NSSize(width: size.width * ratio, height: size.height * ratio)
33 x: rect.minX + (rect.width - drawSize.width) / 2,
34 y: rect.minY + (rect.height - drawSize.height) / 2,
35 width: drawSize.width,
36 height: drawSize.height)
39 private var internalImageShadow: NSShadow?
41 private var imageShadow: NSShadow {
43 if let s = internalImageShadow { return s }
46 s.shadowOffset = NSSize(width: 2, height: -2)
47 s.shadowBlurRadius = 4
48 s.shadowColor = NSColor.darkGray
49 internalImageShadow = s
54 override func draw(_ dirtyRect: NSRect) {
56 NSColor.controlBackgroundColor.set()
57 NSBezierPath.stroke(bounds)
60 NSBezierPath.defaultLineWidth = 1.0
61 NSBezierPath.stroke(bounds)
63 NSBezierPath.clip(bounds.insetBy(dx: 1, dy: 1))
67 let count = images.count
70 var alpha = pow(alphaFactor, Double(count - 1))
72 let offset = bounds.width * 0.1 / 2 / 3
73 let border = offset * 3
74 let rect = bounds.insetBy(dx: border, dy: border)
80 let offsetRect = rect.offsetBy(dx: offset * CGFloat($0.offset), dy: offset * CGFloat($0.offset))
81 let drawRect = imageRect(with: offsetRect, imageSize: $0.element.size)
82 $0.element.draw(in: drawRect, from: .zero, operation: .sourceOver, fraction: CGFloat(alpha))
87 private func imageRect(with rect: NSRect, imageSize: NSSize) -> NSRect {
89 let ratioX = rect.height / imageSize.height
90 let ratioY = rect.width / imageSize.width
91 let ratio = ratioX > ratioY ? ratioY : ratioX
92 let drawSize = NSSize(width: imageSize.width * ratio,
93 height: imageSize.height * ratio)
96 x: rect.minX + (rect.width - drawSize.width) / 2,
97 y: rect.minY + (rect.height - drawSize.height) / 2,
98 width: drawSize.width,
99 height: drawSize.height)