OSDN Git Service

省略できる記法があればそれを用いるようにした
[kcd/KCD.git] / KCD / ScreenshotDetailViewController.swift
1 //
2 //  ScreenshotDetailViewController.swift
3 //  KCD
4 //
5 //  Created by Hori,Masaki on 2016/12/29.
6 //  Copyright © 2016年 Hori,Masaki. All rights reserved.
7 //
8
9 import Cocoa
10
11 class ScreenshotDetailViewController: BridgeViewController {
12     deinit {
13         arrayController.removeObserver(self, forKeyPath: NSSelectionIndexesBinding)
14     }
15     
16     @IBOutlet var imageView: ImageView!
17     
18     override var nibName: String! {
19         return "ScreenshotDetailViewController"
20     }
21     override var contentRect: NSRect {
22         return imageView.convert(imageView.imageRect, to: nil)
23     }
24     
25     private var currentSelection: [ScreenshotInformation] = []
26     
27     override func viewDidLoad() {
28         super.viewDidLoad()
29         
30         arrayController.addObserver(self, forKeyPath: NSSelectionIndexesBinding, context: nil)
31         updateSelections()
32     }
33     
34     override func observeValue(forKeyPath keyPath: String?,
35                                of object: Any?,
36                                change: [NSKeyValueChangeKey: Any]?,
37                                context: UnsafeMutableRawPointer?) {
38         if keyPath == NSSelectionIndexesBinding {
39             updateSelections()
40             return
41         }
42         
43         super.observeValue(forKeyPath: keyPath, of: object, change: change, context: context)
44     }
45     
46     private func updateSelections() {
47         guard let selection = arrayController.selectedObjects as? [ScreenshotInformation]
48             else { return }
49         if currentSelection == selection { return }
50         imageView.images = selection.flatMap { NSImage(contentsOf: $0.url) }
51         currentSelection = selection
52     }
53 }