5 // Created by Hori,Masaki on 2017/01/02.
6 // Copyright © 2017年 Hori,Masaki. All rights reserved.
12 final class ImageView: NSView {
14 var images: [NSImage] = [] {
16 didSet { needsDisplay = true }
19 var imageRect: NSRect {
26 let bounds = self.bounds
27 let offset = bounds.width * 0.1 / 2 / 3
28 let border = offset * 3
29 let rect = bounds.insetBy(dx: border, dy: border)
30 let size = images[0].size
31 let ratio = min(rect.width / size.width, rect.height / size.height)
32 let drawSize = NSSize(width: size.width * ratio, height: size.height * ratio)
35 x: rect.minX + (rect.width - drawSize.width) / 2,
36 y: rect.minY + (rect.height - drawSize.height) / 2,
37 width: drawSize.width,
38 height: drawSize.height)
41 private var internalImageShadow: NSShadow?
43 private var imageShadow: NSShadow {
45 if let s = internalImageShadow {
51 s.shadowOffset = NSSize(width: 2, height: -2)
52 s.shadowBlurRadius = 4
53 s.shadowColor = NSColor.darkGray
54 internalImageShadow = s
59 override func draw(_ dirtyRect: NSRect) {
61 NSColor.controlBackgroundColor.set()
62 NSBezierPath.stroke(bounds)
65 NSBezierPath.defaultLineWidth = 1.0
66 NSBezierPath.stroke(bounds)
68 NSBezierPath.clip(bounds.insetBy(dx: 1, dy: 1))
72 let count = images.count
75 var alpha = pow(alphaFactor, Double(count - 1))
77 let offset = bounds.width * 0.1 / 2 / 3
78 let border = offset * 3
79 let rect = bounds.insetBy(dx: border, dy: border)
86 let offsetRect = rect.offsetBy(dx: offset * CGFloat($0.offset), dy: offset * CGFloat($0.offset))
87 let drawRect = imageRect(with: offsetRect, imageSize: $0.element.size)
88 $0.element.draw(in: drawRect, from: .zero, operation: .sourceOver, fraction: CGFloat(alpha))
93 private func imageRect(with rect: NSRect, imageSize: NSSize) -> NSRect {
95 let ratio = min(rect.width / imageSize.width, rect.height / imageSize.height)
96 let drawSize = NSSize(width: imageSize.width * ratio,
97 height: imageSize.height * ratio)
100 x: rect.minX + (rect.width - drawSize.width) / 2,
101 y: rect.minY + (rect.height - drawSize.height) / 2,
102 width: drawSize.width,
103 height: drawSize.height)