-
Notifications
You must be signed in to change notification settings - Fork 895
/
Copy pathModalTouchBar.swift
80 lines (69 loc) · 2.46 KB
/
ModalTouchBar.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
//
// ModalTouchBar.swift
// TGUIKit
//
// Created by Mikhail Filimonov on 19/09/2018.
// Copyright © 2018 Telegram. All rights reserved.
//
import Cocoa
@available(OSX 10.12.2, *)
private extension NSTouchBarItem.Identifier {
static let modalOK = NSTouchBarItem.Identifier("\(Bundle.main.bundleIdentifier!).touchBar.modal.OK")
static let modalCancel = NSTouchBarItem.Identifier("\(Bundle.main.bundleIdentifier!).touchBar.modal.Cancel")
}
@available(OSX 10.12.2, *)
class ModalTouchBar: NSTouchBar, NSTouchBarDelegate {
private let interactions: ModalInteractions
private let modal:Modal
init(_ interactions: ModalInteractions, modal: Modal) {
self.interactions = interactions
self.modal = modal
super.init()
self.delegate = self
var items: [NSTouchBarItem.Identifier] = []
items.append(.flexibleSpace)
if let _ = interactions.cancelTitle {
items.append(.modalCancel)
}
items.append(.modalOK)
items.append(.flexibleSpace)
self.defaultItemIdentifiers = items
}
@objc private func modalOKAction() {
if let accept = interactions.accept {
accept()
} else {
modal.close()
}
}
@objc private func modalCancelAction() {
if let cancel = interactions.cancel {
cancel()
} else {
modal.close()
}
}
func touchBar(_ touchBar: NSTouchBar, makeItemForIdentifier identifier: NSTouchBarItem.Identifier) -> NSTouchBarItem? {
switch identifier {
case .modalOK:
let item = NSCustomTouchBarItem(identifier: identifier)
let button = NSButton(title: interactions.acceptTitle, target: self, action: #selector(modalOKAction))
button.addWidthConstraint(size: 200)
item.view = button
item.customizationLabel = button.title
return item
case .modalCancel:
let item = NSCustomTouchBarItem(identifier: identifier)
let button = NSButton(title: interactions.cancelTitle!, target: self, action: #selector(modalCancelAction))
button.addWidthConstraint(size: 200)
item.view = button
item.customizationLabel = button.title
return item
default:
return nil
}
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}