Skip to content

Commit

Permalink
MenuBar样式
Browse files Browse the repository at this point in the history
  • Loading branch information
yanue committed Jan 8, 2025
1 parent e83233f commit d5d694c
Show file tree
Hide file tree
Showing 20 changed files with 633 additions and 156 deletions.
30 changes: 29 additions & 1 deletion V2rayU.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@
662CC44D2D1C4D38006E8450 /* RoutingViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 662CC44A2D1C4D38006E8450 /* RoutingViewModel.swift */; };
662CC44E2D1C4D38006E8450 /* SubViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 662CC44B2D1C4D38006E8450 /* SubViewModel.swift */; };
662CC4502D1C4D46006E8450 /* Scanner.swift in Sources */ = {isa = PBXBuildFile; fileRef = 662CC44F2D1C4D46006E8450 /* Scanner.swift */; };
663745332D2EA0F90093A101 /* ProfilePanel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 663745322D2EA0E40093A101 /* ProfilePanel.swift */; };
663745352D2EAF4F0093A101 /* RoutingPanel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 663745342D2EAF420093A101 /* RoutingPanel.swift */; };
663745372D2EC8840093A101 /* MenuSpeed.swift in Sources */ = {isa = PBXBuildFile; fileRef = 663745362D2EC87F0093A101 /* MenuSpeed.swift */; };
663745392D2ECAE40093A101 /* MenuHead.swift in Sources */ = {isa = PBXBuildFile; fileRef = 663745382D2ECAE00093A101 /* MenuHead.swift */; };
6637453B2D2ECF590093A101 /* MenuItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6637453A2D2ECF530093A101 /* MenuItem.swift */; };
66A078BC2D008A3700490469 /* SwiftyBeaver in Frameworks */ = {isa = PBXBuildFile; productRef = 66A078BB2D008A3700490469 /* SwiftyBeaver */; };
66B1B5312D282DAB0032DD09 /* V2rayMetrics.swift in Sources */ = {isa = PBXBuildFile; fileRef = 66B1B5302D282D990032DD09 /* V2rayMetrics.swift */; };
66B1B5332D2913000032DD09 /* ProfileStatModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 66B1B5322D2913000032DD09 /* ProfileStatModel.swift */; };
Expand Down Expand Up @@ -185,6 +190,11 @@
662CC44A2D1C4D38006E8450 /* RoutingViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RoutingViewModel.swift; sourceTree = "<group>"; };
662CC44B2D1C4D38006E8450 /* SubViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SubViewModel.swift; sourceTree = "<group>"; };
662CC44F2D1C4D46006E8450 /* Scanner.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Scanner.swift; sourceTree = "<group>"; };
663745322D2EA0E40093A101 /* ProfilePanel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ProfilePanel.swift; sourceTree = "<group>"; };
663745342D2EAF420093A101 /* RoutingPanel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RoutingPanel.swift; sourceTree = "<group>"; };
663745362D2EC87F0093A101 /* MenuSpeed.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MenuSpeed.swift; sourceTree = "<group>"; };
663745382D2ECAE00093A101 /* MenuHead.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MenuHead.swift; sourceTree = "<group>"; };
6637453A2D2ECF530093A101 /* MenuItem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MenuItem.swift; sourceTree = "<group>"; };
66B1B5302D282D990032DD09 /* V2rayMetrics.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = V2rayMetrics.swift; sourceTree = "<group>"; };
66B1B5322D2913000032DD09 /* ProfileStatModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ProfileStatModel.swift; sourceTree = "<group>"; };
66B1B54C2D2979060032DD09 /* V2rayUTool */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = V2rayUTool; sourceTree = BUILT_PRODUCTS_DIR; };
Expand Down Expand Up @@ -390,6 +400,7 @@
662CC4002D1BED9B006E8450 /* Views */ = {
isa = PBXGroup;
children = (
663745312D2EA0D10093A101 /* AppMenu */,
66E94B312D26B95D003745B5 /* Base */,
662CC3F22D1BED9B006E8450 /* Profile */,
662CC3F82D1BED9B006E8450 /* Setting */,
Expand Down Expand Up @@ -418,6 +429,18 @@
path = Share;
sourceTree = "<group>";
};
663745312D2EA0D10093A101 /* AppMenu */ = {
isa = PBXGroup;
children = (
6637453A2D2ECF530093A101 /* MenuItem.swift */,
663745382D2ECAE00093A101 /* MenuHead.swift */,
663745362D2EC87F0093A101 /* MenuSpeed.swift */,
663745342D2EAF420093A101 /* RoutingPanel.swift */,
663745322D2EA0E40093A101 /* ProfilePanel.swift */,
);
path = AppMenu;
sourceTree = "<group>";
};
66B1B5582D297AE60032DD09 /* Scpipts */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -487,6 +510,7 @@
isa = PBXGroup;
children = (
66B1B5732D297BF70032DD09 /* GoogleService-Info.plist */,
662CC3D82D1BED9B006E8450 /* Info.plist */,
662CC3BF2D1BED9B006E8450 /* App.swift */,
66F66F822D1E450000E77C64 /* AppState.swift */,
662CC3C02D1BED9B006E8450 /* AppDelegate.swift */,
Expand All @@ -497,7 +521,6 @@
662CC3C92D1BED9B006E8450 /* Base */,
662CC3D42D1BED9B006E8450 /* Database */,
662CC3D72D1BED9B006E8450 /* Handler */,
662CC3D82D1BED9B006E8450 /* Info.plist */,
662CC3DD2D1BED9B006E8450 /* Locales */,
662CC3DF2D1BED9B006E8450 /* Preview Content */,
662CC3E22D1BED9B006E8450 /* Service */,
Expand Down Expand Up @@ -667,23 +690,27 @@
66E94B342D26B95D003745B5 /* Theme.swift in Sources */,
66E94B352D26B95D003745B5 /* JsonView.swift in Sources */,
66E94B3D2D27E74F003745B5 /* V2rayStats.swift in Sources */,
663745352D2EAF4F0093A101 /* RoutingPanel.swift in Sources */,
662CC4402D1C4CE9006E8450 /* ShadowsocksRUri.swift in Sources */,
662CC4412D1C4CE9006E8450 /* TrojanUri.swift in Sources */,
66F66F882D1E7D8800E77C64 /* SubscriptionHandler.swift in Sources */,
662CC4422D1C4CE9006E8450 /* BaseShareUri.swift in Sources */,
662CC4432D1C4CE9006E8450 /* VmessUri.swift in Sources */,
6637453B2D2ECF590093A101 /* MenuItem.swift in Sources */,
66E94B392D26B971003745B5 /* SettingView.swift in Sources */,
66E94B3A2D26B971003745B5 /* AppContentView.swift in Sources */,
66E94B3B2D26B971003745B5 /* ProfileListView.swift in Sources */,
662CC4442D1C4CE9006E8450 /* V2rayConfigHandler.swift in Sources */,
662CC4452D1C4CE9006E8450 /* V2rayOutboundHandler.swift in Sources */,
662CC4462D1C4CE9006E8450 /* VlessUri.swift in Sources */,
663745332D2EA0F90093A101 /* ProfilePanel.swift in Sources */,
662CC4472D1C4CE9006E8450 /* ShadowsocksUri.swift in Sources */,
662CC4482D1C4CE9006E8450 /* ShareHandler.swift in Sources */,
662CC4032D1BED9B006E8450 /* String+Extension.swift in Sources */,
662CC4052D1BED9B006E8450 /* Shell.swift in Sources */,
66B1B5312D282DAB0032DD09 /* V2rayMetrics.swift in Sources */,
662CC4062D1BED9B006E8450 /* UserDefaults.swift in Sources */,
663745372D2EC8840093A101 /* MenuSpeed.swift in Sources */,
662CC4072D1BED9B006E8450 /* OutboundView.swift in Sources */,
662CC4082D1BED9B006E8450 /* SecurityView.swift in Sources */,
662CC40A2D1BED9B006E8450 /* V2rayLaunch.swift in Sources */,
Expand Down Expand Up @@ -711,6 +738,7 @@
662CC4232D1BED9B006E8450 /* App.swift in Sources */,
662CC4252D1BED9B006E8450 /* Url+Extension.swift in Sources */,
662CC4262D1BED9B006E8450 /* V2rayStream.swift in Sources */,
663745392D2ECAE40093A101 /* MenuHead.swift in Sources */,
662CC4292D1BED9B006E8450 /* ToastWindow.swift in Sources */,
662CC42A2D1BED9B006E8450 /* V2rayOutbound.swift in Sources */,
662CC44C2D1C4D38006E8450 /* ProfileViewModel.swift in Sources */,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
<key>V2rayU.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>1</integer>
<integer>0</integer>
</dict>
<key>V2rayUTool.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>0</integer>
<integer>1</integer>
</dict>
</dict>
</dict>
Expand Down
34 changes: 32 additions & 2 deletions V2rayU/App.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ struct V2rayUApp: App {
@StateObject var themeManager = ThemeManager()
@NSApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
@StateObject private var appState = AppState.shared
@Environment(\.colorScheme) var colorScheme // 获取当前系统主题模式

init() {
// 初始化
Expand All @@ -20,14 +21,18 @@ struct V2rayUApp: App {
print("userHomeDirectory",userHomeDirectory)
V2rayLaunch.checkInstall()
V2rayLaunch.runAtStart()
V2rayLaunch.runTun2Socks()
// 加载
appState.viewModel.getList()
// V2rayLaunch.runTun2Socks()
}


var body: some Scene {
// 显示 MenuBar
MenuBarExtra("V2rayU", image: appState.icon) {
AppMenuView(openContentViewWindow: openContentViewWindow)
}.menuBarExtraStyle(.window) // 重点,按窗口显示
}
.menuBarExtraStyle(.window) // 重点,按窗口显示
.environment(\.locale, languageManager.currentLocale) // 设置 Environment 的 locale
}

Expand Down Expand Up @@ -99,6 +104,31 @@ struct V2rayUApp: App {
}
}

struct VisualEffectBackground: View {
@Environment(\.colorScheme) var colorScheme // 获取当前系统的颜色模式

var body: some View {
VisualEffectView(effect: colorScheme == .dark ? .dark : .light)
.edgesIgnoringSafeArea(.all)
}
}

struct VisualEffectView: NSViewRepresentable {
var effect: NSVisualEffectView.Material

func makeNSView(context: Context) -> NSVisualEffectView {
let visualEffectView = NSVisualEffectView()
visualEffectView.blendingMode = .withinWindow
visualEffectView.material = effect
visualEffectView.state = .active
return visualEffectView
}

func updateNSView(_ nsView: NSVisualEffectView, context: Context) {
nsView.material = effect
}
}

struct CustomMenuItemView: View {
var body: some View {
Text("Hello world")
Expand Down
14 changes: 14 additions & 0 deletions V2rayU/AppState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ final class AppState: ObservableObject {
@Published var proxyUpSpeed = 0.0
@Published var proxyDownSpeed = 0.0

@StateObject var viewModel = ProfileViewModel()

private var cancellables = Set<AnyCancellable>()

init() {
Expand Down Expand Up @@ -198,6 +200,18 @@ final class AppState: ObservableObject {
UserDefaults.set(forKey: .runMode, value: mode.rawValue)
}

func runRouting(uuid: String) {
UserDefaults.set(forKey: .runningRouting, value: uuid)
self.runningRouting = uuid
V2rayLaunch.startV2rayCore()
}

func runProfile(uuid: String) {
UserDefaults.set(forKey: .runningProfile, value: uuid)
self.runningProfile = uuid
V2rayLaunch.startV2rayCore()
}

func setSpeed(latency: Double, directUpSpeed: Double, directDownSpeed: Double, proxyUpSpeed: Double, proxyDownSpeed: Double) {
self.latency = latency
self.directUpSpeed = directUpSpeed
Expand Down
19 changes: 17 additions & 2 deletions V2rayU/Database/ViewModels/ProfileViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,25 @@ class ProfileViewModel: ObservableObject {

// filter: ["id": 1,"conlmn":"value"]
static func count(filter: [String: (any DatabaseValueConvertible)?]?) -> Int {
guard let filter = filter else { return 0 }
do {
let dbReader = AppDatabase.shared.reader
return try dbReader.read { db in
try ProfileModel.filter(key: filter).fetchCount(db)
var query = ProfileModel.all()
for (column, value) in filter {
if let value = value {
query = query.filter(Column(column) == value)
}
}
return try query.fetchCount(db)
}
} catch {
print("count error: \(error)")
return 0
}
}


static func getFastOne() -> ProfileModel? {
do {
let dbReader = AppDatabase.shared.reader
Expand Down Expand Up @@ -114,10 +122,17 @@ class ProfileViewModel: ObservableObject {

// filter: ["id": 1,"conlmn":"value"]
static func delete(filter: [String: (any DatabaseValueConvertible)?]?) {
guard let filter = filter else { return }
do {
let dbWriter = AppDatabase.shared.dbWriter
try dbWriter.write { db in
try ProfileModel.filter(key: filter).deleteAll(db)
var query = ProfileModel.all()
for (column, value) in filter {
if let value = value {
query = query.filter(Column(column) == value)
}
}
try query.deleteAll(db)
}
} catch {
print("delete error: \(error)")
Expand Down
10 changes: 7 additions & 3 deletions V2rayU/Handler/RoutingHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,11 @@ class RoutingHandler {
return res
}

// dns-rule: { "type": "field", "outboundTag": "dns-out", "network": "udp", "port": 53 }
let dnsRule = getRoutingRule(outTag: "dns_out", port: "53", network: "udp")
print("dnsRule: \(dnsRule)")
// api-rule: {"inboundTag": ["api"], "outboundTag": "api", "type": "field"}
let apiRule = getRoutingRule(outTag: "metrics_out", domain: nil, ip: nil, port: nil,inboundTag: ["metrics_in"])
let apiRule = getRoutingRule(outTag: "metrics_out", inboundTag: ["metrics_in"])

// 根据默认规则生成
var rules: [V2rayRoutingRule] = [apiRule]
Expand Down Expand Up @@ -186,20 +189,21 @@ class RoutingHandler {
settings.domainStrategy = V2rayRouting.domainStrategy(rawValue: self.routing.domainStrategy) ?? .AsIs
}
// 最后添加默认规则
// let proxyRule = getRoutingRule(outTag: "proxy", domain: nil, ip: nil, port: nil)
// let proxyRule = getRoutingRule(outTag: "proxy", )
// rules.append(proxyRule)
settings.rules = rules
return settings
}

func getRoutingRule(outTag: String, domain: [String]?, ip: [String]?, port: String?, inboundTag: [String]? = nil) -> V2rayRoutingRule {
func getRoutingRule(outTag: String, domain: [String]? = nil, ip: [String]? = nil, port: String? = nil, inboundTag: [String]? = nil, network: String? = nil) -> V2rayRoutingRule {
var rule = V2rayRoutingRule()
rule.outboundTag = outTag
rule.inboundTag = inboundTag
rule.type = "field"
rule.domain = domain
rule.ip = ip
rule.port = port
rule.network = network
return rule
}

Expand Down
1 change: 0 additions & 1 deletion V2rayU/Handler/SubscriptionHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ actor SubscriptionHandler {
}

func getOldCount(sub: SubModel) -> Int {
// reload all
return ProfileViewModel.count(filter: [ProfileModel.Columns.subid.name: sub.uuid])
}

Expand Down
61 changes: 29 additions & 32 deletions V2rayU/Handler/V2rayConfigHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,16 @@ import Foundation

let defaultDns = """
{
"hosts": {
},
"servers": [
{
"address": "223.5.5.5",
"domains": [
"geosite:cn"
"servers": [
"8.8.8.8",
"1.1.1.1",
{
"address": "119.29.29.29",
"domains": ["geosite:cn"]
}
],
"expectIPs": [
"geoip:cn"
]
},
"1.1.1.1",
"8.8.8.8",
"https://dns.google/dns-query",
{
"address": "223.5.5.5",
"domains": [
]
"queryStrategy": "UseIPv4"
}
]
}
"""

class V2rayConfigHandler {
Expand Down Expand Up @@ -123,24 +111,33 @@ class V2rayConfigHandler {
self.v2ray.inbounds = inbounds

// ------------------------------------- inbound end ----------------------------------------------
// outbound Freedom
let outboundFreedom = V2rayOutbound()
outboundFreedom.protocol = V2rayProtocolOutbound.freedom
outboundFreedom.tag = "direct"
outboundFreedom.settings = V2rayOutboundFreedom()

// outbound Blackhole
let outboundBlackhole = V2rayOutbound()
outboundBlackhole.protocol = V2rayProtocolOutbound.blackhole
outboundBlackhole.tag = "block"
outboundBlackhole.settings = V2rayOutboundBlackhole()


// outbounds
var outbounds: [V2rayOutbound] = []
outbounds.append(contentsOf: _outbounds)
if !self.forPing {
// outbound Freedom
let outboundFreedom = V2rayOutbound()
outboundFreedom.protocol = .freedom
outboundFreedom.tag = "direct"
outboundFreedom.settings = V2rayOutboundFreedom()

// outbound Blackhole
let outboundBlackhole = V2rayOutbound()
outboundBlackhole.protocol = .blackhole
outboundBlackhole.tag = "block"
outboundBlackhole.settings = V2rayOutboundBlackhole()

// outbound Dns
let outboundDns = V2rayOutbound()
outboundBlackhole.protocol = .dns
outboundBlackhole.tag = "dns_out"
outboundBlackhole.settings = V2rayOutboundDns()

// 添加
outbounds.append(outboundFreedom)
outbounds.append(outboundBlackhole)
// outbounds.append(outboundDns)
}

self.v2ray.outbounds = outbounds
Expand Down
Loading

0 comments on commit d5d694c

Please sign in to comment.