OSDN Git Service

ApplicationDirecroriesの中のアプリケーションに依存する部分を分離した
[kcd/KCD.git] / KCD / ImageView.swift
index c0c3804..83c4c27 100644 (file)
@@ -8,6 +8,7 @@
 
 import Cocoa
 
+///  複数の画像をカスケードして表示する
 final class ImageView: NSView {
     
     var images: [NSImage] = [] {
@@ -17,16 +18,17 @@ final class ImageView: NSView {
     
     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(
@@ -40,7 +42,10 @@ final class ImageView: NSView {
     
     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)
@@ -57,7 +62,7 @@ final class ImageView: NSView {
         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))
@@ -77,18 +82,17 @@ final class ImageView: NSView {
             .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)