NotifierUI // Use extended NSOrderedSet in lieu of array.

This commit is contained in:
ShikiSuen 2022-10-01 10:42:53 +08:00
parent 98df537d81
commit 5b0c80db1b
1 changed files with 26 additions and 9 deletions

View File

@ -26,14 +26,14 @@ public class Notifier: NSWindowController {
// MARK: - Private Declarations // MARK: - Private Declarations
private static var instanceStack: [Notifier] = [] private static var instanceSet: NSMutableOrderedSet = .init()
private let blankValue = "" private let blankValue = ""
@discardableResult private init(_ message: String) { @discardableResult private init(_ message: String) {
currentMessage = message currentMessage = message
let rawMessage = message.replacingOccurrences(of: "\n", with: "") let rawMessage = message.replacingOccurrences(of: "\n", with: "")
let isDuplicated: Bool = { let isDuplicated: Bool = {
if let firstInstanceExisted = Self.instanceStack.first { if let firstInstanceExisted = Self.instanceSet.firstNotifier {
return message == firstInstanceExisted.currentMessage && firstInstanceExisted.isNew return message == firstInstanceExisted.currentMessage && firstInstanceExisted.isNew
} }
return false return false
@ -43,7 +43,12 @@ public class Notifier: NSWindowController {
return return
} }
// Swift // Swift
while Self.instanceStack.count > 3 { Self.instanceStack.removeLast().close() } while Self.instanceSet.count > 3 {
if let instanceToRemove = Self.instanceSet.lastNotifier {
instanceToRemove.close()
Self.instanceSet.remove(instanceToRemove)
}
}
// //
defer { defer {
// 0.3 false // 0.3 false
@ -141,7 +146,7 @@ public class Notifier: NSWindowController {
extension Notifier { extension Notifier {
private func shiftExistingWindowPositions() { private func shiftExistingWindowPositions() {
guard let window = window else { return } guard let window = window else { return }
Self.instanceStack.compactMap(\.window).forEach { theInstanceWindow in Self.instanceSet.arrayOfWindows.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)
@ -159,18 +164,30 @@ extension Notifier {
} }
private func display() { private func display() {
Self.instanceStack.compactMap(\.window).forEach { Self.instanceSet.arrayOfWindows.forEach {
$0.alphaValue -= 0.1 $0.alphaValue -= 0.1
$0.contentView?.subviews.forEach { $0.alphaValue *= 0.5 } $0.contentView?.subviews.forEach { $0.alphaValue *= 0.5 }
} }
shiftExistingWindowPositions() shiftExistingWindowPositions()
fadeIn() fadeIn()
Self.instanceStack.insert(self, at: 0) Self.instanceSet.insert(self, at: 0)
DispatchQueue.main.asyncAfter(deadline: .now() + 1.3) { DispatchQueue.main.asyncAfter(deadline: .now() + 1.3) {
self.close() self.close()
if let idx = Self.instanceStack.firstIndex(where: { $0 === self }) { Self.instanceSet.remove(self)
Self.instanceStack.remove(at: idx)
}
} }
} }
} }
extension NSMutableOrderedSet {
fileprivate var arrayOfWindows: [NSWindow] { compactMap { ($0 as? Notifier)?.window } }
fileprivate var firstNotifier: Notifier? {
for neta in self { if let result = neta as? Notifier { return result } }
return nil
}
fileprivate var lastNotifier: Notifier? {
for neta in reversed { if let result = neta as? Notifier { return result } }
return nil
}
}