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
private static var instanceStack: [Notifier] = []
private static var instanceSet: NSMutableOrderedSet = .init()
private let blankValue = ""
@discardableResult private init(_ message: String) {
currentMessage = message
let rawMessage = message.replacingOccurrences(of: "\n", with: "")
let isDuplicated: Bool = {
if let firstInstanceExisted = Self.instanceStack.first {
if let firstInstanceExisted = Self.instanceSet.firstNotifier {
return message == firstInstanceExisted.currentMessage && firstInstanceExisted.isNew
}
return false
@ -43,7 +43,12 @@ public class Notifier: NSWindowController {
return
}
// 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 {
// 0.3 false
@ -141,7 +146,7 @@ public class Notifier: NSWindowController {
extension Notifier {
private func shiftExistingWindowPositions() {
guard let window = window else { return }
Self.instanceStack.compactMap(\.window).forEach { theInstanceWindow in
Self.instanceSet.arrayOfWindows.forEach { theInstanceWindow in
var theOrigin = theInstanceWindow.frame
theOrigin.origin.y -= (10 + window.frame.height)
theInstanceWindow.setFrame(theOrigin, display: true)
@ -159,18 +164,30 @@ extension Notifier {
}
private func display() {
Self.instanceStack.compactMap(\.window).forEach {
Self.instanceSet.arrayOfWindows.forEach {
$0.alphaValue -= 0.1
$0.contentView?.subviews.forEach { $0.alphaValue *= 0.5 }
}
shiftExistingWindowPositions()
fadeIn()
Self.instanceStack.insert(self, at: 0)
Self.instanceSet.insert(self, at: 0)
DispatchQueue.main.asyncAfter(deadline: .now() + 1.3) {
self.close()
if let idx = Self.instanceStack.firstIndex(where: { $0 === self }) {
Self.instanceStack.remove(at: idx)
}
Self.instanceSet.remove(self)
}
}
}
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
}
}