UTMApp.swift 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. //
  2. // Copyright © 2020 osy. All rights reserved.
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. //
  16. import SwiftUI
  17. import AppIntents
  18. struct UTMApp: App {
  19. let data: UTMData
  20. @NSApplicationDelegateAdaptor(AppDelegate.self) var appDelegate: AppDelegate
  21. init() {
  22. let data = UTMData()
  23. self.data = data
  24. if #available(macOS 13, *) {
  25. AppDependencyManager.shared.add(dependency: data)
  26. }
  27. }
  28. @ViewBuilder
  29. var homeWindow: some View {
  30. ContentView().environmentObject(data)
  31. .onAppear {
  32. appDelegate.data = data
  33. NSApp.scriptingDelegate = appDelegate
  34. }
  35. .onReceive(.vmSessionError) { notification in
  36. if let message = notification.userInfo?["Message"] as? String {
  37. data.showErrorAlert(message: message)
  38. }
  39. }
  40. }
  41. @SceneBuilder
  42. var oldBody: some Scene {
  43. WindowGroup {
  44. homeWindow
  45. }.commands {
  46. VMCommands()
  47. }
  48. Settings {
  49. SettingsView()
  50. }
  51. }
  52. @available(macOS 13, *)
  53. @SceneBuilder
  54. var newBody: some Scene {
  55. Window("UTM Library", id: "home") {
  56. homeWindow
  57. .navigationTitle("UTM")
  58. }.commands {
  59. VMCommands()
  60. }
  61. Settings {
  62. SettingsView()
  63. }
  64. UTMMenuBarExtraScene(data: data)
  65. Window("UTM Server", id: "server") {
  66. UTMServerView().environmentObject(data.remoteServer.state)
  67. }
  68. }
  69. // HACK: SwiftUI doesn't provide if-statement support in SceneBuilder
  70. var body: some Scene {
  71. if #available(macOS 13, *) {
  72. return newBody
  73. } else {
  74. return oldBody
  75. }
  76. }
  77. }