ClashX.Meta/ClashX/AppDelegate.swift

95 lines
3.0 KiB
Swift
Raw Normal View History

2018-06-13 10:44:30 +08:00
//
// AppDelegate.swift
// ClashX
//
// Created by on 2018/6/10.
// Copyright © 2018 west2online. All rights reserved.
//
import Cocoa
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
var statusItem: NSStatusItem!
static let StatusItemIconWidth: CGFloat = NSStatusItem.variableLength
@IBOutlet weak var statusMenu: NSMenu!
@IBOutlet weak var proxySettingMenuItem: NSMenuItem!
let ssQueue = DispatchQueue(label: "com.w2fzu.ssqueue", attributes: .concurrent)
func applicationDidFinishLaunching(_ aNotification: Notification) {
// Insert code here to initialize your application
_ = ProxyConfigManager.install()
statusItem = NSStatusBar.system.statusItem(withLength: AppDelegate.StatusItemIconWidth)
let image : NSImage = NSImage(named: NSImage.Name(rawValue: "menu_icon"))!
image.isTemplate = true
statusItem.image = image
statusItem.menu = statusMenu
updateMenuItem()
startProxy()
}
func applicationWillTerminate(_ aNotification: Notification) {
if ConfigManager.proxyPortAutoSet {
_ = ProxyConfigManager.setUpSystemProxy(port: nil,socksPort: nil)
}
}
func updateMenuItem(){
proxySettingMenuItem.state = ConfigManager.proxyPortAutoSet ? .on : .off
}
func startProxy() {
ssQueue.async {
run()
}
let ports = getPortsC()
ConfigManager.httpProxyPort = Int(String(cString: ports.r0))!
ConfigManager.socksProxyPort = Int(String(cString: ports.r1))!
if ConfigManager.proxyPortAutoSet {
_ = ProxyConfigManager.setUpSystemProxy(port: ConfigManager.httpProxyPort,socksPort: ConfigManager.socksProxyPort)
}
}
@IBAction func actionQuit(_ sender: Any) {
NSApplication.shared.terminate(self)
}
@IBAction func actionSetSystemProxy(_ sender: Any) {
ConfigManager.proxyPortAutoSet = !ConfigManager.proxyPortAutoSet
updateMenuItem()
if ConfigManager.proxyPortAutoSet {
_ = ProxyConfigManager.setUpSystemProxy(port: ConfigManager.httpProxyPort,socksPort: ConfigManager.socksProxyPort)
} else {
_ = ProxyConfigManager.setUpSystemProxy(port: nil,socksPort: nil)
}
}
@IBAction func actionCopyExportCommand(_ sender: Any) {
let pasteboard = NSPasteboard.general
pasteboard.clearContents()
pasteboard.setString("export https_proxy=http://127.0.0.1:\(ConfigManager.httpProxyPort);export http_proxy=http://127.0.0.1:\(ConfigManager.httpProxyPort)", forType: .string)
}
@IBAction func openConfigFolder(_ sender: Any) {
let path = (NSHomeDirectory() as NSString).appendingPathComponent("/.config/clash/config.ini")
NSWorkspace.shared.openFile(path)
}
@IBAction func actionUpdateConfig(_ sender: Any) {
ssQueue.async {
updateConfigC()
}
}
}