-
Notifications
You must be signed in to change notification settings - Fork 895
/
Copy pathNavigationModalView.swift
122 lines (88 loc) · 3.88 KB
/
NavigationModalView.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
//
// NavigationModalView.swift
// TGUIKit
//
// Created by keepcoder on 01/11/2016.
// Copyright © 2016 Telegram. All rights reserved.
//
import Cocoa
class NavigationModalView: Control {
private var textNode:TextNode = TextNode()
private var attributeString:NSAttributedString
let action:NavigationModalAction
weak var viewController:NavigationViewController?
private var lock:Bool = false
init(action:NavigationModalAction, viewController:NavigationViewController) {
self.action = action
let attr:NSMutableAttributedString = NSMutableAttributedString()
_ = attr.append(string: action.reason, color: presentation.colors.text, font: .normal(20.0))
_ = attr.append(string: "\n")
_ = attr.append(string: action.desc, color: presentation.colors.grayText, font: .normal(.text))
self.attributeString = attr.copy() as! NSAttributedString
self.viewController = viewController
// viewController.navigationController?.lock = true
super.init()
self.autoresizingMask = [.width, .height]
set(handler: { control in
let control = control as! NavigationModalView
if !control.lock {
control.close()
}
}, for: .Click)
}
override func updateLocalizationAndTheme(theme: PresentationTheme) {
super.updateLocalizationAndTheme(theme: theme)
let attr:NSMutableAttributedString = NSMutableAttributedString()
_ = attr.append(string: action.reason, color: presentation.colors.text, font: .normal(20.0))
_ = attr.append(string: "\n")
_ = attr.append(string: action.desc, color: presentation.colors.grayText, font: .normal(.text))
self.attributeString = attr.copy() as! NSAttributedString
self.backgroundColor = .clear
needsDisplay = true
}
override func scrollWheel(with event: NSEvent) {
}
override func draw(_ layer: CALayer, in ctx: CGContext) {
ctx.setFillColor(presentation.colors.background.withAlphaComponent(0.9).cgColor)
ctx.fill(layer.frame)
super.draw(layer, in: ctx)
let node = TextNode.layoutText(maybeNode: textNode, attributeString, nil, 3, .end, NSMakeSize(frame.width - 40, frame.height), nil, false, .center)
let f = focus(node.0.size)
node.1.draw(f, in: ctx, backingScaleFactor: backingScaleFactor, backgroundColor: backgroundColor)
}
func close() {
_window?.removeAllHandlers(for: self)
self.lock = true
self.layer?.animateAlpha(from: 1, to: 0, duration: 0.2, removeOnCompletion:false, completion:{[weak self] (completed) in
self?.viewController?.removeModalAction()
})
}
override func removeFromSuperview() {
_window?.removeAllHandlers(for: self)
super.removeFromSuperview()
}
override func viewDidMoveToWindow() {
if window != nil {
self.layer?.animateAlpha(from: 0, to: 1, duration: 0.2)
self._window?.set(escape: { [weak self] _ -> KeyHandlerResult in
self?.close()
return .invoked
}, with: self, priority: .high)
self._window?.set(responder: { () -> NSResponder? in
return nil
}, with: self, priority: .modal)
} else {
// self.viewController?.navigationController?.lock = false
}
}
override func layout() {
super.layout()
self.setNeedsDisplayLayer()
}
required public init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
required public init(frame frameRect: NSRect) {
fatalError("init(frame:) has not been implemented")
}
}