2018-08-06 14:17:04 +08:00
|
|
|
//
|
|
|
|
// 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.
|
2018-08-06 14:17:04 +08:00
|
|
|
//
|
|
|
|
|
|
|
|
import Cocoa
|
|
|
|
|
|
|
|
extension NSUserNotificationCenter {
|
2019-10-20 13:40:50 +08:00
|
|
|
func post(title: String, info: String, identifier: String? = nil) {
|
2018-08-06 14:17:04 +08:00
|
|
|
let notification = NSUserNotification()
|
|
|
|
notification.title = title
|
|
|
|
notification.informativeText = info
|
2018-08-12 00:10:42 +08:00
|
|
|
if identifier != nil {
|
2019-10-20 13:40:50 +08:00
|
|
|
notification.userInfo = ["identifier": identifier!]
|
2018-08-12 00:10:42 +08:00
|
|
|
}
|
2019-10-20 13:40:50 +08:00
|
|
|
delegate = UserNotificationCenterDelegate.shared
|
|
|
|
deliver(notification)
|
2018-08-06 14:17:04 +08:00
|
|
|
}
|
2019-10-20 13:40:50 +08:00
|
|
|
|
2018-08-12 00:10:42 +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")
|
2018-08-12 00:26:57 +08:00
|
|
|
}
|
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: ""))
|
2018-08-12 00:10:42 +08:00
|
|
|
}
|
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)
|
2018-08-19 12:57:15 +08:00
|
|
|
}
|
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
|
|
|
}
|
2018-08-12 00:10:42 +08:00
|
|
|
}
|
|
|
|
|
2019-10-20 13:40:50 +08:00
|
|
|
class UserNotificationCenterDelegate: NSObject, NSUserNotificationCenterDelegate {
|
2018-08-12 00:10:42 +08:00
|
|
|
static let shared = UserNotificationCenterDelegate()
|
2019-10-20 13:40:50 +08:00
|
|
|
|
2018-08-12 00:10:42 +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
|
|
|
|
2018-08-12 00:10:42 +08:00
|
|
|
func userNotificationCenter(_ center: NSUserNotificationCenter, shouldPresent notification: NSUserNotification) -> Bool {
|
|
|
|
return true
|
|
|
|
}
|
2018-08-06 14:17:04 +08:00
|
|
|
}
|