Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add @inlinable where SwiftUI has it #397

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions Sources/TokamakCore/Environment/Environment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,21 @@ protocol EnvironmentReader {
}

@propertyWrapper public struct Environment<Value>: DynamicProperty {
enum Content {
@usableFromInline enum Content {
case keyPath(KeyPath<EnvironmentValues, Value>)
case value(Value)
}

private var content: Content
private let keyPath: KeyPath<EnvironmentValues, Value>
@usableFromInline var content: Content
@inlinable
public init(_ keyPath: KeyPath<EnvironmentValues, Value>) {
content = .keyPath(keyPath)
self.keyPath = keyPath
}

mutating func setContent(from values: EnvironmentValues) {
content = .value(values[keyPath: keyPath])
if case let .keyPath(keyPath) = content {
content = .value(values[keyPath: keyPath])
}
}

public var wrappedValue: Value {
Expand Down
2 changes: 2 additions & 0 deletions Sources/TokamakCore/Environment/EnvironmentKey.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public struct _EnvironmentKeyWritingModifier<Value>: ViewModifier, EnvironmentMo
public let keyPath: WritableKeyPath<EnvironmentValues, Value>
public let value: Value

@inlinable
public init(keyPath: WritableKeyPath<EnvironmentValues, Value>, value: Value) {
self.keyPath = keyPath
self.value = value
Expand All @@ -38,6 +39,7 @@ public struct _EnvironmentKeyWritingModifier<Value>: ViewModifier, EnvironmentMo
}

public extension View {
@inlinable
func environment<V>(
_ keyPath: WritableKeyPath<EnvironmentValues, V>,
_ value: V
Expand Down
4 changes: 4 additions & 0 deletions Sources/TokamakCore/Modifiers/PaddingLayout.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public struct _PaddingLayout: ViewModifier {
public var edges: Edge.Set
public var insets: EdgeInsets?

@inlinable
public init(edges: Edge.Set = .all, insets: EdgeInsets?) {
self.edges = edges
self.insets = insets
Expand All @@ -27,15 +28,18 @@ public struct _PaddingLayout: ViewModifier {
}

public extension View {
@inlinable
func padding(_ insets: EdgeInsets) -> some View {
modifier(_PaddingLayout(insets: insets))
}

@inlinable
func padding(_ edges: Edge.Set = .all, _ length: CGFloat? = nil) -> some View {
let insets = length.map { EdgeInsets(_all: $0) }
return modifier(_PaddingLayout(edges: edges, insets: insets))
}

@inlinable
func padding(_ length: CGFloat) -> some View {
padding(.all, length)
}
Expand Down
5 changes: 5 additions & 0 deletions Sources/TokamakCore/Modifiers/StyleModifiers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public struct _BackgroundModifier<Background>: ViewModifier, EnvironmentReader
public var background: Background
public var alignment: Alignment

@inlinable
public init(background: Background, alignment: Alignment = .center) {
self.background = background
self.alignment = alignment
Expand Down Expand Up @@ -50,6 +51,7 @@ extension _BackgroundModifier: Equatable where Background: Equatable {
}

public extension View {
@inlinable
func background<Background>(
_ background: Background,
alignment: Alignment = .center
Expand All @@ -65,6 +67,7 @@ public struct _OverlayModifier<Overlay>: ViewModifier, EnvironmentReader
public var overlay: Overlay
public var alignment: Alignment

@inlinable
public init(overlay: Overlay, alignment: Alignment = .center) {
self.overlay = overlay
self.alignment = alignment
Expand All @@ -90,12 +93,14 @@ extension _OverlayModifier: Equatable where Overlay: Equatable {
}

public extension View {
@inlinable
func overlay<Overlay>(_ overlay: Overlay, alignment: Alignment = .center) -> some View
where Overlay: View
{
modifier(_OverlayModifier(overlay: overlay, alignment: alignment))
}

@inlinable
func border<S>(_ content: S, width: CGFloat = 1) -> some View where S: ShapeStyle {
overlay(Rectangle().strokeBorder(content, lineWidth: width))
}
Expand Down
8 changes: 4 additions & 4 deletions Sources/TokamakCore/MountedViews/MountedElement.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,10 @@ public class MountedElement<R: Renderer> {
var typeConstructorName: String {
switch element {
case .app: fatalError("""
`App` values aren't supposed to be reconciled, thus the type constructor name is not stored \
for `App` elements. Please report this crash as a bug at \
https://github.com/swiftwasm/Tokamak/issues/new
""")
`App` values aren't supposed to be reconciled, thus the type constructor name is not stored \
for `App` elements. Please report this crash as a bug at \
https://github.com/swiftwasm/Tokamak/issues/new
""")
case let .scene(scene): return scene.typeConstructorName
case let .view(view): return view.typeConstructorName
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/TokamakCore/MountedViews/MountedScene.swift
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ extension _AnyScene {
) -> MountedScene<R> {
var title: String?
if let titledSelf = scene as? TitledScene,
let text = titledSelf.title
let text = titledSelf.title
{
title = _TextProxy(text).rawText
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public struct _PreferenceActionModifier<Key>: _PreferenceWritingModifierProtocol
where Key: PreferenceKey, Key.Value: Equatable
{
public let action: (Key.Value) -> ()
@inlinable
public init(action: @escaping (Key.Value) -> Swift.Void) {
self.action = action
}
Expand Down
4 changes: 4 additions & 0 deletions Sources/TokamakCore/Preferences/_PreferenceReadingView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public struct _DelayedPreferenceView<Key, Content>: View, _PreferenceReadingView
)
public let transform: (_PreferenceValue<Key>) -> Content

@inlinable
public init(transform: @escaping (_PreferenceValue<Key>) -> Content) {
self.transform = transform
}
Expand All @@ -39,6 +40,7 @@ public struct _DelayedPreferenceView<Key, Content>: View, _PreferenceReadingView
}

public extension PreferenceKey {
@inlinable
static func _delay<T>(
_ transform: @escaping (_PreferenceValue<Self>) -> T
) -> some View
Expand All @@ -49,6 +51,7 @@ public extension PreferenceKey {
}

public extension View {
@inlinable
func overlayPreferenceValue<Key, T>(
_ key: Key.Type = Key.self,
@ViewBuilder _ transform: @escaping (Key.Value) -> T
Expand All @@ -58,6 +61,7 @@ public extension View {
Key._delay { self.overlay(transform($0.value)) }
}

@inlinable
func backgroundPreferenceValue<Key, T>(
_ key: Key.Type = Key.self,
@ViewBuilder _ transform: @escaping (Key.Value) -> T
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public struct _PreferenceTransformModifier<Key>: _PreferenceWritingModifierProto
{
public let transform: (inout Key.Value) -> ()

@inlinable
public init(
key _: Key.Type = Key.self,
transform: @escaping (inout Key.Value) -> ()
Expand All @@ -37,6 +38,7 @@ public struct _PreferenceTransformModifier<Key>: _PreferenceWritingModifierProto
}

public extension View {
@inlinable
func transformPreference<K>(
_ key: K.Type = K.self,
_ callback: @escaping (inout K.Value) -> ()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public struct _PreferenceWritingModifier<Key>: _PreferenceWritingModifierProtoco
where Key: PreferenceKey
{
public let value: Key.Value
@inlinable
public init(key: Key.Type = Key.self, value: Key.Value) {
self.value = value
}
Expand All @@ -36,6 +37,7 @@ extension _PreferenceWritingModifier: Equatable where Key.Value: Equatable {
}

public extension View {
@inlinable
func preference<K>(key: K.Type = K.self, value: K.Value) -> some View
where K: PreferenceKey
{
Expand Down
1 change: 1 addition & 0 deletions Sources/TokamakCore/Shapes/ModifiedShapes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public struct _StrokedShape<S>: Shape where S: Shape {
public var shape: S
public var style: StrokeStyle

@inlinable
public init(shape: S, style: StrokeStyle) {
self.shape = shape
self.style = style
Expand Down
1 change: 1 addition & 0 deletions Sources/TokamakCore/Shapes/Shape.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public struct _ShapeView<Content, Style>: PrimitiveView where Content: Shape, St
public var style: Style
public var fillStyle: FillStyle

@inlinable
public init(shape: Content, style: Style, fillStyle: FillStyle = FillStyle()) {
self.shape = shape
self.style = style
Expand Down
4 changes: 3 additions & 1 deletion Sources/TokamakCore/State/Binding.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ typealias Updater<T> = (inout T) -> ()
view's state in-place synchronously, but only schedule an update with
the renderer at a later time.
*/
@propertyWrapper public struct Binding<Value>: DynamicProperty {
@propertyWrapper
@dynamicMemberLookup
public struct Binding<Value>: DynamicProperty {
public var wrappedValue: Value {
get { get() }
nonmutating set { set(newValue) }
Expand Down
10 changes: 8 additions & 2 deletions Sources/TokamakCore/Tokens/Angle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,32 +17,37 @@

public struct Angle: AdditiveArithmetic {
public var radians: Double
public var degrees: Double {
@inlinable public var degrees: Double {
get { radians * (180.0 / .pi) }
set { radians = newValue * (.pi / 180.0) }
}

@inlinable
public init() {
self.init(radians: 0.0)
}

@inlinable
public init(radians: Double) {
self.radians = radians
}

@inlinable
public init(degrees: Double) {
self.init(radians: degrees * (.pi / 180.0))
}

@inlinable
public static func radians(_ radians: Double) -> Angle {
Angle(radians: radians)
}

@inlinable
public static func degrees(_ degrees: Double) -> Angle {
Angle(degrees: degrees)
}

public static let zero: Angle = .radians(0)
@inlinable public static var zero: Angle { .init() }

public static func + (lhs: Self, rhs: Self) -> Self {
.radians(lhs.radians + rhs.radians)
Expand All @@ -64,6 +69,7 @@ public struct Angle: AdditiveArithmetic {
}

extension Angle: Hashable, Comparable {
@inlinable
public static func < (lhs: Self, rhs: Self) -> Bool {
lhs.radians < rhs.radians
}
Expand Down
2 changes: 2 additions & 0 deletions Sources/TokamakCore/Tokens/Color.swift
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,7 @@ public extension EnvironmentValues {
}

public extension View {
@inlinable
func accentColor(_ accentColor: Color?) -> some View {
environment(\.accentColor, accentColor)
}
Expand All @@ -388,6 +389,7 @@ public extension EnvironmentValues {
}

public extension View {
@inlinable
func foregroundColor(_ color: Color?) -> some View {
environment(\.foregroundColor, color)
}
Expand Down
1 change: 1 addition & 0 deletions Sources/TokamakCore/Tokens/ColorScheme.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public extension EnvironmentValues {
}

public extension View {
@inlinable
func colorScheme(_ colorScheme: ColorScheme) -> some View {
environment(\.colorScheme, colorScheme)
}
Expand Down
1 change: 1 addition & 0 deletions Sources/TokamakCore/Tokens/Font.swift
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,7 @@ public extension EnvironmentValues {
}

public extension View {
@inlinable
func font(_ font: Font?) -> some View {
environment(\.font, font)
}
Expand Down
13 changes: 8 additions & 5 deletions Sources/TokamakCore/Views/Containers/ForEach.swift
Original file line number Diff line number Diff line change
Expand Up @@ -104,24 +104,27 @@ public protocol _AnyIDView {
var anyContent: AnyView { get }
}

@usableFromInline
struct IDView<Content, ID>: View, _AnyIDView where Content: View, ID: Hashable {
let content: Content
let id: ID
var anyId: AnyHashable { AnyHashable(id) }
var anyContent: AnyView { AnyView(content) }
@usableFromInline let content: Content
@usableFromInline let id: ID
@usableFromInline var anyId: AnyHashable { AnyHashable(id) }
@usableFromInline var anyContent: AnyView { AnyView(content) }

@inlinable
init(_ content: Content, id: ID) {
self.content = content
self.id = id
}

var body: some View {
@usableFromInline var body: some View {
content
.environment(\._id, AnyHashable(id))
}
}

public extension View {
@inlinable
func id<ID>(_ id: ID) -> some View where ID: Hashable {
IDView(self, id: id)
}
Expand Down
3 changes: 2 additions & 1 deletion Sources/TokamakCore/Views/Containers/Group.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
// limitations under the License.

public struct Group<Content> {
let content: Content
@usableFromInline let content: Content
@inlinable
public init(@ViewBuilder content: () -> Content) {
self.content = content()
}
Expand Down
1 change: 1 addition & 0 deletions Sources/TokamakCore/Views/Layout/GeometryReader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public func makeProxy(from size: CGSize) -> GeometryProxy {

public struct GeometryReader<Content>: PrimitiveView where Content: View {
public let content: (GeometryProxy) -> Content
@inlinable
public init(@ViewBuilder content: @escaping (GeometryProxy) -> Content) {
self.content = content
}
Expand Down
1 change: 1 addition & 0 deletions Sources/TokamakCore/Views/Layout/HStack.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public struct HStack<Content>: PrimitiveView where Content: View {
public let spacing: CGFloat?
public let content: Content

@inlinable
public init(
alignment: VerticalAlignment = .center,
spacing: CGFloat? = nil,
Expand Down
1 change: 1 addition & 0 deletions Sources/TokamakCore/Views/Layout/VStack.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public struct VStack<Content>: PrimitiveView where Content: View {
public let spacing: CGFloat?
public let content: Content

@inlinable
public init(
alignment: HorizontalAlignment = .center,
spacing: CGFloat? = nil,
Expand Down
2 changes: 2 additions & 0 deletions Sources/TokamakCore/Views/Layout/ZStack.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public struct Alignment: Equatable {
public var horizontal: HorizontalAlignment
public var vertical: VerticalAlignment

@inlinable
public init(
horizontal: HorizontalAlignment,
vertical: VerticalAlignment
Expand Down Expand Up @@ -48,6 +49,7 @@ public struct ZStack<Content>: PrimitiveView where Content: View {
public let spacing: CGFloat?
public let content: Content

@inlinable
public init(
alignment: Alignment = .center,
spacing: CGFloat? = nil,
Expand Down
Loading