2
0

VMToolbarDisplayMenuView.swift 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. struct VMToolbarDisplayMenuView: View {
  18. @Binding var state: VMWindowState
  19. @EnvironmentObject private var session: VMSessionState
  20. #if os(visionOS)
  21. @Environment(\.supportsMultipleWindows) private var supportsMultipleWindows
  22. @Environment(\.openWindow) private var openWindow
  23. #else
  24. private var supportsMultipleWindows: Bool {
  25. UIApplication.shared.supportsMultipleScenes
  26. }
  27. #endif
  28. var body: some View {
  29. Menu {
  30. Menu {
  31. Picker("", selection: $state.device) {
  32. MenuLabel("None", systemImage: "rectangle.dashed").tag(nil as VMWindowState.Device?)
  33. ForEach(session.devices) { device in
  34. switch device {
  35. case .serial(_, let index):
  36. MenuLabel("Serial \(index): \(session.qemuConfig.serials[index].target.prettyValue)", systemImage: "rectangle.connected.to.line.below").tag(device as VMWindowState.Device?)
  37. case .display(_, let index):
  38. MenuLabel("Display \(index): \(session.qemuConfig.displays[index].hardware.prettyValue)", systemImage: "display").tag(device as VMWindowState.Device?)
  39. }
  40. }
  41. }
  42. } label: {
  43. MenuLabel("Current Window", systemImage: "rectangle.inset.filled.on.rectangle")
  44. }
  45. if let externalWindowBinding = session.externalWindowBinding {
  46. Menu {
  47. Button {
  48. externalWindowBinding.wrappedValue.toggleDisplayResize()
  49. } label: {
  50. MenuLabel("Zoom/Reset", systemImage: externalWindowBinding.isViewportChanged.wrappedValue ? "arrow.down.right.and.arrow.up.left" : "arrow.up.left.and.arrow.down.right")
  51. }
  52. Picker("", selection: externalWindowBinding.device) {
  53. MenuLabel("None", systemImage: "rectangle.dashed").tag(nil as VMWindowState.Device?)
  54. ForEach(session.devices) { device in
  55. switch device {
  56. case .serial(_, let index):
  57. MenuLabel("Serial \(index): \(session.qemuConfig.serials[index].target.prettyValue)", systemImage: "rectangle.connected.to.line.below").tag(device as VMWindowState.Device?)
  58. case .display(_, let index):
  59. MenuLabel("Display \(index): \(session.qemuConfig.displays[index].hardware.prettyValue)", systemImage: "display").tag(device as VMWindowState.Device?)
  60. }
  61. }
  62. }
  63. } label: {
  64. MenuLabel("External Monitor", systemImage: "rectangle.on.rectangle")
  65. }
  66. }
  67. if supportsMultipleWindows {
  68. Divider()
  69. Button {
  70. #if os(visionOS)
  71. openWindow(value: session.newWindow())
  72. #else
  73. UIApplication.shared.requestSceneSessionActivation(nil, userActivity: nil, options: nil, errorHandler: nil)
  74. #endif
  75. } label: {
  76. MenuLabel("New Window…", systemImage: "plus.rectangle.on.rectangle")
  77. }
  78. }
  79. } label: {
  80. Label("Display", systemImage: "rectangle.on.rectangle")
  81. }.overlay(Badge(count: session.devices.count), alignment: .topTrailing)
  82. }
  83. }
  84. private struct Badge: View {
  85. let count: Int
  86. var body: some View {
  87. if count > 1 {
  88. ZStack(alignment: .center) {
  89. Circle().fill(.white)
  90. Image(systemName: count <= 50 ? "\(count).circle.fill" : "infinity.circle.fill")
  91. .foregroundColor(.red)
  92. }.frame(width: 16, height: 16)
  93. .allowsHitTesting(false)
  94. } else {
  95. EmptyView()
  96. }
  97. }
  98. }
  99. private extension View {
  100. @ViewBuilder
  101. func customBadge(_ count: Int) -> some View {
  102. if #available(iOS 15, *) {
  103. self.badge(count)
  104. } else {
  105. self.overlay(Badge(count: count), alignment: .topTrailing)
  106. }
  107. }
  108. }