Skip to content

Commit

Permalink
refactor: wrapping for semantic tokens of grids (#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 567893d commit 031894d
Show file tree
Hide file tree
Showing 13 changed files with 139 additions and 84 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import Foundation
import OUDSTokensRaw
import OUDSTokensSemantic

extension OUDSTheme: GridSemanticTokens {
extension OUDSGridSemanticTokensWrapper: GridSemanticTokens {
@objc open var gridCompactColumnGap: GridSemanticToken { GridRawTokens.gridColumnGap100 }
@objc open var gridCompactMargin: GridSemanticToken { GridRawTokens.gridMargin300 }
@objc open var gridCompactMaxWidth: GridSemanticToken { GridRawTokens.gridMaxWidthCompact }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ extension OUDSTheme {
public final func gridMinWidth(for sizeClass: OUDSUserInterfaceSizeClass) -> GridRawToken {
switch sizeClass {
case .extraCompact:
return gridExtraCompactMinWidth
return grids.gridExtraCompactMinWidth
case .compact:
return gridCompactMinWidth
return grids.gridCompactMinWidth
case .regular:
return gridRegularMinWidth
return grids.gridRegularMinWidth
@unknown default:
fatalError("🤖 Raw token unavailable for \(sizeClass)!")
}
Expand All @@ -40,11 +40,11 @@ extension OUDSTheme {
public final func gridMaxWidth(for sizeClass: OUDSUserInterfaceSizeClass) -> GridRawToken {
switch sizeClass {
case .extraCompact:
return gridExtraCompactMaxWidth
return grids.gridExtraCompactMaxWidth
case .compact:
return gridCompactMaxWidth
return grids.gridCompactMaxWidth
case .regular:
return gridRegularMaxWidth
return grids.gridRegularMaxWidth
@unknown default:
fatalError("🤖 Raw token unavailable for \(sizeClass)!")
}
Expand All @@ -56,11 +56,11 @@ extension OUDSTheme {
public final func gridMargin(for sizeClass: OUDSUserInterfaceSizeClass) -> GridRawToken {
switch sizeClass {
case .extraCompact:
return gridExtraCompactMargin
return grids.gridExtraCompactMargin
case .compact:
return gridCompactMargin
return grids.gridCompactMargin
case .regular:
return gridRegularMargin
return grids.gridRegularMargin
@unknown default:
fatalError("🤖 Raw token unavailable for \(sizeClass)!")
}
Expand All @@ -72,11 +72,11 @@ extension OUDSTheme {
public final func gridColumnGap(for sizeClass: OUDSUserInterfaceSizeClass) -> GridRawToken {
switch sizeClass {
case .extraCompact:
return gridExtraCompactColumnGap
return grids.gridExtraCompactColumnGap
case .compact:
return gridCompactColumnGap
return grids.gridCompactColumnGap
case .regular:
return gridRegularColumnGap
return grids.gridRegularColumnGap
@unknown default:
fatalError("🤖 Raw token unavailable for \(sizeClass)!")
}
Expand Down
11 changes: 10 additions & 1 deletion OUDS/Core/OUDS/Sources/OUDSTheme/OUDSTheme.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ open class OUDSTheme: @unchecked Sendable {
/// 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?

Expand All @@ -57,16 +60,19 @@ open class OUDSTheme: @unchecked Sendable {
/// - colors: An object providing all the color semantic tokens, by default `OUDSColorSemanticTokensWrapper`
/// - 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(),
elevations: AllElevationSemanticTokens = OUDSElevationSemanticTokensWrapper(),
fonts: AllFontSemanticTokens = OUDSFontSemanticTokensWrapper()) {
fonts: AllFontSemanticTokens = OUDSFontSemanticTokensWrapper(),
grids: AllGridSemanticTokens = OUDSGridSemanticTokensWrapper()) {
self.borders = borders
self.opacities = opacities
self.colors = colors
self.elevations = elevations
self.fonts = fonts
self.grids = grids
customFontFamily = nil
}

Expand All @@ -77,18 +83,21 @@ open class OUDSTheme: @unchecked Sendable {
/// - colors: An object providing all the color semantic tokens, as `AllColorSemanticTokens` 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`
/// - customFontFamily: Set `nil` if system font to use, otherwise use the `FontFamilySemanticToken` you want to apply
public init(borders: AllBorderSemanticTokens,
opacities: AllOpacitySemanticTokens,
colors: AllColorSemanticTokens,
elevations: AllElevationSemanticTokens,
fonts: AllFontSemanticTokens,
grids: AllGridSemanticTokens,
customFontFamily: FontFamilySemanticToken?) {
self.borders = borders
self.opacities = opacities
self.colors = colors
self.elevations = elevations
self.fonts = fonts
self.grids = grids
self.customFontFamily = customFontFamily
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//
// Software Name: OUDS iOS
// SPDX-FileCopyrightText: Copyright (c) Orange SA
// SPDX-License-Identifier: MIT
//
// This software is distributed under the MIT license,
// the text of which is available at https://opensource.org/license/MIT/
// or see the "LICENSE" file for more details.
//
// Authors: See CONTRIBUTORS.txt
// Software description: A SwiftUI components library with code examples for Orange Unified Design System
//

import Foundation
import OUDSTokensRaw
import OUDSTokensSemantic

// swiftlint:disable required_deinit

/// Overrides **all** the grid semantic tokens (from its super class, i.e. `OUDSGridSemanticTokensWrapper` so as to test overriding of them (unit tests)
/// and to act like smoke tests with crashing tests if some tokens disappeared.
final class MockThemeGridSemanticTokensWrapper: OUDSGridSemanticTokensWrapper {

static let mockThemeGridRawToken: GridRawToken = 3_630

override public var gridExtraCompactMinWidth: GridSemanticToken { Self.mockThemeGridRawToken }
override public var gridExtraCompactMaxWidth: GridSemanticToken { Self.mockThemeGridRawToken }
override public var gridExtraCompactMargin: GridSemanticToken { Self.mockThemeGridRawToken }
override public var gridExtraCompactColumnGap: GridSemanticToken { Self.mockThemeGridRawToken }

override public var gridCompactMinWidth: GridSemanticToken { Self.mockThemeGridRawToken }
override public var gridCompactMaxWidth: GridSemanticToken { Self.mockThemeGridRawToken }
override public var gridCompactMargin: GridSemanticToken { Self.mockThemeGridRawToken }
override public var gridCompactColumnGap: GridSemanticToken { Self.mockThemeGridRawToken }

override public var gridRegularMinWidth: GridSemanticToken { Self.mockThemeGridRawToken }
override public var gridRegularMaxWidth: GridSemanticToken { Self.mockThemeGridRawToken }
override public var gridRegularMargin: GridSemanticToken { Self.mockThemeGridRawToken }
override public var gridRegularColumnGap: GridSemanticToken { Self.mockThemeGridRawToken }
}

// swiftlint:enable required_deinit

This file was deleted.

4 changes: 4 additions & 0 deletions OUDS/Core/OUDS/Tests/OUDSTheme/MockThemes/MockTheme.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import OUDSTokensSemantic
/// - ``MockThemeOpacitySemanticTokensWrapper`` for opacities
/// - ``MockThemeColorSemanticTokensWrapper`` for colors
/// - ``MockThemeElevationSemanticTokensWrapper`` for elevations
/// - ``MockThemeFontSemanticTokensWrapper`` for fonts
/// - ``MockThemeGridSemanticTokensWrapper`` for grids
open class MockTheme: OUDSTheme, @unchecked Sendable {

convenience init() {
Expand All @@ -36,6 +38,7 @@ open class MockTheme: OUDSTheme, @unchecked Sendable {
colors: colors,
elevations: MockThemeElevationSemanticTokensWrapper(),
fonts: MockThemeFontSemanticTokensWrapper(),
grids: MockThemeGridSemanticTokensWrapper(),
customFontFamily: nil)
}

Expand All @@ -45,6 +48,7 @@ open class MockTheme: OUDSTheme, @unchecked Sendable {
colors: MockThemeColorSemanticTokensWrapper(),
elevations: MockThemeElevationSemanticTokensWrapper(),
fonts: MockThemeFontSemanticTokensWrapper(),
grids: MockThemeGridSemanticTokensWrapper(),
customFontFamily: customFont)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,63 +34,63 @@ final class TestThemeOverrideOfGridSemanticTokens: XCTestCase {
}

func testInheritedThemeCanOverrideSemanticTokenGridIOSExtraCompactMinWidth() throws {
XCTAssertNotEqual(inheritedTheme.gridExtraCompactMinWidth, abstractTheme.gridExtraCompactMinWidth)
XCTAssertTrue(inheritedTheme.gridExtraCompactMinWidth == MockTheme.mockThemeGridRawToken)
XCTAssertNotEqual(inheritedTheme.grids.gridExtraCompactMinWidth, abstractTheme.grids.gridExtraCompactMinWidth)
XCTAssertTrue(inheritedTheme.grids.gridExtraCompactMinWidth == MockThemeGridSemanticTokensWrapper.mockThemeGridRawToken)
}

func testInheritedThemeCanOverrideSemanticTokenGridIOSExtraCompactMaxWidth() throws {
XCTAssertNotEqual(inheritedTheme.gridExtraCompactMaxWidth, abstractTheme.gridExtraCompactMaxWidth)
XCTAssertTrue(inheritedTheme.gridExtraCompactMaxWidth == MockTheme.mockThemeGridRawToken)
XCTAssertNotEqual(inheritedTheme.grids.gridExtraCompactMaxWidth, abstractTheme.grids.gridExtraCompactMaxWidth)
XCTAssertTrue(inheritedTheme.grids.gridExtraCompactMaxWidth == MockThemeGridSemanticTokensWrapper.mockThemeGridRawToken)
}

func testInheritedThemeCanOverrideSemanticTokenGridIOSExtraCompactMargin() throws {
XCTAssertNotEqual(inheritedTheme.gridExtraCompactMargin, abstractTheme.gridExtraCompactMargin)
XCTAssertTrue(inheritedTheme.gridExtraCompactMargin == MockTheme.mockThemeGridRawToken)
XCTAssertNotEqual(inheritedTheme.grids.gridExtraCompactMargin, abstractTheme.grids.gridExtraCompactMargin)
XCTAssertTrue(inheritedTheme.grids.gridExtraCompactMargin == MockThemeGridSemanticTokensWrapper.mockThemeGridRawToken)
}

func testInheritedThemeCanOverrideSemanticTokenGridIOSExtraCompactColumnGap() throws {
XCTAssertNotEqual(inheritedTheme.gridExtraCompactColumnGap, abstractTheme.gridExtraCompactColumnGap)
XCTAssertTrue(inheritedTheme.gridExtraCompactColumnGap == MockTheme.mockThemeGridRawToken)
XCTAssertNotEqual(inheritedTheme.grids.gridExtraCompactColumnGap, abstractTheme.grids.gridExtraCompactColumnGap)
XCTAssertTrue(inheritedTheme.grids.gridExtraCompactColumnGap == MockThemeGridSemanticTokensWrapper.mockThemeGridRawToken)
}

func testInheritedThemeCanOverrideSemanticTokenGridIOSCompactMinWidth() throws {
XCTAssertNotEqual(inheritedTheme.gridCompactMinWidth, abstractTheme.gridCompactMinWidth)
XCTAssertTrue(inheritedTheme.gridCompactMinWidth == MockTheme.mockThemeGridRawToken)
XCTAssertNotEqual(inheritedTheme.grids.gridCompactMinWidth, abstractTheme.grids.gridCompactMinWidth)
XCTAssertTrue(inheritedTheme.grids.gridCompactMinWidth == MockThemeGridSemanticTokensWrapper.mockThemeGridRawToken)
}

func testInheritedThemeCanOverrideSemanticTokenGridIOSCompactMaxWidth() throws {
XCTAssertNotEqual(inheritedTheme.gridCompactMaxWidth, abstractTheme.gridCompactMaxWidth)
XCTAssertTrue(inheritedTheme.gridCompactMaxWidth == MockTheme.mockThemeGridRawToken)
XCTAssertNotEqual(inheritedTheme.grids.gridCompactMaxWidth, abstractTheme.grids.gridCompactMaxWidth)
XCTAssertTrue(inheritedTheme.grids.gridCompactMaxWidth == MockThemeGridSemanticTokensWrapper.mockThemeGridRawToken)
}

func testInheritedThemeCanOverrideSemanticTokenGridIOSCompactMargin() throws {
XCTAssertNotEqual(inheritedTheme.gridCompactMargin, abstractTheme.gridCompactMargin)
XCTAssertTrue(inheritedTheme.gridCompactMargin == MockTheme.mockThemeGridRawToken)
XCTAssertNotEqual(inheritedTheme.grids.gridCompactMargin, abstractTheme.grids.gridCompactMargin)
XCTAssertTrue(inheritedTheme.grids.gridCompactMargin == MockThemeGridSemanticTokensWrapper.mockThemeGridRawToken)
}

func testInheritedThemeCanOverrideSemanticTokenGridIOSCompactColumnGap() throws {
XCTAssertNotEqual(inheritedTheme.gridCompactColumnGap, abstractTheme.gridCompactColumnGap)
XCTAssertTrue(inheritedTheme.gridCompactColumnGap == MockTheme.mockThemeGridRawToken)
XCTAssertNotEqual(inheritedTheme.grids.gridCompactColumnGap, abstractTheme.grids.gridCompactColumnGap)
XCTAssertTrue(inheritedTheme.grids.gridCompactColumnGap == MockThemeGridSemanticTokensWrapper.mockThemeGridRawToken)
}

func testInheritedThemeCanOverrideSemanticTokenGridIOSRegularMinWidth() throws {
XCTAssertNotEqual(inheritedTheme.gridRegularMinWidth, abstractTheme.gridRegularMinWidth)
XCTAssertTrue(inheritedTheme.gridRegularMinWidth == MockTheme.mockThemeGridRawToken)
XCTAssertNotEqual(inheritedTheme.grids.gridRegularMinWidth, abstractTheme.grids.gridRegularMinWidth)
XCTAssertTrue(inheritedTheme.grids.gridRegularMinWidth == MockThemeGridSemanticTokensWrapper.mockThemeGridRawToken)
}

func testInheritedThemeCanOverrideSemanticTokenGridIOSRegularMaxWidth() throws {
XCTAssertNotEqual(inheritedTheme.gridRegularMaxWidth, abstractTheme.gridRegularMaxWidth)
XCTAssertTrue(inheritedTheme.gridRegularMaxWidth == MockTheme.mockThemeGridRawToken)
XCTAssertNotEqual(inheritedTheme.grids.gridRegularMaxWidth, abstractTheme.grids.gridRegularMaxWidth)
XCTAssertTrue(inheritedTheme.grids.gridRegularMaxWidth == MockThemeGridSemanticTokensWrapper.mockThemeGridRawToken)
}

func testInheritedThemeCanOverrideSemanticTokenGridIOSRegularMargin() throws {
XCTAssertNotEqual(inheritedTheme.gridRegularMargin, abstractTheme.gridRegularMargin)
XCTAssertTrue(inheritedTheme.gridRegularMargin == MockTheme.mockThemeGridRawToken)
XCTAssertNotEqual(inheritedTheme.grids.gridRegularMargin, abstractTheme.grids.gridRegularMargin)
XCTAssertTrue(inheritedTheme.grids.gridRegularMargin == MockThemeGridSemanticTokensWrapper.mockThemeGridRawToken)
}

func testInheritedThemeCanOverrideSemanticTokenGridIOSRegularColumnGap() throws {
XCTAssertNotEqual(inheritedTheme.gridRegularColumnGap, abstractTheme.gridRegularColumnGap)
XCTAssertTrue(inheritedTheme.gridRegularColumnGap == MockTheme.mockThemeGridRawToken)
XCTAssertNotEqual(inheritedTheme.grids.gridRegularColumnGap, abstractTheme.grids.gridRegularColumnGap)
XCTAssertTrue(inheritedTheme.grids.gridRegularColumnGap == MockThemeGridSemanticTokensWrapper.mockThemeGridRawToken)
}
}

Expand Down
7 changes: 5 additions & 2 deletions OUDS/Core/Themes/Orange/Sources/OrangeTheme.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,19 @@ open class OrangeTheme: OUDSTheme, @unchecked Sendable {
/// - colors: An object providing all the color semantic tokens, as `AllColorSemanticTokens` 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,
elevations: AllElevationSemanticTokens,
fonts: AllFontSemanticTokens) {
fonts: AllFontSemanticTokens,
grids: AllGridSemanticTokens) {
super.init(borders: borders,
opacities: opacities,
colors: colors,
elevations: elevations,
fonts: fonts)
fonts: fonts,
grids: grids)
}

deinit { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
//

/// A class which wraps all **border semantic tokens** and expose them.
/// This wrapper should be integrated as a ``BorderSemanticTokens`` implementation inside `OUDSTheme` so as to provide
/// This wrapper should be integrated as a ``AllBorderSemanticTokens`` implementation inside `OUDSTheme` so as to provide
/// all tokens to the users.
open class OUDSBorderSemanticTokensWrapper {

Expand Down
Loading

0 comments on commit 031894d

Please sign in to comment.