Skip to content

Commit

Permalink
Differentiate the main UTM window from VM windows
Browse files Browse the repository at this point in the history
Fixes utmapp#3099 by setting isExcludedFromWindowsMenu, then adding a custom
'UTM' menu item that brings the main UTM window front
  • Loading branch information
ktprograms committed Oct 2, 2021
1 parent 8b8eeee commit a6ad188
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
12 changes: 12 additions & 0 deletions Platform/Shared/VMCommands.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ import SwiftUI
@available(iOS 14, macOS 11, *)
struct VMCommands: Commands {
@Environment(\.openURL) private var openURL
#if os(macOS)
var appDelegate: AppDelegate
#endif

@CommandsBuilder
var body: some Commands {
Expand All @@ -34,6 +37,15 @@ struct VMCommands: Commands {
}
SidebarCommands()
ToolbarCommands()
#if os(macOS)
CommandGroup(replacing: .windowArrangement) {
Button {
appDelegate.mainWindow?.makeKeyAndOrderFront(nil)
} label: {
Text("UTM")
}
}
#endif
CommandGroup(replacing: .help) {
Button(action: { openLink("https://mac.getutm.app/gallery/") }, label: {
Text("Virtual Machine Gallery")
Expand Down
9 changes: 8 additions & 1 deletion Platform/UTMApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,14 @@ struct UTMApp: App {
var body: some Scene {
WindowGroup {
ContentView().environmentObject(data)
}.commands { VMCommands() }
}.commands {
#if os(macOS)
// Since you can't make a ApplicationDelegateAdaptor a StateObject, we have to go the whole hog and pass the entire appDelegate.
VMCommands(appDelegate: appDelegate)
#else
VMCommands()
#endif
}
#if os(macOS)
Settings {
SettingsView()
Expand Down
20 changes: 20 additions & 0 deletions Platform/macOS/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,27 @@
//

class AppDelegate: NSObject, NSApplicationDelegate {
var mainWindow: NSWindow?

func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool {
true
}

func applicationDidFinishLaunching(_ notification: Notification) {
if let window = NSApplication.shared.windows.first {
mainWindow = window
window.isExcludedFromWindowsMenu = true
}
}

func applicationDockMenu(_ sender: NSApplication) -> NSMenu? {
let menu = NSMenu()
let utmItem = NSMenuItem(title: "UTM", action: #selector(mainWindowFront), keyEquivalent: "")
menu.addItem(utmItem)
return menu
}

@objc func mainWindowFront() {
mainWindow?.makeKeyAndOrderFront(nil)
}
}

0 comments on commit a6ad188

Please sign in to comment.