Skip to content

Commit

Permalink
Add strikethrough modifier
Browse files Browse the repository at this point in the history
  • Loading branch information
carson-katri committed Apr 11, 2023
1 parent 08ee3b0 commit ff06b04
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
//
// StrikethroughModifier.swift
// LiveViewNative
//
// Created by Carson Katri on 4/6/2023.
//

import SwiftUI

/// Adds a line through ``Text``.
///
/// The line can be customized with the ``pattern`` and ``color`` arguments.
///
/// ```html
/// <Text modifiers={strikethrough(@native, color: :red, pattern: :dash_dot)}>
/// Hello, world!
/// </Text>
/// ```
///
/// ## Arguments
/// * [`is_active`](doc:StrikethroughModifier/isActive)
/// * ``pattern``
/// * ``color``
#if swift(>=5.8)
@_documentation(visibility: public)
#endif
struct StrikethroughModifier: ViewModifier, Decodable {
/// `is_active`, enables/disables the effect.
#if swift(>=5.8)
@_documentation(visibility: public)
#endif
private let isActive: Bool

/// The pattern to use for the line. Defaults to `solid`.
///
/// Possible values:
/// * `dash`
/// * `dash_dot`
/// * `dash_dot_dot`
/// * `dot`
/// * `solid`
#if swift(>=5.8)
@_documentation(visibility: public)
#endif
private let pattern: SwiftUI.Text.LineStyle.Pattern

/// The color of the line. Defaults to `nil`.
///
/// See ``LiveViewNative/SwiftUI/Color`` for details on creating colors.
#if swift(>=5.8)
@_documentation(visibility: public)
#endif
private let color: SwiftUI.Color?

init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)

self.isActive = try container.decode(Bool.self, forKey: .isActive)
switch try container.decode(String.self, forKey: .pattern) {
case "dash": self.pattern = .dash
case "dash_dot": self.pattern = .dashDot
case "dash_dot_dot": self.pattern = .dashDotDot
case "dot": self.pattern = .dot
case "solid": self.pattern = .solid
case let `default`: throw DecodingError.dataCorrupted(.init(codingPath: container.codingPath, debugDescription: "unknown pattern '\(`default`)'"))
}

self.color = try container.decodeIfPresent(SwiftUI.Color.self, forKey: .color)
}

func body(content: Content) -> some View {
content.strikethrough(isActive, pattern: pattern, color: color)
}

enum CodingKeys: String, CodingKey {
case isActive = "is_active"
case pattern
case color
}
}
3 changes: 3 additions & 0 deletions Sources/LiveViewNative/Registries/BuiltinRegistry.swift
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ struct BuiltinRegistry: BuiltinRegistryProtocol {
case renameAction = "rename_action"
case rotation3DEffect = "rotation_3d_effect"
case rotationEffect = "rotation_effect"
case strikethrough
case tag
case textSelection = "text_selection"
case tint
Expand Down Expand Up @@ -250,6 +251,8 @@ struct BuiltinRegistry: BuiltinRegistryProtocol {
try Rotation3DEffectModifier(from: decoder)
case .rotationEffect:
try RotationEffectModifier(from: decoder)
case .strikethrough:
try StrikethroughModifier(from: decoder)
case .tag:
try TagModifier(from: decoder)
case .textSelection:
Expand Down
11 changes: 11 additions & 0 deletions lib/live_view_native_swift_ui/modifiers/strikethrough.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
defmodule LiveViewNativeSwiftUi.Modifiers.Strikethrough do
use LiveViewNativePlatform.Modifier

alias LiveViewNativeSwiftUi.Types.Color

modifier_schema "strikethrough" do
field :is_active, :boolean, default: true
field :pattern, Ecto.Enum, values: ~w(dash dash_dot dash_dot_dot dot solid)a, default: :solid
field :color, Color
end
end
1 change: 1 addition & 0 deletions lib/live_view_native_swift_ui/platform.ex
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ defmodule LiveViewNativeSwiftUi.Platform do
padding: Modifiers.Padding,
rotation_3d_effect: Modifiers.Rotation3DEffect,
rotation_effect: Modifiers.RotationEffect,
strikethrough: Modifiers.Strikethrough,
tag: Modifiers.Tag,
text_selection: Modifiers.TextSelection,
tint: Modifiers.Tint,
Expand Down

0 comments on commit ff06b04

Please sign in to comment.