diff --git a/Example/Package.swift b/Example/Package.swift index f7ff6e8..446fefc 100644 --- a/Example/Package.swift +++ b/Example/Package.swift @@ -6,7 +6,7 @@ let package = Package( name: "SPMGenExample", platforms: [ .macOS(.v10_15), - .iOS(.v13) + .iOS(.v13), ], products: [ .library( @@ -33,12 +33,12 @@ let package = Package( .product( name: "SPMResources", package: "spmgen" - ) + ), ], resources: [ .process("Resources") ] ), - .target(name: "SPMGenExampleCore") + .target(name: "SPMGenExampleCore"), ] ) diff --git a/Example/Shared/SPMGenExampleApp.swift b/Example/Shared/SPMGenExampleApp.swift index 6e13493..85e05f9 100644 --- a/Example/Shared/SPMGenExampleApp.swift +++ b/Example/Shared/SPMGenExampleApp.swift @@ -9,16 +9,16 @@ import SPMGenExampleApp import SwiftUI #if os(iOS) -public typealias CocoaApplicationDelegateAdaptor = UIApplicationDelegateAdaptor + public typealias CocoaApplicationDelegateAdaptor = UIApplicationDelegateAdaptor #elseif os(macOS) -public typealias CocoaApplicationDelegateAdaptor = NSApplicationDelegateAdaptor + public typealias CocoaApplicationDelegateAdaptor = NSApplicationDelegateAdaptor #endif @main struct SPMGenExampleApp: App { @CocoaApplicationDelegateAdaptor var appDelegate: AppDelegate - + var body: some Scene { WindowGroup { ContentView() diff --git a/Example/Sources/SPMGenExampleApp/AppDelegate.swift b/Example/Sources/SPMGenExampleApp/AppDelegate.swift index b01edb2..ba4ee63 100644 --- a/Example/Sources/SPMGenExampleApp/AppDelegate.swift +++ b/Example/Sources/SPMGenExampleApp/AppDelegate.swift @@ -1,30 +1,27 @@ import SPMGenExampleResources #if os(iOS) -import UIKit -public typealias CocoaApplication = UIApplication -public typealias CocoaApplicationDelegate = UIApplicationDelegate + import UIKit + public typealias CocoaApplication = UIApplication + public typealias CocoaApplicationDelegate = UIApplicationDelegate -public final class AppDelegate: NSObject, CocoaApplicationDelegate { - public func application( - _ application: CocoaApplication, - didFinishLaunchingWithOptions launchOptions: [ - CocoaApplication.LaunchOptionsKey : Any - ]? = nil - ) -> Bool { - CocoaFont.bootstrap() - return true + public final class AppDelegate: NSObject, CocoaApplicationDelegate { + public func application( + _ application: CocoaApplication, + didFinishLaunchingWithOptions launchOptions: [CocoaApplication.LaunchOptionsKey: Any]? = nil + ) -> Bool { + CocoaFont.bootstrap() + return true + } } -} #elseif os(macOS) -import AppKit -public typealias CocoaApplication = NSApplication -public typealias CocoaApplicationDelegate = NSApplicationDelegate + import AppKit + public typealias CocoaApplication = NSApplication + public typealias CocoaApplicationDelegate = NSApplicationDelegate -public final class AppDelegate: NSObject, CocoaApplicationDelegate { - public func applicationDidFinishLaunching(_ notification: Notification) { - CocoaFont.bootstrap() + public final class AppDelegate: NSObject, CocoaApplicationDelegate { + public func applicationDidFinishLaunching(_ notification: Notification) { + CocoaFont.bootstrap() + } } -} #endif - diff --git a/Example/Sources/SPMGenExampleApp/ContentView.swift b/Example/Sources/SPMGenExampleApp/ContentView.swift index 8e8cbe7..ff7eec1 100644 --- a/Example/Sources/SPMGenExampleApp/ContentView.swift +++ b/Example/Sources/SPMGenExampleApp/ContentView.swift @@ -1,9 +1,9 @@ -import SwiftUI import SPMGenExampleResources +import SwiftUI public struct ContentView: View { public init() {} - + public var body: some View { NavigationView { #if os(iOS) @@ -23,32 +23,32 @@ public struct ContentView: View { ) ) #elseif os(macOS) - List { - NavigationLink( - "Image", - destination: imageContent() - ) - NavigationLink( - "Color", - destination: colorContent() - ) - NavigationLink( - "Fonts", - destination: List { - systemFontsContent() - customFontsContent() - } - ) - } + List { + NavigationLink( + "Image", + destination: imageContent() + ) + NavigationLink( + "Color", + destination: colorContent() + ) + NavigationLink( + "Fonts", + destination: List { + systemFontsContent() + customFontsContent() + } + ) + } #endif } } - + private func systemFontsContent() -> some View { let fontWeights: [Font.Weight] = [ .ultraLight, .thin, .light, .regular, .medium, .semibold, - .bold, .heavy, .black + .bold, .heavy, .black, ] return Section(header: Text("System")) { ForEach(fontWeights, id: \.self) { fontWeight in @@ -58,7 +58,7 @@ public struct ContentView: View { } } } - + private func customFontsContent() -> some View { Section(header: Text("Custom")) { ForEach(CocoaFont.customFonts, id: \.name) { font in @@ -68,7 +68,7 @@ public struct ContentView: View { } } } - + private func colorContent() -> some View { Color .resource(.colorExample) @@ -76,7 +76,7 @@ public struct ContentView: View { .edgesIgnoringSafeArea(.all) .overlay(CurrentColorSchemeLabel()) } - + private func imageContent() -> some View { Image .resource(.imageExample) @@ -91,14 +91,14 @@ public struct ContentView: View { private struct DimmView: View { @Environment(\.colorScheme) private var colorScheme - + var body: some View { VStack { LinearGradient( gradient: Gradient( colors: [ (colorScheme == .dark ? .black : .white), - .clear + .clear, ] ), startPoint: .top, @@ -112,26 +112,28 @@ private struct DimmView: View { private struct CurrentColorSchemeLabel: View { @Environment(\.colorScheme) private var colorScheme - + var body: some View { HStack { Text("ColorScheme:") - .font(Font( - colorScheme == .dark - ? CocoaFont.primary(ofSize: 18, weight: .bold) - : CocoaFont.secondary(ofSize: 18, weight: .bold) - )) + .font( + Font( + colorScheme == .dark + ? CocoaFont.primary(ofSize: 18, weight: .bold) + : CocoaFont.secondary(ofSize: 18, weight: .bold) + ) + ) Text("\(name(for: colorScheme))") .font(.system(size: 18, weight: .regular)) } - .padding() - .background( - (colorScheme == .dark ? Color.black : .white) - .opacity(0.4) - ) - .cornerRadius(8) + .padding() + .background( + (colorScheme == .dark ? Color.black : .white) + .opacity(0.4) + ) + .cornerRadius(8) } - + private func name(for colorSheme: ColorScheme) -> String { switch colorScheme { case .light: return "Light" diff --git a/Example/Sources/SPMGenExampleCore/Extesnions/Optional+.swift b/Example/Sources/SPMGenExampleCore/Extesnions/Optional+.swift index ba456af..214313f 100644 --- a/Example/Sources/SPMGenExampleCore/Extesnions/Optional+.swift +++ b/Example/Sources/SPMGenExampleCore/Extesnions/Optional+.swift @@ -2,15 +2,15 @@ extension Optional { public var isNil: Bool { self == nil } - + public var isNotNil: Bool { self != nil } - + public func or(_ defaultValue: @autoclosure () -> Wrapped?) -> Wrapped? { self ?? defaultValue() } - + public func or(_ defaultValue: @autoclosure () -> Wrapped) -> Wrapped { self ?? defaultValue() } diff --git a/Example/Sources/SPMGenExampleResources/Extensions/Font+.swift b/Example/Sources/SPMGenExampleResources/Extensions/Font+.swift index 829b3cf..cfbe109 100644 --- a/Example/Sources/SPMGenExampleResources/Extensions/Font+.swift +++ b/Example/Sources/SPMGenExampleResources/Extensions/Font+.swift @@ -2,11 +2,11 @@ import SPMGenExampleCore import SPMResources #if os(iOS) -import UIKit -public typealias CocoaFont = UIFont + import UIKit + public typealias CocoaFont = UIFont #elseif os(macOS) -import AppKit -public typealias CocoaFont = NSFont + import AppKit + public typealias CocoaFont = NSFont #endif extension CocoaFont { @@ -33,16 +33,19 @@ extension CocoaFont { .montserratSemiBold, .montserratSemiBoldItalic, .montserratThin, - .montserratThinItalic + .montserratThinItalic, ] } - + @discardableResult public static func bootstrap() -> Bool { register(customFonts).allSatisfy(\.isRegistered) } - - public static func system(ofSize size: CGFloat, weight: CocoaFont.Weight) -> CocoaFont { + + public static func system( + ofSize size: CGFloat, + weight: CocoaFont.Weight + ) -> CocoaFont { .systemFont(ofSize: size, weight: weight) } } @@ -53,121 +56,129 @@ extension CocoaFont { case italic } /// Montserrat - public static func primary(ofSize size: CGFloat, weight: Weight, style: Style = .default) -> CocoaFont { + public static func primary( + ofSize size: CGFloat, + weight: Weight, + style: Style = .default + ) -> CocoaFont { switch style { case .default: switch weight { case .ultraLight: return CocoaFont.resource(.montserratThin, ofSize: size) .or(.system(ofSize: size, weight: weight)) - + case .thin: return CocoaFont.resource(.montserratExtraLight, ofSize: size) .or(.system(ofSize: size, weight: weight)) - + case .light: return CocoaFont.resource(.montserratLight, ofSize: size) .or(.system(ofSize: size, weight: weight)) - + case .regular: return CocoaFont.resource(.montserratRegular, ofSize: size) .or(.system(ofSize: size, weight: weight)) - + case .medium: return CocoaFont.resource(.montserratMedium, ofSize: size) .or(.system(ofSize: size, weight: weight)) - + case .semibold: return CocoaFont.resource(.montserratSemiBold, ofSize: size) .or(.system(ofSize: size, weight: weight)) - + case .bold: return CocoaFont.resource(.montserratBold, ofSize: size) .or(.system(ofSize: size, weight: weight)) - + case .heavy: return CocoaFont.resource(.montserratExtraBold, ofSize: size) .or(.system(ofSize: size, weight: weight)) - + case .black: return CocoaFont.resource(.montserratBlack, ofSize: size) .or(.system(ofSize: size, weight: weight)) - + default: return .system(ofSize: size, weight: weight) } - + case .italic: switch weight { case .ultraLight: return CocoaFont.resource(.montserratThinItalic, ofSize: size) .or(.system(ofSize: size, weight: weight)) - + case .thin: return CocoaFont.resource(.montserratExtraLightItalic, ofSize: size) .or(.system(ofSize: size, weight: weight)) - + case .light: return CocoaFont.resource(.montserratLightItalic, ofSize: size) .or(.system(ofSize: size, weight: weight)) - + case .regular: return CocoaFont.resource(.montserratItalic, ofSize: size) .or(.system(ofSize: size, weight: weight)) - + case .medium: return CocoaFont.resource(.montserratMediumItalic, ofSize: size) .or(.system(ofSize: size, weight: weight)) - + case .semibold: return CocoaFont.resource(.montserratSemiBoldItalic, ofSize: size) .or(.system(ofSize: size, weight: weight)) - + case .bold: return CocoaFont.resource(.montserratBoldItalic, ofSize: size) .or(.system(ofSize: size, weight: weight)) - + case .heavy: return CocoaFont.resource(.montserratExtraBoldItalic, ofSize: size) .or(.system(ofSize: size, weight: weight)) - + case .black: return CocoaFont.resource(.montserratBlackItalic, ofSize: size) .or(.system(ofSize: size, weight: weight)) - + default: return CocoaFont.system(ofSize: size, weight: weight) } } } - + /// Arimo - public static func secondary(ofSize size: CGFloat, weight: Weight, style: Style = .default) -> CocoaFont { + public static func secondary( + ofSize size: CGFloat, + weight: Weight, + style: Style = .default + ) -> CocoaFont { switch style { case .default: if weight == .regular { return CocoaFont.resource(.arimoRegular, ofSize: size) .or(.system(ofSize: size, weight: weight)) } - + if weight == .bold { return CocoaFont.resource(.arimoBold, ofSize: size) .or(.system(ofSize: size, weight: weight)) } - + return .system(ofSize: size, weight: weight) - + case .italic: if weight == .regular { return CocoaFont.resource(.arimoItalic, ofSize: size) .or(.system(ofSize: size, weight: weight)) } - + if weight == .bold { return CocoaFont.resource(.arimoBoldItalic, ofSize: size) .or(.system(ofSize: size, weight: weight)) } - + return .system(ofSize: size, weight: weight) } } diff --git a/Example/Sources/SPMGenExampleResources/SPMGen.swift b/Example/Sources/SPMGenExampleResources/SPMGen.swift index c235cbf..1df5b91 100644 --- a/Example/Sources/SPMGenExampleResources/SPMGen.swift +++ b/Example/Sources/SPMGenExampleResources/SPMGen.swift @@ -119,7 +119,7 @@ extension ImageResource { #if canImport(SwiftUI) import SwiftUI - + @available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, *) extension Color { public static func resource( @@ -132,7 +132,7 @@ extension ImageResource { #if os(iOS) import UIKit - + extension UIColor { @available(iOS 11.0, *) public static func resource( @@ -174,7 +174,7 @@ extension ImageResource { #if canImport(SwiftUI) import SwiftUI - + @available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, *) extension Font { public static func resource( @@ -187,7 +187,7 @@ extension ImageResource { ) } } - + @available(iOS 14.0, macOS 11.0, tvOS 14.0, watchOS 7.0, *) extension Font { public static func resource( @@ -216,7 +216,7 @@ extension ImageResource { #if os(iOS) import UIKit - + extension CTFont { public static func resource( _ resource: FontResource, @@ -262,7 +262,7 @@ extension ImageResource { CTFontManagerRegisterGraphicsFont(font, &error) return error == nil } - + @discardableResult public static func registerIfNeeded( _ resources: [FontResource] @@ -270,7 +270,8 @@ extension ImageResource { let installedFonts: Set = Set(installed()) return resources.map { let isNotRegistered = !installedFonts.contains($0.name) - return ($0, isNotRegistered ? register($0) : true, isNotRegistered) } + return ($0, isNotRegistered ? register($0) : true, isNotRegistered) + } } @discardableResult @@ -284,7 +285,7 @@ extension ImageResource { familyNames.sorted() .map { (family: $0, fonts: fontNames(forFamilyName: $0).sorted()) } } - + public static func installed() -> [String] { familyNames.flatMap { fontNames(forFamilyName: $0) } } @@ -312,7 +313,7 @@ extension ImageResource { ) -> NSFont? { NSFont(name: resource.name, size: size) } - + @discardableResult public static func registerIfNeeded( _ resource: FontResource @@ -346,7 +347,8 @@ extension ImageResource { let installedFonts = installedFontset() return resources.map { let isNotRegistered = !installedFonts.contains($0.name) - return ($0, isNotRegistered ? register($0) : true, isNotRegistered) } + return ($0, isNotRegistered ? register($0) : true, isNotRegistered) + } } @discardableResult @@ -363,7 +365,7 @@ extension ImageResource { }.or((family, [String]())) } } - + public static func installedFontset() -> Set { Set( (CTFontManagerCopyAvailableFontFamilyNames() as Array).flatMap { family -> [String] in @@ -371,7 +373,8 @@ extension ImageResource { return NSFontManager.shared.availableMembers(ofFontFamily: family) .map { $0.compactMap { $0.first as? String } } .or([String]()) - } else { + } + else { return [String]() } } @@ -382,7 +385,7 @@ extension ImageResource { #if canImport(SwiftUI) import SwiftUI - + @available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, *) extension Image { public static func resource(