NotifierUI // Use extended NSOrderedSet in lieu of array.
This commit is contained in:
parent
98df537d81
commit
5b0c80db1b
|
@ -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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue