import Cocoa
+/// 複数の画像をカスケードして表示する
final class ImageView: NSView {
var images: [NSImage] = [] {
var imageRect: NSRect {
- if images.isEmpty { return .zero }
+ if images.isEmpty {
+
+ return .zero
+ }
let bounds = self.bounds
let offset = bounds.width * 0.1 / 2 / 3
let border = offset * 3
let rect = bounds.insetBy(dx: border, dy: border)
let size = images[0].size
- let ratioX = rect.height / size.height
- let ratioY = rect.width / size.width
- let ratio = ratioX > ratioY ? ratioY : ratioX
+ let ratio = min(rect.width / size.width, rect.height / size.height)
let drawSize = NSSize(width: size.width * ratio, height: size.height * ratio)
return NSRect(
private var imageShadow: NSShadow {
- if let s = internalImageShadow { return s }
+ if let s = internalImageShadow {
+
+ return s
+ }
let s = NSShadow()
s.shadowOffset = NSSize(width: 2, height: -2)
NSBezierPath.stroke(bounds)
NSColor.black.set()
- NSBezierPath.setDefaultLineWidth(1.0)
+ NSBezierPath.defaultLineWidth = 1.0
NSBezierPath.stroke(bounds)
NSBezierPath.clip(bounds.insetBy(dx: 1, dy: 1))
.enumerated()
.reversed()
.forEach {
+
let offsetRect = rect.offsetBy(dx: offset * CGFloat($0.offset), dy: offset * CGFloat($0.offset))
let drawRect = imageRect(with: offsetRect, imageSize: $0.element.size)
- $0.element.draw(in: drawRect, from: .zero, operation: NSCompositeSourceOver, fraction: CGFloat(alpha))
+ $0.element.draw(in: drawRect, from: .zero, operation: .sourceOver, fraction: CGFloat(alpha))
alpha /= alphaFactor
}
}
private func imageRect(with rect: NSRect, imageSize: NSSize) -> NSRect {
- let ratioX = rect.height / imageSize.height
- let ratioY = rect.width / imageSize.width
- let ratio = ratioX > ratioY ? ratioY : ratioX
+ let ratio = min(rect.width / imageSize.width, rect.height / imageSize.height)
let drawSize = NSSize(width: imageSize.width * ratio,
height: imageSize.height * ratio)