feature: add jsbridge support for break connection

This commit is contained in:
yicheng 2020-03-11 10:02:57 +08:00
parent 648c9737ea
commit 0e2be2dac4
2 changed files with 20 additions and 3 deletions

View File

@ -15,10 +15,13 @@ class ConnectionManager {
}
}
private static var closeMenuItem: NSMenuItem?
static func addCloseOptionMenuItem(_ menu: inout NSMenu) {
let item = NSMenuItem(title: NSLocalizedString("Auto Close Connection", comment: ""), action: #selector(optionMenuItemTap(sender:)), keyEquivalent: "")
item.target = ConnectionManager.self
menu.addItem(item)
closeMenuItem = item
updateMenuItemStatus(item)
}
@ -38,9 +41,9 @@ class ConnectionManager {
}
}
private extension ConnectionManager {
static func updateMenuItemStatus(_ item: NSMenuItem) {
item.state = enableAutoClose ? .on : .off
extension ConnectionManager {
static func updateMenuItemStatus(_ item: NSMenuItem? = closeMenuItem) {
item?.state = enableAutoClose ? .on : .off
}
@objc static func optionMenuItemTap(sender: NSMenuItem) {

View File

@ -85,6 +85,20 @@ class JsBridgeUtil {
}
}
bridge.registerHandler("getBreakConnections") { _, responseCallback in
responseCallback?(ConnectionManager.enableAutoClose)
}
bridge.registerHandler("setBreakConnections") { anydata, responseCallback in
if let enable = anydata as? Bool {
ConnectionManager.enableAutoClose = enable
ConnectionManager.updateMenuItemStatus()
responseCallback?(true)
} else {
responseCallback?(false)
}
}
bridge.registerHandler("speedTest") { anydata, responseCallback in
if let proxyName = anydata as? String {
ApiRequest.getProxyDelay(proxyName: proxyName) { delay in