|
| 1 | +// |
| 2 | +// Software Name: OUDS iOS |
| 3 | +// SPDX-FileCopyrightText: Copyright (c) Orange SA |
| 4 | +// SPDX-License-Identifier: MIT |
| 5 | +// |
| 6 | +// This software is distributed under the MIT license, |
| 7 | +// the text of which is available at https://opensource.org/license/MIT/ |
| 8 | +// or see the "LICENSE" file for more details. |
| 9 | +// |
| 10 | +// Authors: See CONTRIBUTORS.txt |
| 11 | +// Software description: A SwiftUI components library with code examples for Orange Unified Design System |
| 12 | +// |
| 13 | + |
| 14 | +import Foundation |
| 15 | +import OUDSTokensRaw |
| 16 | +import OUDSTokensSemantic |
| 17 | +import SwiftUI |
| 18 | + |
| 19 | +// [File not generated by the tokenator] |
| 20 | +// Create an issue for update https://github.com/Orange-OpenSource/ouds-ios/issues/new?template=token_update.yml |
| 21 | + |
| 22 | +// swiftlint:disable line_length |
| 23 | + |
| 24 | +/// Defines provider objects for `ColorModeSemanticTokens` so as to pack them as light and dark mode colors. |
| 25 | +/// These values can be overriden inside `OrangeThemeColorModeSemanticTokensProvider` subclasses (in extensions or not, in the same module or not) thanks to the `@objc open` combination. |
| 26 | +extension OrangeThemeColorModeSemanticTokensProvider: ColorModeMultipleSemanticTokens { |
| 27 | + |
| 28 | + // MARK: - Multiple tokens |
| 29 | + |
| 30 | + @objc open var modeOnBrandPrimary: MultipleColorModeSemanticTokens { MultipleColorModeSemanticTokens(light: modeOnBrandPrimaryLight, dark: modeOnBrandPrimaryDark) } |
| 31 | + |
| 32 | + @objc open var modeOnBrandSecondary: MultipleColorModeSemanticTokens { MultipleColorModeSemanticTokens(light: modeOnBrandSecondaryLight, dark: modeOnBrandSecondaryDark) } |
| 33 | + |
| 34 | + @objc open var modeOnBrandTertiary: MultipleColorModeSemanticTokens { MultipleColorModeSemanticTokens(light: modeOnBrandTertiaryLight, dark: modeOnBrandTertiaryDark) } |
| 35 | + |
| 36 | + @objc open var modeOnStatusAccentEmphasized: MultipleColorModeSemanticTokens { MultipleColorModeSemanticTokens(light: modeOnStatusAccentEmphasizedLight, dark: modeOnStatusAccentEmphasizedDark) } |
| 37 | + |
| 38 | + @objc open var modeOnStatusAccentMuted: MultipleColorModeSemanticTokens { MultipleColorModeSemanticTokens(light: modeOnStatusAccentMutedLight, dark: modeOnStatusAccentMutedDark) } |
| 39 | + |
| 40 | + @objc open var modeOnStatusInfoEmphasized: MultipleColorModeSemanticTokens { MultipleColorModeSemanticTokens(light: modeOnStatusInfoEmphasizedLight, dark: modeOnStatusInfoEmphasizedDark) } |
| 41 | + |
| 42 | + @objc open var modeOnStatusInfoMuted: MultipleColorModeSemanticTokens { MultipleColorModeSemanticTokens(light: modeOnStatusInfoMutedLight, dark: modeOnStatusInfoMutedDark) } |
| 43 | + |
| 44 | + @objc open var modeOnStatusNegativeEmphasized: MultipleColorModeSemanticTokens { MultipleColorModeSemanticTokens(light: modeOnStatusNegativeEmphasizedLight, dark: modeOnStatusNegativeEmphasizedDark) } |
| 45 | + |
| 46 | + @objc open var modeOnStatusNegativeMuted: MultipleColorModeSemanticTokens { MultipleColorModeSemanticTokens(light: modeOnStatusNegativeMutedLight, dark: modeOnStatusNegativeMutedDark) } |
| 47 | + |
| 48 | + @objc open var modeOnStatusNeutralEmphasized: MultipleColorModeSemanticTokens { MultipleColorModeSemanticTokens(light: modeOnStatusNeutralEmphasizedLight, dark: modeOnStatusNeutralEmphasizedDark) } |
| 49 | + |
| 50 | + @objc open var modeOnStatusNeutralMuted: MultipleColorModeSemanticTokens { MultipleColorModeSemanticTokens(light: modeOnStatusNeutralMutedLight, dark: modeOnStatusNeutralMutedDark) } |
| 51 | + |
| 52 | + @objc open var modeOnStatusPositiveEmphasized: MultipleColorModeSemanticTokens { MultipleColorModeSemanticTokens(light: modeOnStatusPositiveEmphasizedLight, dark: modeOnStatusPositiveEmphasizedDark) } |
| 53 | + |
| 54 | + @objc open var modeOnStatusPositiveMuted: MultipleColorModeSemanticTokens { MultipleColorModeSemanticTokens(light: modeOnStatusPositiveMutedLight, dark: modeOnStatusPositiveMutedDark) } |
| 55 | + |
| 56 | + @objc open var modeOnStatusWarningEmphasized: MultipleColorModeSemanticTokens { MultipleColorModeSemanticTokens(light: modeOnStatusWarningEmphasizedLight, dark: modeOnStatusWarningEmphasizedDark) } |
| 57 | + |
| 58 | + @objc open var modeOnStatusWarningMuted: MultipleColorModeSemanticTokens { MultipleColorModeSemanticTokens(light: modeOnStatusWarningMutedLight, dark: modeOnStatusWarningMutedDark) } |
| 59 | + |
| 60 | + // MARK: - Additional rules |
| 61 | + |
| 62 | + /// Depending to the given `mode`, should return the SwfitUI `ColorScheme` to consider. |
| 63 | + /// For Orange theme, the rules are: |
| 64 | + /// - if light version is dark mode and dark version is light mode, reverse the color scheme |
| 65 | + /// - if light and dark versions are light mode, return light color scheme |
| 66 | + /// - if light and dark versions are dark mode, return dark color scheme |
| 67 | + /// - otherwise return current color scheme |
| 68 | + /// |
| 69 | + /// - Parameters: |
| 70 | + /// - mode: The color mode token to test |
| 71 | + /// - current: The current color scheme |
| 72 | + /// - Returns: The color scheme to consider depending to the theme rules |
| 73 | + public func useColorScheme(for mode: MultipleColorModeSemanticTokens, |
| 74 | + _ current: ColorScheme) -> ColorScheme { |
| 75 | + |
| 76 | + let lightColorScheme = ColorModeRawTokens.colorScheme(for: mode.light) |
| 77 | + let darkColorScheme = ColorModeRawTokens.colorScheme(for: mode.dark) |
| 78 | + |
| 79 | + // Reverse the color scheme to apply |
| 80 | + if lightColorScheme == .dark && darkColorScheme == .light { |
| 81 | + return (current == .light ? .dark : .light) |
| 82 | + } |
| 83 | + |
| 84 | + // Force light |
| 85 | + if lightColorScheme == .light && darkColorScheme == .light { |
| 86 | + return .light |
| 87 | + } |
| 88 | + |
| 89 | + // Force dark |
| 90 | + if lightColorScheme == .dark && darkColorScheme == .dark { |
| 91 | + return .dark |
| 92 | + } |
| 93 | + |
| 94 | + // Otherwise use device configuration |
| 95 | + return current |
| 96 | + } |
| 97 | + |
| 98 | + /// Depending to the given `mode`, should return a flag saying if monochrome mode must be considered. |
| 99 | + /// For Orange theme, the rule is : |
| 100 | + /// - if both light and dark versions are monochrome, returns true, otherwise false |
| 101 | + /// |
| 102 | + /// - Parameter mode: The color mode token to test |
| 103 | + /// - Returns: The flag for monochrome mode depending to the theme rules |
| 104 | + public func isMonochrome(mode: MultipleColorModeSemanticTokens) -> Bool { |
| 105 | + let lightIsMonochrome = ColorModeRawTokens.isMonochrome(mode: mode.light) |
| 106 | + let darkIsMonochrome = ColorModeRawTokens.isMonochrome(mode: mode.dark) |
| 107 | + return lightIsMonochrome == true && darkIsMonochrome == true |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +// swiftlint:enable line_length |
0 commit comments