VMCardView.swift 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. struct VMCardView: View {
  18. @ObservedObject var vm: VMData
  19. @EnvironmentObject private var data: UTMData
  20. #if os(macOS)
  21. typealias PlatformImage = NSImage
  22. #else
  23. typealias PlatformImage = UIImage
  24. #endif
  25. var body: some View {
  26. HStack {
  27. if vm.isShortcut {
  28. Logo(logo: PlatformImage(contentsOfURL: vm.detailsIconUrl))
  29. .overlay(Image(systemName: "arrowshape.turn.up.forward.fill")
  30. .resizable()
  31. .frame(width: 8, height: 8)
  32. .aspectRatio(contentMode: .fit), alignment: .bottomLeading)
  33. } else {
  34. Logo(logo: PlatformImage(contentsOfURL: vm.detailsIconUrl))
  35. }
  36. VStack(alignment: .leading) {
  37. Text(vm.detailsTitleLabel)
  38. .font(.headline)
  39. Text(vm.detailsSubtitleLabel)
  40. .font(.subheadline)
  41. }.lineLimit(1)
  42. .truncationMode(.tail)
  43. Spacer()
  44. if vm.isStopped {
  45. #if !os(visionOS) // tap target too small
  46. Button {
  47. data.run(vm: vm)
  48. } label: {
  49. Label("Run", systemImage: "play.circle.fill")
  50. .font(.largeTitle)
  51. .labelStyle(.iconOnly)
  52. }
  53. #endif
  54. } else if vm.isBusy {
  55. Spinner(size: .large)
  56. }
  57. }.padding([.top, .bottom], 10)
  58. .buttonStyle(.plain)
  59. #if os(macOS)
  60. .onDoubleClick {
  61. data.run(vm: vm)
  62. }
  63. #else
  64. .simultaneousGesture(TapGesture(count: 2).onEnded {
  65. data.run(vm: vm)
  66. })
  67. #endif
  68. }
  69. }
  70. #if os(macOS)
  71. @available(macOS 11, *)
  72. struct Logo: View {
  73. let logo: NSImage?
  74. var body: some View {
  75. Group {
  76. if logo != nil {
  77. Image(nsImage: logo!)
  78. .resizable()
  79. .frame(width: 30.0, height: 30.0)
  80. .aspectRatio(contentMode: .fit)
  81. } else {
  82. Image(systemName: "desktopcomputer")
  83. .resizable()
  84. .frame(width: 30.0, height: 30.0)
  85. .foregroundColor(.accentColor)
  86. }
  87. }
  88. }
  89. }
  90. #else // iOS
  91. struct Logo: View {
  92. let logo: UIImage?
  93. var body: some View {
  94. Group {
  95. if logo != nil {
  96. Image(uiImage: logo!)
  97. .resizable()
  98. .frame(width: 30.0, height: 30.0)
  99. .aspectRatio(contentMode: .fit)
  100. } else {
  101. Image(systemName: "desktopcomputer")
  102. .resizable()
  103. .frame(width: 30.0, height: 30.0)
  104. }
  105. }
  106. }
  107. }
  108. #endif
  109. struct VMCardView_Previews: PreviewProvider {
  110. static var previews: some View {
  111. VMCardView(vm: VMData(from: .empty))
  112. }
  113. }