Skip to content

Commit

Permalink
chore: change parameters and attributes order for themes (#79)
Browse files Browse the repository at this point in the history
Signed-off-by: Pierre-Yves Lapersonne <[email protected]>
  • Loading branch information
pylapp committed Dec 13, 2024
1 parent 031894d commit 892f6e2
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 77 deletions.
46 changes: 23 additions & 23 deletions OUDS/Core/Components/Sources/Extensions/View+Typography.swift

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import SwiftUI
struct TypographyModifier: ViewModifier {

/// The name of a possible custom font family, or `nil` if the font is use is _system font_
let customFontFamily: FontFamilyRawToken?
let fontFamily: FontFamilyRawToken?
/// The typography to apply for *compact* or *regular* modes, i.e. font tokens
let font: MultipleFontCompositeRawTokens

Expand Down Expand Up @@ -61,7 +61,7 @@ struct TypographyModifier: ViewModifier {
// using UIFontMetrics to scale the font size, ensuring Dynamic Type support
let scaledFontSize = UIFontMetrics.default.scaledValue(for: fontSize)

if let fontFamilyName = customFontFamily {
if let fontFamilyName = fontFamily {
let composedFontFamily = fontFamilyName.compose(withFont: "\(adaptiveFont.weight.fontWeight)")
let customFont: Font = .custom(composedFontFamily, size: adaptiveFont.size)
return customFont
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public static let typeBold1050 = FontCompositeRawToken(size: fontSize1050, lineH
```

However the _theme_ must know which _font family_ to apply, and this font family can be a _custom one_ or the _system one_.
Thus, we let the users define the font family they want by overriding the `customFontFamily` property. This value will be used to compute the typography, if not defined the systme font will be used.
Thus, we let the users define the font family they want by overriding the `fontFamily` property. This value will be used to compute the typography, if not defined the systme font will be used.

Thus, if you want to apply a specific typography to a `View`, supposing you defined previously the semantic tokens, just call the method you want and gives as parameter the theme (to get the custom font if defined):

Expand Down
54 changes: 27 additions & 27 deletions OUDS/Core/OUDS/Sources/OUDSTheme/OUDSTheme.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,76 +29,76 @@ open class OUDSTheme: @unchecked Sendable {

// MARK: - Properties

/// All border semantic tokens exposed in one object
public let borders: AllBorderSemanticTokens

/// All opacity semantic tokens exposed in one object
public let opacities: AllOpacitySemanticTokens

/// All color semantic tokens exposed in one object
public let colors: AllColorSemanticTokens

/// All border semantic tokens exposed in one object
public let borders: AllBorderSemanticTokens

/// All elevation semantic tokens exposed in one object
public let elevations: AllElevationSemanticTokens

/// A theme can have a custom font which is not the system font
public let fontFamily: FontFamilySemanticToken?

/// All font semantic tokens exposed in one object
public let fonts: AllFontSemanticTokens

/// All grid semantic tokens exposed in one object
public let grids: AllGridSemanticTokens

/// A theme can have a custom font which is not the system font
public let customFontFamily: FontFamilySemanticToken?
/// All opacity semantic tokens exposed in one object
public let opacities: AllOpacitySemanticTokens

// MARK: - Initializers

/// Defines a basic kind of abstract theme to subclass then.
/// No custom font family will be used.
/// - Parameters:
/// - borders: An object providing all the border semantic tokens, by default `OUDSBorderSemanticTokensWrapper`
/// - opacities: An object providing all the opacity semantic tokens, by default `OUDSOpacitySemanticTokensWrapper`
/// - colors: An object providing all the color semantic tokens, by default `OUDSColorSemanticTokensWrapper`
/// - borders: An object providing all the border semantic tokens, by default `OUDSBorderSemanticTokensWrapper`
/// - elevations: An object providing all the elevation semantic tokens, by default `OUDSElevationSemanticTokensWrapper`
/// - fonts: An object providing all the font semantic tokens, by default `OUDSFontSemanticTokensWrapper`
/// - grids: An object providing all the grid semantic tokens, by default `OUDSGridSemanticTokensWrapper`
public init(borders: AllBorderSemanticTokens = OUDSBorderSemanticTokensWrapper(),
opacities: AllOpacitySemanticTokens = OUDSOpacitySemanticTokensWrapper(),
colors: AllColorSemanticTokens = OUDSColorSemanticTokensWrapper(),
/// - opacities: An object providing all the opacity semantic tokens, by default `OUDSOpacitySemanticTokensWrapper`
public init(colors: AllColorSemanticTokens = OUDSColorSemanticTokensWrapper(),
borders: AllBorderSemanticTokens = OUDSBorderSemanticTokensWrapper(),
elevations: AllElevationSemanticTokens = OUDSElevationSemanticTokensWrapper(),
fonts: AllFontSemanticTokens = OUDSFontSemanticTokensWrapper(),
grids: AllGridSemanticTokens = OUDSGridSemanticTokensWrapper()) {
self.borders = borders
self.opacities = opacities
grids: AllGridSemanticTokens = OUDSGridSemanticTokensWrapper(),
opacities: AllOpacitySemanticTokens = OUDSOpacitySemanticTokensWrapper()) {
self.colors = colors
self.borders = borders
self.elevations = elevations
fontFamily = nil
self.fonts = fonts
self.grids = grids
customFontFamily = nil
self.opacities = opacities
}

/// Defines a basic kind of abstract theme to subclass then.
/// - Parameters:
/// - borders: An object providing all the border semantic tokens, as `BorderSemanticTokens` implementation
/// - opacities: An object providing all the opacity semantic tokens, as `OpacitySemanticTokens` implementation
/// - colors: An object providing all the color semantic tokens, as `AllColorSemanticTokens` implementation
/// - borders: An object providing all the border semantic tokens, as `BorderSemanticTokens` implementation
/// - elevations: An object providing all the elevation semantic tokens, by default `AllElevationSemanticTokens`
/// - fontFamily: Set `nil` if system font to use, otherwise use the `FontFamilySemanticToken` you want to apply
/// - fonts: An object providing all the font semantic tokens, by default `AllFontemanticTokens`
/// - grids: An object providing all the grid semantic tokens, by default `AllGridSemanticTokens`
/// - customFontFamily: Set `nil` if system font to use, otherwise use the `FontFamilySemanticToken` you want to apply
public init(borders: AllBorderSemanticTokens,
opacities: AllOpacitySemanticTokens,
colors: AllColorSemanticTokens,
/// - opacities: An object providing all the opacity semantic tokens, as `OpacitySemanticTokens` implementation
public init(colors: AllColorSemanticTokens,
borders: AllBorderSemanticTokens,
elevations: AllElevationSemanticTokens,
fontFamily: FontFamilySemanticToken?,
fonts: AllFontSemanticTokens,
grids: AllGridSemanticTokens,
customFontFamily: FontFamilySemanticToken?) {
self.borders = borders
self.opacities = opacities
opacities: AllOpacitySemanticTokens) {
self.colors = colors
self.borders = borders
self.elevations = elevations
self.fontFamily = fontFamily
self.fonts = fonts
self.grids = grids
self.customFontFamily = customFontFamily
self.opacities = opacities
}

deinit { }
Expand Down
20 changes: 10 additions & 10 deletions OUDS/Core/OUDS/Tests/OUDSTheme/MockThemes/MockTheme.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,28 +28,28 @@ import OUDSTokensSemantic
open class MockTheme: OUDSTheme, @unchecked Sendable {

convenience init() {
self.init(customFont: nil)
self.init(fontFamily: nil)
}

/// For ``OtherMockTheme``
init(colors: AllColorSemanticTokens) {
super.init(borders: MockThemeBorderSemanticTokensWrapper(),
opacities: MockThemeOpacitySemanticTokensWrapper(),
colors: colors,
super.init(colors: colors,
borders: MockThemeBorderSemanticTokensWrapper(),
elevations: MockThemeElevationSemanticTokensWrapper(),
fontFamily: nil,
fonts: MockThemeFontSemanticTokensWrapper(),
grids: MockThemeGridSemanticTokensWrapper(),
customFontFamily: nil)
opacities: MockThemeOpacitySemanticTokensWrapper())
}

init(customFont: String?) {
super.init(borders: MockThemeBorderSemanticTokensWrapper(),
opacities: MockThemeOpacitySemanticTokensWrapper(),
colors: MockThemeColorSemanticTokensWrapper(),
init(fontFamily: String?) {
super.init(colors: MockThemeColorSemanticTokensWrapper(),
borders: MockThemeBorderSemanticTokensWrapper(),
elevations: MockThemeElevationSemanticTokensWrapper(),
fontFamily: fontFamily,
fonts: MockThemeFontSemanticTokensWrapper(),
grids: MockThemeGridSemanticTokensWrapper(),
customFontFamily: customFont)
opacities: MockThemeOpacitySemanticTokensWrapper())
}

deinit { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ final class TestThemeOverrideOfThemeProperties: XCTestCase {
inheritedTheme = MockTheme(customFont: "Mr R08øT")

Check failure on line 31 in OUDS/Core/OUDS/Tests/OUDSTheme/TestThemeOverrideOfThemeProperties.swift

View workflow job for this annotation

GitHub Actions / unit-test

incorrect argument label in call (have 'customFont:', expected 'fontFamily:')
}

func testInheritedThemeCanOverrideCustomFontFamily() throws {
XCTAssertNotEqual(inheritedTheme.customFontFamily, abstractTheme.customFontFamily)
XCTAssertTrue(inheritedTheme.customFontFamily == "Mr R08øT")
func testInheritedThemeCanOverrideFontFamily() throws {
XCTAssertNotEqual(inheritedTheme.fontFamily, abstractTheme.fontFamily)
XCTAssertTrue(inheritedTheme.fontFamily == "Mr R08øT")
}
}

Expand Down
20 changes: 10 additions & 10 deletions OUDS/Core/Themes/Orange/Sources/OrangeTheme.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,24 +36,24 @@ open class OrangeTheme: OUDSTheme, @unchecked Sendable {

/// Initializes the `OrangeTheme` and lets children classes to user their own tokens implementations
/// - Parameters:
/// - borders: An object providing all the border semantic tokens, as `BorderSemanticTokens` implementation
/// - opacities: An object providing all the opacity semantic tokens, as `OpacitySemanticTokens` implementation
/// - colors: An object providing all the color semantic tokens, as `AllColorSemanticTokens` implementation
/// - borders: An object providing all the border semantic tokens, as `BorderSemanticTokens` implementation
/// - elevations: An object providing all the elevation semantic tokens, by default `AllElevationSemanticTokens`
/// - fonts: An object providing all the font semantic tokens, by default `AllFontemanticTokens`
/// - grids: An object providing all the grid semantic tokens, by default `AllGridSemanticTokens`
override public init(borders: AllBorderSemanticTokens,
opacities: AllOpacitySemanticTokens,
colors: AllColorSemanticTokens,
/// - opacities: An object providing all the opacity semantic tokens, as `OpacitySemanticTokens` implementation
override public init(colors: AllColorSemanticTokens,
borders: AllBorderSemanticTokens,
elevations: AllElevationSemanticTokens,
fonts: AllFontSemanticTokens,
grids: AllGridSemanticTokens) {
super.init(borders: borders,
opacities: opacities,
colors: colors,
grids: AllGridSemanticTokens,
opacities: AllOpacitySemanticTokens) {
super.init(colors: colors,
borders: borders,
elevations: elevations,
fonts: fonts,
grids: grids)
grids: grids,
opacities: opacities)
}

deinit { }
Expand Down
2 changes: 1 addition & 1 deletion Showcase/Showcase/Pages/Tokens/Font/FontTokenPage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ struct FontTokenPage: View {
.foregroundStyle(theme.colors.colorContentDefault.color(for: colorScheme))

Group {
Text("family (\(theme.customFontFamily ?? "system")), ")
Text("family (\(theme.fontFamily ?? "system")), ")
+ Text("weight (\(token.weight)), ")
+ Text("size (\(token.size, specifier: "%.2f")), ")
+ Text("lineHeight (\(token.lineHeight, specifier: "%.2f")), ")
Expand Down

0 comments on commit 892f6e2

Please sign in to comment.