OSDN Git Service

guard の書き方を統一した
[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 final class ScreenshotDetailViewController: BridgeViewController {
12     
13     deinit {
14         
15         arrayController.removeObserver(self, forKeyPath: NSBindingName.selectionIndexes.rawValue)
16     }
17     
18     @IBOutlet var imageView: ImageView!
19     
20     override var nibName: NSNib.Name {
21         
22         return .nibName(instanceOf: self)
23     }
24     
25     override var contentRect: NSRect {
26         
27         return imageView.convert(imageView.imageRect, to: nil)
28     }
29     
30     private var currentSelection: [ScreenshotInformation] = []
31     
32     override func viewDidLoad() {
33         
34         super.viewDidLoad()
35         
36         arrayController.addObserver(self, forKeyPath: NSBindingName.selectionIndexes.rawValue, context: nil)
37         updateSelections()
38     }
39     
40     override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey: Any]?, context: UnsafeMutableRawPointer?) {
41         
42         if keyPath == NSBindingName.selectionIndexes.rawValue {
43             
44             updateSelections()
45             
46             return
47         }
48         
49         super.observeValue(forKeyPath: keyPath, of: object, change: change, context: context)
50     }
51     
52     private func updateSelections() {
53         
54         guard let selection = arrayController.selectedObjects as? [ScreenshotInformation] else { return }
55         
56         if currentSelection == selection { return }
57         
58         imageView.images = selection.flatMap { NSImage(contentsOf: $0.url) }
59         currentSelection = selection
60     }
61 }