Skip to content

Commit

Permalink
fix: remove fake window which the trick way to open setting (#543)
Browse files Browse the repository at this point in the history
* fix: remove fake window which the trick way to open setting

* fix: call openSettings way

* fix: resolve warning

---------

Co-authored-by: tisfeng <[email protected]>
  • Loading branch information
AkaShark and tisfeng authored May 9, 2024
1 parent f5e9ec1 commit 5ccf682
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 30 deletions.
42 changes: 12 additions & 30 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 @@ -64,13 +74,6 @@ 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
1 change: 1 addition & 0 deletions Easydict/objc/ViewController/View/Titlebar/EZTitlebar.m
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ - (NSMenu *)quickActionMenu {
NSLocalizedString(@"replace_newline_with_space", nil);
NSLocalizedString(@"remove_code_comment_symbols", nil);
NSLocalizedString(@"split_words", nil);
NSLocalizedString(@"go_to_settings", nil);
*/
];

Expand Down

0 comments on commit 5ccf682

Please sign in to comment.