Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add default dark styles for Views #241

Merged
merged 21 commits into from
Aug 10, 2020
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Sources/TokamakCore/Modifiers/FlexFrameLayout.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@ public struct _FlexFrameLayout: ViewModifier {
public let maxHeight: CGFloat?
public let alignment: Alignment

// These are special cases in SwiftUI, where the child
// will request the entire width/height of the parent.
public var fillWidth: Bool {
minWidth == 0 && maxWidth == .infinity
}

public var fillHeight: Bool {
minHeight == 0 && maxHeight == .infinity
}

init(minWidth: CGFloat? = nil,
idealWidth: CGFloat? = nil,
maxWidth: CGFloat? = nil,
Expand Down
13 changes: 10 additions & 3 deletions Sources/TokamakCore/Modifiers/ModifiedContent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,23 @@

/// A value with a modifier applied to it.
public struct ModifiedContent<Content, Modifier> {
@Environment(\.self) public var environment
public typealias Body = Never
public let content: Content
public let modifier: Modifier
public private(set) var content: Content
public private(set) var modifier: Modifier

@inlinable public init(content: Content, modifier: Modifier) {
public init(content: Content, modifier: Modifier) {
self.content = content
self.modifier = modifier
}
}

extension ModifiedContent: EnvironmentReader where Modifier: EnvironmentReader {
mutating func setContent(from values: EnvironmentValues) {
modifier.setContent(from: values)
}
}

extension ModifiedContent: View, ParentView where Content: View, Modifier: ViewModifier {
public var body: Body {
neverBody("ModifiedContent<View, ViewModifier>")
Expand Down
30 changes: 26 additions & 4 deletions Sources/TokamakCore/Modifiers/StyleModifiers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
// Created by Carson Katri on 6/29/20.
//

public struct _BackgroundModifier<Background>: ViewModifier where Background: View {
public struct _BackgroundModifier<Background>: ViewModifier, EnvironmentReader
where Background: View {
public var environment: EnvironmentValues!
public var background: Background
public var alignment: Alignment

Expand All @@ -27,9 +29,18 @@ public struct _BackgroundModifier<Background>: ViewModifier where Background: Vi
public func body(content: Content) -> some View {
content
}

mutating func setContent(from values: EnvironmentValues) {
environment = values
}
}

extension _BackgroundModifier: Equatable where Background: Equatable {}
extension _BackgroundModifier: Equatable where Background: Equatable {
public static func == (lhs: _BackgroundModifier<Background>,
rhs: _BackgroundModifier<Background>) -> Bool {
lhs.background == rhs.background
}
}

extension View {
public func background<Background>(
Expand All @@ -40,7 +51,9 @@ extension View {
}
}

public struct _OverlayModifier<Overlay>: ViewModifier where Overlay: View {
public struct _OverlayModifier<Overlay>: ViewModifier, EnvironmentReader
where Overlay: View {
public var environment: EnvironmentValues!
public var overlay: Overlay
public var alignment: Alignment

Expand All @@ -55,9 +68,18 @@ public struct _OverlayModifier<Overlay>: ViewModifier where Overlay: View {
overlay
}
}

mutating func setContent(from values: EnvironmentValues) {
environment = values
}
}

extension _OverlayModifier: Equatable where Overlay: Equatable {}
extension _OverlayModifier: Equatable where Overlay: Equatable {
public static func == (lhs: _OverlayModifier<Overlay>,
rhs: _OverlayModifier<Overlay>) -> Bool {
lhs.overlay == rhs.overlay
}
}

extension View {
public func overlay<Overlay>(_ overlay: Overlay,
Expand Down
1 change: 1 addition & 0 deletions Sources/TokamakCore/Shapes/ModifiedShapes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
//

public struct _StrokedShape<S>: Shape where S: Shape {
@Environment(\.self) public var environment
public var shape: S
public var style: StrokeStyle

Expand Down
1 change: 1 addition & 0 deletions Sources/TokamakCore/Shapes/Shape.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public struct FillStyle: Equatable, ShapeStyle {
}

public struct _ShapeView<Content, Style>: View where Content: Shape, Style: ShapeStyle {
@Environment(\.self) public var environment
@Environment(\.foregroundColor) public var foregroundColor
public var shape: Content
public var style: Style
Expand Down
74 changes: 74 additions & 0 deletions Sources/TokamakCore/Styles/NavigationLinkStyle.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// Copyright 2020 Tokamak contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Created by Carson Katri on 8/2/20.
//

public struct _NavigationLinkStyleConfiguration: View {
public let body: AnyView
public let isSelected: Bool
}

public protocol _NavigationLinkStyle {
associatedtype Body: View
typealias Configuration = _NavigationLinkStyleConfiguration
func makeBody(configuration: Configuration) -> Self.Body
}

public struct _DefaultNavigationLinkStyle: _NavigationLinkStyle {
public func makeBody(configuration: Configuration) -> some View {
configuration.foregroundColor(.accentColor)
}
}

public struct _AnyNavigationLinkStyle: _NavigationLinkStyle {
public typealias Body = AnyView

private let bodyClosure: (_NavigationLinkStyleConfiguration) -> AnyView
public let type: Any.Type

public init<S: _NavigationLinkStyle>(_ style: S) {
type = S.self
bodyClosure = { configuration in
AnyView(style.makeBody(configuration: configuration))
}
}

public func makeBody(configuration: Configuration) -> AnyView {
bodyClosure(configuration)
}
}

public enum _NavigationLinkStyleKey: EnvironmentKey {
public static var defaultValue: _AnyNavigationLinkStyle {
_AnyNavigationLinkStyle(_DefaultNavigationLinkStyle())
}
}

extension EnvironmentValues {
var _navigationLinkStyle: _AnyNavigationLinkStyle {
get {
self[_NavigationLinkStyleKey.self]
}
set {
self[_NavigationLinkStyleKey.self] = newValue
}
}
}

extension View {
public func _navigationLinkStyle<S: _NavigationLinkStyle>(_ style: S) -> some View {
environment(\._navigationLinkStyle, _AnyNavigationLinkStyle(style))
}
}
125 changes: 84 additions & 41 deletions Sources/TokamakCore/Tokens/Color.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,41 +16,61 @@
//

public struct Color: Hashable, Equatable {
// FIXME: This is not injected.
@Environment(\.accentColor) static var envAccentColor
public static func == (lhs: Self, rhs: Self) -> Bool {
var lightEnv = EnvironmentValues()
lightEnv.colorScheme = .light
var darkEnv = EnvironmentValues()
darkEnv.colorScheme = .dark
return lhs._evaluate(lightEnv) == rhs._evaluate(lightEnv) &&
lhs._evaluate(darkEnv) == rhs._evaluate(darkEnv)
}

public func hash(into hasher: inout Hasher) {
hasher.combine(evaluator(EnvironmentValues()))
}

public enum RGBColorSpace {
case sRGB
case sRGBLinear
case displayP3
}

public let red: Double
public let green: Double
public let blue: Double
public let opacity: Double
public let space: RGBColorSpace
public struct _RGBA: Hashable, Equatable {
public let red: Double
public let green: Double
public let blue: Double
public let opacity: Double
public let space: RGBColorSpace
}

let evaluator: (EnvironmentValues) -> _RGBA

private init(_ evaluator: @escaping (EnvironmentValues) -> _RGBA) {
self.evaluator = evaluator
}

public init(_ colorSpace: RGBColorSpace = .sRGB,
red: Double,
green: Double,
blue: Double,
opacity: Double = 1) {
self.red = red
self.green = green
self.blue = blue
self.opacity = opacity
space = colorSpace
self.init { _ in
_RGBA(red: red,
green: green,
blue: blue,
opacity: opacity,
space: colorSpace)
}
}

public init(_ colorSpace: RGBColorSpace = .sRGB,
white: Double,
opacity: Double = 1) {
red = white
green = white
blue = white
self.opacity = opacity
space = colorSpace
self.init(colorSpace,
red: white,
green: white,
blue: white,
opacity: opacity)
}

// Source for the formula:
Expand All @@ -60,15 +80,26 @@ public struct Color: Hashable, Equatable {
brightness: Double,
opacity: Double = 1) {
let a = saturation * min(brightness / 2, 1 - (brightness / 2))
let f: (Int) -> Double = { n in
let f = { (n: Int) -> Double in
let k = Double((n + Int(hue * 12)) % 12)
return brightness - (a * max(-1, min(k - 3, 9 - k, 1)))
}
red = f(0)
green = f(8)
blue = f(4)
self.opacity = opacity
space = .sRGB
self.init(.sRGB,
red: f(0),
green: f(8),
blue: f(4),
opacity: opacity)
}

/// Create a `Color` dependent on the current `ColorScheme`.
public static func _withScheme(_ evaluator: @escaping (ColorScheme) -> Self) -> Self {
.init {
evaluator($0.colorScheme)._evaluate($0)
}
}

public func _evaluate(_ environment: EnvironmentValues) -> _RGBA {
evaluator(environment)
}
}

Expand All @@ -84,9 +115,27 @@ extension Color {
public static let yellow: Self = .init(red: 1.00, green: 0.84, blue: 0.04)
public static let pink: Self = .init(red: 1.00, green: 0.22, blue: 0.37)
public static let purple: Self = .init(red: 0.75, green: 0.36, blue: 0.95)
// FIXME: Switch to use colorScheme
public static let primary: Self = .black
public static let primary: Self = .init {
switch $0.colorScheme {
case .light:
return .init(red: 0,
green: 0,
blue: 0,
opacity: 1,
space: .sRGB)
case .dark:
return .init(red: 1,
green: 1,
blue: 1,
opacity: 1,
space: .sRGB)
}
}

public static let secondary: Self = .gray
public static let accentColor: Self = .init {
($0.accentColor ?? Self.blue)._evaluate($0)
}

public init(_ color: UIColor) {
self = color.color
Expand All @@ -96,11 +145,11 @@ extension Color {
extension Color: ExpressibleByIntegerLiteral {
/// Allows initializing value of `Color` type from hex values
public init(integerLiteral bitMask: UInt32) {
red = Double((bitMask & 0xFF0000) >> 16) / 255
green = Double((bitMask & 0x00FF00) >> 8) / 255
blue = Double(bitMask & 0x0000FF) / 255
opacity = 1
space = .sRGB
self.init(.sRGB,
red: Double((bitMask & 0xFF0000) >> 16) / 255,
green: Double((bitMask & 0x00FF00) >> 8) / 255,
blue: Double(bitMask & 0x0000FF) / 255,
opacity: 1)
}
}

Expand All @@ -117,11 +166,11 @@ extension Color {
else {
return nil
}
self.red = Double(red) / 255
self.green = Double(green) / 255
self.blue = Double(blue) / 255
opacity = 1
space = .sRGB
self.init(.sRGB,
red: Double(red) / 255,
green: Double(green) / 255,
blue: Double(blue) / 255,
opacity: 1)
}
}

Expand Down Expand Up @@ -153,12 +202,6 @@ extension View {
}
}

extension Color {
public static var accentColor: Self {
envAccentColor ?? .blue
}
}

struct ForegroundColorKey: EnvironmentKey {
static let defaultValue: Color? = nil
}
Expand Down
Loading