ClashX.Meta/ClashX/Views/ProxyGroupSpeedTestMenuItem...

119 lines
3.5 KiB
Swift
Raw Normal View History

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 Carbon
2019-10-16 22:46:03 +08:00
import Cocoa
class ProxyGroupSpeedTestMenuItem: NSMenuItem {
var proxyGroup: ClashProxy
2019-10-31 15:38:18 +08:00
var testType: TestType
2019-10-20 13:40:50 +08:00
init(group: ClashProxy) {
2019-10-16 22:46:03 +08:00
proxyGroup = group
2019-10-28 15:02:34 +08:00
switch group.type {
case .urltest, .fallback:
2019-10-31 15:38:18 +08:00
testType = .reTest
2019-10-28 15:02:34 +08:00
case .select:
2019-10-31 15:38:18 +08:00
testType = .benchmark
2019-10-28 15:02:34 +08:00
default:
2019-10-31 15:38:18 +08:00
testType = .unknown
}
super.init(title: NSLocalizedString("Benchmark", comment: ""), action: nil, keyEquivalent: "")
switch testType {
case .benchmark:
view = ProxyGroupSpeedTestMenuItemView(testType.title)
case .reTest:
title = testType.title
case .unknown:
2019-10-28 15:02:34 +08:00
assertionFailure()
}
2019-10-16 22:46:03 +08:00
}
2019-10-20 13:40:50 +08:00
2019-10-16 22:46:03 +08:00
required init(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
2019-11-02 00:04:31 +08:00
fileprivate class ProxyGroupSpeedTestMenuItemView: MenuItemBaseView {
private let label: NSTextField
2019-10-28 15:02:34 +08:00
2019-10-31 15:38:18 +08:00
init(_ title: String) {
label = NSTextField(labelWithString: title)
2019-11-02 13:27:57 +08:00
label.font = type(of: self).labelFont
label.sizeToFit()
2019-11-02 11:55:18 +08:00
let rect = NSRect(x: 0, y: 0, width: label.bounds.width + 40, height: 20)
2019-11-02 00:04:31 +08:00
super.init(frame: rect, handleClick: true, autolayout: false)
addSubview(label)
label.frame = NSRect(x: 20, y: 0, width: label.bounds.width, height: 20)
2019-11-02 00:04:31 +08:00
label.textColor = NSColor.labelColor
}
2019-10-20 13:40:50 +08:00
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
2019-11-02 11:55:18 +08:00
override var labels: [NSTextField] {
return [label]
}
2019-11-02 00:04:31 +08:00
override func didClickView() {
startBenchmark()
}
2019-10-20 13:40:50 +08:00
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
setNeedsDisplay(bounds)
for proxyName in group.speedtestAble {
2019-10-20 12:58:14 +08:00
testGroup.enter()
ApiRequest.getProxyDelay(proxyName: proxyName) { delay in
let delayStr = delay == 0 ? "fail" : "\(delay) ms"
NotificationCenter.default.post(name: kSpeedTestFinishForProxy,
object: nil,
2019-10-20 13:40:50 +08:00
userInfo: ["proxyName": proxyName, "delay": delayStr])
2019-10-20 12:58:14 +08:00
testGroup.leave()
}
}
2019-10-20 13:40:50 +08:00
2019-10-20 12:58:14 +08:00
testGroup.notify(queue: .main) {
[weak self] in
2019-10-20 13:40:50 +08:00
guard let self = self, let menu = self.enclosingMenuItem else { return }
2019-10-20 12:58:14 +08:00
self.label.stringValue = menu.title
menu.isEnabled = true
2019-11-02 00:04:31 +08:00
self.setNeedsDisplay()
2019-10-20 12:58:14 +08:00
}
}
2019-11-02 11:55:18 +08:00
override func draw(_ dirtyRect: NSRect) {
super.draw(dirtyRect)
2019-11-02 00:04:31 +08:00
label.textColor = (enclosingMenuItem?.isEnabled ?? true) ? NSColor.labelColor : NSColor.placeholderTextColor
updateBackground(label)
}
}
2019-10-28 15:02:34 +08:00
2019-10-31 15:38:18 +08:00
extension ProxyGroupSpeedTestMenuItem {
2019-10-28 15:02:34 +08:00
enum TestType {
case benchmark
case reTest
2019-10-31 15:38:18 +08:00
case unknown
2019-10-28 15:02:34 +08:00
var title: String {
switch self {
case .benchmark: return NSLocalizedString("Benchmark", comment: "")
case .reTest: return NSLocalizedString("ReTest", comment: "")
2019-10-31 15:38:18 +08:00
case .unknown: return ""
2019-10-28 15:02:34 +08:00
}
}
}
}