diff --git a/NativeDemo/TokamakDemo.xcodeproj/project.pbxproj b/NativeDemo/TokamakDemo.xcodeproj/project.pbxproj index 2aeb75782..153cc2b8d 100644 --- a/NativeDemo/TokamakDemo.xcodeproj/project.pbxproj +++ b/NativeDemo/TokamakDemo.xcodeproj/project.pbxproj @@ -7,6 +7,8 @@ objects = { /* Begin PBXBuildFile section */ + 854A1A9124B3E3630027BC32 /* ToggleDemo.swift in Sources */ = {isa = PBXBuildFile; fileRef = 85CBD5DE24B3BF090066468A /* ToggleDemo.swift */; }; + 854A1A9324B3F28F0027BC32 /* ToggleDemo.swift in Sources */ = {isa = PBXBuildFile; fileRef = 85CBD5DE24B3BF090066468A /* ToggleDemo.swift */; }; 85ED186A24AD38F20085DFA0 /* UIAppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 85ED186924AD38F20085DFA0 /* UIAppDelegate.swift */; }; 85ED188A24AD3CD60085DFA0 /* macOS.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 85ED188724AD3CC30085DFA0 /* macOS.storyboard */; }; 85ED188C24AD3CF10085DFA0 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 85ED188B24AD3CF10085DFA0 /* LaunchScreen.storyboard */; }; @@ -72,6 +74,7 @@ /* End PBXCopyFilesBuildPhase section */ /* Begin PBXFileReference section */ + 85CBD5DE24B3BF090066468A /* ToggleDemo.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ToggleDemo.swift; sourceTree = ""; }; 85ED184A24AD379A0085DFA0 /* TokamakDemo Native.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "TokamakDemo Native.app"; sourceTree = BUILT_PRODUCTS_DIR; }; 85ED185224AD379A0085DFA0 /* TokamakDemo Native.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "TokamakDemo Native.app"; sourceTree = BUILT_PRODUCTS_DIR; }; 85ED186924AD38F20085DFA0 /* UIAppDelegate.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UIAppDelegate.swift; sourceTree = ""; }; @@ -162,6 +165,7 @@ 85ED189D24AD425E0085DFA0 /* TokamakDemo.swift */, 85ED189E24AD425E0085DFA0 /* Counter.swift */, 85ED189F24AD425E0085DFA0 /* TextFieldDemo.swift */, + 85CBD5DE24B3BF090066468A /* ToggleDemo.swift */, 85ED18A024AD425E0085DFA0 /* EnvironmentDemo.swift */, B51F214F24B920B400CF2583 /* PathDemo.swift */, B56F22DF24BC89FD001738DF /* ColorDemo.swift */, @@ -321,6 +325,7 @@ 85ED18A924AD425E0085DFA0 /* TokamakDemo.swift in Sources */, 85ED18AD24AD425E0085DFA0 /* TextFieldDemo.swift in Sources */, 85ED18A724AD425E0085DFA0 /* ForEachDemo.swift in Sources */, + 854A1A9124B3E3630027BC32 /* ToggleDemo.swift in Sources */, 85ED18A524AD425E0085DFA0 /* TextDemo.swift in Sources */, 85ED18AB24AD425E0085DFA0 /* Counter.swift in Sources */, ); @@ -342,6 +347,7 @@ 85ED18B624AD42D70085DFA0 /* NSAppDelegate.swift in Sources */, 85ED18AC24AD425E0085DFA0 /* Counter.swift in Sources */, 85ED18A824AD425E0085DFA0 /* ForEachDemo.swift in Sources */, + 854A1A9324B3F28F0027BC32 /* ToggleDemo.swift in Sources */, 85ED18AE24AD425E0085DFA0 /* TextFieldDemo.swift in Sources */, 85ED18A624AD425E0085DFA0 /* TextDemo.swift in Sources */, ); @@ -490,7 +496,7 @@ ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor; CLANG_ENABLE_MODULES = YES; CODE_SIGN_STYLE = Automatic; - DEVELOPMENT_TEAM = ""; + DEVELOPMENT_TEAM = 288H3WAR3W; ENABLE_PREVIEWS = YES; INFOPLIST_FILE = "iOS Info.plist"; IPHONEOS_DEPLOYMENT_TARGET = 13.0; @@ -512,7 +518,7 @@ ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor; CLANG_ENABLE_MODULES = YES; CODE_SIGN_STYLE = Automatic; - DEVELOPMENT_TEAM = ""; + DEVELOPMENT_TEAM = 288H3WAR3W; ENABLE_PREVIEWS = YES; INFOPLIST_FILE = "iOS Info.plist"; IPHONEOS_DEPLOYMENT_TARGET = 13.0; diff --git a/Sources/TokamakCore/Shapes/Path.swift b/Sources/TokamakCore/Shapes/Path.swift index 54d48aa63..840b78d08 100644 --- a/Sources/TokamakCore/Shapes/Path.swift +++ b/Sources/TokamakCore/Shapes/Path.swift @@ -86,9 +86,11 @@ public struct Path: Equatable, LosslessStringConvertible { public init(roundedRect rect: CGRect, cornerSize: CGSize, style: RoundedCornerStyle = .circular) { - storage = .roundedRect(FixedRoundedRect(rect: rect, - cornerSize: cornerSize, - style: style)) + storage = .roundedRect(FixedRoundedRect( + rect: rect, + cornerSize: cornerSize, + style: style + )) } public init(roundedRect rect: CGRect, diff --git a/Sources/TokamakCore/StackReconciler.swift b/Sources/TokamakCore/StackReconciler.swift index 5197e07aa..e86bd58f2 100644 --- a/Sources/TokamakCore/StackReconciler.swift +++ b/Sources/TokamakCore/StackReconciler.swift @@ -29,13 +29,14 @@ public final class StackReconciler { view: V, target: R.TargetType, renderer: R, + environment: EnvironmentValues, scheduler: @escaping (@escaping () -> ()) -> () ) { self.renderer = renderer self.scheduler = scheduler rootTarget = target - rootView = view.makeMountedView(target, EnvironmentValues()) + rootView = view.makeMountedView(target, environment) rootView.mount(with: self) } diff --git a/Sources/TokamakCore/Styles/ToggleStyle.swift b/Sources/TokamakCore/Styles/ToggleStyle.swift new file mode 100644 index 000000000..5fe18bb92 --- /dev/null +++ b/Sources/TokamakCore/Styles/ToggleStyle.swift @@ -0,0 +1,76 @@ +// 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. +// +// Created by Jed Fox on 07/04/2020. +// +// swiftlint:disable line_length +// Adapted from https://github.com/SwiftWebUI/SwiftWebUI/blob/16b84d46/Sources/SwiftWebUI/Views/Forms/Toggle.swift +// swiftlint:enable line_length +// + +// NOTE: ToggleStyleConfiguration.label is supposed to be a special Never View. +// It seems like during the rendering process it’s dynamically replaced with the actual label. +// That’s complicated so instead we’re providing the label view directly. + +public struct ToggleStyleConfiguration { + public let label: AnyView + @Binding public var isOn: Swift.Bool +} + +public protocol ToggleStyle { + associatedtype Body: View + + func makeBody(configuration: Self.Configuration) -> Self.Body + + typealias Configuration = ToggleStyleConfiguration +} + +public struct _AnyToggleStyle: ToggleStyle { + public typealias Body = AnyView + + private let bodyClosure: (ToggleStyleConfiguration) -> AnyView + + public init(_ style: S) { + bodyClosure = { configuration in + AnyView(style.makeBody(configuration: configuration)) + } + } + + public func makeBody(configuration: ToggleStyleConfiguration) -> AnyView { + bodyClosure(configuration) + } +} + +public enum ToggleStyleKey: EnvironmentKey { + public static var defaultValue: _AnyToggleStyle { + fatalError("\(self) must have a renderer-provided default value") + } +} + +extension EnvironmentValues { + var toggleStyle: _AnyToggleStyle { + get { + self[ToggleStyleKey.self] + } + set { + self[ToggleStyleKey.self] = newValue + } + } +} + +extension View { + public func toggleStyle(_ style: S) -> some View where S: ToggleStyle { + environment(\.toggleStyle, _AnyToggleStyle(style)) + } +} diff --git a/Sources/TokamakCore/Views/Button.swift b/Sources/TokamakCore/Views/Button.swift index 876911f2e..08d4c0225 100644 --- a/Sources/TokamakCore/Views/Button.swift +++ b/Sources/TokamakCore/Views/Button.swift @@ -46,7 +46,7 @@ public struct Button