OSDN Git Service

コーディングスタイルを変更
[kcd/KCD.git] / KCD / BridgeViewController.swift
1 //
2 //  BridgeViewController.swift
3 //  KCD
4 //
5 //  Created by Hori,Masaki on 2016/12/30.
6 //  Copyright © 2016年 Hori,Masaki. All rights reserved.
7 //
8
9 import Cocoa
10
11 protocol ScreenshotSharingProvider {
12     
13     var contentRect: NSRect { get }
14 }
15
16 class BridgeViewController: NSViewController {
17     
18     @IBOutlet private(set) var arrayController: NSArrayController!
19     
20     deinit {
21         
22         arrayController.unbind(.contentArray)
23         arrayController.unbind(.sortDescriptors)
24         arrayController.unbind(.selectionIndexes)
25         arrayController.unbind(.filterPredicate)
26     }
27     
28     override var representedObject: Any? {
29         
30         didSet {
31             
32             guard let representedObject = representedObject as? ScreenshotModel else {
33                 
34                 return
35             }
36             
37             arrayController.bind(.contentArray, to: representedObject, withKeyPath: #keyPath(ScreenshotModel.screenshots))
38             arrayController.bind(.sortDescriptors, to: representedObject, withKeyPath: #keyPath(ScreenshotModel.sortDescriptors))
39             arrayController.bind(.selectionIndexes, to: representedObject, withKeyPath: #keyPath(ScreenshotModel.selectedIndexes))
40             arrayController.bind(.filterPredicate, to: representedObject, withKeyPath: #keyPath(ScreenshotModel.filterPredicate))
41         }
42     }
43     
44     override func prepare(for segue: NSStoryboardSegue, sender: Any?) {
45         
46         (sender as? NSControl)?.action = nil
47         (segue.destinationController as? NSViewController)?.representedObject = representedObject
48     }
49 }
50
51 extension BridgeViewController: NSSharingServicePickerDelegate, ScreenshotSharingProvider {
52     
53     @objc var contentRect: NSRect {
54         
55         fatalError("Must implemente")
56     }
57     
58     private var appendKanColleTag: Bool {
59         
60         return UserDefaults.standard[.appendKanColleTag]
61     }
62     
63     private var tagString: String? {
64         
65         let tag = LocalizedStrings.tweetTag.string
66         if tag == "" {
67             
68             return ""
69         }
70         
71         return "#\(tag)"
72     }
73     
74     @IBAction func share(_ sender: AnyObject?) {
75         
76         guard let view = sender as? NSView else {
77             
78             return
79         }
80         
81         let picker = NSSharingServicePicker(items: itemsForShareingServicePicker())
82         picker.delegate = self
83         picker.show(relativeTo: view.bounds, of: view, preferredEdge: .minX)
84     }
85     
86     private func itemsForShareingServicePicker() -> [AnyObject] {
87         
88         guard let informations = arrayController.selectedObjects as? [ScreenshotInformation] else {
89             
90             return []
91         }
92         
93         let images: [NSImage] = informations.compactMap { NSImage(contentsOf: $0.url) }
94         if !appendKanColleTag {
95             
96             return images
97         }
98         if let tag = tagString {
99             
100             return ["\n\(tag)" as AnyObject] + images
101         }
102         
103         return images
104     }
105 }
106
107 extension BridgeViewController: NSSharingServiceDelegate {
108     
109     func sharingServicePicker(_ sharingServicePicker: NSSharingServicePicker,
110                               delegateFor sharingService: NSSharingService) -> NSSharingServiceDelegate? {
111         
112         return self
113     }
114     
115     func sharingService(_ sharingService: NSSharingService, sourceFrameOnScreenForShareItem item: Any) -> NSRect {
116         
117         return self.view.window?.convertToScreen(contentRect) ?? .zero
118     }
119     
120     func sharingService(_ sharingService: NSSharingService,
121                         transitionImageForShareItem item: Any,
122                         contentRect: UnsafeMutablePointer<NSRect>) -> NSImage? {
123         
124         return item as? NSImage
125     }
126     
127     func sharingService(_ sharingService: NSSharingService,
128                         sourceWindowForShareItems items: [Any],
129                         sharingContentScope: UnsafeMutablePointer<NSSharingService.SharingContentScope>) -> NSWindow? {
130         
131         return view.window
132     }
133 }
134
135 @available(OSX 10.12.2, *)
136 extension BridgeViewController: NSSharingServicePickerTouchBarItemDelegate {
137     
138     func items(for pickerTouchBarItem: NSSharingServicePickerTouchBarItem) -> [Any] {
139         
140         return itemsForShareingServicePicker()
141     }
142 }