Skip to content

Commit

Permalink
menu增加路由选择
Browse files Browse the repository at this point in the history
  • Loading branch information
yanue committed Jun 26, 2024
1 parent d01a07a commit adb73ff
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 0 deletions.
40 changes: 40 additions & 0 deletions V2rayU/Base.lproj/MainMenu.xib
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
<outlet property="manualMode" destination="hTu-27-5OL" id="lHt-aQ-ZNU"/>
<outlet property="newVersionItem" destination="SdL-DD-qZe" id="WXr-Gw-0HS"/>
<outlet property="pacMode" destination="NrZ-oB-7yd" id="d3C-wO-jAI"/>
<outlet property="routingMenu" destination="ykF-uD-Nb1" id="qTS-BH-9rV"/>
<outlet property="serverItems" destination="cio-ej-HL5" id="0Z8-41-eCi"/>
<outlet property="statusMenu" destination="9n3-TW-9ur" id="Hgg-sa-fM8"/>
<outlet property="toggleV2rayItem" destination="SML-EF-rdT" id="am7-ue-GN3"/>
Expand Down Expand Up @@ -85,6 +86,45 @@
</connections>
</menuItem>
<menuItem isSeparatorItem="YES" id="EF9-7H-2Xy"/>
<menuItem title="Routing" id="VNg-as-9we" userLabel="Routing">
<modifierMask key="keyEquivalentModifierMask"/>
<menu key="submenu" title="Routing" id="ykF-uD-Nb1">
<items>
<menuItem title="Routing..." tag="-1" id="tKM-4Z-Gyr">
<attributedString key="attributedTitle"/>
<modifierMask key="keyEquivalentModifierMask"/>
<connections>
<action selector="goRouting:" target="hcZ-V7-Dui" id="gVK-Fb-7uq"/>
</connections>
</menuItem>
<menuItem isSeparatorItem="YES" id="pSb-Qp-dXC"/>
<menuItem title="Global" identifier="routingGlobal" id="qhf-Kw-wBL" userLabel="Global">
<connections>
<action selector="switchRouting:" target="hcZ-V7-Dui" id="j4n-az-30A"/>
</connections>
</menuItem>
<menuItem title="Bypassing the LAN Address" tag="1" identifier="routingLan" id="xcE-uG-T3N">
<connections>
<action selector="switchRouting:" target="hcZ-V7-Dui" id="ssU-OD-c6I"/>
</connections>
</menuItem>
<menuItem title="Bypassing mainland address" tag="2" identifier="routingCn" id="Y6v-yx-HRg">
<connections>
<action selector="switchRouting:" target="hcZ-V7-Dui" id="oYE-YR-VSp"/>
</connections>
</menuItem>
<menuItem title="Bypassing LAN and mainland address" tag="3" identifier="routingLanAncCn" id="YQd-ir-Ilb">
<connections>
<action selector="switchRouting:" target="hcZ-V7-Dui" id="OVM-pr-94W"/>
</connections>
</menuItem>
</items>
</menu>
<connections>
<action selector="switchRouting:" target="hcZ-V7-Dui" id="cPc-yB-1qO"/>
</connections>
</menuItem>
<menuItem isSeparatorItem="YES" id="vxZ-8S-14f"/>
<menuItem title="Servers" id="cio-ej-HL5" userLabel="ServerItems">
<modifierMask key="keyEquivalentModifierMask"/>
<menu key="submenu" title="Servers" id="NCx-DY-Hm4"/>
Expand Down
32 changes: 32 additions & 0 deletions V2rayU/MainMenu.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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)
}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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()
Expand Down
4 changes: 4 additions & 0 deletions V2rayU/Preference/PreferenceRouting.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit adb73ff

Please sign in to comment.