Skip to content

Commit 84c2fc7

Browse files
authored
[iOS][macOS] Move PlatformProviding code to FluentUI_common (#2198) (#2199)
* Move implementation of PlatformThemeProviding to common * Same for FontInfo and GlobalTokens
1 parent c849c7c commit 84c2fc7

13 files changed

Lines changed: 25 additions & 58 deletions

Sources/FluentUI_common/Core/Theme/FluentTheme+Tokens.swift renamed to Sources/FluentUI_common/Core/Theme/Extensions/FluentTheme+Tokens.swift

File renamed without changes.

Sources/FluentUI_iOS/Core/Theme/FluentTheme+iOS.swift renamed to Sources/FluentUI_common/Core/Theme/Extensions/iOS/FluentTheme+iOS.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33
// Licensed under the MIT License.
44
//
55

6-
#if canImport(FluentUI_common)
7-
import FluentUI_common
8-
#endif
6+
#if canImport(UIKit)
97
import SwiftUI
108

119
/// Extending `FluentTheme` to implement this protocol allows for platform-specific implementation of token values.
@@ -356,3 +354,4 @@ extension FluentTheme: PlatformThemeProviding {
356354
}
357355
}
358356
}
357+
#endif // canImport(UIKit)

Sources/FluentUI_iOS/Core/Theme/FluentTheme+visionOS.swift renamed to Sources/FluentUI_common/Core/Theme/Extensions/iOS/FluentTheme+visionOS.swift

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@
55

66
#if os(visionOS)
77

8-
#if canImport(FluentUI_common)
9-
import FluentUI_common
10-
#endif
118
import SwiftUI
129

1310
extension FluentTheme {

Sources/FluentUI_iOS/Core/Theme/FontInfo+iOS.swift renamed to Sources/FluentUI_common/Core/Theme/Extensions/iOS/FontInfo+iOS.swift

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33
// Licensed under the MIT License.
44
//
55

6-
#if canImport(FluentUI_common)
7-
import FluentUI_common
8-
#endif
6+
#if canImport(UIKit)
97
import SwiftUI
108

119
extension FluentFontInfo : PlatformFontInfoProviding {
@@ -23,4 +21,5 @@ extension FluentFontInfo : PlatformFontInfoProviding {
2321
(12.0, .caption),
2422
(11.0, .caption2)
2523
]
26-
}
24+
}
25+
#endif // canImport(UIKit)

Sources/FluentUI_iOS/Core/Theme/GlobalTokens+iOS.swift renamed to Sources/FluentUI_common/Core/Theme/Extensions/iOS/GlobalTokens+iOS.swift

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33
// Licensed under the MIT License.
44
//
55

6-
#if canImport(FluentUI_common)
7-
import FluentUI_common
8-
#endif
6+
#if canImport(UIKit)
97
import SwiftUI
108

119
extension GlobalTokens : PlatformGlobalTokenProviding {
@@ -31,4 +29,5 @@ extension GlobalTokens : PlatformGlobalTokenProviding {
3129
return 60.0
3230
}
3331
}
34-
}
32+
}
33+
#endif // canImport(UIKit)

Sources/FluentUI_macOS/Theme/FluentTheme+macOS.swift renamed to Sources/FluentUI_common/Core/Theme/Extensions/macOS/FluentTheme+macOS.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33
// Licensed under the MIT License.
44
//
55

6-
#if canImport(FluentUI_common)
7-
import FluentUI_common
8-
#endif
6+
#if canImport(AppKit)
97
import SwiftUI
108

119
extension FluentTheme: PlatformThemeProviding {
@@ -355,3 +353,4 @@ extension FluentTheme: PlatformThemeProviding {
355353
}
356354
}
357355
}
356+
#endif // canImport(AppKit)

Sources/FluentUI_macOS/Theme/FontInfo+macOS.swift renamed to Sources/FluentUI_common/Core/Theme/Extensions/macOS/FontInfo+macOS.swift

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33
// Licensed under the MIT License.
44
//
55

6-
#if canImport(FluentUI_common)
7-
import FluentUI_common
8-
#endif
6+
#if canImport(AppKit)
97
import SwiftUI
108

119
extension FluentFontInfo : PlatformFontInfoProviding {
@@ -23,4 +21,5 @@ extension FluentFontInfo : PlatformFontInfoProviding {
2321
// Note: `10.0: .caption` and `10.0: .caption2` are removed to avoid needing duplicate size key values.
2422
// But it's okay because Apple's scaling curve is identical between it and `.footnote`.
2523
]
26-
}
24+
}
25+
#endif // canImport(AppKit)

Sources/FluentUI_macOS/Theme/GlobalTokens+macOS.swift renamed to Sources/FluentUI_common/Core/Theme/Extensions/macOS/GlobalTokens+macOS.swift

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33
// Licensed under the MIT License.
44
//
55

6-
#if canImport(FluentUI_common)
7-
import FluentUI_common
8-
#endif
6+
#if canImport(AppKit)
97
import SwiftUI
108

119
extension GlobalTokens : PlatformGlobalTokenProviding {
@@ -31,4 +29,5 @@ extension GlobalTokens : PlatformGlobalTokenProviding {
3129
return 44.0
3230
}
3331
}
34-
}
32+
}
33+
#endif // canImport(AppKit)

Sources/FluentUI_common/Core/Theme/FluentTheme.swift

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -124,17 +124,9 @@ public class FluentTheme: NSObject, ObservableObject {
124124
private var controlTokenSets: [String: Any] = [:]
125125

126126
private static var platformThemeProvider: any PlatformThemeProviding.Type {
127-
// We need slightly different implementations depending on how our package is loaded.
128-
#if SWIFT_PACKAGE || COCOAPODS
129-
// In this case, the protocol conformance happens in a different module, so we need to
130-
// convert the type conditionally and fail if something goes wrong.
131-
guard let platformThemeProvider = self as? PlatformThemeProviding.Type else {
132-
preconditionFailure("Unable to initialize FluentTheme: does not conform to PlatformThemeProviding")
133-
}
134-
#else
135-
// Otherwise, we're all in one module and thus the type conversion is guaranteed.
127+
// Because this conformance is conditional on OS, let's keep an explicit type check
128+
// here, which will cause a build break if a new OS is added.
136129
let platformThemeProvider = self as PlatformThemeProviding.Type
137-
#endif
138130
return platformThemeProvider
139131
}
140132
}

Sources/FluentUI_common/Core/Theme/PlatformGlobalTokensProviding.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66
import SwiftUI
77

88
/// Extending `GlobalTokens` to implement this protocol allows for platform-specific implementation.
9-
public protocol PlatformGlobalTokenProviding {
9+
protocol PlatformGlobalTokenProviding {
1010
static func fontSize(for token: GlobalTokens.FontSizeToken) -> CGFloat
11-
}
11+
}

0 commit comments

Comments
 (0)