gh-XcodesOrg-XcodesApp/Xcodes/XcodesApp.swift

65 lines
2.2 KiB
Swift

import SwiftUI
import AppKit
@main
struct XcodesApp: App {
@NSApplicationDelegateAdaptor(AppDelegate.self) var appDelegate: AppDelegate
@StateObject private var appState = AppState()
var body: some Scene {
WindowGroup("Xcodes") {
XcodeListView()
.environmentObject(appState)
}
.commands {
CommandGroup(replacing: .appInfo) {
Button("About Xcodes") {
appDelegate.showAboutWindow()
}
}
}
Settings {
SettingsView()
.environmentObject(appState)
}
}
}
class AppDelegate: NSObject, NSApplicationDelegate {
private lazy var aboutWindow = configure(NSWindow(
contentRect: .zero,
styleMask: [.closable, .resizable, .miniaturizable, .titled],
backing: .buffered,
defer: false
)) {
$0.title = "About Xcodes"
$0.contentView = NSHostingView(rootView: AboutView(showAcknowledgementsWindow: showAcknowledgementsWindow))
$0.isReleasedWhenClosed = false
}
private let acknowledgementsWindow = configure(NSWindow(
contentRect: .zero,
styleMask: [.closable, .resizable, .miniaturizable, .titled],
backing: .buffered,
defer: false
)) {
$0.title = "Xcodes Acknowledgements"
$0.contentView = NSHostingView(rootView: AcknowledgmentsView())
$0.isReleasedWhenClosed = false
}
/// If we wanted to use only SwiftUI API to do this we could make a new WindowGroup and use openURL and handlesExternalEvents.
/// WindowGroup lets the user open more than one window right now, which is a little strange for an About window.
/// (It's also weird that the main Xcode list window can be opened more than once, there should only be one.)
/// To work around this, an AppDelegate holds onto a single instance of an NSWindow that is shown here.
func showAboutWindow() {
aboutWindow.center()
aboutWindow.makeKeyAndOrderFront(nil)
}
func showAcknowledgementsWindow() {
acknowledgementsWindow.center()
acknowledgementsWindow.makeKeyAndOrderFront(nil)
}
}