DevelopmentPreview.swift 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #if DEBUG
  2. import Foundation
  3. import SwiftUI
  4. /// Isolated, debug-only fixtures for simulator layout snapshots. Never uses player storage.
  5. enum DevelopmentPreview {
  6. static var screen: String? {
  7. let arguments = ProcessInfo.processInfo.arguments
  8. guard let index = arguments.firstIndex(of: "-joker-preview"), arguments.indices.contains(index + 1) else { return nil }
  9. return arguments[index + 1]
  10. }
  11. static var isEnabled: Bool { screen != nil }
  12. static var viewport: CGSize? {
  13. let arguments = ProcessInfo.processInfo.arguments
  14. guard isEnabled, let index = arguments.firstIndex(of: "-joker-capture-size"), arguments.indices.contains(index + 1) else { return nil }
  15. let dimensions = arguments[index + 1].split(separator: "x").compactMap { Double($0) }
  16. guard dimensions.count == 2, dimensions.allSatisfy({ $0 >= 300 && $0 <= 1600 }) else { return nil }
  17. return CGSize(width: dimensions[0], height: dimensions[1])
  18. }
  19. static var language: AppLanguage {
  20. let arguments = ProcessInfo.processInfo.arguments
  21. guard let index = arguments.firstIndex(of: "-joker-language"), arguments.indices.contains(index + 1) else { return .english }
  22. return AppLanguage(rawValue: arguments[index + 1]) ?? .english
  23. }
  24. @MainActor static func prepare(_ store: GameStore) async {
  25. if screen == "platform-live-probe" { await PlatformDiagnostics.run(); return }
  26. if screen == "facebook-sdk-configuration" {
  27. FacebookSDKClient.verifyConfigurationAPI()
  28. return
  29. }
  30. guard let screen, ["continue", "game", "daily-resume"].contains(screen) else { return }
  31. await store.startGame(difficulty: .hard, day: screen == "continue" ? nil : .now)
  32. if screen != "game" {
  33. _ = store.hint(at: nil)
  34. store.leaveGame()
  35. }
  36. }
  37. }
  38. /// Fit a chosen window size inside the simulator screen; never changes device orientation.
  39. struct DevelopmentPreviewViewport: ViewModifier {
  40. @ViewBuilder func body(content: Content) -> some View {
  41. if let size = DevelopmentPreview.viewport {
  42. GeometryReader { geometry in
  43. content.frame(width: size.width, height: size.height)
  44. .scaleEffect(min(geometry.size.width / size.width, geometry.size.height / size.height))
  45. .frame(width: geometry.size.width, height: geometry.size.height)
  46. }.background(Palette.background)
  47. } else { content }
  48. }
  49. }
  50. #endif