Created
January 9, 2024 23:16
-
-
Save nicholaslythall/11ae936e4d868f95ca04a6e1d805f185 to your computer and use it in GitHub Desktop.
DynamicShapeStyle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct DynamicShapeStyle: ShapeStyle { | |
let light: AnyShapeStyle | |
let dark: AnyShapeStyle | |
init<Light: ShapeStyle, Dark: ShapeStyle>(light: Light, dark: Dark) { | |
self.light = AnyShapeStyle(light) | |
self.dark = AnyShapeStyle(dark) | |
} | |
func resolve(in environment: EnvironmentValues) -> some ShapeStyle { | |
switch environment.colorScheme { | |
case .light: | |
light | |
case .dark: | |
dark | |
@unknown default: | |
light | |
} | |
} | |
} | |
extension ShapeStyle where Self == DynamicShapeStyle { | |
static func `dynamic`<Light: ShapeStyle, Dark: ShapeStyle>(light: Light, dark: Dark) -> DynamicShapeStyle { | |
return .init(light: light, dark: dark) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment