Merge remote-tracking branch 'upstream/develop' into account-switcher-a11y

This commit is contained in:
Jed Fox 2022-11-14 22:33:44 -05:00
commit 69d2071570
No known key found for this signature in database
GPG Key ID: 0B61D18EA54B47E1
3 changed files with 635 additions and 511 deletions

View File

@ -8,6 +8,9 @@
#elseif os(tvOS) || os(watchOS)
import UIKit
#endif
#if canImport(SwiftUI)
import SwiftUI
#endif
// Deprecated typealiases
@available(*, deprecated, renamed: "ColorAsset.Color", message: "This typealias will be removed in SwiftGen 7.0")
@ -284,6 +287,24 @@ public final class ColorAsset {
return color
}()
#if os(iOS) || os(tvOS)
@available(iOS 11.0, tvOS 11.0, *)
public func color(compatibleWith traitCollection: UITraitCollection) -> Color {
let bundle = Bundle.module
guard let color = Color(named: name, in: bundle, compatibleWith: traitCollection) else {
fatalError("Unable to load color asset named \(name).")
}
return color
}
#endif
#if canImport(SwiftUI)
@available(iOS 13.0, tvOS 13.0, watchOS 6.0, macOS 10.15, *)
public private(set) lazy var swiftUIColor: SwiftUI.Color = {
SwiftUI.Color(asset: self)
}()
#endif
fileprivate init(name: String) {
self.name = name
}
@ -303,6 +324,16 @@ public extension ColorAsset.Color {
}
}
#if canImport(SwiftUI)
@available(iOS 13.0, tvOS 13.0, watchOS 6.0, macOS 10.15, *)
public extension SwiftUI.Color {
init(asset: ColorAsset) {
let bundle = Bundle.module
self.init(asset.name, bundle: bundle)
}
}
#endif
public struct ImageAsset {
public fileprivate(set) var name: String
@ -312,6 +343,7 @@ public struct ImageAsset {
public typealias Image = UIImage
#endif
@available(iOS 8.0, tvOS 9.0, watchOS 2.0, macOS 10.7, *)
public var image: Image {
let bundle = Bundle.module
#if os(iOS) || os(tvOS)
@ -327,9 +359,28 @@ public struct ImageAsset {
}
return result
}
#if os(iOS) || os(tvOS)
@available(iOS 8.0, tvOS 9.0, *)
public func image(compatibleWith traitCollection: UITraitCollection) -> Image {
let bundle = Bundle.module
guard let result = Image(named: name, in: bundle, compatibleWith: traitCollection) else {
fatalError("Unable to load image asset named \(name).")
}
return result
}
#endif
#if canImport(SwiftUI)
@available(iOS 13.0, tvOS 13.0, watchOS 6.0, macOS 10.15, *)
public var swiftUIImage: SwiftUI.Image {
SwiftUI.Image(asset: self)
}
#endif
}
public extension ImageAsset.Image {
@available(iOS 8.0, tvOS 9.0, watchOS 2.0, *)
@available(macOS, deprecated,
message: "This initializer is unsafe on macOS, please use the ImageAsset.image property")
convenience init?(asset: ImageAsset) {
@ -343,3 +394,23 @@ public extension ImageAsset.Image {
#endif
}
}
#if canImport(SwiftUI)
@available(iOS 13.0, tvOS 13.0, watchOS 6.0, macOS 10.15, *)
public extension SwiftUI.Image {
init(asset: ImageAsset) {
let bundle = Bundle.module
self.init(asset.name, bundle: bundle)
}
init(asset: ImageAsset, label: Text) {
let bundle = Bundle.module
self.init(asset.name, bundle: bundle, label: label)
}
init(decorative asset: ImageAsset) {
let bundle = Bundle.module
self.init(decorative: asset.name, bundle: bundle)
}
}
#endif

View File

@ -1,18 +1,20 @@
// swiftlint:disable all
// Generated using SwiftGen https://github.com/SwiftGen/SwiftGen
#if os(OSX)
#if os(macOS)
import AppKit.NSFont
#elseif os(iOS) || os(tvOS) || os(watchOS)
import UIKit.UIFont
#endif
#if canImport(SwiftUI)
import SwiftUI
#endif
// Deprecated typealiases
@available(*, deprecated, renamed: "FontConvertible.Font", message: "This typealias will be removed in SwiftGen 7.0")
public typealias Font = FontConvertible.Font
// swiftlint:disable superfluous_disable_command
// swiftlint:disable file_length
// swiftlint:disable superfluous_disable_command file_length implicit_return
// MARK: - Fonts
@ -36,7 +38,7 @@ public struct FontConvertible {
public let family: String
public let path: String
#if os(OSX)
#if os(macOS)
public typealias Font = NSFont
#elseif os(iOS) || os(tvOS) || os(watchOS)
public typealias Font = UIFont
@ -49,12 +51,41 @@ public struct FontConvertible {
return font
}
#if canImport(SwiftUI)
@available(iOS 13.0, tvOS 13.0, watchOS 6.0, macOS 10.15, *)
public func swiftUIFont(size: CGFloat) -> SwiftUI.Font {
return SwiftUI.Font.custom(self, size: size)
}
@available(iOS 14.0, tvOS 14.0, watchOS 7.0, macOS 11.0, *)
public func swiftUIFont(fixedSize: CGFloat) -> SwiftUI.Font {
return SwiftUI.Font.custom(self, fixedSize: fixedSize)
}
@available(iOS 14.0, tvOS 14.0, watchOS 7.0, macOS 11.0, *)
public func swiftUIFont(size: CGFloat, relativeTo textStyle: SwiftUI.Font.TextStyle) -> SwiftUI.Font {
return SwiftUI.Font.custom(self, size: size, relativeTo: textStyle)
}
#endif
public func register() {
// swiftlint:disable:next conditional_returns_on_newline
guard let url = url else { return }
CTFontManagerRegisterFontsForURL(url as CFURL, .process, nil)
}
fileprivate func registerIfNeeded() {
#if os(iOS) || os(tvOS) || os(watchOS)
if !UIFont.fontNames(forFamilyName: family).contains(name) {
register()
}
#elseif os(macOS)
if let url = url, CTFontManagerGetScopeForURL(url as CFURL) == .none {
register()
}
#endif
}
fileprivate var url: URL? {
// swiftlint:disable:next implicit_return
return Bundle.module.url(forResource: path, withExtension: nil)
@ -63,16 +94,34 @@ public struct FontConvertible {
public extension FontConvertible.Font {
convenience init?(font: FontConvertible, size: CGFloat) {
#if os(iOS) || os(tvOS) || os(watchOS)
if !UIFont.fontNames(forFamilyName: font.family).contains(font.name) {
font.register()
}
#elseif os(OSX)
if let url = font.url, CTFontManagerGetScopeForURL(url as CFURL) == .none {
font.register()
}
#endif
font.registerIfNeeded()
self.init(name: font.name, size: size)
}
}
#if canImport(SwiftUI)
@available(iOS 13.0, tvOS 13.0, watchOS 6.0, macOS 10.15, *)
public extension SwiftUI.Font {
static func custom(_ font: FontConvertible, size: CGFloat) -> SwiftUI.Font {
font.registerIfNeeded()
return custom(font.name, size: size)
}
}
@available(iOS 14.0, tvOS 14.0, watchOS 7.0, macOS 11.0, *)
public extension SwiftUI.Font {
static func custom(_ font: FontConvertible, fixedSize: CGFloat) -> SwiftUI.Font {
font.registerIfNeeded()
return custom(font.name, fixedSize: fixedSize)
}
static func custom(
_ font: FontConvertible,
size: CGFloat,
relativeTo textStyle: SwiftUI.Font.TextStyle
) -> SwiftUI.Font {
font.registerIfNeeded()
return custom(font.name, size: size, relativeTo: textStyle)
}
}
#endif