Skip to content

Commit 7f1356c

Browse files
committed
Use popover instead of menu
1 parent fd361b1 commit 7f1356c

File tree

5 files changed

+329
-118
lines changed

5 files changed

+329
-118
lines changed

NetSpeedMonitor.xcodeproj/project.pbxproj

+6
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
370BAA1322341CA400243B91 /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 370BAA1222341CA400243B91 /* ViewController.swift */; };
1212
370BAA1522341CA500243B91 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 370BAA1422341CA500243B91 /* Assets.xcassets */; };
1313
370BAA1822341CA500243B91 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 370BAA1622341CA500243B91 /* Main.storyboard */; };
14+
373EB28E2404D94F0042B13C /* NetSpeedViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 373EB28D2404D94F0042B13C /* NetSpeedViewController.swift */; };
1415
/* End PBXBuildFile section */
1516

1617
/* Begin PBXFileReference section */
@@ -21,6 +22,7 @@
2122
370BAA1722341CA500243B91 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = "<group>"; };
2223
370BAA1922341CA500243B91 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
2324
370BAA1A22341CA500243B91 /* NetSpeedMonitor.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = NetSpeedMonitor.entitlements; sourceTree = "<group>"; };
25+
373EB28D2404D94F0042B13C /* NetSpeedViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NetSpeedViewController.swift; sourceTree = "<group>"; };
2426
/* End PBXFileReference section */
2527

2628
/* Begin PBXFrameworksBuildPhase section */
@@ -57,6 +59,7 @@
5759
370BAA1222341CA400243B91 /* ViewController.swift */,
5860
370BAA1422341CA500243B91 /* Assets.xcassets */,
5961
370BAA1622341CA500243B91 /* Main.storyboard */,
62+
373EB28D2404D94F0042B13C /* NetSpeedViewController.swift */,
6063
370BAA1922341CA500243B91 /* Info.plist */,
6164
370BAA1A22341CA500243B91 /* NetSpeedMonitor.entitlements */,
6265
);
@@ -141,6 +144,7 @@
141144
files = (
142145
370BAA1322341CA400243B91 /* ViewController.swift in Sources */,
143146
370BAA1122341CA400243B91 /* AppDelegate.swift in Sources */,
147+
373EB28E2404D94F0042B13C /* NetSpeedViewController.swift in Sources */,
144148
);
145149
runOnlyForDeploymentPostprocessing = 0;
146150
};
@@ -285,6 +289,7 @@
285289
"$(inherited)",
286290
"@executable_path/../Frameworks",
287291
);
292+
MARKETING_VERSION = 1.1;
288293
PRODUCT_BUNDLE_IDENTIFIER = elegracer.NetSpeedMonitor;
289294
PRODUCT_NAME = "$(TARGET_NAME)";
290295
SWIFT_VERSION = 5.0;
@@ -303,6 +308,7 @@
303308
"$(inherited)",
304309
"@executable_path/../Frameworks",
305310
);
311+
MARKETING_VERSION = 1.1;
306312
PRODUCT_BUNDLE_IDENTIFIER = elegracer.NetSpeedMonitor;
307313
PRODUCT_NAME = "$(TARGET_NAME)";
308314
SWIFT_VERSION = 5.0;

NetSpeedMonitor/AppDelegate.swift

+53-60
Original file line numberDiff line numberDiff line change
@@ -11,32 +11,18 @@ import Cocoa
1111
@NSApplicationMain
1212
class AppDelegate: NSObject, NSApplicationDelegate {
1313

14-
@IBOutlet var menu: NSMenu!
15-
@IBOutlet var quitMenuItem: NSMenuItem!
16-
@IBOutlet var item1: NSMenuItem!
17-
@IBOutlet var item2: NSMenuItem!
18-
@IBOutlet var item3: NSMenuItem!
19-
@IBOutlet var item4: NSMenuItem!
20-
@IBOutlet var item5: NSMenuItem!
21-
@IBOutlet var item6: NSMenuItem!
22-
@IBOutlet var item7: NSMenuItem!
23-
@IBOutlet var item8: NSMenuItem!
24-
@IBOutlet var item9: NSMenuItem!
25-
@IBOutlet var item10: NSMenuItem!
26-
27-
lazy var menuItems: [NSMenuItem] = [
28-
item1, item2, item3, item4, item5, item6, item7, item8, item9, item10
29-
]
30-
14+
let popover = NSPopover()
15+
let netspeedViewController = NetSpeedViewController.freshController()
16+
3117
var processSpeeds: [(name: String, download: Double, upload: Double)] = []
32-
18+
3319
var uploadSpeed: Double = 0.0
3420
var downloadSpeed: Double = 0.0
3521
var uploadMetric: String = "KB"
3622
var downloadMetric: String = "KB"
3723
var statusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength)
3824
var timer: Timer!
39-
25+
4026
var statusBarTextAttributes : [NSAttributedString.Key : Any] {
4127
let paragraphStyle = NSMutableParagraphStyle()
4228
paragraphStyle.maximumLineHeight = 10
@@ -55,65 +41,35 @@ class AppDelegate: NSObject, NSApplicationDelegate {
5541
NSAttributedString.Key.paragraphStyle: paragraphStyle
5642
] as [NSAttributedString.Key : Any]
5743
}
58-
44+
5945
func updateSpeed() {
60-
downloadSpeed = 0.0
61-
uploadSpeed = 0.0
62-
for speed in processSpeeds {
63-
downloadSpeed += speed.download
64-
uploadSpeed += speed.upload
65-
}
66-
downloadMetric = "KB"
67-
if (downloadSpeed > 1024.0) {
68-
downloadSpeed /= 1024.0
69-
downloadMetric = "MB"
70-
}
71-
uploadMetric = "KB"
72-
if (uploadSpeed > 1024.0) {
73-
uploadSpeed /= 1024.0
74-
uploadMetric = "MB"
75-
}
76-
7746
if let button = statusItem.button {
7847
button.attributedTitle = NSAttributedString(string: "\n\(String(format: "%7.2lf", uploadSpeed)) \(uploadMetric)/s ↑\n\(String(format: "%7.2lf", downloadSpeed)) \(downloadMetric)/s ↓", attributes: statusBarTextAttributes)
7948
}
80-
for i in 0..<menuItems.count {
81-
downloadMetric = "KB"
82-
if (processSpeeds[i].download > 1024.0) {
83-
processSpeeds[i].download /= 1024.0
84-
downloadMetric = "MB"
85-
}
86-
uploadMetric = "KB"
87-
if (processSpeeds[i].upload > 1024.0) {
88-
processSpeeds[i].upload /= 1024.0
89-
uploadMetric = "MB"
90-
}
91-
menuItems[i].isHidden = false
92-
menuItems[i].attributedTitle = NSAttributedString(string: "\(processSpeeds[i].name) \(String(format: "%7.2lf", processSpeeds[i].download)) \(downloadMetric)/s ↓ \(String(format: "%7.2lf", processSpeeds[i].upload)) \(uploadMetric)/s ↑", attributes: menuItemTextAttributes)
93-
}
49+
50+
netspeedViewController.processes = processSpeeds
51+
netspeedViewController.tableView.reloadData()
9452
}
9553

9654
func applicationDidFinishLaunching(_ aNotification: Notification) {
9755
statusItem.length = 75
9856
if let button = statusItem.button {
9957
button.attributedTitle = NSAttributedString(string: "\n\(String(format: "%7.2lf", 0.0)) KB/s ↑\n\(String(format: "%7.2lf", 0.0)) KB/s ↓", attributes: statusBarTextAttributes)
58+
button.action = #selector(togglePopover(_:))
10059
}
101-
102-
quitMenuItem.action = #selector(NSApplication.terminate(_:))
103-
104-
statusItem.menu = menu
105-
60+
popover.contentViewController = netspeedViewController
61+
10662
timer = Timer.scheduledTimer(withTimeInterval: 2, repeats: true) { timer in
10763
DispatchQueue.global(qos: .background).async {
10864
let topTask = Process()
10965
topTask.launchPath = "/usr/bin/env"
11066
topTask.arguments = ["nettop", "-d", "-P", "-J", "bytes_in,bytes_out", "-x", "-L", "2", "-c"]
111-
67+
11268
let outpipe = Pipe()
11369
topTask.standardOutput = outpipe
11470
topTask.launch()
11571
topTask.waitUntilExit()
116-
72+
11773
if let outputString = String(data: outpipe.fileHandleForReading.readDataToEndOfFile(), encoding: .utf8) {
11874
let splitStrings = outputString.split(separator: "\n")
11975
let length = splitStrings.count / 2
@@ -124,14 +80,51 @@ class AppDelegate: NSObject, NSApplicationDelegate {
12480
}
12581
self.processSpeeds.sort(by: {$0.download > $1.download})
12682
}
127-
83+
12884
topTask.terminate()
129-
85+
86+
self.downloadSpeed = 0.0
87+
self.uploadSpeed = 0.0
88+
for speed in self.processSpeeds {
89+
self.downloadSpeed += speed.download
90+
self.uploadSpeed += speed.upload
91+
}
92+
if (self.downloadSpeed > 1024.0) {
93+
self.downloadSpeed /= 1024.0
94+
self.downloadMetric = "MB"
95+
} else {
96+
self.downloadMetric = "KB"
97+
}
98+
if (self.uploadSpeed > 1024.0) {
99+
self.uploadSpeed /= 1024.0
100+
self.uploadMetric = "MB"
101+
} else {
102+
self.uploadMetric = "KB"
103+
}
104+
130105
DispatchQueue.main.async {
131106
self.updateSpeed()
132107
}
133108
}
134109
}
135110
RunLoop.current.add(timer, forMode: .common)
136111
}
112+
113+
@objc func togglePopover(_ sender: Any?) {
114+
if popover.isShown {
115+
closePopover(sender: sender)
116+
} else {
117+
showPopover(sender: sender)
118+
}
119+
}
120+
121+
func showPopover(sender: Any?) {
122+
if let button = statusItem.button {
123+
popover.show(relativeTo: button.bounds, of: button, preferredEdge: NSRectEdge.minY)
124+
}
125+
}
126+
127+
func closePopover(sender: Any?) {
128+
popover.performClose(sender)
129+
}
137130
}

0 commit comments

Comments
 (0)