Skip to content

Commit

Permalink
refactor: wrapping for semantic tokens of colors (#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 e560187 commit 9d4df70
Show file tree
Hide file tree
Showing 52 changed files with 2,136 additions and 2,027 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ import OUDSTokensSemantic

// swiftlint:disable line_length

/// Defines wrapper objects for `ColorSemanticToken`.
/// These values can be overriden inside `OUDSTheme` subclasses (in extensions or not, in the same module or not) thanks to the `@objc open` combination.
/// Defines wrapper objects for ``ColorSemanticToken`` so as to pack them as light and dark mode colors.
/// These values can be overriden inside `OUDSColorSemanticTokensWrapper` subclasses (in extensions or not, in the same module or not) thanks to the `@objc open` combination.
/// Some tokens do not have values assigned in the design system, and must be overriden.
/// Some tokens must be overiden in `OrangeTheme` side because they rely on Orange brand colors.
/// Some tokens must be overriden in `OrangeTheme` side because they rely on Orange brand colors.
/// Helps to expose color semantic tokens with two values to use depending to the color scheme (*Figma* cannot manage such tokens and generate them).
extension OUDSTheme: ColorMultipleSemanticTokens {
extension OUDSColorSemanticTokensWrapper: ColorMultipleSemanticTokens {

// MARK: - Color - Opacity - Invisible

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import OUDSTokensSemantic

// swiftlint:disable identifier_name

extension OUDSTheme: ColorSemanticTokens {
extension OUDSColorSemanticTokensWrapper: ColorSemanticTokens {
@objc open var colorOpacityInvisibleBlackLight: ColorSemanticToken { ColorRawTokens.colorOpacityBlack0 }
@objc open var colorOpacityInvisibleWhiteLight: ColorSemanticToken { ColorRawTokens.colorOpacityWhite0 }
@objc open var colorOpacityInvisibleBlackDark: ColorSemanticToken { ColorRawTokens.colorOpacityWhite0 }
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 @@ -35,6 +35,9 @@ open class OUDSTheme: @unchecked Sendable {
/// All opacity semantic tokens exposed in one object
public let opacities: OpacitySemanticTokens

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

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

Expand All @@ -45,23 +48,29 @@ open class OUDSTheme: @unchecked Sendable {
/// - 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`
public init(borders: BorderSemanticTokens = OUDSBorderSemanticTokensWrapper(),
opacities: OpacitySemanticTokens = OUDSOpacitySemanticTokensWrapper()) {
opacities: OpacitySemanticTokens = OUDSOpacitySemanticTokensWrapper(),
colors: AllColorSemanticTokens = OUDSColorSemanticTokensWrapper()) {
self.borders = borders
self.opacities = opacities
self.colors = colors
customFontFamily = nil
}

/// 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
/// - customFontFamily: Set `nil` if system font to use, otherwise use the `FontFamilySemanticToken` you want to apply
public init(borders: BorderSemanticTokens,
opacities: OpacitySemanticTokens,
colors: AllColorSemanticTokens,
customFontFamily: FontFamilySemanticToken?) {
self.borders = borders
self.opacities = opacities
self.colors = colors
self.customFontFamily = customFontFamily
}

Expand Down
1,134 changes: 570 additions & 564 deletions OUDS/Core/OUDS/Tests/OUDSTheme/MockThemes/MockTheme+ColorSemanticTokens.swift

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions OUDS/Core/OUDS/Tests/OUDSTheme/MockThemes/MockTheme.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,25 @@ import OUDSTokensSemantic
/// Uses its own tokens wrappers for tests:
/// - ``MockThemeBorderSemanticTokensWrapper`` for borders
/// - ``MockThemeOpacitySemanticTokensWrapper`` for opacities
/// - ``MockThemeColorSemanticTokensWrapper`` for colors
open class MockTheme: OUDSTheme, @unchecked Sendable {

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

// For `OtherMockTheme`
init(colors: AllColorSemanticTokens) {
super.init(borders: MockThemeBorderSemanticTokensWrapper(),
opacities: MockThemeOpacitySemanticTokensWrapper(),
colors: colors,
customFontFamily: nil)
}

init(customFont: String?) {
super.init(borders: MockThemeBorderSemanticTokensWrapper(),
opacities: MockThemeOpacitySemanticTokensWrapper(),
colors: MockThemeColorSemanticTokensWrapper(),
customFontFamily: customFont)
}

Expand Down
14 changes: 13 additions & 1 deletion OUDS/Core/OUDS/Tests/OUDSTheme/MockThemes/OtherMockTheme.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,22 @@ import OUDSTokensSemantic
// swiftlint:disable type_body_length
// swiftlint:disable identifier_name

// MARK: - Other Mock Theme

final class OtherMockTheme: MockTheme, @unchecked Sendable {

public init() {
super.init(colors: OtherMockThemeColorSemanticTokensWrapper())
}
}

// MARK: - Other Mock Theme Color Semantic Tokens Wrapper

/// Some color semantic tokens may be not implemented, making fatalError() be triggered at `OUDSTheme` level and tests crash.
/// `OrangeTheme` is not accessible here.
/// Thus we use another mock theme to make comparisons with `MockTheme` and ensure overriding property of tokens is still working.
/// This case of not implemented token only happens with color tokens. (╯° °)╯︵ ┻━┻
final class OtherMockTheme: MockTheme, @unchecked Sendable {
open class OtherMockThemeColorSemanticTokensWrapper: MockThemeColorSemanticTokensWrapper {

static let otherMockThemeMultipleColorSemanticTokens = MultipleColorSemanticTokens("#FF0000")
static let otherMockThemeSemanticColorToken: ColorSemanticToken = "#00FF00"
Expand Down
Loading

0 comments on commit 9d4df70

Please sign in to comment.