UTMExternalSceneDelegate.swift 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. //
  2. // Copyright © 2022 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. class UTMExternalSceneDelegate: NSObject, UIWindowSceneDelegate, ObservableObject {
  18. var window: UIWindow?
  19. var screen: UIScreen?
  20. func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
  21. guard let windowScene = (scene as? UIWindowScene) else { return }
  22. if session.role == .windowExternalDisplay {
  23. let window = UIWindow(windowScene: windowScene)
  24. let viewController = UIHostingController(rootView: UTMSingleWindowView())
  25. window.rootViewController = viewController
  26. self.window = window
  27. window.isHidden = false
  28. setupDisplayLinkIfNecessary()
  29. }
  30. }
  31. func windowScene(_ windowScene: UIWindowScene, didUpdate previousCoordinateSpace: UICoordinateSpace, interfaceOrientation previousInterfaceOrientation: UIInterfaceOrientation, traitCollection previousTraitCollection: UITraitCollection) {
  32. setupDisplayLinkIfNecessary()
  33. }
  34. weak var linkedScreen: UIScreen?
  35. func setupDisplayLinkIfNecessary() {
  36. let currentScreen = self.screen
  37. if currentScreen != linkedScreen {
  38. // Set up display link
  39. self.linkedScreen = currentScreen
  40. }
  41. }
  42. }