Skip to content

Commit

Permalink
fix(swiftui): fix error of opening settings windows on macos 14+
Browse files Browse the repository at this point in the history
In macos 14 and above versions, we should open app's settings window using SettingsLink. The
traditional approach does not work on macos 14+.
  • Loading branch information
genshen committed Feb 3, 2024
1 parent df3d4f3 commit 90732fe
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions swiftui-client/wssocks-ustb-client/menu/MenuBarView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,22 @@ struct MenuBarView: View {
var body: some View {
VStack{
HStack{
Button (action:{
showPref()
}, label: {
if #available(macOS 11.0, *) {
if #available(macOS 14.0, *) {
SettingsLink{
Image(systemName: "gear.circle.fill")
} else {
Text("偏好设置")
}
})
} else {
Button (action:{
showPref()
}, label: {
if #available(macOS 11.0, *) {
Image(systemName: "gear.circle.fill")
} else {
Text("偏好设置")
}
})
}

Button (action:{
openNetworkProxyPreferences()
}, label: {
Expand Down Expand Up @@ -266,12 +273,15 @@ end tell

@Environment(\.openURL) var openURL
private func showPref() {
// see: https://stackoverflow.com/a/69600396/10068476
NSApp.activate(ignoringOtherApps: true)
// see: https://stackoverflow.com/a/65356627/10068476
if #available(macOS 13, *) {
NSApp.sendAction(Selector(("showSettingsWindow:")), to: nil, from: nil)
} else {
NSApp.sendAction(Selector(("showPreferencesWindow:")), to: nil, from: nil)
}
NSApp.windows.first?.orderFrontRegardless()
}

private func quitApp() {
Expand Down

0 comments on commit 90732fe

Please sign in to comment.