diff --git a/V2rayU/Base.lproj/MainMenu.xib b/V2rayU/Base.lproj/MainMenu.xib index 19542c3..b3af050 100644 --- a/V2rayU/Base.lproj/MainMenu.xib +++ b/V2rayU/Base.lproj/MainMenu.xib @@ -24,6 +24,7 @@ + @@ -85,6 +86,45 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/V2rayU/MainMenu.swift b/V2rayU/MainMenu.swift index fdb578b..9761338 100644 --- a/V2rayU/MainMenu.swift +++ b/V2rayU/MainMenu.swift @@ -27,6 +27,7 @@ class MenuController: NSObject, NSMenuDelegate { @IBOutlet weak var v2rayStatusItem: NSMenuItem! @IBOutlet weak var serverItems: NSMenuItem! @IBOutlet weak var newVersionItem: NSMenuItem! + @IBOutlet weak var routingMenu: NSMenu! // when menu.xib loaded override func awakeFromNib() { @@ -38,6 +39,8 @@ class MenuController: NSObject, NSMenuDelegate { // hide new version newVersionItem.isHidden = true + self.showRouring(); + // windowWillClose Notification NotificationCenter.default.addObserver(self, selector: #selector(configWindowWillClose(notification:)), name: NSWindow.willCloseNotification, object: nil) } @@ -124,6 +127,23 @@ class MenuController: NSObject, NSMenuDelegate { } } + func showRouring() { + let routingRule = Int(UserDefaults.get(forKey: .routingRule) ?? "0") ?? 0 + DispatchQueue.main.async { + // 假设 routingMenu 已经连接并且有一个子菜单 + if self.routingMenu.items != nil { + // 确保 routingMenu 已经连接 + for item in self.routingMenu.items { + if item.tag == routingRule { + item.state = .on + } else { + item.state = .off + } + } + } + } + } + func getServerMenus() -> NSMenu { // default let curSer = UserDefaults.get(forKey: .v2rayCurrentServerName) @@ -268,7 +288,19 @@ class MenuController: NSObject, NSMenuDelegate { UserDefaults.set(forKey: .runMode, value: RunMode.pac.rawValue) V2rayLaunch.restartV2ray() } + + @IBAction func goRouting(_ sender: NSMenuItem) { + DispatchQueue.main.async { + preferencesWindowController.show(preferencePane: .routingTab) + } + } + @IBAction func switchRouting(_ sender: NSMenuItem) { + UserDefaults.set(forKey: .routingRule, value: String(sender.tag)); + self.showRouring(); + V2rayLaunch.restartV2ray() + } + @IBAction func switchGlobalMode(_ sender: NSMenuItem) { UserDefaults.set(forKey: .runMode, value: RunMode.global.rawValue) V2rayLaunch.restartV2ray() diff --git a/V2rayU/Preference/PreferenceRouting.swift b/V2rayU/Preference/PreferenceRouting.swift index 9f46ae8..dcaf8db 100644 --- a/V2rayU/Preference/PreferenceRouting.swift +++ b/V2rayU/Preference/PreferenceRouting.swift @@ -82,6 +82,10 @@ final class PreferenceRoutingViewController: NSViewController, PreferencePane { (domains, ips) = self.parseDomainOrIp(domainIpStr: self.blockTextView.string) UserDefaults.setArray(forKey: .routingBlockDomains, value: domains) UserDefaults.setArray(forKey: .routingBlockIps, value: ips) + + + // 更新菜单 + menuController.showRouring(); // set current server item and reload v2ray-core V2rayLaunch.restartV2ray()