Skip to content

Commit

Permalink
Update project structure
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyle-Ye committed Dec 4, 2023
1 parent a254960 commit 702ce69
Show file tree
Hide file tree
Showing 31 changed files with 243 additions and 90 deletions.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
protocol _GraphInputsModifier {}

@frozen
public struct _EnvironmentKeyTransformModifier<Value> : ViewModifier, _GraphInputsModifier {
public var keyPath: Swift.WritableKeyPath<EnvironmentValues, Value>
public var transform: (inout Value) -> Swift.Void

@inlinable
public init(keyPath: Swift.WritableKeyPath<EnvironmentValues, Value>, transform: @escaping (inout Value) -> Swift.Void) {
self.keyPath = keyPath
self.transform = transform
}
public static func _makeInputs(modifier: _GraphValue<_EnvironmentKeyTransformModifier<Value>>, inputs: inout _GraphInputs) {

}
public typealias Body = Never
}

extension View {
@inlinable
public func transformEnvironment<V>(
_ keyPath: WritableKeyPath<EnvironmentValues, V>,
transform: @escaping (inout V) -> Void
) -> some View {
modifier(_EnvironmentKeyTransformModifier(
keyPath: keyPath,
transform: transform
))
}
}
7 changes: 7 additions & 0 deletions Sources/OpenSwiftUI/Internal/Other/Utils.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
@inlinable
@inline(__always)
func asOptional<Value>(_ value: Value) -> Value? {
func unwrap<T>() -> T { value as! T }
let optionalValue: Value? = unwrap()
return optionalValue
}
8 changes: 8 additions & 0 deletions Sources/OpenSwiftUI/OpenSwiftUI.docc/Views/Controls.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,11 @@ Use these built-in controls and indicators when composing custom views, and styl
### Linking to other content

- ``Link``

### Sizing controls

- ``View/controlSize(_:)``

- ``EnvironmentValues/controlSize``

- ``ControlSize``
7 changes: 0 additions & 7 deletions Sources/OpenSwiftUI/UIElements/Font/TODO/Font.swift
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,3 @@ extension Font {
}
}

public enum ControlSize: Hashable, CaseIterable, Sendable {
case mini
case small
case regular
case large
case extraLarge
}
28 changes: 28 additions & 0 deletions Sources/OpenSwiftUI/Views/Controls/ControlSize/ControlSize.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//
// ControlSize.swift
// OpenSwiftUI
//
// Created by Kyle on 2023/12/5.
// Lastest Version: iOS 17.0
// Status: Complete

/// The size classes, like regular or small, that you can apply to controls
/// within a view.
@available(tvOS, unavailable)
public enum ControlSize: CaseIterable, Sendable {
/// A control version that is minimally sized.
case mini

/// A control version that is proportionally smaller size for space-constrained views.
case small

/// A control version that is the default size.
case regular

/// A control version that is prominently sized.
case large

case extraLarge
}

extension ControlSize: Hashable {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
//
// ControlSizeKey.swift
// OpenSwiftUI
//
// Created by Kyle on 2023/11/5.
// Lastest Version: iOS 15.5
// Status: Complete
// ID: 50E368DED9ACE8B6BEC08FF7781AF4B1

private struct ControlSizeKey: EnvironmentKey {
static let defaultValue: ControlSize = .regular
}

extension EnvironmentValues {
/// The size to apply to controls within a view.
///
/// The default is ``ControlSize/regular``.
@available(tvOS, unavailable)
public var controlSize: ControlSize {
get { self[ControlSizeKey.self] }
set { self[ControlSizeKey.self] = newValue }
}
}

@available(tvOS, unavailable)
extension View {
/// Sets the size for controls within this view.
///
/// Use `controlSize(_:)` to override the system default size for controls
/// in this view. In this example, a view displays several typical controls
/// at `.mini`, `.small` and `.regular` sizes.
///
/// struct ControlSize: View {
/// var body: some View {
/// VStack {
/// MyControls(label: "Mini")
/// .controlSize(.mini)
/// MyControls(label: "Small")
/// .controlSize(.small)
/// MyControls(label: "Regular")
/// .controlSize(.regular)
/// }
/// .padding()
/// .frame(width: 450)
/// .border(Color.gray)
/// }
/// }
///
/// struct MyControls: View {
/// var label: String
/// @State private var value = 3.0
/// @State private var selected = 1
/// var body: some View {
/// HStack {
/// Text(label + ":")
/// Picker("Selection", selection: $selected) {
/// Text("option 1").tag(1)
/// Text("option 2").tag(2)
/// Text("option 3").tag(3)
/// }
/// Slider(value: $value, in: 1...10)
/// Button("OK") { }
/// }
/// }
/// }
///
/// ![A screenshot showing several controls of various
/// sizes.](SwiftUI-View-controlSize.png)
///
/// - Parameter controlSize: One of the control sizes specified in the
/// ``ControlSize`` enumeration.
@inlinable
public func controlSize(_ controlSize: ControlSize) -> some View {
environment(\.controlSize, controlSize)
}
}
51 changes: 51 additions & 0 deletions Sources/OpenSwiftUI/Views/Controls/EnabledKey.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
//
// EnabledKey.swift
// OpenSwiftUI
//
// Created by Kyle on 2023/10/9.
// Lastest Version: iOS 15.5
// Status: Complete
// ID: 6C7FC77DDFF6AC5E011A44B5658DAD66

private struct EnabledKey: EnvironmentKey {
static var defaultValue: Bool { true }
}

extension EnvironmentValues {
/// A Boolean value that indicates whether the view associated with this
/// environment allows user interaction.
///
/// The default value is `true`.
public var isEnabled: Bool {
get { self[EnabledKey.self] }
set { self[EnabledKey.self] = newValue }
}
}

extension View {
/// Adds a condition that controls whether users can interact with this
/// view.
///
/// The higher views in a view hierarchy can override the value you set on
/// this view. In the following example, the button isn't interactive
/// because the outer `disabled(_:)` modifier overrides the inner one:
///
/// HStack {
/// Button(Text("Press")) {}
/// .disabled(false)
/// }
/// .disabled(true)
///
/// - Parameter disabled: A Boolean value that determines whether users can
/// interact with this view.
///
/// - Returns: A view that controls whether users can interact with this
/// view.
@inlinable
public func disabled(_ disabled: Bool) -> some View {
modifier(_EnvironmentKeyTransformModifier(
keyPath: \.isEnabled,
transform: { $0 = $0 && !disabled }
))
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
//
// EnvironmentValues+OpenURLActionKey.swift
// OpenURLActionKey.swift
// OpenSwiftUI
//
// Created by Kyle on 2023/11/26.
// Lastest Version: iOS 15.5
// Status: WIP
// Status: Complete

#if canImport(Darwin)
#if os(iOS) || os(tvOS)
Expand All @@ -18,6 +18,47 @@ import AppKit
#endif
#endif

struct OpenURLActionKey: EnvironmentKey {
static let defaultValue = OpenURLAction(
handler: .system { url, completion in
#if os(iOS) || os(tvOS)
UIApplication.shared.open(url, options: [:], completionHandler: completion)
#elseif os(macOS)
NSWorkspace.shared.open(url, configuration: .init()) { _, error in
completion(error != nil)
}
#else
fatalError("Unimplemented")
#endif
},
isDefault: true
)
}

struct OpenSensitiveURLActionKey: EnvironmentKey {
static let defaultValue = OpenURLAction(
handler: .system { url, completion in
#if DEBUG && os(iOS)
let config = _LSOpenConfiguration()
config.isSensitive = true
let scene = UIApplication.shared.connectedScenes.first
config.targetConnectionEndpoint = scene?._currentOpenApplicationEndpoint
guard let workspace = LSApplicationWorkspace.default() else {
return
}
workspace.open(url, configuration: config, completionHandler: completion)
#else
fatalError("Unimplemented")
#endif
},
isDefault: true
)
}

struct HostingViewOpenURLActionKey: EnvironmentKey {
static let defaultValue: OpenURLAction? = nil
}

extension EnvironmentValues {
/// An action that opens a URL.
///
Expand Down Expand Up @@ -110,44 +151,3 @@ extension EnvironmentValues {
set { self[OpenSensitiveURLActionKey.self] = newValue }
}
}

struct OpenURLActionKey: EnvironmentKey {
static let defaultValue = OpenURLAction(
handler: .system { url, completion in
#if os(iOS) || os(tvOS)
UIApplication.shared.open(url, options: [:], completionHandler: completion)
#elseif os(macOS)
NSWorkspace.shared.open(url, configuration: .init()) { _, error in
completion(error != nil)
}
#else
fatalError("Unimplemented")
#endif
},
isDefault: true
)
}

struct OpenSensitiveURLActionKey: EnvironmentKey {
static let defaultValue = OpenURLAction(
handler: .system { url, completion in
#if DEBUG && os(iOS)
let config = _LSOpenConfiguration()
config.isSensitive = true
let scene = UIApplication.shared.connectedScenes.first
config.targetConnectionEndpoint = scene?._currentOpenApplicationEndpoint
guard let workspace = LSApplicationWorkspace.default() else {
return
}
workspace.open(url, configuration: config, completionHandler: completion)
#else
fatalError("Unimplemented")
#endif
},
isDefault: true
)
}

struct HostingViewOpenURLActionKey: EnvironmentKey {
static let defaultValue: OpenURLAction? = nil
}

0 comments on commit 702ce69

Please sign in to comment.