NotifierUI // Fix memory and CPU issues by limiting instances.

This commit is contained in:
ShikiSuen 2022-10-01 09:05:32 +08:00
parent cb81da6581
commit 98df537d81
1 changed files with 13 additions and 11 deletions

View File

@ -42,8 +42,11 @@ public class Notifier: NSWindowController {
super.init(window: nil)
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) {
self.isNew = false
}
@ -137,8 +140,8 @@ public class Notifier: NSWindowController {
extension Notifier {
private func shiftExistingWindowPositions() {
guard let window = window, !Self.instanceStack.isEmpty else { return }
for theInstanceWindow in Self.instanceStack.compactMap(\.window) {
guard let window = window else { return }
Self.instanceStack.compactMap(\.window).forEach { theInstanceWindow in
var theOrigin = theInstanceWindow.frame
theOrigin.origin.y -= (10 + window.frame.height)
theInstanceWindow.setFrame(theOrigin, display: true)
@ -156,19 +159,18 @@ extension Notifier {
}
private func display() {
let existingInstanceArray = Self.instanceStack.compactMap(\.window)
if !existingInstanceArray.isEmpty {
existingInstanceArray.forEach {
$0.alphaValue -= 0.1
$0.contentView?.subviews.forEach { $0.alphaValue *= 0.5 }
}
Self.instanceStack.compactMap(\.window).forEach {
$0.alphaValue -= 0.1
$0.contentView?.subviews.forEach { $0.alphaValue *= 0.5 }
}
shiftExistingWindowPositions()
fadeIn()
Self.instanceStack.insert(self, at: 0)
DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
DispatchQueue.main.asyncAfter(deadline: .now() + 1.3) {
self.close()
Self.instanceStack.removeAll(where: { $0.window == nil })
if let idx = Self.instanceStack.firstIndex(where: { $0 === self }) {
Self.instanceStack.remove(at: idx)
}
}
}
}