Skip to content

Commit

Permalink
✨ Use Text.Case & implement more environments
Browse files Browse the repository at this point in the history
  • Loading branch information
KrLite committed Dec 18, 2024
1 parent 19cbe69 commit 10dcc73
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 39 deletions.
35 changes: 20 additions & 15 deletions Sources/Luminare/Components/Auxiliary/LuminareButtonStyles.swift
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@ public struct LuminareCompactButtonStyle: ButtonStyle {

public struct LuminareFilled: ViewModifier {
@Environment(\.isEnabled) private var isEnabled
@Environment(\.luminareHasBackground) private var hasBackground
@Environment(\.luminareButtonMaterial) private var material
@Environment(\.luminareButtonHighlightOnHover) private var highlightOnHover

Expand Down Expand Up @@ -344,27 +345,31 @@ public struct LuminareFilled: ViewModifier {
}

public func body(content: Content) -> some View {
content
.background(with: material) {
Group {
if isEnabled {
if isPressed {
Rectangle()
.foregroundStyle(pressed)
} else if highlightOnHover, isHovering {
Rectangle()
.foregroundStyle(hovering)
if hasBackground {
content
.background(with: material) {
Group {
if isEnabled {
if isPressed {
Rectangle()
.foregroundStyle(pressed)
} else if highlightOnHover, isHovering {
Rectangle()
.foregroundStyle(hovering)
} else {
Rectangle()
.foregroundStyle(fill)
}
} else {
Rectangle()
.foregroundStyle(fill)
}
} else {
Rectangle()
.foregroundStyle(fill)
}
.opacity(isEnabled ? 1 : 0.5)
}
.opacity(isEnabled ? 1 : 0.5)
}
} else {
content
}
}
}

Expand Down
18 changes: 5 additions & 13 deletions Sources/Luminare/Components/LuminareTextEditor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -105,21 +105,13 @@ public struct LuminareTextEditor: View {
"LuminareTextEditor",
traits: .sizeThatFitsLayout
) {
@Previewable @State var text = """
Cupidatat ipsum consequat est cupidatat veniam sint et voluptate commodo.
Incididunt elit commodo culpa officia sint quis ullamco qui deserunt consequat.
Cillum et ullamco cupidatat. Dolore id ea culpa labore Lorem commodo esse.
Adipisicing dolore officia exercitation dolor occaecat do esse non occaecat reprehenderit duis magna.
Labore eu culpa tempor adipisicing qui incididunt tempor in nostrud dolore velit magna veniam ex.
Veniam et incididunt amet Lorem sit irure.
Anim duis enim enim velit exercitation.
"""
@Previewable @State var text = ""
@Previewable @State var selection: TextSelection? = .none

LuminareTextEditor(text: $text)

LuminareTextEditor(text: $text, selection: $selection)

LuminareTextEditor(text: $text, selection: $selection)
.disabled(true)
// LuminareTextEditor(text: $text, selection: $selection)
//
// LuminareTextEditor(text: $text, selection: $selection)
// .disabled(true)
}
1 change: 1 addition & 0 deletions Sources/Luminare/Utilities/EnvironmentValues.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ public extension EnvironmentValues {
@Entry var luminareMinHeight: CGFloat = 34
@Entry var luminareHorizontalPadding: CGFloat = 8
@Entry var luminareIsBordered: Bool = true
@Entry var luminareHasBackground: Bool = true
@Entry var luminareHasDividers: Bool = true

// MARK: Button Styles
Expand Down
34 changes: 34 additions & 0 deletions Sources/Luminare/Utilities/Extensions/Text+Extensions.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//
// Text+Extensions.swift
// Luminare
//
// Created by KrLite on 2024/12/18.
//

import SwiftUI

extension Text.Case: Codable {
enum CodingKeys: String, CodingKey {
case rawValue
}

public init(from decoder: any Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
switch try values.decode(String.self, forKey: .rawValue) {
case "uppercase":
self = .uppercase
case "lowercase":
self = .lowercase
default:
throw DecodingError.dataCorrupted(.init(
codingPath: [CodingKeys.rawValue],
debugDescription: "Unknown case"
))
}
}

public func encode(to encoder: any Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(self, forKey: .rawValue)
}
}
4 changes: 4 additions & 0 deletions Sources/Luminare/Utilities/Extensions/View+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,10 @@ public extension View {
environment(\.luminareIsBordered, bordered)
}

@ViewBuilder func luminareHasBackground(_ hasBackground: Bool = true) -> some View {
environment(\.luminareHasBackground, hasBackground)
}

@ViewBuilder func luminareHasDividers(_ hasDividers: Bool = true) -> some View {
environment(\.luminareHasDividers, hasDividers)
}
Expand Down
15 changes: 4 additions & 11 deletions Sources/Luminare/Utilities/StringFormatStyle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public struct StringFormatStyle: Equatable, Hashable, Codable, Sendable, Parseab
///
/// - `.custom(.uppercased, "@@")`
/// - Parses `#42ab0e` to `@@42AB0E`.
case custom(TextCase, String)
case custom(Text.Case, String)

/// Parse a hex value using a specified strategy.
/// - Parameter value: The hex value to parse.
Expand All @@ -100,6 +100,9 @@ public struct StringFormatStyle: Equatable, Hashable, Codable, Sendable, Parseab
Self.uppercased
case .lowercase:
Self.lowercased
@unknown default:
// Unknown!
Self.lowercased
}

return try prefix + branch.parse(value)
Expand All @@ -117,14 +120,4 @@ public struct StringFormatStyle: Equatable, Hashable, Codable, Sendable, Parseab
// don't need conversions
value
}

/// Represents a text case.
public enum TextCase: String, Equatable, Hashable, Identifiable, CaseIterable, Codable, Sendable {
/// The uppercase.
case uppercase
/// The lowercase.
case lowercase

public var id: Self { self }
}
}

0 comments on commit 10dcc73

Please sign in to comment.