Skip to content
Open
Changes from all 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
81 changes: 55 additions & 26 deletions ScreenCut/ScreenCut/Action/ScreenCutApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

}
}

Expand All @@ -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])
}
}