-
Notifications
You must be signed in to change notification settings - Fork 566
Expand file tree
/
Copy pathNotificationManager+Animation.swift
More file actions
148 lines (130 loc) · 5.09 KB
/
NotificationManager+Animation.swift
File metadata and controls
148 lines (130 loc) · 5.09 KB
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
import Cocoa
extension NotificationManager {
func showWithAnimation(
notification: NotificationInstance, screen: NSScreen, timeoutSeconds: Double
) {
let screenRect = screen.visibleFrame
let finalX = screenRect.maxX - panelWidth() - Layout.rightMargin + buttonOverhang()
let y = notification.panel.frame.minY
notification.panel.setFrame(
NSRect(
x: screenRect.maxX + Layout.slideInOffset,
y: y,
width: panelWidth(),
height: panelHeight()
),
display: false
)
notification.panel.orderFrontRegardless()
notification.panel.makeKeyAndOrderFront(nil)
animate(duration: Timing.slideIn, timing: .easeOut) {
notification.panel.animator().setFrame(
NSRect(x: finalX, y: y, width: self.panelWidth(), height: self.panelHeight()),
display: true
)
notification.panel.animator().alphaValue = 1.0
} completion: {
self.refreshTrackingAreas(for: notification)
self.updateHoverForAll(atScreenPoint: NSEvent.mouseLocation)
if timeoutSeconds > 0 {
notification.startDismissTimer(timeoutSeconds: timeoutSeconds)
}
}
}
func animateExpansion(notification: NotificationInstance, isExpanded: Bool) {
let currentFrame = notification.panel.frame
let targetHeight = panelHeight(expanded: isExpanded)
let newFrame = NSRect(
x: currentFrame.minX,
y: currentFrame.minY - (targetHeight - currentFrame.height),
width: currentFrame.width,
height: targetHeight
)
guard let effectView = findEffectView(in: notification) else {
notification.isAnimating = false
return
}
if isExpanded {
animateToExpanded(notification: notification, effectView: effectView, frame: newFrame)
} else {
animateToCompact(notification: notification, frame: newFrame)
}
}
private func animateToExpanded(
notification: NotificationInstance, effectView: NSVisualEffectView, frame: NSRect
) {
notification.pauseDismissTimer()
notification.compactContentView?.isHidden = true
let expandedView = createExpandedNotificationView(notification: notification)
expandedView.translatesAutoresizingMaskIntoConstraints = false
expandedView.alphaValue = 0
notification.expandedContentView = expandedView
animate(duration: Timing.expansion, timing: .easeInEaseOut) {
notification.panel.animator().setFrame(frame, display: true)
} completion: {
effectView.addSubview(expandedView)
self.pinToEdges(expandedView, in: effectView)
self.animate(duration: Timing.fadeIn, timing: .easeOut) {
expandedView.animator().alphaValue = 1.0
} completion: {
self.finishExpansionAnimation(notification)
}
}
}
private func animateToCompact(notification: NotificationInstance, frame: NSRect) {
notification.clearExpandedCountdownLabel()
notification.expandedContentView?.removeFromSuperview()
notification.expandedContentView = nil
notification.compactContentView?.alphaValue = 0
notification.compactContentView?.isHidden = false
animate(duration: Timing.expansion, timing: .easeInEaseOut) {
notification.panel.animator().setFrame(frame, display: true)
notification.compactContentView?.animator().alphaValue = 1.0
} completion: {
if !notification.clickableView.isHovering {
notification.resumeDismissTimer()
}
self.finishExpansionAnimation(notification)
}
}
private func finishExpansionAnimation(_ notification: NotificationInstance) {
notification.isAnimating = false
notification.clickableView.updateTrackingAreas()
repositionNotifications()
}
private func findEffectView(in notification: NotificationInstance) -> NSVisualEffectView? {
notification.clickableView.subviews.first?.subviews.first as? NSVisualEffectView
}
private func pinToEdges(_ view: NSView, in container: NSView) {
NSLayoutConstraint.activate([
view.leadingAnchor.constraint(
equalTo: container.leadingAnchor, constant: Layout.expandedPaddingHorizontal),
view.trailingAnchor.constraint(
equalTo: container.trailingAnchor, constant: -Layout.expandedPaddingHorizontal),
view.topAnchor.constraint(
equalTo: container.topAnchor, constant: Layout.expandedPaddingVertical),
view.bottomAnchor.constraint(
equalTo: container.bottomAnchor, constant: -Layout.expandedPaddingVertical),
])
}
private func refreshTrackingAreas(for notification: NotificationInstance) {
DispatchQueue.main.async {
notification.clickableView.updateTrackingAreas()
notification.clickableView.window?.invalidateCursorRects(for: notification.clickableView)
notification.clickableView.window?.resetCursorRects()
}
}
func animate(
duration: TimeInterval,
timing: CAMediaTimingFunctionName,
animations: @escaping () -> Void,
completion: @escaping () -> Void
) {
NSAnimationContext.runAnimationGroup(
{ context in
context.duration = duration
context.timingFunction = CAMediaTimingFunction(name: timing)
animations()
}, completionHandler: completion)
}
}