-
-
Save sturdysturge/1737e58a8ff21ff8e54543b72537c7fe to your computer and use it in GitHub Desktop.
RevDoc PreviewContext 2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct LargePreviewContextKey: PreviewContextKey { | |
static var defaultValue: Custom = .veryLarge | |
typealias Value = Custom | |
} | |
enum Custom { | |
case large, veryLarge, tooBig | |
var size: CGSize { | |
switch self { | |
case .large: | |
return CGSize(width: 400, height: 400) | |
case .veryLarge: | |
return CGSize(width: 500, height: 500) | |
case .tooBig: | |
return CGSize(width: 600, height: 600) | |
} | |
} | |
} | |
struct LargePreviewContext: PreviewContext { | |
subscript<Key>(key: Key.Type) -> Key.Value where Key : PreviewContextKey { | |
return key.defaultValue | |
} | |
let custom: Custom | |
public init(family: Custom) { | |
self.custom = family | |
} | |
} | |
extension View { | |
public func previewContext<C>(custom: C) -> some View where C : PreviewContext { | |
return Group { | |
if let context = custom as? LargePreviewContext { | |
self.previewLayout(.fixed(width: context.custom.size.width, height: context.custom.size.height))} | |
else { | |
self.previewContext(custom) | |
} | |
} | |
} | |
} | |
struct ContentView_Previews: PreviewProvider { | |
static var previews: some View { | |
ContentView() | |
.previewContext(LargePreviewContext(family: .veryLarge)) | |
ContentView() | |
.previewContext(custom: LargePreviewContext(family: .veryLarge)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment