UTMPlaceholderVMView.swift 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 UTMPlaceholderVMView<Content>: View where Content: View {
  18. let title: String
  19. let subtitle: String
  20. let progress: CGFloat?
  21. let imageOverlaySystemName: String
  22. let popover: () -> Content
  23. let onRemove: () -> Void
  24. @State private var showingDetails = false
  25. #if os(macOS)
  26. @State private var showRemoveButton = false
  27. #endif
  28. var body: some View {
  29. HStack(alignment: .center) {
  30. /// Computer with download symbol on its screen
  31. Image(systemName: "desktopcomputer")
  32. .resizable()
  33. .frame(width: 30.0, height: 30.0)
  34. .aspectRatio(contentMode: .fit)
  35. .overlay(
  36. Image(systemName: imageOverlaySystemName)
  37. .font(Font.caption.weight(Font.Weight.medium))
  38. .offset(y: -5)
  39. )
  40. .foregroundColor(.gray)
  41. VStack(alignment: .leading) {
  42. Text(title)
  43. .font(.headline)
  44. if let progress = progress {
  45. MinimalProgressView(fractionCompleted: progress)
  46. }
  47. Text(subtitle)
  48. .font(.caption)
  49. }
  50. .foregroundColor(.gray)
  51. #if os(macOS)
  52. Spacer()
  53. /// macOS gets an on-hover cancel button
  54. Button(action: onRemove, label: {
  55. Image(systemName: "xmark.circle")
  56. .accessibility(label: Text("Remove"))
  57. })
  58. .clipShape(Circle())
  59. .disabled(!showRemoveButton)
  60. .opacity(showRemoveButton ? 1 : 0)
  61. #endif
  62. }.padding([.top, .bottom], 10)
  63. .onTapGesture(perform: toggleDetailsPopup)
  64. .popover(isPresented: $showingDetails, content: popover)
  65. .onDisappear {
  66. showingDetails = false
  67. }
  68. #if os(macOS)
  69. .onHover(perform: { hovering in
  70. self.showRemoveButton = hovering
  71. })
  72. #endif
  73. }
  74. private func toggleDetailsPopup() {
  75. showingDetails.toggle()
  76. }
  77. }
  78. struct MinimalProgressView: View {
  79. let fractionCompleted: CGFloat
  80. private var accessibilityLabel: String {
  81. return NumberFormatter.localizedString(from: fractionCompleted as NSNumber, number: .percent)
  82. }
  83. var body: some View {
  84. Text(" ") /// to create a seamless layout with the rest of the text
  85. .font(.subheadline)
  86. .frame(maxWidth: .infinity)
  87. .overlay(
  88. GeometryReader { frame in
  89. RoundedRectangle(cornerRadius: frame.size.height/5)
  90. .fill(Color.secondary)
  91. .frame(width: frame.size.width, height: frame.size.height/3)
  92. .offset(y: frame.size.height/3)
  93. RoundedRectangle(cornerRadius: frame.size.height/5)
  94. .fill(Color.accentColor)
  95. .frame(width: frame.size.width * fractionCompleted, height: frame.size.height/3)
  96. .offset(y: frame.size.height/3)
  97. }
  98. )
  99. .accessibilityLabel(accessibilityLabel)
  100. }
  101. }
  102. struct UTMPlaceholderVMView_Previews: PreviewProvider {
  103. static var previews: some View {
  104. UTMPlaceholderVMView(title: "Title", subtitle: "Subtitle", progress: nil, imageOverlaySystemName: "arrow.down.circle.fill", popover: {
  105. EmptyView()
  106. }, onRemove: {
  107. }).frame(width: 350, height: 100)
  108. }
  109. }