ClashX.Meta/ClashX/Extensions/NSUserNotificationCenter+Ex...

74 lines
2.6 KiB
Swift
Raw Normal View History

//
// NSUserNotificationCenter+Extension.swift
// ClashX
//
// Created by CYC on 2018/8/6.
2018-08-08 13:47:38 +08:00
// Copyright © 2018 yichengchen. All rights reserved.
//
import Cocoa
extension NSUserNotificationCenter {
2019-10-20 13:40:50 +08:00
func post(title: String, info: String, identifier: String? = nil) {
let notification = NSUserNotification()
notification.title = title
notification.informativeText = info
if identifier != nil {
2019-10-20 13:40:50 +08:00
notification.userInfo = ["identifier": identifier!]
}
2019-10-20 13:40:50 +08:00
delegate = UserNotificationCenterDelegate.shared
deliver(notification)
}
2019-10-20 13:40:50 +08:00
func postConfigFileChangeDetectionNotice() {
2019-10-20 13:40:50 +08:00
post(title: NSLocalizedString("Config file have been changed", comment: ""),
info: NSLocalizedString("Tap to reload config", comment: ""),
identifier: "postConfigFileChangeDetectionNotice")
}
2019-10-20 13:40:50 +08:00
func postStreamApiConnectFail(api: String) {
post(title: "\(api) api connect error!",
info: NSLocalizedString("Use reload config to try reconnect.", comment: ""))
}
2019-10-20 13:40:50 +08:00
func postConfigErrorNotice(msg: String) {
2019-07-28 12:39:49 +08:00
let configName = ConfigManager.selectConfigName.count > 0 ? "\(ConfigManager.selectConfigName).yaml" : ""
2019-10-20 13:40:50 +08:00
let message = "\(configName): \(msg)"
post(title: NSLocalizedString("Config loading Fail!", comment: ""), info: message)
}
2019-10-20 13:40:50 +08:00
2019-03-01 17:39:17 +08:00
func postSpeedTestBeginNotice() {
2019-10-20 13:40:50 +08:00
post(title: NSLocalizedString("Benchmark", comment: ""),
info: NSLocalizedString("Benchmark has begun, please wait.", comment: ""))
2019-03-01 17:39:17 +08:00
}
2019-10-20 13:40:50 +08:00
2019-03-01 17:39:17 +08:00
func postSpeedTestingNotice() {
2019-10-20 13:40:50 +08:00
post(title: NSLocalizedString("Benchmark", comment: ""),
info: NSLocalizedString("Benchmark is processing, please wait.", comment: ""))
2019-03-01 17:39:17 +08:00
}
2019-10-20 13:40:50 +08:00
2019-02-19 09:45:23 +08:00
func postSpeedTestFinishNotice() {
2019-10-20 13:40:50 +08:00
post(title: NSLocalizedString("Benchmark", comment: ""),
info: NSLocalizedString("Benchmark Finished!", comment: ""))
2019-02-19 09:45:23 +08:00
}
}
2019-10-20 13:40:50 +08:00
class UserNotificationCenterDelegate: NSObject, NSUserNotificationCenterDelegate {
static let shared = UserNotificationCenterDelegate()
2019-10-20 13:40:50 +08:00
func userNotificationCenter(_ center: NSUserNotificationCenter, didActivate notification: NSUserNotification) {
switch notification.userInfo?["identifier"] as? String {
case "postConfigFileChangeDetectionNotice":
NotificationCenter.default.post(Notification(name: kShouldUpDateConfig))
center.removeAllDeliveredNotifications()
default:
break
}
}
2019-10-20 13:40:50 +08:00
func userNotificationCenter(_ center: NSUserNotificationCenter, shouldPresent notification: NSUserNotification) -> Bool {
return true
}
}