-
Notifications
You must be signed in to change notification settings - Fork 10
/
OutputPrefsViewController.swift
101 lines (80 loc) · 2.89 KB
/
OutputPrefsViewController.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
//
// OutputPrefsViewController.swift
// Subler
//
// Created by Damiano Galassi on 30/11/2017.
//
import Cocoa
import MP42Foundation
class OutputPrefsViewController: NSViewController, TokenChangeObserver {
@IBOutlet var movieField: NSTokenField!
@IBOutlet var tvShowField: NSTokenField!
var moviePopover: NSPopover?
var tvShowPopover: NSPopover?
let tokenDelegate: TokenDelegate
override var nibName: NSNib.Name? {
return "OutputPrefsViewController"
}
init() {
let separators: CharacterSet = CharacterSet(charactersIn: "{}")
self.tokenDelegate = TokenDelegate(displayMenu: true,
displayString: { localizedMetadataKeyName($0.text.trimmingCharacters(in: separators)) })
super.init(nibName: nil, bundle: nil)
self.title = NSLocalizedString("Filename", comment: "")
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad() {
super.viewDidLoad()
tokenDelegate.delegate = self
movieField.delegate = tokenDelegate
tvShowField.delegate = tokenDelegate
movieField.tokenizingCharacterSet = CharacterSet(charactersIn: "/")
tvShowField.tokenizingCharacterSet = CharacterSet(charactersIn: "/")
movieField.objectValue = MetadataPrefs.movieFormatTokens
tvShowField.objectValue = MetadataPrefs.tvShowFormatTokens
}
private func save() {
MetadataPrefs.movieFormatTokens = movieField.objectValue as! [Token]
MetadataPrefs.tvShowFormatTokens = tvShowField.objectValue as! [Token]
}
// MARK: Actions
@IBAction func showMovieTokens(_ sender: NSView) {
if let popover = tvShowPopover {
popover.close()
tvShowPopover = nil
}
if let popover = moviePopover {
popover.close()
moviePopover = nil
}
else {
moviePopover = showTokensPopover(tokens: MP42Metadata.writableMetadata, view: sender)
}
}
@IBAction func showTvShowTokens(_ sender: NSView) {
if let popover = moviePopover {
popover.close()
moviePopover = nil
}
if let popover = tvShowPopover {
popover.close()
tvShowPopover = nil
}
else {
tvShowPopover = showTokensPopover(tokens: MP42Metadata.writableMetadata, view: sender)
}
}
private func showTokensPopover(tokens: [String], view: NSView) -> NSPopover {
let tokensController = TokensViewController(tokens: tokens)
let p = NSPopover()
p.contentViewController = tokensController
p.show(relativeTo: view.bounds, of: view, preferredEdge: NSRectEdge.maxY)
return p
}
// MARK: Format Token Field Delegate
func tokenDidChange(_ obj: Notification?) {
save()
}
}