diff --git a/ScreenCut/ScreenCut/Action/ScreenCutApp.swift b/ScreenCut/ScreenCut/Action/ScreenCutApp.swift index 696b64d..8fad241 100644 --- a/ScreenCut/ScreenCut/Action/ScreenCutApp.swift +++ b/ScreenCut/ScreenCut/Action/ScreenCutApp.swift @@ -25,34 +25,22 @@ struct ScreenCutApp: App { } var body: some Scene { -// 直接使用MenuBar 替代掉主窗口 + // 直接使用MenuBar 替代掉主窗口 MenuBarExtra("", systemImage: "scissors"){ - Button("截屏") { - ScreenCut.saveScreenFullImage() - } - .padding() - Button("选择截屏") { - NSCursor.crosshair.set() - ScreenshotWindow().makeKeyAndOrderFront(nil) - } - Divider() - Button("偏好设置") { - let aboutWindowController = PreferenceSettingsViewController() - aboutWindowController.showWindow(nil) - } - .padding() - Button("关于"){ - let aboutWindowController = AboutWindowController() - aboutWindowController.showWindow(nil) - } - .padding() - Divider() - Button("退出") { - NSApplication.shared.terminate(nil) - } - .keyboardShortcut("Q", modifiers: [.command]) + MenuBarContentView() + } + + // SwiftUI 原生的设置窗口 + Settings { + PreferenceSettingsView() + .frame(width: 560, height: 500) + } + + // SwiftUI 原生的关于窗口 + WindowGroup("关于", id: "about") { + AboutView() + .frame(width: 540, height: 200) } - } } @@ -66,3 +54,44 @@ extension Scene { } } } + +// 菜单栏内容,使用 SwiftUI 的 openSettings/openWindow 打开设置与关于窗口 +private struct MenuBarContentView: View { + @Environment(\.openWindow) private var openWindow + @Environment(\.openSettings) private var openSettings + + var body: some View { + Button("截屏") { + ScreenCut.saveScreenFullImage() + } + .padding() + Button("选择截屏") { + NSCursor.crosshair.set() + ScreenshotWindow().makeKeyAndOrderFront(nil) + } + Divider() + Button("偏好设置") { + if #available(macOS 13.0, *) { + openSettings() + } else { + let controller = PreferenceSettingsViewController() + controller.showWindow(nil) + } + } + .padding() + Button("关于"){ + if #available(macOS 13.0, *) { + openWindow(id: "about") + } else { + let controller = AboutWindowController() + controller.showWindow(nil) + } + } + .padding() + Divider() + Button("退出") { + NSApplication.shared.terminate(nil) + } + .keyboardShortcut("Q", modifiers: [.command]) + } +}