NotifierUI // Fix memory and CPU issues by limiting instances.
This commit is contained in:
parent
cb81da6581
commit
98df537d81
|
@ -42,8 +42,11 @@ public class Notifier: NSWindowController {
|
||||||
super.init(window: nil)
|
super.init(window: nil)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
// 剔除溢出的副本,讓 Swift 自動回收之。
|
||||||
|
while Self.instanceStack.count > 3 { Self.instanceStack.removeLast().close() }
|
||||||
// 正式進入處理環節。
|
// 正式進入處理環節。
|
||||||
defer { // 先讓新通知標記自此開始過 0.3 秒自動變為 false。
|
defer {
|
||||||
|
// 先讓新通知標記自此開始過 0.3 秒自動變為 false。
|
||||||
DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
|
DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
|
||||||
self.isNew = false
|
self.isNew = false
|
||||||
}
|
}
|
||||||
|
@ -137,8 +140,8 @@ public class Notifier: NSWindowController {
|
||||||
|
|
||||||
extension Notifier {
|
extension Notifier {
|
||||||
private func shiftExistingWindowPositions() {
|
private func shiftExistingWindowPositions() {
|
||||||
guard let window = window, !Self.instanceStack.isEmpty else { return }
|
guard let window = window else { return }
|
||||||
for theInstanceWindow in Self.instanceStack.compactMap(\.window) {
|
Self.instanceStack.compactMap(\.window).forEach { theInstanceWindow in
|
||||||
var theOrigin = theInstanceWindow.frame
|
var theOrigin = theInstanceWindow.frame
|
||||||
theOrigin.origin.y -= (10 + window.frame.height)
|
theOrigin.origin.y -= (10 + window.frame.height)
|
||||||
theInstanceWindow.setFrame(theOrigin, display: true)
|
theInstanceWindow.setFrame(theOrigin, display: true)
|
||||||
|
@ -156,19 +159,18 @@ extension Notifier {
|
||||||
}
|
}
|
||||||
|
|
||||||
private func display() {
|
private func display() {
|
||||||
let existingInstanceArray = Self.instanceStack.compactMap(\.window)
|
Self.instanceStack.compactMap(\.window).forEach {
|
||||||
if !existingInstanceArray.isEmpty {
|
$0.alphaValue -= 0.1
|
||||||
existingInstanceArray.forEach {
|
$0.contentView?.subviews.forEach { $0.alphaValue *= 0.5 }
|
||||||
$0.alphaValue -= 0.1
|
|
||||||
$0.contentView?.subviews.forEach { $0.alphaValue *= 0.5 }
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
shiftExistingWindowPositions()
|
shiftExistingWindowPositions()
|
||||||
fadeIn()
|
fadeIn()
|
||||||
Self.instanceStack.insert(self, at: 0)
|
Self.instanceStack.insert(self, at: 0)
|
||||||
DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
|
DispatchQueue.main.asyncAfter(deadline: .now() + 1.3) {
|
||||||
self.close()
|
self.close()
|
||||||
Self.instanceStack.removeAll(where: { $0.window == nil })
|
if let idx = Self.instanceStack.firstIndex(where: { $0 === self }) {
|
||||||
|
Self.instanceStack.remove(at: idx)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue