forked from alvincrisuy/clashX
-
Notifications
You must be signed in to change notification settings - Fork 621
Expand file tree
/
Copy pathProxyGroupSpeedTestMenuItem.swift
More file actions
161 lines (136 loc) · 4.92 KB
/
ProxyGroupSpeedTestMenuItem.swift
File metadata and controls
161 lines (136 loc) · 4.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
//
// ProxyGroupSpeedTestMenuItem.swift
// ClashX
//
// Created by yicheng on 2019/10/15.
// Copyright © 2019 west2online. All rights reserved.
//
import Carbon
import Cocoa
class ProxyGroupSpeedTestMenuItem: NSMenuItem {
let proxyGroup: ClashProxy
let testType: TestType
init(group: ClashProxy) {
proxyGroup = group
if group.type.isAutoGroup {
testType = .reTest
} else if group.type == .select {
testType = .benchmark
} else {
testType = .unknown
}
super.init(title: NSLocalizedString("Benchmark", comment: ""), action: nil, keyEquivalent: "")
target = self
action = #selector(healthCheck)
switch testType {
case .benchmark:
view = ProxyGroupSpeedTestMenuItemView(testType.title)
case .reTest:
title = testType.title
case .unknown:
assertionFailure()
}
}
@available(*, unavailable)
required init(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
@objc func healthCheck() {
guard testType == .reTest else { return }
ApiRequest.getGroupDelay(groupName: proxyGroup.name) { _ in }
menu?.cancelTracking()
}
}
extension ProxyGroupSpeedTestMenuItem: ProxyGroupMenuHighlightDelegate {
func highlight(item: NSMenuItem?) {
(view as? ProxyGroupSpeedTestMenuItemView)?.isHighlighted = item == self
}
}
private class ProxyGroupSpeedTestMenuItemView: MenuItemBaseView {
private let label: NSTextField
init(_ title: String) {
label = NSTextField(labelWithString: title)
label.font = type(of: self).labelFont
label.sizeToFit()
let rect = NSRect(x: 0, y: 0, width: label.bounds.width + 40, height: 20)
super.init(frame: rect, autolayout: false)
addSubview(label)
label.frame = NSRect(x: 20, y: 0, width: label.bounds.width, height: 20)
label.textColor = NSColor.labelColor
}
@available(*, unavailable)
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override var cells: [NSCell?] {
return [label.cell]
}
override var labels: [NSTextField] {
return [label]
}
override func didClickView() {
startBenchmark()
}
private func startBenchmark() {
guard let group = (enclosingMenuItem as? ProxyGroupSpeedTestMenuItem)?.proxyGroup
else { return }
label.stringValue = NSLocalizedString("Testing", comment: "")
enclosingMenuItem?.isEnabled = false
setNeedsDisplay()
// Create a dispatch group to track all proxy tests
let dispatchGroup = DispatchGroup()
// Start testing each proxy individually
group.all?.forEach { proxyName in
dispatchGroup.enter()
ApiRequest.getProxyDelay(proxyName: proxyName) { delay in
defer { dispatchGroup.leave() }
guard let menu = self.enclosingMenuItem else { return }
var delayStr = NSLocalizedString("fail", comment: "")
var delayValue = 0
if delay != 0 {
delayStr = "\(delay) ms"
delayValue = delay
}
// Update UI on main thread
DispatchQueue.main.async {
// Update menu items directly
if let proxyMenu = menu.submenu {
for item in proxyMenu.items {
if let proxyItem = item as? ProxyMenuItem, proxyItem.proxyName == proxyName {
proxyItem.updateDelay(delayStr, rawValue: delayValue)
}
}
}
// Post notification for other observers
NotificationCenter.default.post(
name: .speedTestFinishForProxy,
object: nil,
userInfo: ["proxyName": proxyName,
"delay": delayStr,
"rawValue": delayValue])
}
}
}
// When all tests are complete
dispatchGroup.notify(queue: .main) { [weak self] in
guard let self = self, let menu = self.enclosingMenuItem else { return }
self.label.stringValue = menu.title
menu.isEnabled = true
self.setNeedsDisplay()
}
}
}
extension ProxyGroupSpeedTestMenuItem {
enum TestType {
case benchmark
case reTest
case unknown
var title: String {
switch self {
case .benchmark: return NSLocalizedString("Benchmark", comment: "")
case .reTest: return NSLocalizedString("ReTest", comment: "")
case .unknown: return ""
}
}
}
}