From 5349a279ce0fc497ec78cee27e34e558daac5619 Mon Sep 17 00:00:00 2001
From: yicheng <11733500+yichengchen@users.noreply.github.com>
Date: Mon, 27 Apr 2020 23:34:07 +0800
Subject: [PATCH] improve: add should quit check
# Conflicts:
# ClashX/AppDelegate.swift
# ClashX/Info.plist
---
ClashX/AppDelegate.swift | 38 ++++++++++++++++++-
.../General/Managers/SystemProxyManager.swift | 4 +-
ClashX/Info.plist | 4 ++
ClashX/Views/ProxyItemView.swift | 2 +-
4 files changed, 44 insertions(+), 4 deletions(-)
diff --git a/ClashX/AppDelegate.swift b/ClashX/AppDelegate.swift
index 34a7445..7d5a8e2 100644
--- a/ClashX/AppDelegate.swift
+++ b/ClashX/AppDelegate.swift
@@ -112,12 +112,46 @@ class AppDelegate: NSObject, NSApplicationDelegate {
setupNetworkNotifier()
}
- func applicationWillTerminate(_ aNotification: Notification) {
+ func applicationShouldTerminate(_ sender: NSApplication) -> NSApplication.TerminateReply {
+ let group = DispatchGroup()
+ var shouldWait = false
+
if ConfigManager.shared.proxyPortAutoSet && !ConfigManager.shared.isProxySetByOtherVariable.value {
+ Logger.log("ClashX quit need clean proxy setting")
+ shouldWait = true
+ group.enter()
let port = ConfigManager.shared.currentConfig?.port ?? 0
let socketPort = ConfigManager.shared.currentConfig?.socketPort ?? 0
- SystemProxyManager.shared.disableProxy(port: port, socksPort: socketPort)
+ SystemProxyManager.shared.disableProxy(port: port, socksPort: socketPort) {
+ group.leave()
+ }
}
+
+ if !shouldWait {
+ Logger.log("ClashX quit without clean waiting")
+ return .terminateNow
+ }
+
+ statusItem.menu = nil
+
+ DispatchQueue.global(qos: .default).async {
+ let res = group.wait(timeout: .now() + 5)
+ switch res {
+ case .success:
+ Logger.log("ClashX quit after clean up finish")
+ case .timedOut:
+ Logger.log("ClashX quit after clean up timeout")
+ }
+ DispatchQueue.main.async {
+ NSApp.reply(toApplicationShouldTerminate: true)
+ }
+ }
+
+ Logger.log("ClashX quit wait for clean up")
+ return .terminateLater
+ }
+
+ func applicationWillTerminate(_ aNotification: Notification) {
UserDefaults.standard.set(0, forKey: "launch_fail_times")
}
diff --git a/ClashX/General/Managers/SystemProxyManager.swift b/ClashX/General/Managers/SystemProxyManager.swift
index 3a17d9c..0d0ff4f 100644
--- a/ClashX/General/Managers/SystemProxyManager.swift
+++ b/ClashX/General/Managers/SystemProxyManager.swift
@@ -70,7 +70,7 @@ class SystemProxyManager: NSObject {
SystemProxyManager.shared.disableProxy(port: port, socksPort: socketPort)
}
- func disableProxy(port: Int, socksPort: Int, forceDisable: Bool = false) {
+ func disableProxy(port: Int, socksPort: Int, forceDisable: Bool = false, complete: (() -> Void)? = nil) {
Logger.log("disableProxy", level: .debug)
if disableRestoreProxy || forceDisable {
@@ -78,6 +78,7 @@ class SystemProxyManager: NSObject {
if let error = error {
Logger.log("disableProxy \(error)", level: .error)
}
+ complete?()
}
return
}
@@ -86,6 +87,7 @@ class SystemProxyManager: NSObject {
if let error = error {
Logger.log("restoreProxy \(error)", level: .error)
}
+ complete?()
})
}
diff --git a/ClashX/Info.plist b/ClashX/Info.plist
index c33f38c..16f2afb 100644
--- a/ClashX/Info.plist
+++ b/ClashX/Info.plist
@@ -84,6 +84,10 @@
identifier "com.west2online.ClashX" and anchor apple generic and certificate leaf[subject.CN] = "Mac Developer: chen yicheng (96U846XGYH)" and certificate 1[field.1.2.840.113635.100.6.2.1] /* exists */
+ NSSupportsAutomaticTermination
+
+ NSSupportsSuddenTermination
+
SMPrivilegedExecutables
com.west2online.ClashX.ProxyConfigHelper
diff --git a/ClashX/Views/ProxyItemView.swift b/ClashX/Views/ProxyItemView.swift
index 28d9792..fab4655 100644
--- a/ClashX/Views/ProxyItemView.swift
+++ b/ClashX/Views/ProxyItemView.swift
@@ -30,7 +30,7 @@ class ProxyItemView: MenuItemBaseView {
delayLabel.translatesAutoresizingMaskIntoConstraints = false
nameLabel.font = type(of: self).labelFont
- delayLabel.font = NSFont.monospacedDigitSystemFont(ofSize: 10, weight: .regular)
+ delayLabel.font = NSFont.monospacedDigitSystemFont(ofSize: 10, weight: .medium)
nameLabel.alignment = .left
delayLabel.alignment = .right