Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: remove fake window which the trick way to open setting #543

Merged
merged 4 commits into from
May 9, 2024
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 13 additions & 31 deletions Easydict/App/EasydictApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ struct EasydictApp: App {
} label: {
Label {
Text("Easydict")
.openSettingsAccess() // trick way for open setting
.onReceive(NotificationCenter.default.publisher(
for: Notification.Name.openSettings,
object: nil
)) { _ in
DispatchQueue.main.asyncAfter(deadline: .now() + 0.05) {
// calling `openSettings` immediately doesn't work so wait a quick moment
try? openSettings()
}
}
} icon: {
Image(menuBarIcon.rawValue)
.resizable()
Expand All @@ -63,14 +73,7 @@ struct EasydictApp: App {
}
}
}

Window("go_to_settings", id: "go_to_settings") {
FakeViewToOpenSettingsInSonoma(title: "go_to_settings")
.openSettingsAccess()
}
.windowStyle(HiddenTitleBarWindowStyle())
.windowResizability(.contentSize)


Settings {
SettingView().environmentObject(languageState).environment(
\.locale,
Expand All @@ -82,6 +85,8 @@ struct EasydictApp: App {

// MARK: Private

@Environment(\.openSettings) private var openSettings

@NSApplicationDelegateAdaptor private var delegate: AppDelegate

// Use `@Default` will cause a purple warning and continuously call `set` of it.
Expand Down Expand Up @@ -115,29 +120,6 @@ struct EasydictApp: App {
}
}

// MARK: - FakeViewToOpenSettingsInSonoma

struct FakeViewToOpenSettingsInSonoma: View {
@Environment(\.openSettings) private var openSettings
var title: String

var body: some View {
ZStack {}
.frame(width: 0, height: 0)
.onReceive(NotificationCenter.default.publisher(for: Notification.Name.openSettings, object: nil)) { _ in
DispatchQueue.main.asyncAfter(deadline: .now() + 0.05) {
// calling `openSettings` immediately doesn't work so wait a quick moment
try? openSettings()
NSApplication.shared
.windows
.filter(\.canBecomeKey)
.filter { $0.title == title }
.forEach { $0.close() }
}
}
}
}

extension Bool {
var toggledValue: Bool {
get { !self }
Expand Down