Add `PreviewProvider` protocol (#328)

* Add `PreviewProvider` protocol

No functionality behind, just makes it easier to integrate with existing SwiftUI projects.

* Update PreviewProvider.swift

* Add missing preview-related types and modifiers
This commit is contained in:
Max Desiatov 2020-12-07 17:55:20 +00:00 committed by GitHub
parent 8230e98072
commit 302cd3b108
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 74 additions and 0 deletions

View File

@ -0,0 +1,64 @@
// Copyright 2020 Tokamak contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/// This protocol has no functionality currently, and is only provided for compatibility purposes.
public protocol PreviewProvider {
associatedtype Previews: View
@ViewBuilder static var previews: Previews { get }
}
public struct PreviewDevice: RawRepresentable, ExpressibleByStringLiteral {
public let rawValue: String
public init(rawValue: String) {
self.rawValue = rawValue
}
public init(stringLiteral: String) {
rawValue = stringLiteral
}
}
public protocol PreviewContextKey {
associatedtype Value
static var defaultValue: Self.Value { get }
}
public protocol PreviewContext {
subscript<Key>(key: Key.Type) -> Key.Value where Key: PreviewContextKey { get }
}
public enum PreviewLayout {
case device
case sizeThatFits
case fixed(width: CGFloat, height: CGFloat)
}
public extension View {
func previewDevice(_ value: PreviewDevice?) -> some View {
self
}
func previewLayout(_ value: PreviewLayout) -> some View {
self
}
func previewDisplayName(_ value: String?) -> some View {
self
}
func previewContext<C>(_ value: C) -> some View where C: PreviewContext {
self
}
}

View File

@ -139,3 +139,5 @@ public extension Text {
_concatenating(lhs: lhs, rhs: rhs)
}
}
public typealias PreviewProvider = TokamakCore.PreviewProvider

View File

@ -27,3 +27,9 @@ public struct ToggleDemo: View {
}
}
}
struct ToggleDemo_Previews: PreviewProvider {
static var previews: some View {
ToggleDemo()
}
}

View File

@ -97,3 +97,5 @@ public extension Text {
_concatenating(lhs: lhs, rhs: rhs)
}
}
public typealias PreviewProvider = TokamakCore.PreviewProvider