From 85d6ef5b826c186e34f558aff3db34ca63f6a5c7 Mon Sep 17 00:00:00 2001 From: tisfeng Date: Sat, 4 May 2024 00:01:12 +0800 Subject: [PATCH] perf: improve showAboutWindow --- Easydict/App/EasydictApp.swift | 22 +++++++++++++++++++ .../SettingView/Tabs/TabView/AboutTab.swift | 15 ------------- 2 files changed, 22 insertions(+), 15 deletions(-) diff --git a/Easydict/App/EasydictApp.swift b/Easydict/App/EasydictApp.swift index 82b133893..5dbf9f90e 100644 --- a/Easydict/App/EasydictApp.swift +++ b/Easydict/App/EasydictApp.swift @@ -91,6 +91,28 @@ struct EasydictApp: App { @Default(.selectedMenuBarIcon) private var menuBarIcon @StateObject private var languageState = LanguageState() + + @State var aboutWindow: NSWindow? + + private func showAboutWindow() { + if let aboutWindow = aboutWindow { + aboutWindow.makeKeyAndOrderFront(nil) + } else { + aboutWindow = NSWindow( + contentRect: NSRect(x: 0, y: 0, width: 500, height: 220), + styleMask: [.titled, .closable], + backing: .buffered, defer: false + ) + aboutWindow?.titleVisibility = .hidden + aboutWindow?.titlebarAppearsTransparent = true + aboutWindow?.isReleasedWhenClosed = false + aboutWindow?.center() + if #available(macOS 13, *) { + aboutWindow?.contentView = NSHostingView(rootView: SettingsAboutTab()) + } + aboutWindow?.makeKeyAndOrderFront(nil) + } + } } // MARK: - FakeViewToOpenSettingsInSonoma diff --git a/Easydict/Swift/View/SettingView/Tabs/TabView/AboutTab.swift b/Easydict/Swift/View/SettingView/Tabs/TabView/AboutTab.swift index 8d187f9bc..f012ccc7f 100644 --- a/Easydict/Swift/View/SettingView/Tabs/TabView/AboutTab.swift +++ b/Easydict/Swift/View/SettingView/Tabs/TabView/AboutTab.swift @@ -92,18 +92,3 @@ struct AboutTab: View { #Preview { AboutTab() } - -@available(macOS 13, *) -func showAboutWindow() { - let aboutWindow = NSWindow( - contentRect: NSRect(x: 0, y: 0, width: 500, height: 220), - styleMask: [.titled, .closable], - backing: .buffered, defer: false - ) - aboutWindow.titleVisibility = .hidden - aboutWindow.titlebarAppearsTransparent = true - aboutWindow.center() - aboutWindow.contentView = NSHostingView(rootView: SettingsAboutTab()) - aboutWindow.isReleasedWhenClosed = false - aboutWindow.makeKeyAndOrderFront(nil) -}