OSDN Git Service

Swift4.1に更新
[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             guard let representedObject = representedObject as? ScreenshotModel else { return }
32             
33             arrayController.bind(.contentArray, to: representedObject, withKeyPath: #keyPath(ScreenshotModel.screenshots))
34             arrayController.bind(.sortDescriptors, to: representedObject, withKeyPath: #keyPath(ScreenshotModel.sortDescriptors))
35             arrayController.bind(.selectionIndexes, to: representedObject, withKeyPath: #keyPath(ScreenshotModel.selectedIndexes))
36             arrayController.bind(.filterPredicate, to: representedObject, withKeyPath: #keyPath(ScreenshotModel.filterPredicate))
37         }
38     }
39     
40     override func prepare(for segue: NSStoryboardSegue, sender: Any?) {
41         
42         (sender as? NSControl)?.action = nil
43         (segue.destinationController as? NSViewController)?.representedObject = representedObject
44     }
45 }
46
47 extension BridgeViewController: NSSharingServicePickerDelegate, ScreenshotSharingProvider {
48     
49     @objc var contentRect: NSRect {
50         
51         fatalError("Must implemente")
52     }
53     
54     private var appendKanColleTag: Bool {
55         
56         return UserDefaults.standard[.appendKanColleTag]
57     }
58     
59     private var tagString: String? {
60         
61         let tag = LocalizedStrings.tweetTag.string
62         if tag == "" { return "" }
63         
64         return "#\(tag)"
65     }
66     
67     @IBAction func share(_ sender: AnyObject?) {
68         
69         guard let view = sender as? NSView else { return }
70         
71         let picker = NSSharingServicePicker(items: itemsForShareingServicePicker())
72         picker.delegate = self
73         picker.show(relativeTo: view.bounds, of: view, preferredEdge: .minX)
74     }
75     
76     private func itemsForShareingServicePicker() -> [AnyObject] {
77         
78         guard let informations = arrayController.selectedObjects as? [ScreenshotInformation] else { return [] }
79         
80         let images: [NSImage] = informations.compactMap { NSImage(contentsOf: $0.url) }
81         if !appendKanColleTag { return images }
82         if let tag = tagString {
83             
84             return ["\n\(tag)" as AnyObject] + images
85         }
86         
87         return images
88     }
89 }
90
91 extension BridgeViewController: NSSharingServiceDelegate {
92     
93     func sharingServicePicker(_ sharingServicePicker: NSSharingServicePicker,
94                               delegateFor sharingService: NSSharingService) -> NSSharingServiceDelegate? {
95         
96         return self
97     }
98     
99     func sharingService(_ sharingService: NSSharingService, sourceFrameOnScreenForShareItem item: Any) -> NSRect {
100         
101         return self.view.window?.convertToScreen(contentRect) ?? .zero
102     }
103     
104     func sharingService(_ sharingService: NSSharingService,
105                         transitionImageForShareItem item: Any,
106                         contentRect: UnsafeMutablePointer<NSRect>) -> NSImage? {
107         
108         return item as? NSImage
109     }
110     
111     func sharingService(_ sharingService: NSSharingService,
112                         sourceWindowForShareItems items: [Any],
113                         sharingContentScope: UnsafeMutablePointer<NSSharingService.SharingContentScope>) -> NSWindow? {
114         
115         return view.window
116     }
117 }
118
119 @available(OSX 10.12.2, *)
120 extension BridgeViewController: NSSharingServicePickerTouchBarItemDelegate {
121     
122     func items(for pickerTouchBarItem: NSSharingServicePickerTouchBarItem) -> [Any] {
123         
124         return itemsForShareingServicePicker()
125     }
126 }