2019-10-16 22:46:03 +08:00
|
|
|
//
|
|
|
|
// ProxyGroupSpeedTestMenuItem.swift
|
|
|
|
// ClashX
|
|
|
|
//
|
|
|
|
// Created by yicheng on 2019/10/15.
|
|
|
|
// Copyright © 2019 west2online. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Cocoa
|
|
|
|
|
|
|
|
class ProxyGroupSpeedTestMenuItem: NSMenuItem {
|
|
|
|
var proxyGroup: ClashProxy
|
2019-10-17 17:07:47 +08:00
|
|
|
init(group:ClashProxy) {
|
2019-10-16 22:46:03 +08:00
|
|
|
proxyGroup = group
|
2019-10-19 11:44:39 +08:00
|
|
|
super.init(title: NSLocalizedString("Benchmark", comment: ""), action: nil, keyEquivalent: "")
|
|
|
|
view = ProxyGroupSpeedTestMenuItemView(title: title)
|
2019-10-16 22:46:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
required init(coder: NSCoder) {
|
|
|
|
fatalError("init(coder:) has not been implemented")
|
|
|
|
}
|
|
|
|
}
|
2019-10-17 17:07:47 +08:00
|
|
|
|
|
|
|
fileprivate class ProxyGroupSpeedTestMenuItemView: NSView {
|
|
|
|
let label: NSTextField
|
2019-10-19 11:44:39 +08:00
|
|
|
let font = NSFont.menuFont(ofSize: 14)
|
|
|
|
init(title: String) {
|
|
|
|
label = NSTextField(labelWithString: title)
|
|
|
|
label.font = font
|
|
|
|
label.sizeToFit()
|
|
|
|
super.init(frame: NSRect(x: 0, y: 0, width: label.bounds.width + 40, height: 20))
|
2019-10-17 17:07:47 +08:00
|
|
|
translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
heightAnchor.constraint(equalToConstant: 20).isActive = true
|
|
|
|
addSubview(label)
|
2019-10-19 11:44:39 +08:00
|
|
|
label.frame = NSRect(x: 20, y: 0, width: label.bounds.width, height: 20)
|
|
|
|
|
2019-10-17 17:07:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
required init?(coder: NSCoder) {
|
|
|
|
fatalError("init(coder:) has not been implemented")
|
|
|
|
}
|
|
|
|
|
|
|
|
private func startBenchmark() {
|
|
|
|
guard let group = (enclosingMenuItem as? ProxyGroupSpeedTestMenuItem)?.proxyGroup else {return}
|
2019-10-20 12:58:14 +08:00
|
|
|
|
|
|
|
let testGroup = DispatchGroup()
|
|
|
|
label.stringValue = NSLocalizedString("Testing", comment: "")
|
|
|
|
enclosingMenuItem?.isEnabled = false
|
2019-10-17 17:07:47 +08:00
|
|
|
for proxyName in group.speedtestAble {
|
2019-10-20 12:58:14 +08:00
|
|
|
testGroup.enter()
|
2019-10-17 17:07:47 +08:00
|
|
|
ApiRequest.getProxyDelay(proxyName: proxyName) { delay in
|
|
|
|
let delayStr = delay == 0 ? "fail" : "\(delay) ms"
|
|
|
|
NotificationCenter.default.post(name: kSpeedTestFinishForProxy,
|
|
|
|
object: nil,
|
|
|
|
userInfo: ["proxyName": proxyName,"delay": delayStr])
|
2019-10-20 12:58:14 +08:00
|
|
|
testGroup.leave()
|
2019-10-17 17:07:47 +08:00
|
|
|
}
|
|
|
|
}
|
2019-10-20 12:58:14 +08:00
|
|
|
|
|
|
|
testGroup.notify(queue: .main) {
|
|
|
|
[weak self] in
|
|
|
|
guard let self = self, let menu = self.enclosingMenuItem else {return}
|
|
|
|
self.label.stringValue = menu.title
|
|
|
|
self.label.textColor = NSColor.labelColor
|
|
|
|
menu.isEnabled = true
|
|
|
|
}
|
2019-10-17 17:07:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
override func mouseUp(with event: NSEvent) {
|
|
|
|
startBenchmark()
|
|
|
|
}
|
|
|
|
|
|
|
|
override func draw(_ dirtyRect: NSRect) {
|
|
|
|
super.draw(dirtyRect)
|
|
|
|
guard let menu = enclosingMenuItem else {return}
|
|
|
|
if menu.isHighlighted {
|
|
|
|
NSColor.selectedMenuItemColor.setFill()
|
|
|
|
label.textColor = NSColor.white
|
|
|
|
} else {
|
|
|
|
NSColor.clear.setFill()
|
2019-10-20 12:58:14 +08:00
|
|
|
if enclosingMenuItem?.isEnabled ?? true {
|
|
|
|
label.textColor = NSColor.labelColor
|
|
|
|
} else {
|
|
|
|
label.textColor = NSColor.gray
|
|
|
|
}
|
2019-10-17 17:07:47 +08:00
|
|
|
}
|
|
|
|
dirtyRect.fill()
|
|
|
|
}
|
|
|
|
}
|