Files
VirtualBuddy/VirtualBuddyGuest/GuestAppDelegate.swift
Guilherme Rambo 9af56efdf1 Guest metadata service & UI (#802)
* Add new guest system information service to collect up-to-date details about the guest that can be displayed offline in the host

* Tweak get info wording

* Update agents

* Add preview library item with guest system info

* Improve VM metadata overlays in library view

* Xcode update

* Improve VM get info UI
2026-11-03 12:51:13 -03:00

107 lines
3.2 KiB
Swift

import Cocoa
import SwiftUI
import VirtualUI
import VirtualWormhole
import OSLog
@NSApplicationMain
@MainActor
final class GuestAppDelegate: NSObject, NSApplicationDelegate {
private let logger = Logger(subsystem: Bundle.main.bundleIdentifier ?? "Guest", category: "GuestAppDelegate")
private lazy var launchAtLoginManager = GuestLaunchAtLoginManager()
private let hostConnection = GuestHostSession()
private var connectionTask: Task<Void, Never>?
deinit { connectionTask?.cancel() }
private lazy var sharedFolders = GuestSharedFoldersManager()
private lazy var dashboardItem: StatusItemManager = {
StatusItemManager(
configuration: .default.id("dashboard"),
statusItem: .button(label: { Image("StatusItem") }),
content: GuestDashboard()
.environmentObject(self.launchAtLoginManager)
.environment(self.hostConnection)
.environmentObject(self.sharedFolders)
)
}()
private var shouldShowPanelAfterLaunching: Bool {
get { !UserDefaults.standard.bool(forKey: "shownPanelOnFirstLauch") || UserDefaults.standard.bool(forKey: "ShowPanelOnLaunch") }
set {
UserDefaults.standard.set(!newValue, forKey: "shownPanelOnFirstLauch")
UserDefaults.standard.synchronize()
}
}
private let installer = GuestAppInstaller()
func applicationWillFinishLaunching(_ notification: Notification) {
do {
try installer.installIfNeeded()
} catch {
let alert = NSAlert(error: error)
alert.runModal()
}
}
func applicationDidFinishLaunching(_ notification: Notification) {
/// Skip regular app activation if installation is needed (i.e. running from disk image).
guard !installer.needsInstall else { return }
launchAtLoginManager.autoEnableIfNeeded()
connectionTask = hostConnection.start()
Task {
try? await sharedFolders.mount()
}
dashboardItem.install()
perform(#selector(showPanelForFirstLaunchIfNeeded), with: nil, afterDelay: 0.5)
}
func applicationShouldHandleReopen(_ sender: NSApplication, hasVisibleWindows flag: Bool) -> Bool {
showPanel()
return true
}
@objc private func showPanelForFirstLaunchIfNeeded() {
guard shouldShowPanelAfterLaunching else { return }
shouldShowPanelAfterLaunching = false
NSObject.cancelPreviousPerformRequests(withTarget: self, selector: #selector(showPanelForFirstLaunchIfNeeded), object: nil)
showPanel()
}
@objc private func showPanel() {
dashboardItem.showPanel()
}
private var startedFinalSendBeforeTermination = false
func applicationShouldTerminate(_ sender: NSApplication) -> NSApplication.TerminateReply {
logger.debug(#function)
guard !startedFinalSendBeforeTermination else { return .terminateLater }
startedFinalSendBeforeTermination = true
Task {
await hostConnection.prepareForTermination()
NSApp.reply(toApplicationShouldTerminate: true)
}
return .terminateLater
}
}