Skip to content

Commit

Permalink
Move control styles to modifiers
Browse files Browse the repository at this point in the history
Closes #471
Closes #472
Closes #474
Closes #475
Closes #476
Closes #477
  • Loading branch information
shadowfacts committed Apr 19, 2023
1 parent e5979e1 commit 114f9eb
Show file tree
Hide file tree
Showing 22 changed files with 626 additions and 397 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
//
// ButtonStyleModifier.swift
// LiveViewNative
//
// Created by Shadowfacts on 4/4/23.
//

import SwiftUI

/// Alters the visual style of any buttons within this view.
///
/// ## Arguments
/// - ``style``
///
/// ## Topics
/// ### Supporting Types
/// - ``ButtonStyle``
#if swift(>=5.8)
@_documentation(visibility: public)
#endif
struct ButtonStyleModifier: ViewModifier, Decodable {
/// The style to apply to buttons.
#if swift(>=5.8)
@_documentation(visibility: public)
#endif
private let style: ButtonStyle

func body(content: Content) -> some View {
content.applyButtonStyle(style)
}
}

/// A style for the ``Button`` element.
#if swift(>=5.8)
@_documentation(visibility: public)
#endif
private enum ButtonStyle: String, Decodable {
#if swift(>=5.8)
@_documentation(visibility: public)
#endif
case automatic
#if swift(>=5.8)
@_documentation(visibility: public)
#endif
case bordered
/// `bordered_prominent`
#if swift(>=5.8)
@_documentation(visibility: public)
#endif
case borderedProminent = "bordered_prominent"
#if swift(>=5.8)
@_documentation(visibility: public)
#endif
case borderless
#if swift(>=5.8)
@_documentation(visibility: public)
#endif
case plain
}

private extension View {
@ViewBuilder
func applyButtonStyle(_ style: ButtonStyle) -> some View {
switch style {
case .automatic:
self.buttonStyle(.automatic)
case .bordered:
self.buttonStyle(.bordered)
case .borderedProminent:
self.buttonStyle(.borderedProminent)
case .borderless:
self.buttonStyle(.borderless)
case .plain:
self.buttonStyle(.plain)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
//
// DatePickerStyleModifier.swift
// LiveViewNative
//
// Created by Shadowfacts on 4/4/23.
//

import SwiftUI

/// Alters the visual style of any date pickers within this view.
///
/// ## Arguments
/// - ``style``
///
/// ## Topics
/// ### Supporting Types
/// - ``DatePickerStyle``
#if swift(>=5.8)
@_documentation(visibility: public)
#endif
@available(iOS 16.0, macOS 13.0, *)
struct DatePickerStyleModifier: ViewModifier, Decodable {
/// The style to apply to date pickers.
#if swift(>=5.8)
@_documentation(visibility: public)
#endif
private let style: DatePickerStyle

func body(content: Content) -> some View {
#if os(iOS) || os(macOS)
content.applyDatePickerStyle(style)
#else
content
#endif
}
}

/// The style of a ``DatePicker``.
#if swift(>=5.8)
@_documentation(visibility: public)
#endif
private enum DatePickerStyle: String, Decodable {
#if swift(>=5.8)
@_documentation(visibility: public)
#endif
@available(iOS 16.0, tvOS 16.0, watchOS 9.0, macOS 13.0, *)
case automatic
#if swift(>=5.8)
@_documentation(visibility: public)
#endif
@available(iOS 16.0, tvOS 16.0, watchOS 9.0, macOS 13.0, *)
case compact
#if swift(>=5.8)
@_documentation(visibility: public)
#endif
@available(iOS 16.0, tvOS 16.0, watchOS 9.0, macOS 13.0, *)
case graphical
#if swift(>=5.8)
@_documentation(visibility: public)
#endif
@available(iOS 16.0, *)
case wheel
#if swift(>=5.8)
@_documentation(visibility: public)
#endif
@available(macOS 13.0, *)
case field
/// `stepper_field`
#if swift(>=5.8)
@_documentation(visibility: public)
#endif
@available(macOS 13.0, *)
case stepperField = "stepper_field"
}

#if os(iOS) || os(macOS)
private extension View {
@ViewBuilder
func applyDatePickerStyle(_ style: DatePickerStyle?) -> some View {
switch style {
case nil:
self
case .automatic:
self.datePickerStyle(.automatic)
case .compact:
self.datePickerStyle(.compact)
case .graphical:
self.datePickerStyle(.graphical)
case .wheel:
#if os(iOS)
self.datePickerStyle(.wheel)
#endif
case .field:
#if os(macOS)
self.datePickerStyle(.field)
#endif
case .stepperField:
#if os(macOS)
self.datePickerStyle(.stepperField)
#endif
}
}
}
#endif
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
//
// GaugeStyleModifier.swift
// LiveViewNative
//
// Created by Shadowfacts on 4/4/23.
//

import SwiftUI

/// Alters the visual style of any gauges within this view.
///
/// ## Arguments
/// - ``style``
///
/// ## Topics
/// ### Supporting Types
/// - ``GaugeStyle``
#if swift(>=5.8)
@_documentation(visibility: public)
#endif
@available(iOS 16.0, watchOS 9.0, macOS 13.0, *)
struct GaugeStyleModifier: ViewModifier, Decodable {
/// The style to apply to gauges.
#if swift(>=5.8)
@_documentation(visibility: public)
#endif
private let style: GaugeStyle

func body(content: Content) -> some View {
#if !os(tvOS)
content.applyGaugeStyle(style)
#else
content
#endif
}
}

/// A style for a ``Gauge`` element.
#if swift(>=5.8)
@_documentation(visibility: public)
#endif
fileprivate enum GaugeStyle: String, Decodable {
/// `accessory_circular_capacity`
#if swift(>=5.8)
@_documentation(visibility: public)
#endif
case accessoryCircularCapacity = "accessory_circular_capacity"
/// `accessory_linear_capacity`
#if swift(>=5.8)
@_documentation(visibility: public)
#endif
case accessoryLinearCapacity = "accessory_linear_capacity"
/// `accessory_circular`
#if swift(>=5.8)
@_documentation(visibility: public)
#endif
case accessoryCircular = "accessory_circular"
#if swift(>=5.8)
@_documentation(visibility: public)
#endif
case automatic
/// `linear_capacity`
#if swift(>=5.8)
@_documentation(visibility: public)
#endif
case linearCapacity = "linear_capacity"
/// `accessory_linear`
#if swift(>=5.8)
@_documentation(visibility: public)
#endif
case accessoryLinear = "accessory_linear"
}

#if !os(tvOS)
fileprivate extension View {
@ViewBuilder
func applyGaugeStyle(_ style: GaugeStyle) -> some View {
switch style {
case .accessoryCircularCapacity:
self.gaugeStyle(.accessoryCircularCapacity)
case .accessoryLinearCapacity:
self.gaugeStyle(.accessoryLinearCapacity)
case .accessoryCircular:
self.gaugeStyle(.accessoryCircular)
case .automatic:
self.gaugeStyle(.automatic)
case .linearCapacity:
self.gaugeStyle(.linearCapacity)
case .accessoryLinear:
self.gaugeStyle(.accessoryLinear)
}
}
}
#endif
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
//
// PickerStyleModifier.swift
// LiveViewNative
//
// Created by Shadowfacts on 4/4/23.
//

import SwiftUI

/// Alters the visual style of any pickers within this view.
///
/// ## Arguments
/// - ``style``
///
/// ## Topics
/// ### Supporting Types
/// - ``PickerStyle``
#if swift(>=5.8)
@_documentation(visibility: public)
#endif
struct PickerStyleModifier: ViewModifier, Decodable {
/// The style to apply to pickers.
#if swift(>=5.8)
@_documentation(visibility: public)
#endif
private let style: PickerStyle

func body(content: Content) -> some View {
content.applyPickerStyle(style)
}
}

/// The visual style of a ``Picker`` element.
#if swift(>=5.8)
@_documentation(visibility: public)
#endif
private enum PickerStyle: String, Decodable {
#if swift(>=5.8)
@_documentation(visibility: public)
#endif
case automatic
case inline
#if swift(>=5.8)
@_documentation(visibility: public)
#endif
@available(iOS 16.0, macOS 13.0, *)
case menu
/// `navigation_link`
#if swift(>=5.8)
@_documentation(visibility: public)
#endif
@available(iOS 16.0, tvOS 16.0, watchOS 9.0, *)
case navigationLink = "navigation_link"
/// `radio_group`
#if swift(>=5.8)
@_documentation(visibility: public)
#endif
@available(macOS 13.0, *)
case radioGroup = "radio_group"
#if swift(>=5.8)
@_documentation(visibility: public)
#endif
@available(iOS 16.0, tvOS 16.0, macOS 13.0, *)
case segmented
#if swift(>=5.8)
@_documentation(visibility: public)
#endif
@available(iOS 16.0, watchOS 9.0, *)
case wheel
}

private extension View {
@ViewBuilder
func applyPickerStyle(_ style: PickerStyle) -> some View {
switch style {
case .automatic:
self.pickerStyle(.automatic)
case .inline:
self.pickerStyle(.inline)
case .menu:
#if os(iOS) || os(macOS)
self.pickerStyle(.menu)
#endif
case .navigationLink:
#if !os(macOS)
self.pickerStyle(.navigationLink)
#endif
case .radioGroup:
#if os(macOS)
self.pickerStyle(.radioGroup)
#endif
case .segmented:
#if !os(watchOS)
self.pickerStyle(.segmented)
#endif
case .wheel:
#if os(iOS) || os(watchOS)
self.pickerStyle(.wheel)
#endif
}
}
}
Loading

0 comments on commit 114f9eb

Please sign in to comment.