-
-
Notifications
You must be signed in to change notification settings - Fork 289
/
ConversionCompletedViewController.swift
285 lines (232 loc) · 7.88 KB
/
ConversionCompletedViewController.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
import Cocoa
import Quartz
import UserNotifications
import StoreKit
final class ConversionCompletedViewController: NSViewController {
@IBOutlet private var draggableFileWrapper: NSView!
@IBOutlet private var fileNameLabel: Label!
@IBOutlet private var fileSizeLabel: Label!
@IBOutlet private var saveAsButton: NSButton!
@IBOutlet private var shareButton: NSButton!
@IBOutlet private var copyButton: NSButton!
@IBOutlet private var openButton: NSButton!
@IBOutlet private var wrapperView: NSView!
private let draggableFile = DraggableFile()
private var conversion: GIFGenerator.Conversion!
private var gifUrl: URL!
private let tooltip = Tooltip(
identifier: "conversionCompletedTips",
text: "Go ahead and drag the thumbnail to the Finder or Safari! You can also press Space to enlarge the preview.",
showOnlyOnce: true,
maxWidth: 260
)
convenience init(conversion: GIFGenerator.Conversion, gifUrl: URL) {
self.init()
self.conversion = conversion
self.gifUrl = gifUrl
}
override func viewDidLoad() {
super.viewDidLoad()
setUpUI()
setUpDropView()
setUp()
NSApp.requestUserAttention(.informationalRequest)
if !NSApp.isActive || view.window?.isVisible == false {
let notification = UNMutableNotificationContent()
notification.title = "Conversion Completed"
notification.subtitle = conversion.sourceURL.filename
notification.sound = .default
let request = UNNotificationRequest(identifier: "conversionCompleted", content: notification, trigger: nil)
// UNUserNotificationCenter.current().add(request)
AppDelegate.shared.notificationCenter.add(request)
}
}
override func viewDidAppear() {
super.viewDidAppear()
// This is needed for Quick Look to work.
view.window?.makeFirstResponder(self)
if wrapperView.isHidden {
if !NSWorkspace.shared.accessibilityDisplayShouldReduceMotion {
draggableFile.layer?.animateScaleMove(
fromScale: 3,
fromY: view.frame.height + draggableFile.frame.size.height
)
}
wrapperView.fadeIn(duration: .seconds(0.5), delay: .seconds(0.15), completion: nil)
}
delay(.seconds(1)) { [weak self] in
guard let self else {
return
}
tooltip.show(from: draggableFile, preferredEdge: .maxY)
}
SSApp.requestReviewAfterBeingCalledThisManyTimes([5, 100, 1000])
}
private func setUpUI() {
wrapperView.isHidden = true
fileNameLabel.maximumNumberOfLines = 1
fileNameLabel.textColor = .labelColor
fileNameLabel.font = .systemFont(ofSize: 14, weight: .semibold)
fileSizeLabel.maximumNumberOfLines = 1
fileSizeLabel.textColor = .secondaryLabelColor
fileSizeLabel.font = .systemFont(ofSize: 12)
draggableFileWrapper.addSubview(draggableFile)
draggableFileWrapper.wantsLayer = true
draggableFileWrapper.layer?.masksToBounds = false
draggableFile.constrainEdgesToSuperview()
// TODO: Find a better icon for this.
openButton.image = NSImage(systemSymbolName: "plus", accessibilityDescription: "New conversion")
SSApp.runOnce(identifier: "showOpenButtonLabel") {
openButton.imagePosition = .imageLeading
openButton.title = "Open"
openButton.sizeToFit()
openButton.frame.x -= 34
}
}
private func setUp() {
draggableFile.fileUrl = gifUrl
fileNameLabel.text = gifUrl.filename
fileSizeLabel.text = gifUrl.fileSizeFormatted
copyButton.onAction = { [weak self] _ in
self?.copyGif()
}
shareButton.sendAction(on: .leftMouseDown)
shareButton.onAction = { [weak self] _ in
self?.shareGif()
}
saveAsButton.onAction = { [weak self] _ in
self?.saveGif()
}
}
private func copyGif() {
NSPasteboard.general.with {
// swiftlint:disable:next legacy_objc_type
$0.writeObjects([gifUrl as NSURL])
$0.setString(gifUrl.filenameWithoutExtension, forType: .urlName)
}
copyButton.title = "Copied!"
copyButton.isEnabled = false
SSApp.runOnce(identifier: "copyWarning") {
NSAlert.showModal(
for: copyButton.window,
title: "The GIF was copied to the clipboard.",
message: "However…",
buttonTitles: [
"Continue"
],
defaultButtonIndex: -1
)
NSAlert.showModal(
for: copyButton.window,
title: "Please read!",
message: "Many apps like Chrome and Slack do not properly handle copied animated GIFs and will paste them as non-animated PNG.\n\nInstead, drag and drop the GIF into such apps.",
defaultButtonIndex: -1
)
}
delay(.seconds(1)) { [weak self] in
guard let self else {
return
}
copyButton.title = "Copy"
copyButton.isEnabled = true
}
}
private func shareGif() {
// swiftlint:disable:next legacy_objc_type
NSSharingService.share(items: [gifUrl as NSURL], from: shareButton)
}
private func saveGif() {
let inputUrl = conversion.sourceURL
let panel = NSSavePanel()
panel.canCreateDirectories = true
panel.allowedContentTypes = [.gif]
panel.nameFieldStringValue = inputUrl.filenameWithoutExtension
panel.message = "Choose where to save the GIF"
// Prevent the default directory to be a temporary directory or read-only volume, for example, when directly dragging a screen recording into Gifski. Setting it to the downloads directory is required as otherwise it will automatically use the same directory as used in the open panel, which could be a read-only volume.
panel.directoryURL = inputUrl.directoryURL.canBeDefaultSavePanelDirectory
? inputUrl.directoryURL
: (Defaults[.previousSaveDirectory] ?? URL.downloadsDirectory)
panel.beginSheetModal(for: view.window!) { [weak self] response in
guard
let self,
response == .OK,
let outputUrl = panel.url
else {
return
}
// Give the system time to close the sheet.
DispatchQueue.main.async {
do {
try FileManager.default.copyItem(at: self.gifUrl, to: outputUrl, overwrite: true)
Defaults[.previousSaveDirectory] = outputUrl.directoryURL
} catch {
error.presentAsModalSheet(for: self.view.window)
self.saveGif()
}
}
}
}
private func setUpDropView() {
let videoDropController = VideoDropViewController(dropLabelIsHidden: true)
add(childController: videoDropController)
}
@IBAction
private func backButtonAction(_ sender: NSButton) {
// It's safe to force-unwrap as there's no scenario where it will be nil.
let viewController = AppDelegate.shared.previousEditViewController!
viewController.isConverting = false
push(viewController: viewController)
}
@IBAction
private func openButtonAction(_ sender: NSButton) {
AppDelegate.shared.mainWindowController.presentOpenPanel()
}
}
// MARK: First responders
extension ConversionCompletedViewController {
@objc
func copy(_ sender: Any) {
copyGif()
}
@objc
func saveDocumentAs(_ sender: Any) {
saveGif()
}
}
extension ConversionCompletedViewController: QLPreviewPanelDataSource {
@IBAction
private func quickLook(_ sender: Any) {
quickLookPreviewItems(nil)
}
override func quickLook(with event: NSEvent) {
quickLookPreviewItems(nil)
}
override func quickLookPreviewItems(_ sender: Any?) {
guard let panel = QLPreviewPanel.shared() else {
return
}
panel.toggle()
}
override func acceptsPreviewPanelControl(_ panel: QLPreviewPanel!) -> Bool { true }
override func beginPreviewPanelControl(_ panel: QLPreviewPanel!) {
panel.delegate = self
panel.dataSource = self
}
override func endPreviewPanelControl(_ panel: QLPreviewPanel!) {
panel.dataSource = nil
panel.delegate = nil
}
func numberOfPreviewItems(in panel: QLPreviewPanel!) -> Int { 1 }
func previewPanel(_ panel: QLPreviewPanel!, previewItemAt index: Int) -> QLPreviewItem! {
// swiftlint:disable:next legacy_objc_type
gifUrl as NSURL
}
}
extension ConversionCompletedViewController: QLPreviewPanelDelegate {
func previewPanel(_ panel: QLPreviewPanel!, sourceFrameOnScreenFor item: QLPreviewItem!) -> CGRect {
draggableFile.imageView?.boundsInScreenCoordinates ?? .zero
}
func previewPanel(_ panel: QLPreviewPanel!, transitionImageFor item: QLPreviewItem!, contentRect: UnsafeMutablePointer<CGRect>!) -> Any! {
draggableFile.image
}
}