aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRoopesh Chander <roop@roopc.net>2018-12-28 19:29:09 +0530
committerRoopesh Chander <roop@roopc.net>2019-01-14 14:52:29 +0530
commitb5751b63214b51f2b576eb40694f6bb7f9bcd7d4 (patch)
tree11d8d008b27709b1e66fbb91df0def63763aa411
parentmacOS: Add status bar icon (diff)
downloadwireguard-apple-b5751b63214b51f2b576eb40694f6bb7f9bcd7d4.tar.xz
wireguard-apple-b5751b63214b51f2b576eb40694f6bb7f9bcd7d4.zip
macOS: Create status bar with tunnel names
Signed-off-by: Roopesh Chander <roop@roopc.net>
-rw-r--r--WireGuard/WireGuard.xcodeproj/project.pbxproj4
-rw-r--r--WireGuard/WireGuard/UI/macOS/AppDelegate.swift24
-rw-r--r--WireGuard/WireGuard/UI/macOS/StatusMenu.swift34
3 files changed, 56 insertions, 6 deletions
diff --git a/WireGuard/WireGuard.xcodeproj/project.pbxproj b/WireGuard/WireGuard.xcodeproj/project.pbxproj
index 8532456..cbe1b78 100644
--- a/WireGuard/WireGuard.xcodeproj/project.pbxproj
+++ b/WireGuard/WireGuard.xcodeproj/project.pbxproj
@@ -106,6 +106,7 @@
6FB1BDD921D50F5300A991BF /* LocalizationHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6FE1765921C90E87002690EA /* LocalizationHelper.swift */; };
6FB1BDDA21D5170800A991BF /* NetworkExtension.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 6FB1BDB621D4F8B800A991BF /* NetworkExtension.framework */; };
6FBA101521D613F90051C35F /* Application.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6FBA101321D613F30051C35F /* Application.swift */; };
+ 6FBA101821D656000051C35F /* StatusMenu.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6FBA101621D655340051C35F /* StatusMenu.swift */; };
6FDEF7E421846C1A00D8FBF6 /* libwg-go.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 6FDEF7E321846C1A00D8FBF6 /* libwg-go.a */; };
6FDEF7E62185EFB200D8FBF6 /* QRScanViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6FDEF7E52185EFAF00D8FBF6 /* QRScanViewController.swift */; };
6FDEF7FB21863B6100D8FBF6 /* unzip.c in Sources */ = {isa = PBXBuildFile; fileRef = 6FDEF7F621863B6100D8FBF6 /* unzip.c */; };
@@ -248,6 +249,7 @@
6FB1BDA021D4E00A00A991BF /* libwg-go.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; path = "libwg-go.a"; sourceTree = BUILT_PRODUCTS_DIR; };
6FB1BDB621D4F8B800A991BF /* NetworkExtension.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = NetworkExtension.framework; path = Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk/System/Library/Frameworks/NetworkExtension.framework; sourceTree = DEVELOPER_DIR; };
6FBA101321D613F30051C35F /* Application.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Application.swift; sourceTree = "<group>"; };
+ 6FBA101621D655340051C35F /* StatusMenu.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StatusMenu.swift; sourceTree = "<group>"; };
6FDEF7E321846C1A00D8FBF6 /* libwg-go.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; path = "libwg-go.a"; sourceTree = BUILT_PRODUCTS_DIR; };
6FDEF7E52185EFAF00D8FBF6 /* QRScanViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = QRScanViewController.swift; sourceTree = "<group>"; };
6FDEF7F621863B6100D8FBF6 /* unzip.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = unzip.c; sourceTree = "<group>"; };
@@ -457,6 +459,7 @@
6FB1BD6621D2607E00A991BF /* Info.plist */,
6FB1BD6721D2607E00A991BF /* WireGuard.entitlements */,
6FBA101321D613F30051C35F /* Application.swift */,
+ 6FBA101621D655340051C35F /* StatusMenu.swift */,
);
path = macOS;
sourceTree = "<group>";
@@ -1022,6 +1025,7 @@
6FB1BDBE21D50F0200A991BF /* Logger.swift in Sources */,
6FB1BDBF21D50F0200A991BF /* TunnelConfiguration+WgQuickConfig.swift in Sources */,
6FB1BDC021D50F0200A991BF /* NETunnelProviderProtocol+Extension.swift in Sources */,
+ 6FBA101821D656000051C35F /* StatusMenu.swift in Sources */,
6FB1BDC121D50F0200A991BF /* String+ArrayConversion.swift in Sources */,
6FB1BDC221D50F0300A991BF /* LegacyConfigMigration.swift in Sources */,
6FB1BDC321D50F0300A991BF /* TunnelConfiguration.swift in Sources */,
diff --git a/WireGuard/WireGuard/UI/macOS/AppDelegate.swift b/WireGuard/WireGuard/UI/macOS/AppDelegate.swift
index 9630135..ba3e902 100644
--- a/WireGuard/WireGuard/UI/macOS/AppDelegate.swift
+++ b/WireGuard/WireGuard/UI/macOS/AppDelegate.swift
@@ -6,14 +6,26 @@ import Cocoa
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
+ var statusItem: NSStatusItem?
+
func applicationDidFinishLaunching(_ aNotification: Notification) {
- // Insert code here to initialize your application
- }
+ TunnelsManager.create { [weak self] result in
+ guard let self = self else { return }
+ guard result.isSuccess else { return } // TODO: Show alert
- func applicationWillTerminate(_ aNotification: Notification) {
- // Insert code here to tear down your application
+ let tunnelsManager: TunnelsManager = result.value!
+ let statusMenu = StatusMenu(tunnelsManager: tunnelsManager)
+ self.statusItem = createStatusBarItem(with: statusMenu)
+ }
}
-
-
}
+func createStatusBarItem(with statusMenu: StatusMenu) -> NSStatusItem {
+ let statusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.squareLength)
+ if let statusBarImage = NSImage(named: "WireGuardMacStatusBarIcon") {
+ statusBarImage.isTemplate = true
+ statusItem.button?.image = statusBarImage
+ }
+ statusItem.menu = statusMenu
+ return statusItem
+}
diff --git a/WireGuard/WireGuard/UI/macOS/StatusMenu.swift b/WireGuard/WireGuard/UI/macOS/StatusMenu.swift
new file mode 100644
index 0000000..4ac4b91
--- /dev/null
+++ b/WireGuard/WireGuard/UI/macOS/StatusMenu.swift
@@ -0,0 +1,34 @@
+// SPDX-License-Identifier: MIT
+// Copyright © 2018 WireGuard LLC. All Rights Reserved.
+
+import Cocoa
+
+class StatusMenu: NSMenu {
+
+ let tunnelsManager: TunnelsManager
+
+ init(tunnelsManager: TunnelsManager) {
+ self.tunnelsManager = tunnelsManager
+ super.init(title: "WireGuard Status Bar Menu")
+ addTunnelMenuItems()
+ }
+
+ required init(coder decoder: NSCoder) {
+ fatalError("init(coder:) has not been implemented")
+ }
+
+ func addTunnelMenuItems() {
+ for index in 0 ..< tunnelsManager.numberOfTunnels() {
+ let tunnel = tunnelsManager.tunnel(at: index)
+ let menuItem = NSMenuItem(title: tunnel.name, action: #selector(tunnelClicked(sender:)), keyEquivalent: "")
+ menuItem.target = self
+ menuItem.representedObject = tunnel
+ addItem(menuItem)
+ }
+ }
+
+ @objc func tunnelClicked(sender: AnyObject) {
+ guard let tunnel = sender.representedObject as? TunnelContainer else { return }
+ print("Tunnel \(tunnel.name) clicked")
+ }
+}